Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 50
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 / 1
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 / 27
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 * @license GPL-2.0-or-later
6 * @file
7 */
8
9namespace MediaWiki\Api;
10
11use MediaWiki\Auth\AuthenticationResponse;
12use MediaWiki\Auth\AuthManager;
13use MediaWiki\Utils\UrlUtils;
14
15/**
16 * Link an account with AuthManager
17 *
18 * @ingroup API
19 */
20class ApiLinkAccount extends ApiBase {
21
22    public function __construct(
23        ApiMain $main,
24        string $action,
25        private readonly AuthManager $authManager,
26        private readonly UrlUtils $urlUtils,
27    ) {
28        parent::__construct( $main, $action, 'link' );
29    }
30
31    /** @inheritDoc */
32    public function getFinalDescription() {
33        // A bit of a hack to append 'api-help-authmanager-general-usage'
34        $msgs = parent::getFinalDescription();
35        $msgs[] = $this->msg( 'api-help-authmanager-general-usage',
36            $this->getModulePrefix(),
37            $this->getModuleName(),
38            $this->getModulePath(),
39            AuthManager::ACTION_LINK,
40            $this->needsToken(),
41        );
42        return $msgs;
43    }
44
45    public function execute() {
46        if ( !$this->getUser()->isNamed() ) {
47            $this->dieWithError( 'apierror-mustbeloggedin-linkaccounts', 'notloggedin' );
48        }
49
50        $this->checkUserRightsAny( 'editmyprivateinfo' );
51
52        $params = $this->extractRequestParams();
53
54        $this->requireAtLeastOneParameter( $params, 'continue', 'returnurl' );
55
56        if ( $params['returnurl'] !== null ) {
57            $bits = $this->urlUtils->parse( $params['returnurl'] );
58            if ( !$bits || $bits['scheme'] === '' ) {
59                $encParamName = $this->encodeParamName( 'returnurl' );
60                $this->dieWithError(
61                    [ 'apierror-badurl', $encParamName, wfEscapeWikiText( $params['returnurl'] ) ],
62                    "badurl_{$encParamName}"
63                );
64            }
65        }
66
67        $helper = new ApiAuthManagerHelper( $this, $this->authManager );
68
69        // Check security-sensitive operation status
70        $helper->securitySensitiveOperation( 'LinkAccounts' );
71
72        // Make sure it's possible to link accounts
73        if ( !$this->authManager->canLinkAccounts() ) {
74            $this->getResult()->addValue( null, 'linkaccount', $helper->formatAuthenticationResponse(
75                AuthenticationResponse::newFail( $this->msg( 'userlogin-cannot-' . AuthManager::ACTION_LINK ) )
76            ) );
77            return;
78        }
79
80        // Perform the link step
81        if ( $params['continue'] ) {
82            $reqs = $helper->loadAuthenticationRequests( AuthManager::ACTION_LINK_CONTINUE );
83            $res = $this->authManager->continueAccountLink( $reqs );
84        } else {
85            $reqs = $helper->loadAuthenticationRequests( AuthManager::ACTION_LINK );
86            $res = $this->authManager->beginAccountLink( $this->getUser(), $reqs, $params['returnurl'] );
87        }
88
89        $this->getResult()->addValue( null, 'linkaccount',
90            $helper->formatAuthenticationResponse( $res ) );
91    }
92
93    /** @inheritDoc */
94    public function isReadMode() {
95        return false;
96    }
97
98    /** @inheritDoc */
99    public function isWriteMode() {
100        return true;
101    }
102
103    /** @inheritDoc */
104    public function needsToken() {
105        return 'csrf';
106    }
107
108    /** @inheritDoc */
109    public function getAllowedParams() {
110        return ApiAuthManagerHelper::getStandardParams( AuthManager::ACTION_LINK,
111            'requests', 'messageformat', 'mergerequestfields', 'returnurl', 'continue'
112        );
113    }
114
115    /** @inheritDoc */
116    public function dynamicParameterDocumentation() {
117        return [ 'api-help-authmanagerhelper-additional-params', AuthManager::ACTION_LINK ];
118    }
119
120    /** @inheritDoc */
121    protected function getExamplesMessages() {
122        return [
123            'action=linkaccount&provider=Example&linkreturnurl=http://example.org/&linktoken=123ABC'
124                => 'apihelp-linkaccount-example-link',
125        ];
126    }
127
128    /** @inheritDoc */
129    public function getHelpUrls() {
130        return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Linkaccount';
131    }
132}
133
134/** @deprecated class alias since 1.43 */
135class_alias( ApiLinkAccount::class, 'ApiLinkAccount' );