Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 65
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 / 65
0.00% covered (danger)
0.00%
0 / 6
132
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 execute
0.00% covered (danger)
0.00%
0 / 36
0.00% covered (danger)
0.00%
0 / 1
42
 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 / 16
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 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @since 1.27
22 */
23
24use MediaWiki\Auth\AuthManager;
25use MediaWiki\MainConfigNames;
26use Wikimedia\ParamValidator\ParamValidator;
27
28/**
29 * A query action to return meta information about AuthManager state.
30 *
31 * @ingroup API
32 */
33class ApiQueryAuthManagerInfo extends ApiQueryBase {
34
35    private AuthManager $authManager;
36
37    /**
38     * @param ApiQuery $query
39     * @param string $moduleName
40     * @param AuthManager $authManager
41     */
42    public function __construct(
43        ApiQuery $query,
44        $moduleName,
45        AuthManager $authManager
46    ) {
47        parent::__construct( $query, $moduleName, 'ami' );
48        $this->authManager = $authManager;
49    }
50
51    public function execute() {
52        $params = $this->extractRequestParams();
53        $helper = new ApiAuthManagerHelper( $this, $this->authManager );
54        $ret = [
55            'canauthenticatenow' => $this->authManager->canAuthenticateNow(),
56            'cancreateaccounts' => $this->authManager->canCreateAccounts(),
57            'canlinkaccounts' => $this->authManager->canLinkAccounts(),
58        ];
59
60        if ( $params['securitysensitiveoperation'] !== null ) {
61            $ret['securitysensitiveoperationstatus'] = $this->authManager->securitySensitiveOperationStatus(
62                $params['securitysensitiveoperation']
63            );
64        }
65
66        if ( $params['requestsfor'] ) {
67            $action = $params['requestsfor'];
68
69            $preservedReq = $helper->getPreservedRequest();
70            if ( $preservedReq ) {
71                $ret += [
72                    'haspreservedstate' => $preservedReq->hasStateForAction( $action ),
73                    'hasprimarypreservedstate' => $preservedReq->hasPrimaryStateForAction( $action ),
74                    'preservedusername' => (string)$preservedReq->username,
75                ];
76            } else {
77                $ret += [
78                    'haspreservedstate' => false,
79                    'hasprimarypreservedstate' => false,
80                    'preservedusername' => '',
81                ];
82            }
83
84            $reqs = $this->authManager->getAuthenticationRequests( $action, $this->getUser() );
85
86            // Filter out blacklisted requests, depending on the action
87            switch ( $action ) {
88                case AuthManager::ACTION_CHANGE:
89                    $reqs = ApiAuthManagerHelper::blacklistAuthenticationRequests( $reqs,
90                        $this->getConfig()->get( MainConfigNames::ChangeCredentialsBlacklist )
91                    );
92                    break;
93                case AuthManager::ACTION_REMOVE:
94                    $reqs = ApiAuthManagerHelper::blacklistAuthenticationRequests( $reqs,
95                        $this->getConfig()->get( MainConfigNames::RemoveCredentialsBlacklist )
96                    );
97                    break;
98            }
99
100            $ret += $helper->formatRequests( $reqs );
101        }
102
103        $this->getResult()->addValue( [ 'query' ], $this->getModuleName(), $ret );
104    }
105
106    public function isReadMode() {
107        return false;
108    }
109
110    public function getAllowedParams() {
111        return [
112            'securitysensitiveoperation' => null,
113            'requestsfor' => [
114                ParamValidator::PARAM_TYPE => [
115                    AuthManager::ACTION_LOGIN,
116                    AuthManager::ACTION_LOGIN_CONTINUE,
117                    AuthManager::ACTION_CREATE,
118                    AuthManager::ACTION_CREATE_CONTINUE,
119                    AuthManager::ACTION_LINK,
120                    AuthManager::ACTION_LINK_CONTINUE,
121                    AuthManager::ACTION_CHANGE,
122                    AuthManager::ACTION_REMOVE,
123                    AuthManager::ACTION_UNLINK,
124                ],
125            ],
126        ] + ApiAuthManagerHelper::getStandardParams( '', 'mergerequestfields', 'messageformat' );
127    }
128
129    protected function getExamplesMessages() {
130        return [
131            'action=query&meta=authmanagerinfo&amirequestsfor=' . urlencode( AuthManager::ACTION_LOGIN )
132                => 'apihelp-query+authmanagerinfo-example-login',
133            'action=query&meta=authmanagerinfo&amirequestsfor=' . urlencode( AuthManager::ACTION_LOGIN ) .
134                '&amimergerequestfields=1'
135                => 'apihelp-query+authmanagerinfo-example-login-merged',
136            'action=query&meta=authmanagerinfo&amisecuritysensitiveoperation=foo'
137                => 'apihelp-query+authmanagerinfo-example-securitysensitiveoperation',
138        ];
139    }
140
141    public function getHelpUrls() {
142        return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Authmanagerinfo';
143    }
144}