| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
namespace MediaWiki\Extension\OATHAuth\Tests\Integration\HTMLForm; |
| 22 |
|
| 23 |
use Base32\Base32; |
| 24 |
use jakobo\HOTP\HOTP; |
| 25 |
use MediaWiki\Context\RequestContext; |
| 26 |
use MediaWiki\Extension\OATHAuth\HTMLForm\TOTPDisableForm; |
| 27 |
use MediaWiki\Extension\OATHAuth\Key\TOTPKey; |
| 28 |
use MediaWiki\Extension\OATHAuth\OATHAuthServices; |
| 29 |
use MediaWiki\MainConfigNames; |
| 30 |
use MediaWikiIntegrationTestCase; |
| 31 |
use Wikimedia\TestingAccessWrapper; |
| 32 |
|
| 33 |
|
| 34 |
tf> |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
class TOTPDisableFormTest extends MediaWikiIntegrationTestCase { |
| 39 |
protected function setUp(): void { |
| 40 |
parent::setUp(); |
| 41 |
$this->overrideConfigValue( MainConfigNames::CentralIdLookupProvider, 'local' ); |
| 42 |
} |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
private function setupFormAndKey(): array { |
| 49 |
$user = $this->getTestUser()->getUser(); |
| 50 |
$repository = OATHAuthServices::getInstance( $this->getServiceContainer() ) |
| 51 |
->getUserRepository(); |
| 52 |
$oathUser = $repository->findByUser( $user ); |
| 53 |
$module = OATHAuthServices::getInstance( $this->getServiceContainer() ) |
| 54 |
->getModuleRegistry() |
| 55 |
->getModuleByKey( 'totp' ); |
| 56 |
|
| 57 |
$key = $repository->createKey( |
| 58 |
$oathUser, |
| 59 |
$module, |
| 60 |
TOTPKey::newFromRandom()->jsonSerialize(), |
| 61 |
'127.0.0.1' |
| 62 |
); |
| 63 |
|
| 64 |
$form = new TOTPDisableForm( |
| 65 |
$oathUser, |
| 66 |
$repository, |
| 67 |
$module, |
| 68 |
RequestContext::getMain(), |
| 69 |
); |
| 70 |
|
| 71 |
return [ $form, $key, $oathUser ]; |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
public function testSubmitInvalidCode(): void { |
| 78 |
[ $form ] = $this->setupFormAndKey(); |
| 79 |
$this->assertEquals( |
| 80 |
[ 'oathauth-failedtovalidateoath' ], |
| 81 |
$form->onSubmit( [ 'token' => 'wrong' ] ), |
| 82 |
); |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
public function testSubmitCorrectToken(): void { |
| 89 |
[ $form, $key, $oathUser ] = $this->setupFormAndKey(); |
| 90 |
|
| 91 |
$secret = TestingAccessWrapper::newFromObject( $key )->secret; |
| 92 |
$correctToken = HOTP::generateByTime( |
| 93 |
Base32::decode( $secret['secret'] ), |
| 94 |
$secret['period'], |
| 95 |
)->toHOTP( 6 ); |
| 96 |
|
| 97 |
$this->assertTrue( $form->onSubmit( [ 'token' => $correctToken ] ) ); |
| 98 |
$this->assertEquals( [], $oathUser->getKeys() ); |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
public function testSubmitScratchToken(): void { |
| 105 |
[ $form, $key, $oathUser ] = $this->setupFormAndKey(); |
| 106 |
|
| 107 |
$this->assertTrue( $form->onSubmit( [ 'token' => $key->getScratchTokens()[0] ] ) ); |
| 108 |
$this->assertEquals( [], $oathUser->getKeys() ); |
| 109 |
} |
| 110 |
} |