Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 49
0.00% covered (danger)
0.00%
0 / 10
CRAP
0.00% covered (danger)
0.00%
0 / 1
ApiLinkAccount
0.00% covered (danger)
0.00%
0 / 49
0.00% covered (danger)
0.00%
0 / 10
272
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 getFinalDescription
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
2
 execute
0.00% covered (danger)
0.00%
0 / 26
0.00% covered (danger)
0.00%
0 / 1
56
 isReadMode
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 isWriteMode
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 needsToken
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getAllowedParams
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 dynamicParameterDocumentation
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getExamplesMessages
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 getHelpUrls
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
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
23use MediaWiki\Auth\AuthenticationResponse;
24use MediaWiki\Auth\AuthManager;
25
26/**
27 * Link an account with AuthManager
28 *
29 * @ingroup API
30 */
31class ApiLinkAccount extends ApiBase {
32
33    private AuthManager $authManager;
34
35    /**
36     * @param ApiMain $main
37     * @param string $action
38     * @param AuthManager $authManager
39     */
40    public function __construct(
41        ApiMain $main,
42        $action,
43        AuthManager $authManager
44    ) {
45        parent::__construct( $main, $action, 'link' );
46        $this->authManager = $authManager;
47    }
48
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;
60    }
61
62    public function execute() {
63        if ( !$this->getUser()->isNamed() ) {
64            $this->dieWithError( 'apierror-mustbeloggedin-linkaccounts', 'notloggedin' );
65        }
66
67        $params = $this->extractRequestParams();
68
69        $this->requireAtLeastOneParameter( $params, 'continue', 'returnurl' );
70
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}"
78                );
79            }
80        }
81
82        $helper = new ApiAuthManagerHelper( $this, $this->authManager );
83
84        // Check security-sensitive operation status
85        $helper->securitySensitiveOperation( 'LinkAccounts' );
86
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;
93        }
94
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'] );
102        }
103
104        $this->getResult()->addValue( null, 'linkaccount',
105            $helper->formatAuthenticationResponse( $res ) );
106    }
107
108    public function isReadMode() {
109        return false;
110    }
111
112    public function isWriteMode() {
113        return true;
114    }
115
116    public function needsToken() {
117        return 'csrf';
118    }
119
120    public function getAllowedParams() {
121        return ApiAuthManagerHelper::getStandardParams( AuthManager::ACTION_LINK,
122            'requests', 'messageformat', 'mergerequestfields', 'returnurl', 'continue'
123        );
124    }
125
126    public function dynamicParameterDocumentation() {
127        return [ 'api-help-authmanagerhelper-additional-params', AuthManager::ACTION_LINK ];
128    }
129
130    protected function getExamplesMessages() {
131        return [
132            'action=linkaccount&provider=Example&linkreturnurl=http://example.org/&linktoken=123ABC'
133                => 'apihelp-linkaccount-example-link',
134        ];
135    }
136
137    public function getHelpUrls() {
138        return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Linkaccount';
139    }
140}