]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - includes/api/ApiLinkAccount.php
MediaWiki 1.30.2-scripts2
[autoinstalls/mediawiki.git] / includes / api / ApiLinkAccount.php
1 <?php
2 /**
3  * Copyright © 2016 Wikimedia Foundation and contributors
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  */
22
23 use MediaWiki\Auth\AuthManager;
24 use MediaWiki\Auth\AuthenticationResponse;
25
26 /**
27  * Link an account with AuthManager
28  *
29  * @ingroup API
30  */
31 class ApiLinkAccount extends ApiBase {
32
33         public function __construct( ApiMain $main, $action ) {
34                 parent::__construct( $main, $action, 'link' );
35         }
36
37         public function getFinalDescription() {
38                 // A bit of a hack to append 'api-help-authmanager-general-usage'
39                 $msgs = parent::getFinalDescription();
40                 $msgs[] = ApiBase::makeMessage( 'api-help-authmanager-general-usage', $this->getContext(), [
41                         $this->getModulePrefix(),
42                         $this->getModuleName(),
43                         $this->getModulePath(),
44                         AuthManager::ACTION_LINK,
45                         self::needsToken(),
46                 ] );
47                 return $msgs;
48         }
49
50         public function execute() {
51                 if ( !$this->getUser()->isLoggedIn() ) {
52                         $this->dieWithError( 'apierror-mustbeloggedin-linkaccounts', 'notloggedin' );
53                 }
54
55                 $params = $this->extractRequestParams();
56
57                 $this->requireAtLeastOneParameter( $params, 'continue', 'returnurl' );
58
59                 if ( $params['returnurl'] !== null ) {
60                         $bits = wfParseUrl( $params['returnurl'] );
61                         if ( !$bits || $bits['scheme'] === '' ) {
62                                 $encParamName = $this->encodeParamName( 'returnurl' );
63                                 $this->dieWithError(
64                                         [ 'apierror-badurl', $encParamName, wfEscapeWikiText( $params['returnurl'] ) ],
65                                         "badurl_{$encParamName}"
66                                 );
67                         }
68                 }
69
70                 $helper = new ApiAuthManagerHelper( $this );
71                 $manager = AuthManager::singleton();
72
73                 // Check security-sensitive operation status
74                 $helper->securitySensitiveOperation( 'LinkAccounts' );
75
76                 // Make sure it's possible to link accounts
77                 if ( !$manager->canLinkAccounts() ) {
78                         $this->getResult()->addValue( null, 'linkaccount', $helper->formatAuthenticationResponse(
79                                 AuthenticationResponse::newFail( $this->msg( 'userlogin-cannot-' . AuthManager::ACTION_LINK ) )
80                         ) );
81                         return;
82                 }
83
84                 // Perform the link step
85                 if ( $params['continue'] ) {
86                         $reqs = $helper->loadAuthenticationRequests( AuthManager::ACTION_LINK_CONTINUE );
87                         $res = $manager->continueAccountLink( $reqs );
88                 } else {
89                         $reqs = $helper->loadAuthenticationRequests( AuthManager::ACTION_LINK );
90                         $res = $manager->beginAccountLink( $this->getUser(), $reqs, $params['returnurl'] );
91                 }
92
93                 $this->getResult()->addValue( null, 'linkaccount',
94                         $helper->formatAuthenticationResponse( $res ) );
95         }
96
97         public function isReadMode() {
98                 return false;
99         }
100
101         public function isWriteMode() {
102                 return true;
103         }
104
105         public function needsToken() {
106                 return 'csrf';
107         }
108
109         public function getAllowedParams() {
110                 return ApiAuthManagerHelper::getStandardParams( AuthManager::ACTION_LINK,
111                         'requests', 'messageformat', 'mergerequestfields', 'returnurl', 'continue'
112                 );
113         }
114
115         public function dynamicParameterDocumentation() {
116                 return [ 'api-help-authmanagerhelper-additional-params', AuthManager::ACTION_LINK ];
117         }
118
119         protected function getExamplesMessages() {
120                 return [
121                         'action=linkaccount&provider=Example&linkreturnurl=http://example.org/&linktoken=123ABC'
122                                 => 'apihelp-linkaccount-example-link',
123                 ];
124         }
125
126         public function getHelpUrls() {
127                 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Linkaccount';
128         }
129 }