Debian Codegraph @

includes/session/BotPasswordSessionProvider.php

raw symbols SCIP
1 <?php
2 /**
3 * Session provider for bot passwords
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup Session
22 */
23
24 namespace MediaWiki\Session;
25
26 use InvalidArgumentException;
27 use MediaWiki\MainConfigNames;
28 use MediaWiki\Permissions\GrantsInfo;
29 use MediaWiki\Request\WebRequest;
30 use MediaWiki\User\BotPassword;
31 use MediaWiki\User\User;
32 use MWRestrictions;
33
34 /**
35 * Session provider for bot passwords
36 * @since 1.27
37 */
38 class BotPasswordSessionProvider extends ImmutableSessionProviderWithCookie {
39 /** @var GrantsInfo */
40 private $grantsInfo;
41
42 /** @var bool Whether the current request is an API request. */
43 private $isApiRequest;
44
45 /**
46 * @param GrantsInfo $grantsInfo
47 * @param array $params Keys include:
48 * - priority: (required) Set the priority
49 * - sessionCookieName: Session cookie name. Default is '_BPsession'.
50 * - sessionCookieOptions: Options to pass to WebResponse::setCookie().
51 * - isApiRequest: Whether the current request is an API request. Should be only set in tests.
52 */
53 public function __construct( GrantsInfo $grantsInfo, array $params = [] ) {
54 if ( !isset( $params['sessionCookieName'] ) ) {
55 $params['sessionCookieName'] = '_BPsession';
56 }
57 parent::__construct( $params );
58
59 if ( !isset( $params['priority'] ) ) {
60 throw new InvalidArgumentException( __METHOD__ . ': priority must be specified' );
61 }
62 if ( $params['priority'] < SessionInfo::MIN_PRIORITY ||
63 $params['priority'] > SessionInfo::MAX_PRIORITY
64 ) {
65 throw new InvalidArgumentException( __METHOD__ . ': Invalid priority' );
66 }
67
68 $this->priority = $params['priority'];
69
70 $this->grantsInfo = $grantsInfo;
71
72 $this->isApiRequest = $params['isApiRequest']
73 ?? ( defined( 'MW_API' ) || defined( 'MW_REST_API' ) );
74 }
75
76 public function provideSessionInfo( WebRequest $request ) {
77 // Only relevant for the (Action or REST) API
78 if ( !$this->isApiRequest ) {
79 return null;
80 }
81
82 // Enabled?
83 if ( !$this->getConfig()->get( MainConfigNames::EnableBotPasswords ) ) {
84 return null;
85 }
86
87 // Have a session ID?
88 $id = $this->getSessionIdFromCookie( $request );
89 if ( $id === null ) {
90 return null;
91 }
92
93 return new SessionInfo( $this->priority, [
94 'provider' => $this,
95 'id' => $id,
96 'persisted' => true
97 ] );
98 }
99
100 public function newSessionInfo( $id = null ) {
101 // We don't activate by default
102 return null;
103 }
104
105 /**
106 * Create a new session for a request
107 * @param User $user
108 * @param BotPassword $bp
109 * @param WebRequest $request
110 * @return Session
111 */
112 public function newSessionForRequest( User $user, BotPassword $bp, WebRequest $request ) {
113 $id = $this->getSessionIdFromCookie( $request );
114 $info = new SessionInfo( SessionInfo::MAX_PRIORITY, [
115 'provider' => $this,
116 'id' => $id,
117 'userInfo' => UserInfo::newFromUser( $user, true ),
118 'persisted' => $id !== null,
119 'metadata' => [
120 'centralId' => $bp->getUserCentralId(),
121 'appId' => $bp->getAppId(),
122 'token' => $bp->getToken(),
123 'rights' => $this->grantsInfo->getGrantRights( $bp->getGrants() ),
124 'restrictions' => $bp->getRestrictions()->toJson(),
125 ],
126 ] );
127 $session = $this->getManager()->getSessionFromInfo( $info, $request );
128 $session->persist();
129 return $session;
130 }
131
132 /**
133 * @inheritDoc
134 * @phan-param array &$metadata
135 */
136 public function refreshSessionInfo( SessionInfo $info, WebRequest $request, &$metadata ) {
137 $missingKeys = array_diff(
138 [ 'centralId', 'appId', 'token' ],
139 array_keys( $metadata )
140 );
141 if ( $missingKeys ) {
142 $this->logger->info( 'Session "{session}": Missing metadata: {missing}', [
143 'session' => $info->__toString(),
144 'missing' => implode( ', ', $missingKeys ),
145 ] );
146 return false;
147 }
148
149 $bp = BotPassword::newFromCentralId( $metadata['centralId'], $metadata['appId'] );
150 if ( !$bp ) {
151 $this->logger->info(
152 'Session "{session}": No BotPassword for {centralId} {appId}',
153 [
154 'session' => $info->__toString(),
155 'centralId' => $metadata['centralId'],
156 'appId' => $metadata['appId'],
157 ] );
158 return false;
159 }
160
161 if ( !hash_equals( $metadata['token'], $bp->getToken() ) ) {
162 $this->logger->info( 'Session "{session}": BotPassword token check failed', [
163 'session' => $info->__toString(),
164 'centralId' => $metadata['centralId'],
165 'appId' => $metadata['appId'],
166 ] );
167 return false;
168 }
169
170 $status = $bp->getRestrictions()->check( $request );
171 if ( !$status->isOK() ) {
172 $this->logger->info(
173 'Session "{session}": Restrictions check failed',
174 [
175 'session' => $info->__toString(),
176 'restrictions' => $status->getValue(),
177 'centralId' => $metadata['centralId'],
178 'appId' => $metadata['appId'],
179 ] );
180 return false;
181 }
182
183 // Update saved rights
184 $metadata['rights'] = $this->grantsInfo->getGrantRights( $bp->getGrants() );
185
186 return true;
187 }
188
189 /**
190 * @codeCoverageIgnore
191 * @inheritDoc
192 */
193 public function preventSessionsForUser( $username ) {
194 BotPassword::removeAllPasswordsForUser( $username );
195 }
196
197 public function getAllowedUserRights( SessionBackend $backend ) {
198 if ( $backend->getProvider() !== $this ) {
199 throw new InvalidArgumentException( 'Backend\'s provider isn\'t $this' );
200 }
201 $data = $backend->getProviderMetadata();
202 if ( $data && isset( $data['rights'] ) && is_array( $data['rights'] ) ) {
203 return $data['rights'];
204 }
205
206 // Should never happen
207 $this->logger->debug( __METHOD__ . ': No provider metadata, returning no rights allowed' );
208 return [];
209 }
210
211 public function getRestrictions( ?array $data ): ?MWRestrictions {
212 if ( $data && isset( $data['restrictions'] ) && is_string( $data['restrictions'] ) ) {
213 try {
214 return MWRestrictions::newFromJson( $data['restrictions'] );
215 } catch ( InvalidArgumentException $e ) {
216 $this->logger->warning( __METHOD__ . ': Failed to parse restrictions: {restrictions}', [
217 'restrictions' => $data['restrictions']
218 ] );
219 return null;
220 }
221 }
222 return null;
223 }
224 }