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