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 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
WebAuthnAddKeyForm
0.00% covered (danger)
0.00%
0 / 36
0.00% covered (danger)
0.00%
0 / 5
56
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
2
 getHTML
0.00% covered (danger)
0.00%
0 / 17
0.00% covered (danger)
0.00%
0 / 1
12
 onSuccess
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 onSubmit
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getDescriptors
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3namespace MediaWiki\Extension\OATHAuth\HTMLForm;
4
5use MediaWiki\Context\IContextSource;
6use MediaWiki\Extension\OATHAuth\HTMLField\AddKeyLayout;
7use MediaWiki\Extension\OATHAuth\HTMLField\NoJsInfoField;
8use MediaWiki\Extension\OATHAuth\Key\RecoveryCodeKeys;
9use MediaWiki\Extension\OATHAuth\Module\IModule;
10use MediaWiki\Extension\OATHAuth\OATHAuthModuleRegistry;
11use MediaWiki\Extension\OATHAuth\OATHUser;
12use MediaWiki\Extension\OATHAuth\OATHUserRepository;
13use MediaWiki\Status\Status;
14
15class WebAuthnAddKeyForm extends OATHAuthOOUIHTMLForm {
16
17    use KeySessionStorageTrait;
18    use RecoveryCodesTrait;
19
20    /** @inheritDoc */
21    public function __construct(
22        OATHUser $oathUser,
23        OATHUserRepository $oathRepo,
24        IModule $module,
25        IContextSource $context,
26        OATHAuthModuleRegistry $registry
27    ) {
28        parent::__construct( $oathUser, $oathRepo, $module, $context, $registry );
29
30        $this->setId( 'webauthn-add-key-form' );
31        $this->suppressDefaultSubmit();
32
33        $this->panelPadded = false;
34        $this->panelFramed = false;
35    }
36
37    /** @inheritDoc */
38    public function getHTML( $submitResult ) {
39        $html = parent::getHTML( $submitResult );
40
41        $this->getOutput()->addModules( 'ext.webauthn.register' );
42
43        $moduleDbKeys = $this->oathUser->getKeysForModule( $this->module->getName() );
44
45        $recCodeKeys = [ 'recoverycodekeys' => [] ];
46        if ( array_key_exists( 0, $moduleDbKeys ) ) {
47            $objRecoveryCodeKeys = array_shift( $moduleDbKeys );
48            if ( $objRecoveryCodeKeys instanceof RecoveryCodeKeys ) {
49                $recCodeKeys = $objRecoveryCodeKeys->jsonSerialize();
50            }
51        }
52        $recCodeKeysForDisplay = $this->setKeyDataInSession(
53            'RecoveryCodeKeys',
54            $recCodeKeys,
55        );
56        $recCodeKeysForContent = $this->getRecoveryCodesForDisplay( $recCodeKeysForDisplay );
57        $this->getOutput()->addModules( 'ext.oath.recovery' );
58        $this->getOutput()->addModuleStyles( 'ext.oath.recovery.styles' );
59        $this->setOutputJsConfigVars( $recCodeKeysForContent );
60
61        return $html . $this->generateRecoveryCodesContent( $recCodeKeysForContent, true );
62    }
63
64    public function onSuccess(): void {
65        // Not used - redirect is handled client-side after API call
66    }
67
68    public function onSubmit( array $formData ): Status|bool|array|string {
69        // Registration is handled client-side via API (action=webauthn&func=register)
70        return [ 'webauthn-javascript-required' ];
71    }
72
73    protected function getDescriptors(): array {
74        return [
75            'nojs' => [
76                'class' => NoJsInfoField::class,
77                'section' => 'webauthn-add-key-section-name',
78            ],
79            'name-layout' => [
80                'label-message' => 'oathauth-webauthn-ui-key-register-help',
81                'class' => AddKeyLayout::class,
82                'raw' => true,
83                'section' => 'webauthn-add-key-section-name'
84            ],
85        ];
86    }
87}