Skip to content

Commit

Permalink
add token id getter
Browse files Browse the repository at this point in the history
  • Loading branch information
HichemTab-tech committed Jul 29, 2023
1 parent 62bdf96 commit cf6d401
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 3 deletions.
18 changes: 17 additions & 1 deletion src/Results/BaseResults.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,23 @@ class BaseResults
*/
private ?Exception $exception;

/**
* @var string|null Contains the ID of the token that was validated.
*/
private ?string $tokenId;

/**
* BaseResults constructor.
*
* @param bool $validationSucceed Indicates if the validation process succeeded or not.
* @param string|null $cause Contains a message explaining why the validation process failed.
*/
public function __construct(bool $validationSucceed, ?string $cause = null, ?Exception $exception = null)
public function __construct(bool $validationSucceed, ?string $cause = null, ?Exception $exception = null, ?string $tokenId = null)
{
$this->validationSucceed = $validationSucceed;
$this->cause = $cause;
$this->exception = $exception;
$this->tokenId = $tokenId;
}

/**
Expand Down Expand Up @@ -69,6 +75,16 @@ public function getException(): ?Exception
return $this->exception;
}

/**
* Getter method for the ID of the token that was validated.
*
* @return string|null Contains the ID of the token that was validated.
*/
public function getTokenId(): ?string
{
return $this->tokenId;
}

/**
* Creates a new BaseResultsBuilder object to build a new BaseResults instance.
*
Expand Down
16 changes: 15 additions & 1 deletion src/Results/BaseResultsBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class BaseResultsBuilder
private bool $validationSucceed;
private ?string $cause;
private ?Exception $exception;
private ?string $tokenId;

/**
* BaseResultsBuilder constructor.
Expand All @@ -23,6 +24,7 @@ public function __construct()
$this->validationSucceed = false;
$this->cause = null;
$this->exception = null;
$this->tokenId = null;
}

/**
Expand Down Expand Up @@ -61,14 +63,26 @@ public function setException(?Exception $exception): self
return $this;
}

/**
* Sets the ID of the token that was validated.
*
* @param string|null $tokenId Contains the ID of the token that was validated.
* @return $this This instance of the BaseResultsBuilder object.
*/
public function setTokenId(?string $tokenId): self
{
$this->tokenId = $tokenId;
return $this;
}

/**
* Builds the BaseResults object with the provided values.
*
* @return BaseResults The resulting BaseResults object.
*/
public function build(): BaseResults
{
return new BaseResults($this->validationSucceed, $this->cause, $this->exception);
return new BaseResults($this->validationSucceed, $this->cause, $this->exception, $this->tokenId);
}

/**
Expand Down
4 changes: 3 additions & 1 deletion src/TokensValidation.php
Original file line number Diff line number Diff line change
Expand Up @@ -543,9 +543,9 @@ private static function checkAuthToken_(string $fingerPrint, ?string $authToken,
$authTokenModel = $authTokenModel[0];
$authTokenResultBuilder->setUserId($authTokenModel->userId);
if (!self::isExpired(DateTime::createFromFormat('Y-m-d H:i:s', $authTokenModel->expire_at))) {

if ($authTokenModel->fingerprint == '' || $authTokenModel->fingerprint == $fingerPrint) {
$authTokenResultBuilder->setValidationSucceed(true);
$authTokenResultBuilder->setTokenId(strval($authTokenModel->id??""));
if ($regenerate) {
$newToken = self::regenerateAuthToken($authTokenModel->userId, $token, $authTokenModel->type);
$authTokenResultBuilder->setNewToken($newToken);
Expand Down Expand Up @@ -639,6 +639,7 @@ public static function checkConfirmationCode(string $code, string $encryptedUser
if ($confirmationTokenModel->whatFor == $whatFor || $whatFor == "default") {
$confirmationTokenResultsBuilder->setValidationSucceed(true);
$confirmationTokenResultsBuilder->withWhatFor($whatFor);
$confirmationTokenResultsBuilder->setTokenId(strval($confirmationTokenModel->id??""));
if ($deleteAfterCheck) {
ConfirmationTokenModel::find($confirmationTokenModel->id)->delete();
}
Expand Down Expand Up @@ -790,6 +791,7 @@ public static function checkInvitationToken(string $token, string $whatFor = "de
$invitationResultsBuilder->withUserId($invitationModel->userId);
if (!self::isExpired(DateTime::createFromFormat('Y-m-d H:i:s', $invitationModel->expire_at))) {
if ($invitationModel->whatFor == $whatFor || $whatFor == "default") {
$invitationResultsBuilder->setTokenId(strval($invitationModel->id??""));
$invitationResultsBuilder->setValidationSucceed(true);
$invitationResultsBuilder->withData($invitationModel->data);
$invitationResultsBuilder->withTargetEmail($invitationModel->target_email);
Expand Down

0 comments on commit cf6d401

Please sign in to comment.