Debian Codegraph @

extensions/OATHAuth/tests/phpunit/integration/Api/Module/ApiOATHValidateTest.php

raw symbols SCIP
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @file
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 * @author Taavi Väänänen <[email protected]tf>
34 * @group Database
35 */
36 class ApiOATHValidateTest extends ApiTestCase {
37 protected function setUp(): void {
38 parent::setUp();
39 $this->overrideConfigValue( MainConfigNames::CentralIdLookupProvider, 'local' );
40 }
41
42 /**
43 * @covers \MediaWiki\Extension\OATHAuth\Api\Module\ApiOATHValidate::execute
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 * @covers \MediaWiki\Extension\OATHAuth\Api\Module\ApiOATHValidate::execute
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 * @covers \MediaWiki\Extension\OATHAuth\Api\Module\ApiOATHValidate::execute
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 * @covers \MediaWiki\Extension\OATHAuth\Api\Module\ApiOATHValidate::execute
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 }