Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 76
0.00% covered (danger)
0.00%
0 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
ApiQueryAuthManagerInfo
0.00% covered (danger)
0.00%
0 / 75
0.00% covered (danger)
0.00%
0 / 6
210
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 execute
0.00% covered (danger)
0.00%
0 / 44
0.00% covered (danger)
0.00%
0 / 1
90
 isReadMode
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getAllowedParams
0.00% covered (danger)
0.00%
0 / 19
0.00% covered (danger)
0.00%
0 / 1
2
 getExamplesMessages
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
2
 getHelpUrls
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2/**
3 * Copyright © 2016 Wikimedia Foundation and contributors
4 *
5 * @license GPL-2.0-or-later
6 * @file
7 * @since 1.27
8 */
9
10namespace MediaWiki\Api;
11
12use MediaWiki\Auth\AuthManager;
13use MediaWiki\MainConfigNames;
14use Wikimedia\ParamValidator\ParamValidator;
15
16/**
17 * A query action to return meta information about AuthManager state.
18 *
19 * @ingroup API
20 */
21class ApiQueryAuthManagerInfo extends ApiQueryBase {
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:
79                    $reqs = ApiAuthManagerHelper::blacklistAuthenticationRequests( $reqs,
80                        $this->getConfig()->get( MainConfigNames::ChangeCredentialsBlacklist )
81                    );
82                    break;
83                case AuthManager::ACTION_REMOVE:
84                    $reqs = ApiAuthManagerHelper::blacklistAuthenticationRequests( $reqs,
85                        $this->getConfig()->get( MainConfigNames::RemoveCredentialsBlacklist )
86                    );
87                    break;
88            }
89
90            $ret += $helper->formatRequests( $reqs );
91        }
92
93        $this->getResult()->addValue( [ 'query' ], $this->getModuleName(), $ret );
94    }
95
96    /** @inheritDoc */
97    public function isReadMode() {
98        return false;
99    }
100
101    /** @inheritDoc */
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
124    /** @inheritDoc */
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
137    /** @inheritDoc */
138    public function getHelpUrls() {
139        return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Authmanagerinfo';
140    }
141}
142
143/** @deprecated class alias since 1.43 */
144class_alias( ApiQueryAuthManagerInfo::class, 'ApiQueryAuthManagerInfo' );