45 private $messageFormat;
54 $this->module = $module;
57 $this->messageFormat = $params[
'messageformat'] ??
'wikitext';
58 $this->authManager = $authManager ?: MediaWikiServices::getInstance()->getAuthManager();
68 return new self( $module, $authManager );
77 private function formatMessage( array &$res, $key,
Message $message ) {
78 switch ( $this->messageFormat ) {
83 $res[$key] = $message->
setContext( $this->module )->text();
87 $res[$key] = $message->
setContext( $this->module )->parseAsBlock();
94 'key' => $message->
getKey(),
108 $status = $this->authManager->securitySensitiveOperationStatus( $operation );
110 case AuthManager::SEC_OK:
113 case AuthManager::SEC_REAUTH:
114 $this->module->dieWithError(
'apierror-reauthenticate' );
117 case AuthManager::SEC_FAIL:
118 $this->module->dieWithError(
'apierror-cannotreauthenticate' );
122 throw new UnexpectedValueException(
"Unknown status \"$status\"" );
134 $remove = array_fill_keys( $remove,
true );
135 $reqs = array_filter( $reqs,
static function ( $req ) use ( $remove ) {
136 return !isset( $remove[get_class( $req )] );
148 $params = $this->module->extractRequestParams();
150 $reqs = $this->authManager->getAuthenticationRequests( $action, $this->module->getUser() );
153 $wantedRequests =
null;
154 if ( isset( $params[
'requests'] ) ) {
155 $wantedRequests = array_fill_keys( $params[
'requests'],
true );
156 } elseif ( isset( $params[
'request'] ) ) {
157 $wantedRequests = [ $params[
'request'] =>
true ];
159 if ( $wantedRequests !==
null ) {
160 $reqs = array_filter(
163 return isset( $wantedRequests[$req->
getUniqueId()] );
171 foreach ( $reqs as $req ) {
172 $info = (array)$req->getFieldInfo();
174 $sensitive += array_filter( $info,
static function ( $opts ) {
175 return !empty( $opts[
'sensitive'] );
181 $data = array_intersect_key( $this->module->getRequest()->getValues(), $fields );
182 $this->module->getMain()->markParamsUsed( array_keys( $data ) );
185 $this->module->getMain()->markParamsSensitive( array_keys( $sensitive ) );
186 $this->module->requirePostedParameters( array_keys( $sensitive ),
'noprefix' );
189 return AuthenticationRequest::loadRequestsFromSubmission( $reqs, $data );
199 'status' => $res->status,
202 if ( $res->status === AuthenticationResponse::PASS && $res->username !==
null ) {
203 $ret[
'username'] = $res->username;
206 if ( $res->status === AuthenticationResponse::REDIRECT ) {
207 $ret[
'redirecttarget'] = $res->redirectTarget;
208 if ( $res->redirectApiData !==
null ) {
209 $ret[
'redirectdata'] = $res->redirectApiData;
213 if ( $res->status === AuthenticationResponse::REDIRECT ||
214 $res->status === AuthenticationResponse::UI ||
215 $res->status === AuthenticationResponse::RESTART
220 if ( $res->status === AuthenticationResponse::FAIL ||
221 $res->status === AuthenticationResponse::UI ||
222 $res->status === AuthenticationResponse::RESTART
224 $this->formatMessage( $ret,
'message', $res->message );
228 if ( $res->status === AuthenticationResponse::FAIL ||
229 $res->status === AuthenticationResponse::RESTART
231 $this->module->getRequest()->getSession()->set(
232 'ApiAuthManagerHelper::createRequest',
235 $ret[
'canpreservestate'] = $res->createRequest !==
null;
237 $this->module->getRequest()->getSession()->remove(
'ApiAuthManagerHelper::createRequest' );
249 if ( is_string( $result ) ) {
250 $status = Status::newFatal( $result );
251 } elseif ( $result->status === AuthenticationResponse::PASS ) {
252 $status = Status::newGood();
253 } elseif ( $result->status === AuthenticationResponse::FAIL ) {
254 $status = Status::newFatal( $result->message );
260 LoggerFactory::getInstance(
'authevents' )->info(
"$module API attempt", [
262 'status' => strval( $status ),
272 $ret = $this->module->getRequest()->getSession()->get(
'ApiAuthManagerHelper::createRequest' );
283 $params = $this->module->extractRequestParams();
284 $mergeFields = !empty( $params[
'mergerequestfields'] );
286 $ret = [
'requests' => [] ];
287 foreach ( $reqs as $req ) {
288 $describe = $req->describeCredentials();
290 'id' => $req->getUniqueId(),
293 switch ( $req->required ) {
294 case AuthenticationRequest::OPTIONAL:
295 $reqInfo[
'required'] =
'optional';
297 case AuthenticationRequest::REQUIRED:
298 $reqInfo[
'required'] =
'required';
300 case AuthenticationRequest::PRIMARY_REQUIRED:
301 $reqInfo[
'required'] =
'primary-required';
304 $this->formatMessage( $reqInfo,
'provider', $describe[
'provider'] );
305 $this->formatMessage( $reqInfo,
'account', $describe[
'account'] );
306 if ( !$mergeFields ) {
307 $reqInfo[
'fields'] = $this->formatFields( (array)$req->getFieldInfo() );
309 $ret[
'requests'][] = $reqInfo;
312 if ( $mergeFields ) {
313 $fields = AuthenticationRequest::mergeFieldInfo( $reqs );
314 $ret[
'fields'] = $this->formatFields( $fields );
327 private function formatFields( array $fields ) {
333 $module = $this->module;
336 foreach ( $fields as $name => $field ) {
337 $ret = array_intersect_key( $field, $copy );
339 if ( isset( $field[
'options'] ) ) {
340 $ret[
'options'] = array_map(
static function ( $msg ) use ( $module ) {
341 return $msg->setContext( $module )->plain();
342 }, $field[
'options'] );
345 $this->formatMessage( $ret,
'label', $field[
'label'] );
346 $this->formatMessage( $ret,
'help', $field[
'help'] );
347 $ret[
'optional'] = !empty( $field[
'optional'] );
348 $ret[
'sensitive'] = !empty( $field[
'sensitive'] );
350 $retFields[$name] = $ret;
367 ParamValidator::PARAM_TYPE =>
'string',
368 ParamValidator::PARAM_ISMULTI =>
true,
372 ParamValidator::PARAM_TYPE =>
'string',
373 ParamValidator::PARAM_REQUIRED =>
true,
377 ParamValidator::PARAM_DEFAULT =>
'wikitext',
378 ParamValidator::PARAM_TYPE => [
'html',
'wikitext',
'raw',
'none' ],
381 'mergerequestfields' => [
382 ParamValidator::PARAM_DEFAULT =>
false,
386 ParamValidator::PARAM_DEFAULT =>
false,
390 ParamValidator::PARAM_TYPE =>
'string',
394 ParamValidator::PARAM_DEFAULT =>
false,
400 foreach ( $wantedParams as $name ) {
401 if ( isset( $params[$name] ) ) {
402 $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.
static blacklistAuthenticationRequests(array $reqs, array $remove)
Filter out authentication requests by class name.
formatAuthenticationResponse(AuthenticationResponse $res)
Format an AuthenticationResponse for return.
__construct(ApiBase $module, AuthManager $authManager=null)
formatRequests(array $reqs)
Format an array of AuthenticationRequests for return.
securitySensitiveOperation( $operation)
Call $manager->securitySensitiveOperationStatus()
loadAuthenticationRequests( $action)
Fetch and load the AuthenticationRequests for an action.
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,...
const PARAM_HELP_MSG
(string|array|Message) Specify an alternative i18n documentation message for this parameter.
getModuleName()
Get the name of the module being executed by this instance.
static create( $msg, $code=null, array $data=null)
Create an IApiMessage for the message.
const META_TYPE
Key for the 'type' metadata item.
static setArrayType(array &$arr, $type, $kvpKeyName=null)
Set the array data type.
static setIndexedTagName(array &$arr, $tag)
Set the tag name for numeric-keyed values in XML format.
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.
static stripOuterParagraph( $html)
Strip outer.