56 $this->messageFormat = $params[
'messageformat'] ??
'wikitext';
57 $this->authManager =
$authManager ?: MediaWikiServices::getInstance()->getAuthManager();
77 switch ( $this->messageFormat ) {
86 $res[$key] = $message->
setContext( $this->module )->parseAsBlock();
87 $res[$key] = Parser::stripOuterParagraph(
$res[$key] );
93 'key' => $message->
getKey(),
96 ApiResult::setIndexedTagName( $params,
'param' );
107 $status = $this->authManager->securitySensitiveOperationStatus( $operation );
109 case AuthManager::SEC_OK:
112 case AuthManager::SEC_REAUTH:
113 $this->module->dieWithError(
'apierror-reauthenticate' );
115 case AuthManager::SEC_FAIL:
116 $this->module->dieWithError(
'apierror-cannotreauthenticate' );
119 throw new UnexpectedValueException(
"Unknown status \"$status\"" );
131 $blacklist = array_flip( $blacklist );
132 $reqs = array_filter( $reqs,
function ( $req ) use ( $blacklist ) {
133 return !isset( $blacklist[get_class( $req )] );
145 $params = $this->module->extractRequestParams();
147 $reqs = $this->authManager->getAuthenticationRequests( $action, $this->module->getUser() );
150 $wantedRequests =
null;
151 if ( isset( $params[
'requests'] ) ) {
152 $wantedRequests = array_flip( $params[
'requests'] );
153 } elseif ( isset( $params[
'request'] ) ) {
154 $wantedRequests = [ $params[
'request'] =>
true ];
156 if ( $wantedRequests !==
null ) {
157 $reqs = array_filter(
160 return isset( $wantedRequests[$req->
getUniqueId()] );
168 foreach ( $reqs as $req ) {
169 $info = (array)$req->getFieldInfo();
171 $sensitive += array_filter( $info,
function ( $opts ) {
172 return !empty( $opts[
'sensitive'] );
178 $data = array_intersect_key( $this->module->getRequest()->getValues(), $fields );
179 $this->module->getMain()->markParamsUsed( array_keys( $data ) );
182 $this->module->getMain()->markParamsSensitive( array_keys( $sensitive ) );
183 $this->module->requirePostedParameters( array_keys( $sensitive ),
'noprefix' );
186 return AuthenticationRequest::loadRequestsFromSubmission( $reqs, $data );
196 'status' =>
$res->status,
199 if (
$res->status === AuthenticationResponse::PASS &&
$res->username !==
null ) {
200 $ret[
'username'] =
$res->username;
203 if (
$res->status === AuthenticationResponse::REDIRECT ) {
204 $ret[
'redirecttarget'] =
$res->redirectTarget;
205 if (
$res->redirectApiData !==
null ) {
206 $ret[
'redirectdata'] =
$res->redirectApiData;
210 if (
$res->status === AuthenticationResponse::REDIRECT ||
211 $res->status === AuthenticationResponse::UI ||
212 $res->status === AuthenticationResponse::RESTART
217 if (
$res->status === AuthenticationResponse::FAIL ||
218 $res->status === AuthenticationResponse::UI ||
219 $res->status === AuthenticationResponse::RESTART
222 $ret[
'messagecode'] = ApiMessage::create(
$res->message )->getApiCode();
225 if (
$res->status === AuthenticationResponse::FAIL ||
226 $res->status === AuthenticationResponse::RESTART
228 $this->module->getRequest()->getSession()->set(
229 'ApiAuthManagerHelper::createRequest',
232 $ret[
'canpreservestate'] =
$res->createRequest !==
null;
234 $this->module->getRequest()->getSession()->remove(
'ApiAuthManagerHelper::createRequest' );
246 if ( is_string( $result ) ) {
247 $status = Status::newFatal( $result );
248 } elseif ( $result->status === AuthenticationResponse::PASS ) {
249 $status = Status::newGood();
250 } elseif ( $result->status === AuthenticationResponse::FAIL ) {
251 $status = Status::newFatal( $result->message );
257 LoggerFactory::getInstance(
'authevents' )->info(
"$module API attempt", [
259 'status' => strval( $status ),
269 $ret = $this->module->getRequest()->getSession()->get(
'ApiAuthManagerHelper::createRequest' );
280 $params = $this->module->extractRequestParams();
281 $mergeFields = !empty( $params[
'mergerequestfields'] );
283 $ret = [
'requests' => [] ];
284 foreach ( $reqs as $req ) {
285 $describe = $req->describeCredentials();
287 'id' => $req->getUniqueId(),
288 'metadata' => $req->getMetadata() + [ ApiResult::META_TYPE =>
'assoc' ],
290 switch ( $req->required ) {
291 case AuthenticationRequest::OPTIONAL:
292 $reqInfo[
'required'] =
'optional';
294 case AuthenticationRequest::REQUIRED:
295 $reqInfo[
'required'] =
'required';
297 case AuthenticationRequest::PRIMARY_REQUIRED:
298 $reqInfo[
'required'] =
'primary-required';
301 $this->
formatMessage( $reqInfo,
'provider', $describe[
'provider'] );
302 $this->
formatMessage( $reqInfo,
'account', $describe[
'account'] );
303 if ( !$mergeFields ) {
304 $reqInfo[
'fields'] = $this->
formatFields( (array)$req->getFieldInfo() );
306 $ret[
'requests'][] = $reqInfo;
309 if ( $mergeFields ) {
310 $fields = AuthenticationRequest::mergeFieldInfo( $reqs );
334 foreach ( $fields as $name => $field ) {
335 $ret = array_intersect_key( $field, $copy );
337 if ( isset( $field[
'options'] ) ) {
338 $ret[
'options'] = array_map(
function ( $msg ) use (
$module ) {
339 return $msg->setContext(
$module )->plain();
340 }, $field[
'options'] );
341 ApiResult::setArrayType( $ret[
'options'],
'assoc' );
345 $ret[
'optional'] = !empty( $field[
'optional'] );
346 $ret[
'sensitive'] = !empty( $field[
'sensitive'] );
348 $retFields[$name] = $ret;
351 ApiResult::setArrayType( $retFields,
'assoc' );
365 ApiBase::PARAM_TYPE =>
'string',
366 ApiBase::PARAM_ISMULTI =>
true,
367 ApiBase::PARAM_HELP_MSG => [
'api-help-authmanagerhelper-requests', $action ],
370 ApiBase::PARAM_TYPE =>
'string',
371 ApiBase::PARAM_REQUIRED =>
true,
372 ApiBase::PARAM_HELP_MSG => [
'api-help-authmanagerhelper-request', $action ],
375 ApiBase::PARAM_DFLT =>
'wikitext',
376 ApiBase::PARAM_TYPE => [
'html',
'wikitext',
'raw',
'none' ],
377 ApiBase::PARAM_HELP_MSG =>
'api-help-authmanagerhelper-messageformat',
379 'mergerequestfields' => [
380 ApiBase::PARAM_DFLT =>
false,
381 ApiBase::PARAM_HELP_MSG =>
'api-help-authmanagerhelper-mergerequestfields',
384 ApiBase::PARAM_DFLT =>
false,
385 ApiBase::PARAM_HELP_MSG =>
'api-help-authmanagerhelper-preservestate',
388 ApiBase::PARAM_TYPE =>
'string',
389 ApiBase::PARAM_HELP_MSG =>
'api-help-authmanagerhelper-returnurl',
392 ApiBase::PARAM_DFLT =>
false,
393 ApiBase::PARAM_HELP_MSG =>
'api-help-authmanagerhelper-continue',
398 foreach ( $wantedParams as $name ) {
399 if ( isset( $params[$name] ) ) {
400 $ret[$name] = $params[$name];
Helper class for AuthManager-using API modules.
static newForModule(ApiBase $module, AuthManager $authManager=null)
Static version of the constructor, for chaining.
logAuthenticationResult( $event, $result)
Logs successful or failed authentication.
getPreservedRequest()
Fetch the preserved CreateFromLoginAuthenticationRequest, if any.
static getStandardParams( $action,... $wantedParams)
Fetch the standard parameters this helper recognizes.
formatAuthenticationResponse(AuthenticationResponse $res)
Format an AuthenticationResponse for return.
__construct(ApiBase $module, AuthManager $authManager=null)
ApiBase $module
API module, for context and parameters.
formatRequests(array $reqs)
Format an array of AuthenticationRequests for return.
formatFields(array $fields)
Clean up a field array for output.
formatMessage(array &$res, $key, Message $message)
Format a message for output.
securitySensitiveOperation( $operation)
Call $manager->securitySensitiveOperationStatus()
static blacklistAuthenticationRequests(array $reqs, array $blacklist)
Filter out authentication requests by class name.
loadAuthenticationRequests( $action)
Fetch and load the AuthenticationRequests for an action.
string $messageFormat
Message output format.
This abstract class implements many basic API functions, and is the base of all API classes.
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
getModuleName()
Get the name of the module being executed by this instance.
The Message class deals with fetching and processing of interface message into a variety of formats.
getParams()
Returns the message parameters.
getKey()
Returns the message key.
setContext(IContextSource $context)
Set the language and the title from a context object.