Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame^] | 1 | <?php |
| 2 | |
| 3 | namespace Tests\Providers\Qr; |
| 4 | |
| 5 | use PHPUnit\Framework\TestCase; |
| 6 | use RobThree\Auth\TwoFactorAuth; |
| 7 | use RobThree\Auth\TwoFactorAuthException; |
| 8 | |
| 9 | class IQRCodeProviderTest extends TestCase |
| 10 | { |
| 11 | /** |
| 12 | * @param string $datauri |
| 13 | * |
| 14 | * @return null|array |
| 15 | */ |
| 16 | private function DecodeDataUri($datauri) |
| 17 | { |
| 18 | if (preg_match('/data:(?P<mimetype>[\w\.\-\/]+);(?P<encoding>\w+),(?P<data>.*)/', $datauri, $m) === 1) { |
| 19 | return array( |
| 20 | 'mimetype' => $m['mimetype'], |
| 21 | 'encoding' => $m['encoding'], |
| 22 | 'data' => base64_decode($m['data']) |
| 23 | ); |
| 24 | } |
| 25 | |
| 26 | return null; |
| 27 | } |
| 28 | |
| 29 | /** |
| 30 | * @return void |
| 31 | */ |
| 32 | public function testTotpUriIsCorrect() |
| 33 | { |
| 34 | $qr = new TestQrProvider(); |
| 35 | |
| 36 | $tfa = new TwoFactorAuth('Test&Issuer', 6, 30, 'sha1', $qr); |
| 37 | $data = $this->DecodeDataUri($tfa->getQRCodeImageAsDataUri('Test&Label', 'VMR466AB62ZBOKHE')); |
| 38 | $this->assertEquals('test/test', $data['mimetype']); |
| 39 | $this->assertEquals('base64', $data['encoding']); |
| 40 | $this->assertEquals('otpauth://totp/Test%26Label?secret=VMR466AB62ZBOKHE&issuer=Test%26Issuer&period=30&algorithm=SHA1&digits=6@200', $data['data']); |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * @return void |
| 45 | */ |
| 46 | public function testGetQRCodeImageAsDataUriThrowsOnInvalidSize() |
| 47 | { |
| 48 | $qr = new TestQrProvider(); |
| 49 | |
| 50 | $tfa = new TwoFactorAuth('Test', 6, 30, 'sha1', $qr); |
| 51 | |
| 52 | $this->expectException(TwoFactorAuthException::class); |
| 53 | |
| 54 | $tfa->getQRCodeImageAsDataUri('Test', 'VMR466AB62ZBOKHE', 0); |
| 55 | } |
| 56 | } |