Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 36
0.00% covered (danger)
0.00%
0 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
WebAuthnDisableForm
0.00% covered (danger)
0.00%
0 / 36
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 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 getHTML
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 onSuccess
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 onSubmit
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
12
 getDescriptors
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
2
 authenticate
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
12
1<?php
2
3namespace MediaWiki\Extension\WebAuthn\HTMLForm;
4
5use MediaWiki\Config\ConfigException;
6use MediaWiki\Context\IContextSource;
7use MediaWiki\Extension\OATHAuth\HTMLForm\OATHAuthOOUIHTMLForm;
8use MediaWiki\Extension\OATHAuth\IModule;
9use MediaWiki\Extension\OATHAuth\OATHUser;
10use MediaWiki\Extension\OATHAuth\OATHUserRepository;
11use MediaWiki\Extension\WebAuthn\Authenticator;
12use MediaWiki\SpecialPage\SpecialPage;
13use MediaWiki\Status\Status;
14use MWException;
15
16class WebAuthnDisableForm extends OATHAuthOOUIHTMLForm {
17
18    /**
19     * @var OATHUserRepository
20     */
21    protected $userRepo;
22
23    /**
24     * @var OATHUser
25     */
26    protected $oathUser;
27
28    /**
29     * @inheritDoc
30     */
31    public function __construct(
32        OATHUser $oathUser,
33        OATHUserRepository $oathRepo,
34        IModule $module,
35        IContextSource $context
36    ) {
37        parent::__construct( $oathUser, $oathRepo, $module, $context );
38
39        $this->setId( 'disable-webauthn-form' );
40        $this->suppressDefaultSubmit();
41    }
42
43    /**
44     * @param array|bool|Status|string $submitResult
45     * @return string
46     */
47    public function getHTML( $submitResult ) {
48        if ( $this->wasSubmitted() === false ) {
49            $this->getOutput()->addModules( 'ext.webauthn.disable' );
50            return parent::getHTML( $submitResult );
51        }
52        return '';
53    }
54
55    /**
56     * Add content to output when operation was successful
57     */
58    public function onSuccess() {
59        $this->getOutput()->redirect(
60            SpecialPage::getTitleFor( 'OATHManage' )->getLocalURL()
61        );
62    }
63
64    /**
65     * @param array $formData
66     * @return array|bool
67     * @throws ConfigException
68     * @throws MWException
69     */
70    public function onSubmit( array $formData ) {
71        if ( !isset( $formData['credential'] ) ) {
72            return [ 'oathauth-failedtovalidateoath' ];
73        }
74
75        if ( !$this->authenticate( $formData['credential'] ) ) {
76            return [ 'oathauth-failedtovalidateoath' ];
77        }
78        return true;
79    }
80
81    /**
82     * @return array
83     */
84    protected function getDescriptors() {
85        return [
86            'info' => [
87                'type' => 'info',
88                'default' => wfMessage( 'webauthn-ui-disable-prompt' )->plain(),
89                'section' => 'webauthn-disable-section-name'
90            ],
91            'credential' => [
92                'name' => 'credential',
93                'type' => 'hidden'
94            ]
95        ];
96    }
97
98    /**
99     * @param string $credential
100     * @return bool
101     */
102    private function authenticate( string $credential ): bool {
103        $authenticator = Authenticator::factory( $this->getUser(), $this->getRequest() );
104        if ( !$authenticator->isEnabled() ) {
105            return false;
106        }
107        $authenticationResult = $authenticator->continueAuthentication( [
108            'credential' => $credential
109        ] );
110        if ( $authenticationResult->isGood() ) {
111            $this->oathRepo->removeAll( $this->oathUser, $this->getRequest()->getIP(), true );
112            return true;
113        }
114        return false;
115    }
116}