MediaWiki master
ApiQueryAuthManagerInfo.php
Go to the documentation of this file.
1<?php
10namespace MediaWiki\Api;
11
15
22
23 public function __construct(
24 ApiQuery $query,
25 string $moduleName,
26 private readonly AuthManager $authManager,
27 ) {
28 parent::__construct( $query, $moduleName, 'ami' );
29 }
30
31 public function execute() {
32 $params = $this->extractRequestParams();
33 $helper = new ApiAuthManagerHelper( $this, $this->authManager );
34 $ret = [
35 'canauthenticatenow' => $this->authManager->canAuthenticateNow(),
36 'cancreateaccounts' => $this->authManager->canCreateAccounts(),
37 'canlinkaccounts' => $this->authManager->canLinkAccounts(),
38 ];
39
40 if ( $params['securitysensitiveoperation'] !== null ) {
41 $ret['securitysensitiveoperationstatus'] = $this->authManager->securitySensitiveOperationStatus(
42 $params['securitysensitiveoperation']
43 );
44 }
45
46 if ( $params['requestsfor'] ) {
47 $action = $params['requestsfor'];
48
49 $preservedReq = $helper->getPreservedRequest();
50 if ( $preservedReq ) {
51 $ret += [
52 'haspreservedstate' => $preservedReq->hasStateForAction( $action ),
53 'hasprimarypreservedstate' => $preservedReq->hasPrimaryStateForAction( $action ),
54 'preservedusername' => (string)$preservedReq->username,
55 ];
56 } else {
57 $ret += [
58 'haspreservedstate' => false,
59 'hasprimarypreservedstate' => false,
60 'preservedusername' => '',
61 ];
62 }
63
64 $options = [];
65 if ( $params['reauthenticate'] !== null ) {
66 if ( !$this->getUser()->isRegistered() ) {
67 $this->dieWithError( 'apierror-mustbeloggedin-reauthenticate' );
68 } elseif ( $action !== AuthManager::ACTION_LOGIN ) {
69 $this->dieWithError( $this->msg( 'apierror-invalidparammix-mustusewith',
70 'reauthenticate', 'requestsfor=login' ) );
71 }
72 $options['securityLevel'] = $params['reauthenticate'];
73 }
74 $reqs = $this->authManager->getAuthenticationRequests( $action, $this->getUser(), $options );
75
76 // Filter out blacklisted requests, depending on the action
77 switch ( $action ) {
78 case AuthManager::ACTION_CHANGE:
81 );
82 break;
83 case AuthManager::ACTION_REMOVE:
86 );
87 break;
88 }
89
90 $ret += $helper->formatRequests( $reqs );
91 }
92
93 $this->getResult()->addValue( [ 'query' ], $this->getModuleName(), $ret );
94 }
95
97 public function isReadMode() {
98 return false;
99 }
100
102 public function getAllowedParams() {
103 return [
104 'securitysensitiveoperation' => null,
105 'requestsfor' => [
106 ParamValidator::PARAM_TYPE => [
107 AuthManager::ACTION_LOGIN,
108 AuthManager::ACTION_LOGIN_CONTINUE,
109 AuthManager::ACTION_CREATE,
110 AuthManager::ACTION_CREATE_CONTINUE,
111 AuthManager::ACTION_LINK,
112 AuthManager::ACTION_LINK_CONTINUE,
113 AuthManager::ACTION_CHANGE,
114 AuthManager::ACTION_REMOVE,
115 AuthManager::ACTION_UNLINK,
116 ],
117 ],
118 'reauthenticate' => [
119 ApiBase::PARAM_TYPE => 'string',
120 ],
121 ] + ApiAuthManagerHelper::getStandardParams( '', 'mergerequestfields', 'messageformat' );
122 }
123
125 protected function getExamplesMessages() {
126 return [
127 'action=query&meta=authmanagerinfo&amirequestsfor=' . urlencode( AuthManager::ACTION_LOGIN )
128 => 'apihelp-query+authmanagerinfo-example-login',
129 'action=query&meta=authmanagerinfo&amirequestsfor=' . urlencode( AuthManager::ACTION_LOGIN ) .
130 '&amimergerequestfields=1'
131 => 'apihelp-query+authmanagerinfo-example-login-merged',
132 'action=query&meta=authmanagerinfo&amisecuritysensitiveoperation=foo'
133 => 'apihelp-query+authmanagerinfo-example-securitysensitiveoperation',
134 ];
135 }
136
138 public function getHelpUrls() {
139 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Authmanagerinfo';
140 }
141}
142
144class_alias( ApiQueryAuthManagerInfo::class, 'ApiQueryAuthManagerInfo' );
Helper class for AuthManager-using API modules.
static getStandardParams( $action,... $wantedParams)
Fetch the standard parameters this helper recognizes.
static blacklistAuthenticationRequests(array $reqs, array $remove)
Filter out authentication requests by class name.
dieWithError( $msg, $code=null, $data=null, $httpCode=0)
Abort execution with an error.
Definition ApiBase.php:1522
getModuleName()
Get the name of the module being executed by this instance.
Definition ApiBase.php:557
getResult()
Get the result object.
Definition ApiBase.php:696
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition ApiBase.php:837
A query action to return meta information about AuthManager state.
getExamplesMessages()
Returns usage examples for this module.Return value has query strings as keys, with values being eith...
getAllowedParams()
Returns an array of allowed parameters (parameter name) => (default value) or (parameter name) => (ar...
execute()
Evaluates the parameters, performs the requested query, and sets up the result.
__construct(ApiQuery $query, string $moduleName, private readonly AuthManager $authManager,)
isReadMode()
Indicates whether this module requires read rights.to override bool
getHelpUrls()
Return links to more detailed help pages about the module.1.25, returning boolean false is deprecated...
This is a base class for all Query modules.
This is the main query class.
Definition ApiQuery.php:36
AuthManager is the authentication system in MediaWiki and serves entry point for authentication.
msg( $key,... $params)
Get a Message object with context set Parameters are the same as wfMessage()
A class containing constants representing the names of configuration variables.
const RemoveCredentialsBlacklist
Name constant for the RemoveCredentialsBlacklist setting, for use with Config::get()
const ChangeCredentialsBlacklist
Name constant for the ChangeCredentialsBlacklist setting, for use with Config::get()
Service for formatting and validating API parameters.