| 1 |
<?php |
| 2 |
|
| 3 |
s |
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 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 |
|
| 31 |
|
| 32 |
|
| 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 |
|
| 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 |
|
| 88 |
$helper->securitySensitiveOperation( 'LinkAccounts' ); |
| 89 |
|
| 90 |
|
| 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 |
|
| 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 |
|
| 146 |
class_alias( ApiLinkAccount::class, 'ApiLinkAccount' ); |