aboutsummaryrefslogtreecommitdiff
path: root/RedirectAuth.php
blob: 0d09f3df705dfee88da334109b06ee7c1d3ebfce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
<?php

namespace MediaWiki\Extension\RedirectAuth;

use MediaWiki\Auth\AbstractPrimaryAuthenticationProvider;
use MediaWiki\Auth\AuthManager;
use MediaWiki\Auth\AuthenticationRequest;
use MediaWiki\Auth\AuthenticationResponse;
use MediaWiki\Auth\ButtonAuthenticationRequest;
use MediaWiki\Auth\PrimaryAuthenticationProvider;
use MediaWiki\MediaWikiServices;
use MediaWiki\User\UserFactory;
use Html;
use RequestContext;
use SpecialPage;
use StatusValue;
use TitleValue;
use UnlistedSpecialPage;
use User;

/**
 * This is the class that extension users have to implement.
 */
abstract class ProviderDetails {
    /**
     * Returns the name of the authentication provider.
     */
    abstract function getName(): string;

    /**
     * Returns the HTTPS URL where the user should be redirected if they want to log in.
     * The passed state must be incorporated into the URL (and must be sent back
     * by the identity provider on successful authentication).
     */
    abstract function getRedirectUrl(string $state): string;

    /**
     * Constructs the UserInfo from the URL query returned by the identity provider.
     * Conventionally the query just contains an access token, so this function
     * probably retrieves the user info from some HTTP API.
     */
    abstract function getUserInfo(array $query): UserInfo;

    /**
     * Returns the state from the returned URL query.
     */
    function getStateFromQuery($query): ?string {
        return $query['state'];
    }

    /**
     * Optionally returns a formatter URL, which is used to linkify
     * external user ids on the preferences page. Use $1 for the placeholder
     * where the external user id should be substituted. By default this
     * function returns null, which simply results in no link in the preferences.
     */
    function getUserFormatterUrl(): ?string {
        return null;
    }

    /**
     * Optionally implement this function to prevent the creation of accounts
     * via e.g. Special:CreateAccount if the username is already taken at the
     * external identity provider.
     */
    function testUserExists ( $username ): bool {
        return false;
    }
}

class RedirectAuthRequest extends ButtonAuthenticationRequest {
    public function __construct( \Message $label ) {
        parent::__construct(
            RedirectAuthProvider::BUTTON_NAME,
            $label,
            wfMessage(''), // the help message doesn't actually seem to be used anywhere?
            true
        );
    }
}

class ReturnAuthRequest extends AuthenticationRequest {
    public $query;

    public function getFieldInfo() {
        return [
            'query' => ['type' => 'string'],
        ];
    }

    public function loadFromSubmission ( array $data ) {
        if ( isset( $data['query'] ) ) {
            parse_str($data['query'], $this->query);
            return true;
        }
        return false;
    }
}

class DummyAuthRequest extends AuthenticationRequest {
    public function getFieldInfo() {}
}

class RedirectAuthProvider extends AbstractPrimaryAuthenticationProvider {
    const BUTTON_NAME = 'redirectauth';
    const TOKEN_SALT = 'RedirectAuthProvider';
    const RETURNURL_SESSION_KEY = 'redirectAuthReturnToUrl';
    const EXTERNALID_SESSION_KEY = 'redirectAuthExternalId';

    public function getAuthenticationRequests ( $action, array $options) {
        global $wgRedirectAuth_providerDetails;

        if ($action == AuthManager::ACTION_LOGIN) {
            // 1. Show the "Log in via ..." button on Special:UserLogin

            if (RequestContext::getMain()->getRequest()->getVal('noexternallogin')) {
                // The user said that they already have a wiki account with regular credentials,
                // so we prompt them to log in with these credentials. Showing the "Log in with ..."
                // button in that case would be confusing.
                return [];
            }

            return [new RedirectAuthRequest(wfMessage('redirectauth-log-in-with', $wgRedirectAuth_providerDetails->getName()))];
        } else if ($action == AuthManager::ACTION_LINK) {
            $user = RequestContext::getMain()->getUser();

            if (Mapper::getExternalIdByLocalId($user->mId) == null) {
                return [new RedirectAuthRequest(wfMessage('redirectauth-link-your', $wgRedirectAuth_providerDetails->getName()))];
            }
        } else if ($action == AuthManager::ACTION_REMOVE) {
            $user = RequestContext::getMain()->getUser();

            if (Mapper::getExternalIdByLocalId($user->mId) != null) {
                return [new RedirectAuthRequest(wfMessage('redirectauth-unlink-your', $wgRedirectAuth_providerDetails->getName()))];
            }
        }
        return [];
    }

    public function beginPrimaryAuthentication (array $reqs) {
        $req = AuthenticationRequest::getRequestByClass( $reqs, RedirectAuthRequest::class );
        if ( !$req ) {
            return AuthenticationResponse::newAbstain();
        }
        // 2. The button has been clicked so we redirect the user to the external identity provider

        $this->manager->setAuthenticationSessionData(self::RETURNURL_SESSION_KEY, $req->returnToUrl);

        global $wgRedirectAuth_providerDetails;
        $token = $this->manager->getRequest()->getSession()->getToken(self::TOKEN_SALT);
        $redirectUrl = $wgRedirectAuth_providerDetails->getRedirectUrl($token);
        return AuthenticationResponse::newRedirect([new ReturnAuthRequest()], $redirectUrl);
    }

    public function continuePrimaryAuthentication (array $reqs) {
        $req = AuthenticationRequest::getRequestByClass( $reqs, ReturnAuthRequest::class );

        if ($req) {
            // 4. we are back \o/
            global $wgRedirectAuth_providerDetails;
            $userInfo = $wgRedirectAuth_providerDetails->getUserInfo($req->query);
            if ($userInfo->error) {
                return AuthenticationResponse::newFail(wfMessage('redirectauth-auth-failed', $userInfo->error));
            }

            $confirmed = false;
        } else {
            // 5. the user has chosen what to do
            $req = AuthenticationRequest::getRequestByClass( $reqs, CreateOrLoginAuthRequest::class, true);
            // I don't understand why the third parameter $allowSubclasses needs to be set to true here ... but hey it works

            if ($req) {
                $userInfo = $req->userInfo;
                if ($req->choice == 'log-in' || $req->choice == 'mine') {

                    $params = [
                        'returnto' => 'Special:LinkAccounts',
                        'force' => 'LinkAccounts', // prevents the Sign up button from being shown
                        'noexternallogin' => true  // recognized in getAuthenticationRequests to suppress our login button
                    ];

                    if ($req->choice == 'mine') {
                        $params['wpName'] = $req->userInfo->userName; // just already fill it out for better UX
                    }

                    $target = SpecialPage::getTitleFor('Userlogin')->getLocalURL($params);

                    // AuthenticationResponse::newRedirect throws an error if the requests array is empty so we just pass a dummy request
                    return AuthenticationResponse::newRedirect([new DummyAuthRequest()], $target);
                } else if ($req->choice == 'create-other') {
                    return AuthenticationResponse::newUI([new OtherUsernameAuthRequest($userInfo)], WfMessage('redirectauth-pick-other-username-taken', $userInfo->userName));
                }
                $confirmed = true;
            } else {
                $req = AuthenticationRequest::getRequestByClass( $reqs, OtherUsernameAuthRequest::class, true);
                if ($req) {
                    $confirmed = true;
                    $userInfo = $req->userInfo;
                    $userInfo->userName = $req->username;
                } else {
                    return AuthenticationResponse::newAbstain();
                }
            }
        }

        $userFactory = MediaWikiServices::getInstance()->getUserFactory();

        $localUser = Mapper::getLocalUser($userInfo->userId);
        if ($localUser != null) {
            // The external user id has already been linked to a wiki account, so we're done here.
            return AuthenticationResponse::newPass($localUser->mName);
        }

        // The external user id has not yet been linked to a wiki account.

        $user = $userFactory->newFromName($userInfo->userName, UserFactory::RIGOR_CREATABLE);
        if ($user == null) {
            return AuthenticationResponse::newUI([new OtherUsernameAuthRequest($userInfo)], WfMessage('redirectauth-pick-other-username-invalid'));
        }

        if ($user->isRegistered()) {
            // namespace collision
            if ($req instanceof OtherUsernameAuthRequest) {
                return AuthenticationResponse::newUI([new OtherUsernameAuthRequest($userInfo)], WfMessage('redirectauth-pick-other-username-taken', $userInfo->userName));
            }

            // We create the link manually because if the system message contained [[User:$1|]] MediaWiki would create an
            // action=edit link in case the user page doesn't exist ... and linking an edit page would be confusing.
            $factory = MediaWikiServices::getInstance()->getLinkRendererFactory();
            $linkRenderer = $factory->create();
            $link = $linkRenderer->makeKnownLink(new TitleValue( NS_USER, $userInfo->userName), $userInfo->userName, ['target' => '_blank']);

            return AuthenticationResponse::newUI([new CreateOrLoginAuthRequest($userInfo, true)], wfMessage("redirectauth-collision")->rawParams($link));
        }

        // The username is available.

        if (!$confirmed) {
            // ideally we would set the 3rd parameter of newUI to 'info' but that isn't supported (https://phabricator.wikimedia.org/T320671)
            return AuthenticationResponse::newUI([new CreateOrLoginAuthRequest($userInfo, false)], wfMessage("redirectauth-create-or-log-in"));
        }

        // The user has confirmed that they want to create an account with the username.

        // We cannot directly call Mapper::createMapping here because the $user does not have an id yet.
        // So instead we store $userInfo->userId in the session and retrieve it in autoCreatedAccount.
        $this->manager->setAuthenticationSessionData(self::EXTERNALID_SESSION_KEY, $userInfo->userId);

        return AuthenticationResponse::newPass($user->mName);
    }

    public function autoCreatedAccount($user, $source) {
        // 6. account has been created ... save mapping in database
        $externalId = $this->manager->getAuthenticationSessionData(self::EXTERNALID_SESSION_KEY);
        Mapper::createMapping($user->getId(), $externalId);
        $this->manager->removeAuthenticationSessionData(self::EXTERNALID_SESSION_KEY);
    }

    public function beginPrimaryAccountLink($user, array $reqs) {
        return $this->beginPrimaryAuthentication($reqs);
    }

    public function continuePrimaryAccountLink($user, array $reqs) {
        $req = AuthenticationRequest::getRequestByClass( $reqs, ReturnAuthRequest::class );

        if ($req) {
            global $wgRedirectAuth_providerDetails;
            $userInfo = $wgRedirectAuth_providerDetails->getUserInfo($req->query);
            if ($userInfo->error) {
                return AuthenticationResponse::newFail(wfMessage('redirectauth-auth-failed', $userInfo->error));
            }

            $existingLocal = Mapper::getLocalUser($userInfo->userId);
            if ($existingLocal != null) {
                // FUTURE: instead return UI allowing the user to update the link?
                return AuthenticationResponse::newFail(wfMessage("redirectauth-error-already-linked", $wgRedirectAuth_providerDetails->getName(), $existingLocal->mName));
            }

            Mapper::createMapping($user->mId, $userInfo->userId);
            return AuthenticationResponse::newPass();
        }

        return AuthenticationResponse::newAbstain();
    }

    public function testUserExists ( $username, $flags=User::READ_NORMAL) {
        global $wgRedirectAuth_providerDetails;
        return $wgRedirectAuth_providerDetails->testUserExists($username);
    }

    public function accountCreationType () {
        return PrimaryAuthenticationProvider::TYPE_LINK;
    }

    public function providerAllowsAuthenticationDataChange (AuthenticationRequest $req, $checkData=true) {
        return StatusValue::newGood();
    }

    public function providerChangeAuthenticationData (AuthenticationRequest $req) {
        if ($req->action == AuthManager::ACTION_REMOVE) {
            $user = RequestContext::getMain()->getUser();
            Mapper::deleteMapping($user->mId);
        }
    }

    public function beginPrimaryAccountCreation ( $user, $creator, array $reqs) {
        return AuthenticationResponse::newAbstain();
    }
}

class SpecialRedirectAuthReturn extends UnlistedSpecialPage {
    function __construct() {
        parent::__construct('RedirectAuthReturn');
    }

    function execute($param) {
        // 3. The user was redirected back to the wiki.
        $out = $this->getOutput();
        $request = $this->getRequest();
        $session = $request->getSession();

        $token = $session->getToken(RedirectAuthProvider::TOKEN_SALT);

        global $wgRedirectAuth_providerDetails;
        $state = $wgRedirectAuth_providerDetails->getStateFromQuery($request->getQueryValuesOnly());

        if (!$token->match($state)) {
            $out->prepareErrorPage('State mismatch error');
            $out->addWikiMsg('redirectauth-try-again');
            $out->setStatusCode(400);
            return;
        }

        $authData = $session->getSecret( 'authData' );
        $redirectUrl = $authData[RedirectAuthProvider::RETURNURL_SESSION_KEY] ?? false;

        if (!$redirectUrl) {
            $out->prepareErrorPage('Failed to retrieve return URL from session');
            $out->addWikiMsg('redirectauth-try-again');
            $out->setStatusCode(400);
            return;
        }

        $redirectUrl = wfAppendQuery($redirectUrl, [
            'query' => $request->getRawQueryString(),
        ]);

        $out->redirect($redirectUrl);
    }
}

class CreateOrLogInAuthRequest extends AuthenticationRequest {
    public function __construct(UserInfo $userInfo, bool $nameTaken) {
        $this->userInfo = $userInfo;
        $this->nameTaken = $nameTaken;
    }

    public function getFieldInfo() {
        // Normally field info types are mapped to form descriptor types by
        // AuthManagerSpecialPage::mapFieldInfoTypeToFormDescriptorType
        // but since that function doesn't support 'type'=>'radio' we override
        // the mapping in the onAuthChangeFormFields hook.
        // Note that we still have to return the field here because
        // AuthenticationRequest::loadFromSubmission uses the field info.
        return ['choice' => ['type' => 'string']];
    }
}

class OtherUsernameAuthRequest extends AuthenticationRequest {
    public function __construct(UserInfo $userInfo) {
        $this->userInfo = $userInfo;
    }

    public function getFieldInfo() {
        return [
            'username' => [
                'type' => 'string',
                'value' => $this->userInfo->userName,
            ]
        ];
    }
}

/** classes that are less important **/

class UserInfo {
    public $error = null;
    public $userId = null;
    public $userName = null;

    static function err(string $msg) {
        $ret = new UserInfo;
        $ret->error = $msg;
        return $ret;
    }

    static function ok(string $id, string $name) {
        $ret = new UserInfo;
        $ret->userId = $id;
        $ret->userName = $name;
        return $ret;
    }
}

class Hooks {
    public static function onLoadExtensionSchemaUpdates( $updater ) {
        $updater->addExtensionTable(Mapper::TABLE_NAME, $GLOBALS['wgExtensionDirectory'] . '/RedirectAuth/schema.sql');
    }

    public static function onAuthChangeFormFields( $requests, $fieldInfo, &$formDescriptor, $action ) {
        if ($action == AuthManager::ACTION_LOGIN) {
            $req = AuthenticationRequest::getRequestByClass( $requests, RedirectAuthRequest::class);
            if ($req) {
                $formDescriptor[RedirectAuthProvider::BUTTON_NAME]['weight'] = -2;
                // ideally we would set the autofocus attribute for the button but that isn't supported (https://phabricator.wikimedia.org/T320672)
            }
        } else if ($action == AuthManager::ACTION_LOGIN_CONTINUE) {
            $req = AuthenticationRequest::getRequestByClass( $requests, CreateOrLoginAuthRequest::class, true);
            if ($req) {
                global $wgRedirectAuth_providerDetails;
                $providerName = $wgRedirectAuth_providerDetails->getName();
                if ($req->nameTaken) {
                    $options = [
                        wfMessage('redirectauth-collision-opt-mine')->parse() => 'mine',
                        wfMessage('redirectauth-collision-opt-log-in', $providerName)->parse() => 'log-in',
                        wfMessage('redirectauth-collision-opt-create', $providerName)->parse() => 'create-other',
                    ];
                } else {
                    $options = [
                        wfMessage('redirectauth-opt-create', $requests[0]->userInfo->userName)->parse() => 'create',
                        wfMessage('redirectauth-opt-log-in', $providerName)->parse() => 'log-in',
                    ];
                }
                $formDescriptor['choice'] = [
                    'type' => 'radio',
                    'options' => $options,
                    'default' => 'create'
                ];
            }
        }
    }

    public static function onGetPreferences( User $user, array &$preferences ) {
        global $wgRedirectAuth_providerDetails;
        $externalId = Mapper::getExternalIdByLocalId($user->mId);

        if ($externalId) {
            $formatterUrl = $wgRedirectAuth_providerDetails->getUserFormatterUrl();
            if ($formatterUrl) {
                $html = Html::rawElement('a', ['href' => str_replace('$1', $externalId, $formatterUrl)], $externalId);
            } else {
                $html = Html::rawElement('span', [], $externalId);
            }
            $special = 'UnlinkAccounts';
            $msg = wfMessage('redirectauth-unlink-account');
        } else {
            $special = 'LinkAccounts';
            $msg = wfMessage('redirectauth-link-your', $wgRedirectAuth_providerDetails->getName());
            $html = '';
        }

        $html .= ' ' . new \OOUI\ButtonWidget( [
            'href' => SpecialPage::getTitleFor( $special )->getLinkURL(),
            'label' => $msg->text(),
        ] );

        $preferences['redirectauth'] = [
            'type' => 'info',
            'label-message' => 'redirectauth-linked-account',
            'section' => 'personal/info',
            'default' => $html,
            'raw' => true // we have to use raw HTML because form descriptors aren't flexible enough
        ];
    }

    public static function onSpecialStatsAddExtra( &$extraStats, RequestContext $context ) {
        // ideally we would pass $wgRedirectAuth_providerDetails->getName() as a parameter to the
        // redirectauth-count message but that isn't supported (https://phabricator.wikimedia.org/T320674)
        $extraStats['redirectauth-count'] = Mapper::count();
    }
}

class Mapper {
    const TABLE_NAME = 'redirectauth_mappings';

    static function getLocalUser(string $externalId) {
        $results = wfGetDB(DB_PRIMARY)->select(
            [self::TABLE_NAME, 'user'],
            ['local_id', 'user_name'],
            ['external_id' => $externalId],
            __METHOD__,
            [],
            ['user' => ['JOIN', 'user_id=local_id']]
        );
        if ($results->numRows() == 0) {
            return null;
        }

        $userFactory = MediaWikiServices::getInstance()->getUserFactory();
        $cur = $results->current();
        $user = $userFactory->newFromId($cur->local_id);
        $user->mName = $cur->user_name;
        return $user;
    }

    static function createMapping(int $localId, string $externalId) {
        wfGetDB(DB_PRIMARY)->insert(
            self::TABLE_NAME,
            [
                'local_id' => $localId,
                'external_id' => $externalId,
            ],
            __METHOD__
        );
    }

    static function getExternalIdByLocalId(int $localId) {
        $results = wfGetDB(DB_PRIMARY)->select(
            self::TABLE_NAME,
            ['external_id'],
            ['local_id' => $localId],
            __METHOD__
        );
        if ($results->numRows() == 0) {
            return null;
        }
        return $results->current()->external_id;
    }

    static function getExternalIdByUsername(string $username) {
        // While this function is not used by this extension it is provided for
        // extension users to facilitate the adding of external profile links to
        // the sidebar via the SkinBuildSidebar hook.
        $results = wfGetDB(DB_PRIMARY)->select(
            [self::TABLE_NAME, 'user'],
            ['external_id'],
            [],
            __METHOD__,
            [],
            ['user' => ['INNER JOIN', ['user_id=local_id', 'user_name' => $username]]]
        );
        if ($results->numRows() == 0) {
            return null;
        }
        return $results->current()->external_id;
    }

    static function deleteMapping(int $wikiUserId) {
        $results = wfGetDB(DB_PRIMARY)->delete(
            self::TABLE_NAME,
            ['local_id' => $wikiUserId],
            __METHOD__
        );
    }

    static function count() {
        return wfGetDB(DB_PRIMARY)->selectRowCount(self::TABLE_NAME);
    }
}