Skip to content

Commit

Permalink
bc: verify() method no longer destroys the token by default
Browse files Browse the repository at this point in the history
  • Loading branch information
hidehalo committed Jun 6, 2020
1 parent 0192065 commit 7025f63
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 8 deletions.
5 changes: 3 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
.idea/
.vscode/
.DS_Store/
.DS_Store
composer.lock
*.swp
vendor/
vendor/
builds/
21 changes: 18 additions & 3 deletions src/NumericCaptcha.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,13 @@ private function getToken($key)
*/
public function generate($length = 6, $key = null)
{
$token = '';
$token = $this->nano->formattedId('1234567890', $length);
$ret = $this->cache->set($this->safeKey($key), $token, $this->timeout);
// @codeCoverageIgnoreStart
if (!$ret) {
$token = '';
throw new \RuntimeException('Cache set failed.');
}
// @codeCoverageIgnoreEnd

return $token;
}
Expand All @@ -103,14 +104,28 @@ public function verify($token, $key = null)
$realToken = $this->getToken($key);
if ($realToken && $realToken == $token) {
$ret = VerifyResult::VERIFY_OK;
$this->cache->delete($this->safeKey($key));
} elseif (!$realToken) {
$ret = VerifyResult::VERIFY_EXPIRED;
}
// @codeCoverageIgnoreStart
} catch (Exception $e) {
$ret = VerifyResult::VERIFY_FAILED;
} finally {
// @codeCoverageIgnoreEnd
return new VerifyResult($ret);
}
// @codeCoverageIgnoreStart
}
// @codeCoverageIgnoreEnd

/**
* Destroy token
*
* @param string $key
* @return bool
*/
public function destroy($key = null)
{
return $this->cache->delete($this->safeKey($key));
}
}
10 changes: 7 additions & 3 deletions tests/NumericCaptchaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function testGenerateToken(NumericCaptcha $captcha)
*
* @return void
*/
public function testVerify(NumericCaptcha $captcha)
public function testVerifyNDestroy(NumericCaptcha $captcha)
{
$testLen = mt_rand(6, 64);
$token = $captcha->generate($testLen, "TEST_KEY");
Expand All @@ -36,8 +36,12 @@ public function testVerify(NumericCaptcha $captcha)
$result2 = $captcha->verify($token, "TEST_KEY");
$this->assertTrue($result2->isSuccessful());

$result3 = $captcha->verify($token);
$this->assertTrue($result3->isExpired());
$result3 = $captcha->verify($token, "TEST_KEY");
$this->assertTrue($result3->isSuccessful());

$this->assertTrue($captcha->destroy("TEST_KEY"));
$result4 = $captcha->verify($token, "TEST_KEY");
$this->assertTrue($result4->isExpired());
}

//
Expand Down

0 comments on commit 7025f63

Please sign in to comment.