Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 40
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
RecoveryCodesStatusForm
0.00% covered (danger)
0.00%
0 / 40
0.00% covered (danger)
0.00%
0 / 4
30
0.00% covered (danger)
0.00%
0 / 1
 getHTML
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
2
 getDescriptors
0.00% covered (danger)
0.00%
0 / 17
0.00% covered (danger)
0.00%
0 / 1
6
 onSuccess
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
2
 onSubmit
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3namespace MediaWiki\Extension\OATHAuth\HTMLForm;
4
5use MediaWiki\Extension\OATHAuth\Module\RecoveryCodes;
6use MediaWiki\Logger\LoggerFactory;
7use MediaWiki\Status\Status;
8
9/**
10 * @property RecoveryCodes $module
11 */
12class RecoveryCodesStatusForm extends OATHAuthOOUIHTMLForm {
13    use KeySessionStorageTrait;
14    use RecoveryCodesTrait;
15
16    /** @inheritDoc */
17    public function getHTML( $submitResult ) {
18        $out = $this->getOutput();
19        $out->addModuleStyles( 'ext.oath.recovery.styles' );
20        $out->addModules( 'ext.oath.recovery' );
21        $out->setPageTitleMsg( $this->msg( 'oathauth-recoverycodes-header-create' ) );
22        return parent::getHTML( $submitResult );
23    }
24
25    protected function getDescriptors(): array {
26        if ( $this->oathUser->userHasNonSpecialEnabledKeys() ) {
27            $submitMsg = $this->msg(
28                'oathauth-recoverycodes-create-label',
29                $this->getConfig()->get( 'OATHRecoveryCodesCount' )
30            );
31            $this->setSubmitTextMsg( $submitMsg );
32            $this->setSubmitDestructive();
33            $this->showCancel();
34            $this->setCancelTarget( $this->getTitle() );
35        } else {
36            $this->suppressDefaultSubmit();
37        }
38        return [
39            'warning' => [
40                'type' => 'info',
41                'default' => $this->msg( 'oathauth-recoverycodes-regenerate-warning',
42                    $this->getConfig()->get( 'OATHRecoveryCodesCount' ) )->parse(),
43                'raw' => true,
44            ] ];
45    }
46
47    /**
48     * Add content to output when the operation was successful
49     */
50    public function onSuccess(): void {
51        $key = $this->module->ensureExistence( $this->oathUser );
52
53        $recoveryCodes = $this->getRecoveryCodesForDisplay( $key );
54        $output = $this->getOutput();
55        $output->addModuleStyles( 'ext.oath.recovery.styles' );
56        $output->addModules( 'ext.oath.recovery' );
57        $output->addHtml(
58            (string)$this->generateRecoveryCodesContent( $recoveryCodes )
59        );
60    }
61
62    public function onSubmit( array $formData ): Status|bool|array|string {
63        $key = $this->module->ensureExistence( $this->oathUser );
64        $key->regenerateRecoveryCodeKeys();
65        $this->oathRepo->updateKey( $this->oathUser, $key );
66
67        LoggerFactory::getInstance( 'authentication' )->info(
68            "OATHAuth {user} generated new recovery codes from {clientip}", [
69                'user' => $this->getUser()->getName(),
70                'clientip' => $this->getRequest()->getIP(),
71            ]
72        );
73
74        return true;
75    }
76}