3 * Copyright © 2016 Brad Jorsch <bjorsch@wikimedia.org>
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 use MediaWiki\Auth\AuthManager
;
24 use MediaWiki\Auth\AuthenticationRequest
;
25 use MediaWiki\Auth\AuthenticationResponse
;
28 * Link an account with AuthManager
32 class ApiLinkAccount
extends ApiBase
{
34 public function __construct( ApiMain
$main, $action ) {
35 parent
::__construct( $main, $action, 'link' );
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
,
51 public function execute() {
52 if ( !$this->getUser()->isLoggedIn() ) {
53 $this->dieUsage( 'Must be logged in to link accounts', 'notloggedin' );
56 $params = $this->extractRequestParams();
58 $this->requireAtLeastOneParameter( $params, 'continue', 'returnurl' );
60 if ( $params['returnurl'] !== null ) {
61 $bits = wfParseUrl( $params['returnurl'] );
62 if ( !$bits ||
$bits['scheme'] === '' ) {
63 $encParamName = $this->encodeParamName( 'returnurl' );
65 "Invalid value '{$params['returnurl']}' for url parameter $encParamName",
66 "badurl_{$encParamName}"
71 $helper = new ApiAuthManagerHelper( $this );
72 $manager = AuthManager
::singleton();
74 // Check security-sensitive operation status
75 $helper->securitySensitiveOperation( 'LinkAccounts' );
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
) )
85 // Perform the link step
86 if ( $params['continue'] ) {
87 $reqs = $helper->loadAuthenticationRequests( AuthManager
::ACTION_LINK_CONTINUE
);
88 $res = $manager->continueAccountLink( $reqs );
90 $reqs = $helper->loadAuthenticationRequests( AuthManager
::ACTION_LINK
);
91 $res = $manager->beginAccountLink( $this->getUser(), $reqs, $params['returnurl'] );
94 $this->getResult()->addValue( null, 'linkaccount',
95 $helper->formatAuthenticationResponse( $res ) );
98 public function isReadMode() {
102 public function isWriteMode() {
106 public function needsToken() {
110 public function getAllowedParams() {
111 return ApiAuthManagerHelper
::getStandardParams( AuthManager
::ACTION_LINK
,
112 'requests', 'messageformat', 'mergerequestfields', 'returnurl', 'continue'
116 public function dynamicParameterDocumentation() {
117 return [ 'api-help-authmanagerhelper-additional-params', AuthManager
::ACTION_LINK
];
120 protected function getExamplesMessages() {
122 'action=linkaccount&provider=Example&linkreturnurl=http://example.org/&linktoken=123ABC'
123 => 'apihelp-linkaccount-example-link',
127 public function getHelpUrls() {
128 return 'https://www.mediawiki.org/wiki/API:Linkaccount';