2020namespace MediaWiki \Extension \AuthManagerOAuth ;
2121
2222use League \OAuth2 \Client \Provider \GenericProvider ;
23+ use LogicException ;
2324use MediaWiki \Auth \AuthenticationRequest ;
2425use MediaWiki \Auth \AuthenticationResponse ;
2526use MediaWiki \MediaWikiServices ;
2627
28+ // https://doc.wikimedia.org/mediawiki-core/master/php/classMediaWiki_1_1Auth_1_1AbstractPrimaryAuthenticationProvider.html
2729class AuthManagerOAuthPrimaryAuthenticationProviderextends \MediaWiki \Auth \AbstractPrimaryAuthenticationProvider {
2830
2931private const AUTHENTICATION_SESSION_DATA_STATE ='authmanageroauth:state ' ;
@@ -44,8 +46,9 @@ public function getAuthenticationRequests( $action, array $options ) {
4446}
4547return $ reqs ;
4648}
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 ) ) {
4952$ user = \User::newFromName ($ options ['username ' ] );
5053$ lb = MediaWikiServices::getInstance ()->getDBLoadBalancer ();
5154$ dbr =$ lb ->getConnectionRef (DB_REPLICA );
@@ -68,7 +71,7 @@ public function getAuthenticationRequests( $action, array $options ) {
6871 * All our users need to also be created locally so always return false here.
6972 * @inheritDoc
7073 */
71- public function testUserExists ($ username ,$ flags = User::READ_NORMAL ) {
74+ public function testUserExists ($ username ,$ flags =\ User::READ_NORMAL ) {
7275return false ;
7376}
7477
@@ -77,9 +80,9 @@ public function testUserExists( $username, $flags = User::READ_NORMAL ) {
7780 */
7881public function providerAllowsAuthenticationDataChange (AuthenticationRequest $ req ,$ checkData =true ) {
7982wfDebugLog ('AuthManagerOAuth providerAllowsAuthenticationDataChange ' ,var_export ($ req ,true ) );
80- if (get_class ( $ req) === UnlinkOAuthAccountRequest::class
83+ if ($ reqinstanceof UnlinkOAuthAccountRequest
8184 && ($ req ->action === \MediaWiki \Auth \AuthManager::ACTION_REMOVE
82- ||$ req ->action === \MediaWiki \Auth \AuthManager::ACTION_CHANGE ) ) {
85+ ||$ req ->action === \MediaWiki \Auth \AuthManager::ACTION_UNLINK ) ) {
8386return \StatusValue::newGood ();
8487}
8588return \StatusValue::newGood ('ignored ' );
@@ -90,9 +93,9 @@ public function providerAllowsAuthenticationDataChange( AuthenticationRequest $r
9093 */
9194public function providerChangeAuthenticationData (AuthenticationRequest $ req ) {
9295wfDebugLog ('AuthManagerOAuth providerChangeAuthenticationData ' ,var_export ($ req ,true ) );
93- if (get_class ( $ req) === UnlinkOAuthAccountRequest::class
96+ if ($ reqinstanceof UnlinkOAuthAccountRequest
9497 && ($ req ->action === \MediaWiki \Auth \AuthManager::ACTION_REMOVE
95- ||$ req ->action === \MediaWiki \Auth \AuthManager::ACTION_CHANGE ) ) {
98+ ||$ req ->action === \MediaWiki \Auth \AuthManager::ACTION_UNLINK ) ) {
9699$ user = \User::newFromName ($ req ->username );
97100$ lb = MediaWikiServices::getInstance ()->getDBLoadBalancer ();
98101$ dbr =$ lb ->getConnectionRef (DB_PRIMARY );
@@ -105,6 +108,8 @@ public function providerChangeAuthenticationData( AuthenticationRequest $req ) {
105108],
106109__METHOD__ ,
107110);
111+ }else {
112+ throw new LogicException ("Unexpected unhandled request " );
108113}
109114}
110115
@@ -124,7 +129,7 @@ public function accountCreationType() {
124129private function beginPrimary (array $ reqs ) {
125130wfDebugLog ('AuthManagerOAuth beginPrimary* ' ,var_export ($ reqs ,true ) );
126131$ req = AuthenticationRequest::getRequestByClass ($ reqs , ChooseOAuthProviderRequest::class );
127- if ($ req !==null ) {
132+ if ($ req !==null && $ req instanceof ChooseOAuthProviderRequest ) {
128133$ config = MediaWikiServices::getInstance ()->getConfigFactory ()->makeConfig ('authmanageroauth ' );
129134$ provider =new GenericProvider ($ config ->get ('AuthManagerOAuthConfig ' )[$ req ->amoa_provider ] );
130135$ authorizationUrl =$ provider ->getAuthorizationUrl ( [
@@ -174,7 +179,7 @@ public function beginPrimaryAccountLink( $user, array $reqs ) {
174179 * Convert the response of an OAuth redirect to the identity it represents for further use.
175180 * This asks the OAuth provider to verify the the login and gets the remote username and id.
176181 * @param OAuthProviderAuthenticationRequest $req
177- * @returnOAuthIdentityRequest
182+ * @returnAuthenticationResponse
178183 */
179184private function convertOAuthProviderAuthenticationRequestToOAuthIdentityRequest ($ req ) {
180185$ config = MediaWikiServices::getInstance ()->getConfigFactory ()->makeConfig ('authmanageroauth ' );
@@ -231,22 +236,24 @@ public function continuePrimaryAuthentication( array $reqs ) {
231236wfDebugLog ('AuthManagerOAuth continuePrimaryAuthentication ' ,var_export ($ reqs ,true ) );
232237
233238$ identity_req = AuthenticationRequest::getRequestByClass ($ reqs , OAuthIdentityRequest::class );
234- if ($ identity_req !==null ) {
239+ if ($ identity_req !==null && $ identity_req instanceof OAuthIdentityRequest ) {
235240// Already authenticated with OAuth provider
236241
237242$ choose_local_account_req = AuthenticationRequest::getRequestByClass (
238243$ reqs ,
239244ChooseLocalAccountRequest::class
240245);
241- if ($ choose_local_account_req !==null ) {
246+ if ($ choose_local_account_req !==null
247+ &&$ choose_local_account_reqinstanceof ChooseLocalAccountRequest ) {
242248return AuthenticationResponse::newPass ($ choose_local_account_req ->username );
243249}
244250
245251$ choose_local_username_req = AuthenticationRequest::getRequestByClass (
246252$ reqs ,
247253LocalUsernameInputRequest::class
248254);
249- if ($ choose_local_username_req !==null ) {
255+ if ($ choose_local_username_req !==null
256+ &&$ choose_local_username_reqinstanceof LocalUsernameInputRequest ) {
250257$ user = \User::newFromName ($ choose_local_username_req ->local_username );
251258// TODO FIXME query on primary race condition https://phabricator.wikimedia.org/T138678#3911381
252259if ( !$ user ->isRegistered () ) {
@@ -258,11 +265,16 @@ public function continuePrimaryAuthentication( array $reqs ) {
258265}
259266
260267$ req = AuthenticationRequest::getRequestByClass ($ reqs , OAuthProviderAuthenticationRequest::class );
261- if ($ req !==null ) {
268+ if ($ req !==null && $ req instanceof OAuthProviderAuthenticationRequest ) {
262269$ resp =$ this ->convertOAuthProviderAuthenticationRequestToOAuthIdentityRequest ($ req );
263270if ($ resp ->status !== AuthenticationResponse::PASS ) {
264271return $ resp ;
265272}
273+ if ( !($ resp ->linkRequest instanceof OAuthIdentityRequest ) ) {
274+ throw new LogicException (
275+ "Unexpected createRequest type {$ {get_class ($ req )}}. This should never happen. "
276+ );
277+ }
266278
267279$ lb = MediaWikiServices::getInstance ()->getDBLoadBalancer ();
268280$ dbr =$ lb ->getConnectionRef (DB_REPLICA );
@@ -301,11 +313,16 @@ public function continuePrimaryAuthentication( array $reqs ) {
301313public function continuePrimaryAccountLink ($ user ,array $ reqs ) {
302314wfDebugLog ('AuthManagerOAuth continuePrimaryAccountLink ' ,var_export ($ reqs ,true ) );
303315$ req = AuthenticationRequest::getRequestByClass ($ reqs , OAuthProviderAuthenticationRequest::class );
304- if ($ req !==null ) {
316+ if ($ req !==null && $ req instanceof OAuthProviderAuthenticationRequest ) {
305317$ resp =$ this ->convertOAuthProviderAuthenticationRequestToOAuthIdentityRequest ($ req );
306318if ($ resp ->status !== AuthenticationResponse::PASS ) {
307319return $ resp ;
308320}
321+ if ( !($ resp ->linkRequest instanceof OAuthIdentityRequest ) ) {
322+ throw new LogicException (
323+ "Unexpected createRequest type {$ {get_class ($ req )}}. This should never happen. "
324+ );
325+ }
309326
310327$ lb = MediaWikiServices::getInstance ()->getDBLoadBalancer ();
311328$ dbr =$ lb ->getConnectionRef (DB_PRIMARY );
@@ -354,6 +371,9 @@ public function autoCreatedAccount( $user, $source ) {
354371public function finishAccountCreation ($ user ,$ creator ,AuthenticationResponse $ response ) {
355372wfDebugLog ('AuthManagerOAuth finishAccountCreation ' ,var_export ($ response ,true ) );
356373$ req =$ response ->createRequest ;
374+ if ( !($ reqinstanceof OAuthIdentityRequest ) ) {
375+ throw new LogicException ("Unexpected createRequest type {$ {get_class ($ req )}}. This should never happen. " );
376+ }
357377$ lb = MediaWikiServices::getInstance ()->getDBLoadBalancer ();
358378$ dbr =$ lb ->getConnectionRef (DB_PRIMARY );
359379$ result =$ dbr ->insert (
@@ -366,4 +386,7 @@ public function finishAccountCreation( $user, $creator, AuthenticationResponse $
366386__METHOD__ ,
367387);
368388}
389+
390+ // TODO providerNormalizeUsername()
391+ // TODO providerRevokeAccessForUser
369392}