Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
86.05% covered (warning)
86.05%
37 / 43
57.14% covered (warning)
57.14%
4 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
CaptchaAuthenticationRequest
86.05% covered (warning)
86.05%
37 / 43
57.14% covered (warning)
57.14%
4 / 7
12.39
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getUniqueId
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 loadFromSubmission
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
3
 getAction
42.86% covered (danger)
42.86%
3 / 7
0.00% covered (danger)
0.00%
0 / 1
4.68
 getFieldInfo
100.00% covered (success)
100.00%
21 / 21
100.00% covered (success)
100.00%
1 / 1
1
 getMetadata
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 __set_state
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2
3namespace MediaWiki\Extension\ConfirmEdit\Auth;
4
5use MediaWiki\Auth\AuthenticationRequest;
6use MediaWiki\Auth\AuthManager;
7use MediaWiki\Extension\ConfirmEdit\CaptchaTriggers;
8use MediaWiki\Extension\ConfirmEdit\Hooks;
9
10/**
11 * Generic captcha authentication request class.
12 *
13 *  A captcha consists of some data stored in the session
14 * (e.g. a question and its answer), an ID that references the data, and a solution.
15 */
16class CaptchaAuthenticationRequest extends AuthenticationRequest {
17    /** @var string Identifier of the captcha. Used internally to remember which captcha was used. */
18    public $captchaId;
19
20    /** @var array Information about the captcha (e.g. question text; solution). Exact semantics
21     *    differ between types.
22     */
23    public $captchaData;
24
25    /** @var string Captcha solution submitted by the user. */
26    public $captchaWord;
27
28    /**
29     * @param string $id
30     * @param array $data
31     */
32    public function __construct( $id, $data ) {
33        $this->captchaId = $id;
34        $this->captchaData = $data;
35    }
36
37    /** @inheritDoc */
38    public function getUniqueId() {
39        return 'CaptchaAuthenticationRequest';
40    }
41
42    /** @inheritDoc */
43    public function loadFromSubmission( array $data ) {
44        $success = parent::loadFromSubmission( $data );
45        if ( $success ) {
46            // The captchaId and captchaWord were set from the submission, but captchaData was not.
47            $captcha = Hooks::getInstance( $this->getAction() );
48            $this->captchaData = $captcha->retrieveCaptcha( $this->captchaId );
49            if ( !$this->captchaData ) {
50                return false;
51            }
52        }
53        return $success;
54    }
55
56    public function getAction(): string {
57        // generic action doesn't exist, but *Captcha::getMessage will handle that
58        $action = 'generic';
59        switch ( $this->action ) {
60            case AuthManager::ACTION_LOGIN:
61                $action = CaptchaTriggers::BAD_LOGIN;
62                break;
63            case AuthManager::ACTION_CREATE:
64                $action = CaptchaTriggers::CREATE_ACCOUNT;
65                break;
66        }
67        return $action;
68    }
69
70    /** @inheritDoc */
71    public function getFieldInfo() {
72        $action = $this->getAction();
73        $captcha = Hooks::getInstance( $action );
74
75        return [
76            'captchaId' => [
77                'type' => 'hidden',
78                'value' => $this->captchaId,
79                'label' => wfMessage( 'captcha-id-label' ),
80                'help' => wfMessage( 'captcha-id-help' ),
81            ],
82            'captchaInfo' => [
83                'type' => 'null',
84                'label' => $captcha->getMessage( $action ),
85                'value' => $captcha->getCaptchaInfo( $this->captchaData, $this->captchaId ),
86                'help' => wfMessage( 'captcha-info-help' ),
87            ],
88            'captchaWord' => [
89                'type' => 'string',
90                'label' => wfMessage( 'captcha-label' ),
91                'help' => wfMessage( 'captcha-help' ),
92            ],
93        ];
94    }
95
96    /** @inheritDoc */
97    public function getMetadata() {
98        return ( Hooks::getInstance( $this->getAction() ) )->describeCaptchaType( $this->getAction() );
99    }
100
101    /**
102     * @param array $data
103     * @return CaptchaAuthenticationRequest
104     */
105    public static function __set_state( $data ) {
106        $ret = new static( '', [] );
107        foreach ( $data as $k => $v ) {
108            $ret->$k = $v;
109        }
110        return $ret;
111    }
112}