Debian Codegraph @

includes/api/ApiLinkAccount.php

raw symbols SCIP
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
23 namespace MediaWiki\Api;
24
25 use MediaWiki\Auth\AuthenticationResponse;
26 use MediaWiki\Auth\AuthManager;
27 use MediaWiki\Utils\UrlUtils;
28
29 /**
30 * Link an account with AuthManager
31 *
32 * @ingroup API
33 */
34 class ApiLinkAccount extends ApiBase {
35
36 private AuthManager $authManager;
37 private UrlUtils $urlUtils;
38
39 public function __construct(
40 ApiMain $main,
41 string $action,
42 AuthManager $authManager,
43 UrlUtils $urlUtils
44 ) {
45 parent::__construct( $main, $action, 'link' );
46 $this->authManager = $authManager;
47 $this->urlUtils = $urlUtils;
48 }
49
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,
58 $this->needsToken(),
59 );
60 return $msgs;
61 }
62
63 public function execute() {
64 if ( !$this->getUser()->isNamed() ) {
65 $this->dieWithError( 'apierror-mustbeloggedin-linkaccounts', 'notloggedin' );
66 }
67
68 $this->checkUserRightsAny( 'editmyprivateinfo' );
69
70 $params = $this->extractRequestParams();
71
72 $this->requireAtLeastOneParameter( $params, 'continue', 'returnurl' );
73
74 if ( $params['returnurl'] !== null ) {
75 $bits = $this->urlUtils->parse( $params['returnurl'] );
76 if ( !$bits || $bits['scheme'] === '' ) {
77 $encParamName = $this->encodeParamName( 'returnurl' );
78 $this->dieWithError(
79 [ 'apierror-badurl', $encParamName, wfEscapeWikiText( $params['returnurl'] ) ],
80 "badurl_{$encParamName}"
81 );
82 }
83 }
84
85 $helper = new ApiAuthManagerHelper( $this, $this->authManager );
86
87 // Check security-sensitive operation status
88 $helper->securitySensitiveOperation( 'LinkAccounts' );
89
90 // Make sure it's possible to link accounts
91 if ( !$this->authManager->canLinkAccounts() ) {
92 $this->getResult()->addValue( null, 'linkaccount', $helper->formatAuthenticationResponse(
93 AuthenticationResponse::newFail( $this->msg( 'userlogin-cannot-' . AuthManager::ACTION_LINK ) )
94 ) );
95 return;
96 }
97
98 // Perform the link step
99 if ( $params['continue'] ) {
100 $reqs = $helper->loadAuthenticationRequests( AuthManager::ACTION_LINK_CONTINUE );
101 $res = $this->authManager->continueAccountLink( $reqs );
102 } else {
103 $reqs = $helper->loadAuthenticationRequests( AuthManager::ACTION_LINK );
104 $res = $this->authManager->beginAccountLink( $this->getUser(), $reqs, $params['returnurl'] );
105 }
106
107 $this->getResult()->addValue( null, 'linkaccount',
108 $helper->formatAuthenticationResponse( $res ) );
109 }
110
111 public function isReadMode() {
112 return false;
113 }
114
115 public function isWriteMode() {
116 return true;
117 }
118
119 public function needsToken() {
120 return 'csrf';
121 }
122
123 public function getAllowedParams() {
124 return ApiAuthManagerHelper::getStandardParams( AuthManager::ACTION_LINK,
125 'requests', 'messageformat', 'mergerequestfields', 'returnurl', 'continue'
126 );
127 }
128
129 public function dynamicParameterDocumentation() {
130 return [ 'api-help-authmanagerhelper-additional-params', AuthManager::ACTION_LINK ];
131 }
132
133 protected function getExamplesMessages() {
134 return [
135 'action=linkaccount&provider=Example&linkreturnurl=http://example.org/&linktoken=123ABC'
136 => 'apihelp-linkaccount-example-link',
137 ];
138 }
139
140 public function getHelpUrls() {
141 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Linkaccount';
142 }
143 }
144
145 /** @deprecated class alias since 1.43 */
146 class_alias( ApiLinkAccount::class, 'ApiLinkAccount' );