Update git submodules
[mediawiki.git] / includes / api / ApiLinkAccount.php
blob82dd5e1de2aaf6b92740972537b8c2a16ef7c6a4
1 <?php
2 /**
3 * Copyright © 2016 Wikimedia Foundation and contributors
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.
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.
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
20 * @file
23 use MediaWiki\Auth\AuthenticationResponse;
24 use MediaWiki\Auth\AuthManager;
26 /**
27 * Link an account with AuthManager
29 * @ingroup API
31 class ApiLinkAccount extends ApiBase {
33 private AuthManager $authManager;
35 /**
36 * @param ApiMain $main
37 * @param string $action
38 * @param AuthManager $authManager
40 public function __construct(
41 ApiMain $main,
42 $action,
43 AuthManager $authManager
44 ) {
45 parent::__construct( $main, $action, 'link' );
46 $this->authManager = $authManager;
49 public function getFinalDescription() {
50 // A bit of a hack to append 'api-help-authmanager-general-usage'
51 $msgs = parent::getFinalDescription();
52 $msgs[] = ApiBase::makeMessage( 'api-help-authmanager-general-usage', $this->getContext(), [
53 $this->getModulePrefix(),
54 $this->getModuleName(),
55 $this->getModulePath(),
56 AuthManager::ACTION_LINK,
57 $this->needsToken(),
58 ] );
59 return $msgs;
62 public function execute() {
63 if ( !$this->getUser()->isNamed() ) {
64 $this->dieWithError( 'apierror-mustbeloggedin-linkaccounts', 'notloggedin' );
67 $params = $this->extractRequestParams();
69 $this->requireAtLeastOneParameter( $params, 'continue', 'returnurl' );
71 if ( $params['returnurl'] !== null ) {
72 $bits = wfParseUrl( $params['returnurl'] );
73 if ( !$bits || $bits['scheme'] === '' ) {
74 $encParamName = $this->encodeParamName( 'returnurl' );
75 $this->dieWithError(
76 [ 'apierror-badurl', $encParamName, wfEscapeWikiText( $params['returnurl'] ) ],
77 "badurl_{$encParamName}"
82 $helper = new ApiAuthManagerHelper( $this, $this->authManager );
84 // Check security-sensitive operation status
85 $helper->securitySensitiveOperation( 'LinkAccounts' );
87 // Make sure it's possible to link accounts
88 if ( !$this->authManager->canLinkAccounts() ) {
89 $this->getResult()->addValue( null, 'linkaccount', $helper->formatAuthenticationResponse(
90 AuthenticationResponse::newFail( $this->msg( 'userlogin-cannot-' . AuthManager::ACTION_LINK ) )
91 ) );
92 return;
95 // Perform the link step
96 if ( $params['continue'] ) {
97 $reqs = $helper->loadAuthenticationRequests( AuthManager::ACTION_LINK_CONTINUE );
98 $res = $this->authManager->continueAccountLink( $reqs );
99 } else {
100 $reqs = $helper->loadAuthenticationRequests( AuthManager::ACTION_LINK );
101 $res = $this->authManager->beginAccountLink( $this->getUser(), $reqs, $params['returnurl'] );
104 $this->getResult()->addValue( null, 'linkaccount',
105 $helper->formatAuthenticationResponse( $res ) );
108 public function isReadMode() {
109 return false;
112 public function isWriteMode() {
113 return true;
116 public function needsToken() {
117 return 'csrf';
120 public function getAllowedParams() {
121 return ApiAuthManagerHelper::getStandardParams( AuthManager::ACTION_LINK,
122 'requests', 'messageformat', 'mergerequestfields', 'returnurl', 'continue'
126 public function dynamicParameterDocumentation() {
127 return [ 'api-help-authmanagerhelper-additional-params', AuthManager::ACTION_LINK ];
130 protected function getExamplesMessages() {
131 return [
132 'action=linkaccount&provider=Example&linkreturnurl=http://example.org/&linktoken=123ABC'
133 => 'apihelp-linkaccount-example-link',
137 public function getHelpUrls() {
138 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Linkaccount';