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
23 namespace MediaWiki\Api
;
25 use MediaWiki\Auth\AuthenticationResponse
;
26 use MediaWiki\Auth\AuthManager
;
27 use MediaWiki\Utils\UrlUtils
;
30 * Link an account with AuthManager
34 class ApiLinkAccount
extends ApiBase
{
36 private AuthManager
$authManager;
37 private UrlUtils
$urlUtils;
39 public function __construct(
42 AuthManager
$authManager,
45 parent
::__construct( $main, $action, 'link' );
46 $this->authManager
= $authManager;
47 $this->urlUtils
= $urlUtils;
50 public function getFinalDescription() {
51 // A bit of a hack to append 'api-help-authmanager-general-usage'
52 $msgs = parent
::getFinalDescription();
53 $msgs[] = $this->msg( 'api-help-authmanager-general-usage',
54 $this->getModulePrefix(),
55 $this->getModuleName(),
56 $this->getModulePath(),
57 AuthManager
::ACTION_LINK
,
63 public function execute() {
64 if ( !$this->getUser()->isNamed() ) {
65 $this->dieWithError( 'apierror-mustbeloggedin-linkaccounts', 'notloggedin' );
68 $params = $this->extractRequestParams();
70 $this->requireAtLeastOneParameter( $params, 'continue', 'returnurl' );
72 if ( $params['returnurl'] !== null ) {
73 $bits = $this->urlUtils
->parse( $params['returnurl'] );
74 if ( !$bits ||
$bits['scheme'] === '' ) {
75 $encParamName = $this->encodeParamName( 'returnurl' );
77 [ 'apierror-badurl', $encParamName, wfEscapeWikiText( $params['returnurl'] ) ],
78 "badurl_{$encParamName}"
83 $helper = new ApiAuthManagerHelper( $this, $this->authManager
);
85 // Check security-sensitive operation status
86 $helper->securitySensitiveOperation( 'LinkAccounts' );
88 // Make sure it's possible to link accounts
89 if ( !$this->authManager
->canLinkAccounts() ) {
90 $this->getResult()->addValue( null, 'linkaccount', $helper->formatAuthenticationResponse(
91 AuthenticationResponse
::newFail( $this->msg( 'userlogin-cannot-' . AuthManager
::ACTION_LINK
) )
96 // Perform the link step
97 if ( $params['continue'] ) {
98 $reqs = $helper->loadAuthenticationRequests( AuthManager
::ACTION_LINK_CONTINUE
);
99 $res = $this->authManager
->continueAccountLink( $reqs );
101 $reqs = $helper->loadAuthenticationRequests( AuthManager
::ACTION_LINK
);
102 $res = $this->authManager
->beginAccountLink( $this->getUser(), $reqs, $params['returnurl'] );
105 $this->getResult()->addValue( null, 'linkaccount',
106 $helper->formatAuthenticationResponse( $res ) );
109 public function isReadMode() {
113 public function isWriteMode() {
117 public function needsToken() {
121 public function getAllowedParams() {
122 return ApiAuthManagerHelper
::getStandardParams( AuthManager
::ACTION_LINK
,
123 'requests', 'messageformat', 'mergerequestfields', 'returnurl', 'continue'
127 public function dynamicParameterDocumentation() {
128 return [ 'api-help-authmanagerhelper-additional-params', AuthManager
::ACTION_LINK
];
131 protected function getExamplesMessages() {
133 'action=linkaccount&provider=Example&linkreturnurl=http://example.org/&linktoken=123ABC'
134 => 'apihelp-linkaccount-example-link',
138 public function getHelpUrls() {
139 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Linkaccount';
143 /** @deprecated class alias since 1.43 */
144 class_alias( ApiLinkAccount
::class, 'ApiLinkAccount' );