20
20
namespace MediaWiki \Extension \AuthManagerOAuth ;
21
21
22
22
use League \OAuth2 \Client \Provider \GenericProvider ;
23
+ use LogicException ;
23
24
use MediaWiki \Auth \AuthenticationRequest ;
24
25
use MediaWiki \Auth \AuthenticationResponse ;
25
26
use MediaWiki \MediaWikiServices ;
26
27
28
+ // https://doc.wikimedia.org/mediawiki-core/master/php/classMediaWiki_1_1Auth_1_1AbstractPrimaryAuthenticationProvider.html
27
29
class AuthManagerOAuthPrimaryAuthenticationProvider extends \MediaWiki \Auth \AbstractPrimaryAuthenticationProvider {
28
30
29
31
private const AUTHENTICATION_SESSION_DATA_STATE = 'authmanageroauth:state ' ;
@@ -44,8 +46,9 @@ public function getAuthenticationRequests( $action, array $options ) {
44
46
}
45
47
return $ reqs ;
46
48
}
47
- if ( $ options ['username ' ] !== null && ( $ action === \MediaWiki \Auth \AuthManager::ACTION_REMOVE ||
48
- $ action === \MediaWiki \Auth \AuthManager::ACTION_CHANGE ) ) {
49
+ if ( $ options ['username ' ] !== null
50
+ && ( $ action === \MediaWiki \Auth \AuthManager::ACTION_REMOVE
51
+ || $ action === \MediaWiki \Auth \AuthManager::ACTION_UNLINK ) ) {
49
52
$ user = \User::newFromName ( $ options ['username ' ] );
50
53
$ lb = MediaWikiServices::getInstance ()->getDBLoadBalancer ();
51
54
$ dbr = $ lb ->getConnectionRef ( DB_REPLICA );
@@ -68,7 +71,7 @@ public function getAuthenticationRequests( $action, array $options ) {
68
71
* All our users need to also be created locally so always return false here.
69
72
* @inheritDoc
70
73
*/
71
- public function testUserExists ( $ username , $ flags = User::READ_NORMAL ) {
74
+ public function testUserExists ( $ username , $ flags = \ User::READ_NORMAL ) {
72
75
return false ;
73
76
}
74
77
@@ -77,9 +80,9 @@ public function testUserExists( $username, $flags = User::READ_NORMAL ) {
77
80
*/
78
81
public function providerAllowsAuthenticationDataChange ( AuthenticationRequest $ req , $ checkData = true ) {
79
82
wfDebugLog ( 'AuthManagerOAuth providerAllowsAuthenticationDataChange ' , var_export ( $ req , true ) );
80
- if ( get_class ( $ req ) === UnlinkOAuthAccountRequest::class
83
+ if ( $ req instanceof UnlinkOAuthAccountRequest
81
84
&& ( $ req ->action === \MediaWiki \Auth \AuthManager::ACTION_REMOVE
82
- || $ req ->action === \MediaWiki \Auth \AuthManager::ACTION_CHANGE ) ) {
85
+ || $ req ->action === \MediaWiki \Auth \AuthManager::ACTION_UNLINK ) ) {
83
86
return \StatusValue::newGood ();
84
87
}
85
88
return \StatusValue::newGood ( 'ignored ' );
@@ -90,9 +93,9 @@ public function providerAllowsAuthenticationDataChange( AuthenticationRequest $r
90
93
*/
91
94
public function providerChangeAuthenticationData ( AuthenticationRequest $ req ) {
92
95
wfDebugLog ( 'AuthManagerOAuth providerChangeAuthenticationData ' , var_export ( $ req , true ) );
93
- if ( get_class ( $ req ) === UnlinkOAuthAccountRequest::class
96
+ if ( $ req instanceof UnlinkOAuthAccountRequest
94
97
&& ( $ req ->action === \MediaWiki \Auth \AuthManager::ACTION_REMOVE
95
- || $ req ->action === \MediaWiki \Auth \AuthManager::ACTION_CHANGE ) ) {
98
+ || $ req ->action === \MediaWiki \Auth \AuthManager::ACTION_UNLINK ) ) {
96
99
$ user = \User::newFromName ( $ req ->username );
97
100
$ lb = MediaWikiServices::getInstance ()->getDBLoadBalancer ();
98
101
$ dbr = $ lb ->getConnectionRef ( DB_PRIMARY );
@@ -105,6 +108,8 @@ public function providerChangeAuthenticationData( AuthenticationRequest $req ) {
105
108
],
106
109
__METHOD__ ,
107
110
);
111
+ } else {
112
+ throw new LogicException ( "Unexpected unhandled request " );
108
113
}
109
114
}
110
115
@@ -124,7 +129,7 @@ public function accountCreationType() {
124
129
private function beginPrimary ( array $ reqs ) {
125
130
wfDebugLog ( 'AuthManagerOAuth beginPrimary* ' , var_export ( $ reqs , true ) );
126
131
$ req = AuthenticationRequest::getRequestByClass ( $ reqs , ChooseOAuthProviderRequest::class );
127
- if ( $ req !== null ) {
132
+ if ( $ req !== null && $ req instanceof ChooseOAuthProviderRequest ) {
128
133
$ config = MediaWikiServices::getInstance ()->getConfigFactory ()->makeConfig ( 'authmanageroauth ' );
129
134
$ provider = new GenericProvider ( $ config ->get ( 'AuthManagerOAuthConfig ' )[$ req ->amoa_provider ] );
130
135
$ authorizationUrl = $ provider ->getAuthorizationUrl ( [
@@ -174,7 +179,7 @@ public function beginPrimaryAccountLink( $user, array $reqs ) {
174
179
* Convert the response of an OAuth redirect to the identity it represents for further use.
175
180
* This asks the OAuth provider to verify the the login and gets the remote username and id.
176
181
* @param OAuthProviderAuthenticationRequest $req
177
- * @return OAuthIdentityRequest
182
+ * @return AuthenticationResponse
178
183
*/
179
184
private function convertOAuthProviderAuthenticationRequestToOAuthIdentityRequest ( $ req ) {
180
185
$ config = MediaWikiServices::getInstance ()->getConfigFactory ()->makeConfig ( 'authmanageroauth ' );
@@ -231,22 +236,24 @@ public function continuePrimaryAuthentication( array $reqs ) {
231
236
wfDebugLog ( 'AuthManagerOAuth continuePrimaryAuthentication ' , var_export ( $ reqs , true ) );
232
237
233
238
$ identity_req = AuthenticationRequest::getRequestByClass ( $ reqs , OAuthIdentityRequest::class );
234
- if ( $ identity_req !== null ) {
239
+ if ( $ identity_req !== null && $ identity_req instanceof OAuthIdentityRequest ) {
235
240
// Already authenticated with OAuth provider
236
241
237
242
$ choose_local_account_req = AuthenticationRequest::getRequestByClass (
238
243
$ reqs ,
239
244
ChooseLocalAccountRequest::class
240
245
);
241
- if ( $ choose_local_account_req !== null ) {
246
+ if ( $ choose_local_account_req !== null
247
+ && $ choose_local_account_req instanceof ChooseLocalAccountRequest ) {
242
248
return AuthenticationResponse::newPass ( $ choose_local_account_req ->username );
243
249
}
244
250
245
251
$ choose_local_username_req = AuthenticationRequest::getRequestByClass (
246
252
$ reqs ,
247
253
LocalUsernameInputRequest::class
248
254
);
249
- if ( $ choose_local_username_req !== null ) {
255
+ if ( $ choose_local_username_req !== null
256
+ && $ choose_local_username_req instanceof LocalUsernameInputRequest ) {
250
257
$ user = \User::newFromName ( $ choose_local_username_req ->local_username );
251
258
// TODO FIXME query on primary race condition https://phabricator.wikimedia.org/T138678#3911381
252
259
if ( !$ user ->isRegistered () ) {
@@ -258,11 +265,16 @@ public function continuePrimaryAuthentication( array $reqs ) {
258
265
}
259
266
260
267
$ req = AuthenticationRequest::getRequestByClass ( $ reqs , OAuthProviderAuthenticationRequest::class );
261
- if ( $ req !== null ) {
268
+ if ( $ req !== null && $ req instanceof OAuthProviderAuthenticationRequest ) {
262
269
$ resp = $ this ->convertOAuthProviderAuthenticationRequestToOAuthIdentityRequest ( $ req );
263
270
if ( $ resp ->status !== AuthenticationResponse::PASS ) {
264
271
return $ resp ;
265
272
}
273
+ if ( !( $ resp ->linkRequest instanceof OAuthIdentityRequest ) ) {
274
+ throw new LogicException (
275
+ "Unexpected createRequest type {$ {get_class ($ req )}}. This should never happen. "
276
+ );
277
+ }
266
278
267
279
$ lb = MediaWikiServices::getInstance ()->getDBLoadBalancer ();
268
280
$ dbr = $ lb ->getConnectionRef ( DB_REPLICA );
@@ -301,11 +313,16 @@ public function continuePrimaryAuthentication( array $reqs ) {
301
313
public function continuePrimaryAccountLink ( $ user , array $ reqs ) {
302
314
wfDebugLog ( 'AuthManagerOAuth continuePrimaryAccountLink ' , var_export ( $ reqs , true ) );
303
315
$ req = AuthenticationRequest::getRequestByClass ( $ reqs , OAuthProviderAuthenticationRequest::class );
304
- if ( $ req !== null ) {
316
+ if ( $ req !== null && $ req instanceof OAuthProviderAuthenticationRequest ) {
305
317
$ resp = $ this ->convertOAuthProviderAuthenticationRequestToOAuthIdentityRequest ( $ req );
306
318
if ( $ resp ->status !== AuthenticationResponse::PASS ) {
307
319
return $ resp ;
308
320
}
321
+ if ( !( $ resp ->linkRequest instanceof OAuthIdentityRequest ) ) {
322
+ throw new LogicException (
323
+ "Unexpected createRequest type {$ {get_class ($ req )}}. This should never happen. "
324
+ );
325
+ }
309
326
310
327
$ lb = MediaWikiServices::getInstance ()->getDBLoadBalancer ();
311
328
$ dbr = $ lb ->getConnectionRef ( DB_PRIMARY );
@@ -354,6 +371,9 @@ public function autoCreatedAccount( $user, $source ) {
354
371
public function finishAccountCreation ( $ user , $ creator , AuthenticationResponse $ response ) {
355
372
wfDebugLog ( 'AuthManagerOAuth finishAccountCreation ' , var_export ( $ response , true ) );
356
373
$ req = $ response ->createRequest ;
374
+ if ( !( $ req instanceof OAuthIdentityRequest ) ) {
375
+ throw new LogicException ( "Unexpected createRequest type {$ {get_class ($ req )}}. This should never happen. " );
376
+ }
357
377
$ lb = MediaWikiServices::getInstance ()->getDBLoadBalancer ();
358
378
$ dbr = $ lb ->getConnectionRef ( DB_PRIMARY );
359
379
$ result = $ dbr ->insert (
@@ -366,4 +386,7 @@ public function finishAccountCreation( $user, $creator, AuthenticationResponse $
366
386
__METHOD__ ,
367
387
);
368
388
}
389
+
390
+ // TODO providerNormalizeUsername()
391
+ // TODO providerRevokeAccessForUser
369
392
}
0 commit comments