|
| 1 | +<?php |
| 2 | + |
| 3 | +use CodingLibs\MFA\Totp\GoogleTotp; |
| 4 | + |
| 5 | +it('rejects invalid code formats', function () { |
| 6 | + $secret = GoogleTotp::generateSecret(); |
| 7 | + // Non-numeric and wrong length |
| 8 | + expect(GoogleTotp::verifyCode($secret, 'abcdef', 6, 30, 1))->toBeFalse(); |
| 9 | + expect(GoogleTotp::verifyCode($secret, '123', 6, 30, 1))->toBeFalse(); |
| 10 | + // Very long string |
| 11 | + expect(GoogleTotp::verifyCode($secret, str_repeat('9', 20), 6, 30, 1))->toBeFalse(); |
| 12 | +}); |
| 13 | + |
| 14 | +it('supports 4 and 8 digit OTPs using exact slice', function () { |
| 15 | + $secret = GoogleTotp::generateSecret(); |
| 16 | + $ref = new ReflectionClass(GoogleTotp::class); |
| 17 | + $base32Decode = $ref->getMethod('base32Decode'); |
| 18 | + $base32Decode->setAccessible(true); |
| 19 | + $hotp = $ref->getMethod('hotp'); |
| 20 | + $hotp->setAccessible(true); |
| 21 | + $truncate = $ref->getMethod('truncateToDigits'); |
| 22 | + $truncate->setAccessible(true); |
| 23 | + |
| 24 | + $period = 30; |
| 25 | + $timeSlice = (int) floor(time() / $period); |
| 26 | + $key = $base32Decode->invoke(null, $secret); |
| 27 | + |
| 28 | + $hash = $hotp->invoke(null, $key, $timeSlice); |
| 29 | + $code4 = $truncate->invoke(null, $hash, 4); |
| 30 | + $code8 = $truncate->invoke(null, $hash, 8); |
| 31 | + |
| 32 | + expect(GoogleTotp::verifyCode($secret, $code4, 4, $period, 0))->toBeTrue(); |
| 33 | + expect(GoogleTotp::verifyCode($secret, $code8, 8, $period, 0))->toBeTrue(); |
| 34 | +}); |
| 35 | + |
| 36 | +it('respects zero window (adjacent slice fails)', function () { |
| 37 | + $secret = GoogleTotp::generateSecret(); |
| 38 | + $ref = new ReflectionClass(GoogleTotp::class); |
| 39 | + $base32Decode = $ref->getMethod('base32Decode'); |
| 40 | + $base32Decode->setAccessible(true); |
| 41 | + $hotp = $ref->getMethod('hotp'); |
| 42 | + $hotp->setAccessible(true); |
| 43 | + $truncate = $ref->getMethod('truncateToDigits'); |
| 44 | + $truncate->setAccessible(true); |
| 45 | + |
| 46 | + $period = 30; |
| 47 | + $digits = 6; |
| 48 | + $timeSlice = (int) floor(time() / $period); |
| 49 | + $key = $base32Decode->invoke(null, $secret); |
| 50 | + |
| 51 | + $prev = $truncate->invoke(null, $hotp->invoke(null, $key, $timeSlice - 1), $digits); |
| 52 | + $curr = $truncate->invoke(null, $hotp->invoke(null, $key, $timeSlice), $digits); |
| 53 | + |
| 54 | + expect(GoogleTotp::verifyCode($secret, $prev, $digits, $period, 0))->toBeFalse(); |
| 55 | + expect(GoogleTotp::verifyCode($secret, $curr, $digits, $period, 0))->toBeTrue(); |
| 56 | +}); |
| 57 | + |
| 58 | + |
0 commit comments