Skip to content

Commit

Permalink
Merge pull request #3 from abmmhasan/feature/enhancement
Browse files Browse the repository at this point in the history
Enhanced the OTP functionality and made the documentation more detailed.
  • Loading branch information
abmmhasan authored Nov 26, 2023
2 parents 33800c0 + 5475fe1 commit ac94d39
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,21 @@ $otp = $otpInstance->generate('an unique signature for a cause');

/**
* Verify the OTP
*
* on 3rd parameter setting false will keep the record till the otp is verified or expired
* by default it will keep the record till the key name match or the otp is verified or expired
*/
$otpInstance->verify('an unique signature for a cause', $otp);

/**
* Delete the record
*/
$otpInstance->delete('an unique signature for a cause');

/**
* Flush all the existing OTPs (if any)
*/
$otpInstance->flush()
```
_Note: Generic OTP uses **temporary location** for storage, make sure you have proper access permission_

Expand Down
24 changes: 23 additions & 1 deletion src/OTP.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,34 @@ public function verify(string $signature, int $otp, bool $deleteIfFound = true):
$otpAdapter = $this->cacheAdapter->getItem($signature);
if ($otpAdapter->isHit()) {
$isVerified = hash_equals($otpAdapter->get(), hash($this->hashAlgorithm, $otp));
$deleteIfFound && $this->cacheAdapter->deleteItem($signature);
($deleteIfFound || $isVerified) && $this->cacheAdapter->deleteItem($signature);
return $isVerified;
}
return false;
}

/**
* Deletes an OTP based on the given signature.
*
* @param string $signature The signature of the item to be deleted.
* @return bool True if the item was successfully deleted, false otherwise.
* @throws InvalidArgumentException
*/
public function delete(string $signature): bool
{
return $this->cacheAdapter->deleteItem('ao-otp_' . base64_encode($signature));
}

/**
* Flushes all the OTPs.
*
* @return bool
*/
public function flush(): bool
{
return $this->cacheAdapter->clear();
}

/**
* Generate Secure random number of given length
*
Expand Down

0 comments on commit ac94d39

Please sign in to comment.