Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 26 |
|
0.00% |
0 / 13 |
CRAP | |
0.00% |
0 / 1 |
| RecoveryCodes | |
0.00% |
0 / 26 |
|
0.00% |
0 / 13 |
306 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getName | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getDisplayName | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| newKey | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| getSecondaryAuthProvider | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
| verify | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
20 | |||
| isEnabled | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getManageForm | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getDescriptionMessage | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getDisableWarningMessage | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getAddKeyMessage | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getLoginSwitchButtonMessage | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| isSpecial | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace MediaWiki\Extension\OATHAuth\Module; |
| 4 | |
| 5 | use MediaWiki\Context\IContextSource; |
| 6 | use MediaWiki\Extension\OATHAuth\Auth\RecoveryCodesSecondaryAuthenticationProvider; |
| 7 | use MediaWiki\Extension\OATHAuth\HTMLForm\IManageForm; |
| 8 | use MediaWiki\Extension\OATHAuth\HTMLForm\RecoveryCodesStatusForm; |
| 9 | use MediaWiki\Extension\OATHAuth\IModule; |
| 10 | use MediaWiki\Extension\OATHAuth\Key\RecoveryCodeKeys; |
| 11 | use MediaWiki\Extension\OATHAuth\OATHUser; |
| 12 | use MediaWiki\Extension\OATHAuth\OATHUserRepository; |
| 13 | use MediaWiki\Message\Message; |
| 14 | use UnexpectedValueException; |
| 15 | |
| 16 | class RecoveryCodes implements IModule { |
| 17 | public const MODULE_NAME = "recoverycodes"; |
| 18 | |
| 19 | public function __construct( private readonly OATHUserRepository $userRepository ) { |
| 20 | } |
| 21 | |
| 22 | /** @inheritDoc */ |
| 23 | public function getName() { |
| 24 | return self::MODULE_NAME; |
| 25 | } |
| 26 | |
| 27 | /** @inheritDoc */ |
| 28 | public function getDisplayName() { |
| 29 | return wfMessage( 'oathauth-module-recoverycodes-label' ); |
| 30 | } |
| 31 | |
| 32 | /** |
| 33 | * @inheritDoc |
| 34 | * @throws UnexpectedValueException |
| 35 | */ |
| 36 | public function newKey( array $data ) { |
| 37 | if ( !isset( $data['recoverycodekeys'] ) ) { |
| 38 | throw new UnexpectedValueException( 'oathauth-invalid-recovery-code-data-format' ); |
| 39 | } |
| 40 | return RecoveryCodeKeys::newFromArray( $data ); |
| 41 | } |
| 42 | |
| 43 | public function getSecondaryAuthProvider(): RecoveryCodesSecondaryAuthenticationProvider { |
| 44 | return new RecoveryCodesSecondaryAuthenticationProvider( |
| 45 | $this, |
| 46 | $this->userRepository |
| 47 | ); |
| 48 | } |
| 49 | |
| 50 | public function verify( OATHUser $user, array $data ): bool { |
| 51 | if ( !isset( $data['recoverycode'] ) ) { |
| 52 | return false; |
| 53 | } |
| 54 | |
| 55 | $recoveryCodeKeys = $user->getRecoveryCodes(); |
| 56 | |
| 57 | if ( $recoveryCodeKeys === [] ) { |
| 58 | return false; |
| 59 | } |
| 60 | |
| 61 | /** @var RecoveryCodeKeys $recoveryCodeKey */ |
| 62 | $recoveryCodeKey = $recoveryCodeKeys[0]; |
| 63 | |
| 64 | if ( $recoveryCodeKey->verify( $data, $user ) ) { |
| 65 | return true; |
| 66 | } |
| 67 | |
| 68 | return false; |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Is this module currently enabled for the given user? |
| 73 | */ |
| 74 | public function isEnabled( OATHUser $user ): bool { |
| 75 | return (bool)$user->getRecoveryCodes(); |
| 76 | } |
| 77 | |
| 78 | /** @inheritDoc */ |
| 79 | public function getManageForm( |
| 80 | $action, |
| 81 | OATHUser $user, |
| 82 | OATHUserRepository $repo, |
| 83 | IContextSource $context |
| 84 | ): ?IManageForm { |
| 85 | return new RecoveryCodesStatusForm( $user, $repo, $this, $context ); |
| 86 | } |
| 87 | |
| 88 | /** @inheritDoc */ |
| 89 | public function getDescriptionMessage() { |
| 90 | return wfMessage( 'oathauth-recoverycodes-description' ); |
| 91 | } |
| 92 | |
| 93 | /** @inheritDoc */ |
| 94 | public function getDisableWarningMessage() { |
| 95 | return null; |
| 96 | } |
| 97 | |
| 98 | public function getAddKeyMessage(): ?Message { |
| 99 | return null; |
| 100 | } |
| 101 | |
| 102 | public function getLoginSwitchButtonMessage(): Message { |
| 103 | return wfMessage( 'oathauth-auth-use-recovery-code' ); |
| 104 | } |
| 105 | |
| 106 | /** @inheritDoc */ |
| 107 | public function isSpecial(): bool { |
| 108 | return true; |
| 109 | } |
| 110 | } |