Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 33
0.00% covered (danger)
0.00%
0 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
ApiSecurePollAuth
0.00% covered (danger)
0.00%
0 / 33
0.00% covered (danger)
0.00%
0 / 6
72
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 / 15
0.00% covered (danger)
0.00%
0 / 1
12
 getAllowedParams
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
2
 getExamplesMessages
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 mustBePosted
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 isInternal
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3/**
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 * http://www.gnu.org/copyleft/gpl.html
18 *
19 * @file
20 */
21
22namespace MediaWiki\Extension\SecurePoll\Api;
23
24use MediaWiki\Api\ApiBase;
25use MediaWiki\Api\ApiMain;
26use MediaWiki\Extension\SecurePoll\Context;
27use MediaWiki\Extension\SecurePoll\User\LocalAuth;
28use MediaWiki\Extension\SecurePoll\User\RemoteMWAuth;
29use MediaWiki\User\UserFactory;
30use Wikimedia\ParamValidator\ParamValidator;
31
32/**
33 * API module to authenticate jump-wiki user.
34 *
35 * @ingroup API
36 */
37class ApiSecurePollAuth extends ApiBase {
38    private UserFactory $userFactory;
39
40    public function __construct(
41        ApiMain $mainModule,
42        string $moduleName,
43        UserFactory $userFactory
44    ) {
45        parent::__construct( $mainModule, $moduleName );
46        $this->userFactory = $userFactory;
47    }
48
49    public function execute() {
50        $params = $this->extractRequestParams();
51
52        $user = $this->userFactory->newFromId( $params['id'] );
53        if ( !$user->isRegistered() ) {
54            $this->dieWithError(
55                'securepoll-api-no-user'
56            );
57        }
58        $token = RemoteMWAuth::encodeToken( $user->getToken() );
59        if ( !hash_equals( $params['token'], $token ) ) {
60            $this->dieWithError(
61                'securepoll-api-token-mismatch'
62            );
63        }
64
65        $context = new Context();
66        /** @var LocalAuth $auth */
67        $auth = $context->newAuth( 'local' );
68        $result = $auth->getUserParams( $user );
69        $this->getResult()->addValue( null, $this->getModuleName(), $result );
70    }
71
72    /** @inheritDoc */
73    public function getAllowedParams() {
74        return [
75            'token' => [
76                ParamValidator::PARAM_TYPE => 'string',
77                ParamValidator::PARAM_REQUIRED => true,
78            ],
79            'id' => [
80                ParamValidator::PARAM_TYPE => 'integer',
81                ParamValidator::PARAM_REQUIRED => true,
82            ],
83        ];
84    }
85
86    /** @inheritDoc */
87    protected function getExamplesMessages() {
88        return [
89            'action=securepollauth&token=123ABC&id=1&format=json' =>
90                'apihelp-securepollauth-example-auth',
91        ];
92    }
93
94    /** @inheritDoc */
95    public function mustBePosted() {
96        return true;
97    }
98
99    /** @inheritDoc */
100    public function isInternal() {
101        return true;
102    }
103}