web-base/test/TimeBasedTwoFactorToken.test.php

44 lines
1.3 KiB
PHP
Raw Normal View History

2022-02-20 16:53:26 +01:00
<?php
use Base32\Base32;
use Configuration\Configuration;
use Objects\TwoFactor\TimeBasedTwoFactorToken;
2022-06-20 19:52:31 +02:00
use Objects\DatabaseEntity\User;
2022-02-20 16:53:26 +01:00
class TimeBasedTwoFactorTokenTest extends PHPUnit\Framework\TestCase {
// https://tools.ietf.org/html/rfc6238
public function testTOTP() {
$secret = Base32::encode("12345678901234567890");
$token = new TimeBasedTwoFactorToken($secret);
$totp_tests = [
59 => '94287082',
1111111109 => '07081804',
1111111111 => '14050471',
1234567890 => '89005924',
2000000000 => '69279037',
20000000000 => '65353130',
];
$period = 30;
$totp_length = 8;
foreach ($totp_tests as $seed => $code) {
$generated = $token->generate($seed, $totp_length, $period);
$this->assertEquals($code, $generated, "$code != $generated, at=$seed");
}
}
public function testURL() {
$secret = Base32::encode("12345678901234567890");
2022-06-20 19:52:31 +02:00
$context = new \Objects\Context();
// $context->
2022-02-20 16:53:26 +01:00
$token = new TimeBasedTwoFactorToken($secret);
2022-06-20 19:52:31 +02:00
$siteName = $context->getSettings()->getSiteName();
$username = $context->getUser()->getUsername();
$url = $token->getUrl($context);
2022-02-20 16:53:26 +01:00
$this->assertEquals("otpauth://totp/$username?secret=$secret&issuer=$siteName", $url);
}
}