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();
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' );
116 case AuthManager::SEC_FAIL:
117 $this->module->dieWithError(
'apierror-cannotreauthenticate' );
121 throw new UnexpectedValueException(
"Unknown status \"$status\"" );
133 $remove = array_fill_keys( $remove,
true );
134 $reqs = array_filter( $reqs,
static function ( $req ) use ( $remove ) {
135 return !isset( $remove[get_class( $req )] );
147 $params = $this->module->extractRequestParams();
149 $reqs = $this->authManager->getAuthenticationRequests( $action, $this->module->getUser() );
152 $wantedRequests =
null;
153 if ( isset( $params[
'requests'] ) ) {
154 $wantedRequests = array_fill_keys( $params[
'requests'],
true );
155 } elseif ( isset( $params[
'request'] ) ) {
156 $wantedRequests = [ $params[
'request'] =>
true ];
158 if ( $wantedRequests !==
null ) {
159 $reqs = array_filter(
162 return isset( $wantedRequests[$req->
getUniqueId()] );
170 foreach ( $reqs as $req ) {
171 $info = (array)$req->getFieldInfo();
173 $sensitive += array_filter( $info,
static function ( $opts ) {
174 return !empty( $opts[
'sensitive'] );
180 $data = array_intersect_key( $this->module->getRequest()->getValues(), $fields );
181 $this->module->getMain()->markParamsUsed( array_keys( $data ) );
184 $this->module->getMain()->markParamsSensitive( array_keys( $sensitive ) );
185 $this->module->requirePostedParameters( array_keys( $sensitive ),
'noprefix' );
188 return AuthenticationRequest::loadRequestsFromSubmission( $reqs, $data );
198 'status' =>
$res->status,
201 if (
$res->status === AuthenticationResponse::PASS &&
$res->username !==
null ) {
202 $ret[
'username'] =
$res->username;
205 if (
$res->status === AuthenticationResponse::REDIRECT ) {
206 $ret[
'redirecttarget'] =
$res->redirectTarget;
207 if (
$res->redirectApiData !==
null ) {
208 $ret[
'redirectdata'] =
$res->redirectApiData;
212 if (
$res->status === AuthenticationResponse::REDIRECT ||
213 $res->status === AuthenticationResponse::UI ||
214 $res->status === AuthenticationResponse::RESTART
219 if (
$res->status === AuthenticationResponse::FAIL ||
220 $res->status === AuthenticationResponse::UI ||
221 $res->status === AuthenticationResponse::RESTART
224 $ret[
'messagecode'] = ApiMessage::create(
$res->message )->getApiCode();
227 if (
$res->status === AuthenticationResponse::FAIL ||
228 $res->status === AuthenticationResponse::RESTART
230 $this->module->getRequest()->getSession()->set(
231 'ApiAuthManagerHelper::createRequest',
234 $ret[
'canpreservestate'] =
$res->createRequest !==
null;
236 $this->module->getRequest()->getSession()->remove(
'ApiAuthManagerHelper::createRequest' );
248 if ( is_string( $result ) ) {
249 $status = Status::newFatal( $result );
250 } elseif ( $result->status === AuthenticationResponse::PASS ) {
251 $status = Status::newGood();
252 } elseif ( $result->status === AuthenticationResponse::FAIL ) {
253 $status = Status::newFatal( $result->message );
259 LoggerFactory::getInstance(
'authevents' )->info(
"$module API attempt", [
261 'status' => strval( $status ),
271 $ret = $this->module->getRequest()->getSession()->get(
'ApiAuthManagerHelper::createRequest' );
282 $params = $this->module->extractRequestParams();
283 $mergeFields = !empty( $params[
'mergerequestfields'] );
285 $ret = [
'requests' => [] ];
286 foreach ( $reqs as $req ) {
287 $describe = $req->describeCredentials();
289 'id' => $req->getUniqueId(),
290 'metadata' => $req->getMetadata() + [ ApiResult::META_TYPE =>
'assoc' ],
292 switch ( $req->required ) {
293 case AuthenticationRequest::OPTIONAL:
294 $reqInfo[
'required'] =
'optional';
296 case AuthenticationRequest::REQUIRED:
297 $reqInfo[
'required'] =
'required';
299 case AuthenticationRequest::PRIMARY_REQUIRED:
300 $reqInfo[
'required'] =
'primary-required';
303 $this->
formatMessage( $reqInfo,
'provider', $describe[
'provider'] );
304 $this->
formatMessage( $reqInfo,
'account', $describe[
'account'] );
305 if ( !$mergeFields ) {
306 $reqInfo[
'fields'] = $this->
formatFields( (array)$req->getFieldInfo() );
308 $ret[
'requests'][] = $reqInfo;
311 if ( $mergeFields ) {
312 $fields = AuthenticationRequest::mergeFieldInfo( $reqs );
335 foreach ( $fields as $name => $field ) {
336 $ret = array_intersect_key( $field, $copy );
338 if ( isset( $field[
'options'] ) ) {
339 $ret[
'options'] = array_map(
static function ( $msg ) use (
$module ) {
340 return $msg->setContext(
$module )->plain();
341 }, $field[
'options'] );
342 ApiResult::setArrayType( $ret[
'options'],
'assoc' );
346 $ret[
'optional'] = !empty( $field[
'optional'] );
347 $ret[
'sensitive'] = !empty( $field[
'sensitive'] );
349 $retFields[$name] = $ret;
352 ApiResult::setArrayType( $retFields,
'assoc' );
366 ApiBase::PARAM_TYPE =>
'string',
367 ApiBase::PARAM_ISMULTI =>
true,
368 ApiBase::PARAM_HELP_MSG => [
'api-help-authmanagerhelper-requests', $action ],
371 ApiBase::PARAM_TYPE =>
'string',
372 ApiBase::PARAM_REQUIRED =>
true,
373 ApiBase::PARAM_HELP_MSG => [
'api-help-authmanagerhelper-request', $action ],
376 ApiBase::PARAM_DFLT =>
'wikitext',
377 ApiBase::PARAM_TYPE => [
'html',
'wikitext',
'raw',
'none' ],
378 ApiBase::PARAM_HELP_MSG =>
'api-help-authmanagerhelper-messageformat',
380 'mergerequestfields' => [
381 ApiBase::PARAM_DFLT =>
false,
382 ApiBase::PARAM_HELP_MSG =>
'api-help-authmanagerhelper-mergerequestfields',
385 ApiBase::PARAM_DFLT =>
false,
386 ApiBase::PARAM_HELP_MSG =>
'api-help-authmanagerhelper-preservestate',
389 ApiBase::PARAM_TYPE =>
'string',
390 ApiBase::PARAM_HELP_MSG =>
'api-help-authmanagerhelper-returnurl',
393 ApiBase::PARAM_DFLT =>
false,
394 ApiBase::PARAM_HELP_MSG =>
'api-help-authmanagerhelper-continue',
399 foreach ( $wantedParams as $name ) {
400 if ( isset( $params[$name] ) ) {
401 $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)
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()
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.
static stripOuterParagraph( $html)
Strip outer.