| 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\Api\Module; |
| 22 |
|
| 23 |
use Base32\Base32; |
| 24 |
use jakobo\HOTP\HOTP; |
| 25 |
use MediaWiki\Extension\OATHAuth\Key\TOTPKey; |
| 26 |
use MediaWiki\Extension\OATHAuth\OATHAuthServices; |
| 27 |
use MediaWiki\MainConfigNames; |
| 28 |
use MediaWiki\Permissions\UltimateAuthority; |
| 29 |
use MediaWiki\Tests\Api\ApiTestCase; |
| 30 |
use Wikimedia\TestingAccessWrapper; |
| 31 |
|
| 32 |
|
| 33 |
tf> |
| 34 |
|
| 35 |
|
| 36 |
class ApiOATHValidateTest extends ApiTestCase { |
| 37 |
protected function setUp(): void { |
| 38 |
parent::setUp(); |
| 39 |
$this->overrideConfigValue( MainConfigNames::CentralIdLookupProvider, 'local' ); |
| 40 |
} |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
public function testNonexistentUser() { |
| 46 |
[ $result, ] = $this->doApiRequestWithToken( |
| 47 |
[ |
| 48 |
'action' => 'oathvalidate', |
| 49 |
'user' => 'AAAAAAAAAAAAAAAAAAAAAAAAAAAAA I am fake', |
| 50 |
'data' => '{"token": "123456"}', |
| 51 |
], |
| 52 |
null, |
| 53 |
new UltimateAuthority( $this->getTestUser()->getUserIdentity() ) |
| 54 |
); |
| 55 |
|
| 56 |
$this->assertArraySubmapSame( |
| 57 |
[ |
| 58 |
'oathvalidate' => [ |
| 59 |
'enabled' => false, |
| 60 |
'valid' => false, |
| 61 |
], |
| 62 |
], |
| 63 |
$result |
| 64 |
); |
| 65 |
} |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
public function testDisabled() { |
| 71 |
$testUser = $this->getTestUser(); |
| 72 |
|
| 73 |
[ $result, ] = $this->doApiRequestWithToken( |
| 74 |
[ |
| 75 |
'action' => 'oathvalidate', |
| 76 |
'user' => $testUser->getUserIdentity()->getName(), |
| 77 |
'data' => '{"token": "123456"}', |
| 78 |
], |
| 79 |
null, |
| 80 |
new UltimateAuthority( $testUser->getUserIdentity() ) |
| 81 |
); |
| 82 |
|
| 83 |
$this->assertArraySubmapSame( |
| 84 |
[ |
| 85 |
'oathvalidate' => [ |
| 86 |
'enabled' => false, |
| 87 |
'valid' => false, |
| 88 |
], |
| 89 |
], |
| 90 |
$result |
| 91 |
); |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
public function testCorrectToken() { |
| 98 |
$testUser = $this->getTestUser(); |
| 99 |
|
| 100 |
$key = TOTPKey::newFromRandom(); |
| 101 |
$userRepository = OATHAuthServices::getInstance( $this->getServiceContainer() )->getUserRepository(); |
| 102 |
$userRepository->createKey( |
| 103 |
$userRepository->findByUser( $testUser->getUserIdentity() ), |
| 104 |
OATHAuthServices::getInstance( $this->getServiceContainer() ) |
| 105 |
->getModuleRegistry() |
| 106 |
->getModuleByKey( 'totp' ), |
| 107 |
$key->jsonSerialize(), |
| 108 |
'127.0.0.1' |
| 109 |
); |
| 110 |
|
| 111 |
$secret = TestingAccessWrapper::newFromObject( $key )->secret; |
| 112 |
$correctToken = HOTP::generateByTime( |
| 113 |
Base32::decode( $secret['secret'] ), |
| 114 |
$secret['period'], |
| 115 |
)->toHOTP( 6 ); |
| 116 |
|
| 117 |
[ $result, ] = $this->doApiRequestWithToken( |
| 118 |
[ |
| 119 |
'action' => 'oathvalidate', |
| 120 |
'user' => $testUser->getUserIdentity()->getName(), |
| 121 |
'data' => json_encode( [ 'token' => $correctToken ] ), |
| 122 |
], |
| 123 |
null, |
| 124 |
new UltimateAuthority( $testUser->getUserIdentity() ) |
| 125 |
); |
| 126 |
|
| 127 |
$this->assertArraySubmapSame( |
| 128 |
[ |
| 129 |
'oathvalidate' => [ |
| 130 |
'enabled' => true, |
| 131 |
'valid' => true, |
| 132 |
], |
| 133 |
], |
| 134 |
$result |
| 135 |
); |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
public function testWrongToken() { |
| 142 |
$testUser = $this->getTestUser(); |
| 143 |
|
| 144 |
$userRepository = OATHAuthServices::getInstance( $this->getServiceContainer() )->getUserRepository(); |
| 145 |
$userRepository->createKey( |
| 146 |
$userRepository->findByUser( $testUser->getUserIdentity() ), |
| 147 |
OATHAuthServices::getInstance( $this->getServiceContainer() ) |
| 148 |
->getModuleRegistry() |
| 149 |
->getModuleByKey( 'totp' ), |
| 150 |
TOTPKey::newFromRandom()->jsonSerialize(), |
| 151 |
'127.0.0.1' |
| 152 |
); |
| 153 |
|
| 154 |
[ $result, ] = $this->doApiRequestWithToken( |
| 155 |
[ |
| 156 |
'action' => 'oathvalidate', |
| 157 |
'user' => $testUser->getUserIdentity()->getName(), |
| 158 |
'data' => json_encode( [ 'token' => '000000' ] ), |
| 159 |
], |
| 160 |
null, |
| 161 |
new UltimateAuthority( $testUser->getUserIdentity() ) |
| 162 |
); |
| 163 |
|
| 164 |
$this->assertArraySubmapSame( |
| 165 |
[ |
| 166 |
'oathvalidate' => [ |
| 167 |
'enabled' => true, |
| 168 |
'valid' => false, |
| 169 |
], |
| 170 |
], |
| 171 |
$result |
| 172 |
); |
| 173 |
} |
| 174 |
} |