TOTP: Cleanup uses of getFirstKey, getModule

Bug: T242031
Change-Id: Ibaa6515421c86168412b0bb30ae5655774304326
This commit is contained in:
Taavi Väänänen 2023-12-28 12:19:14 +02:00
parent d71e0a1952
commit 93b7dfc3ed
No known key found for this signature in database
GPG key ID: EF242F709F912FBE

View file

@ -54,18 +54,21 @@ class TOTP implements IModule {
/**
* @param OATHUser $user
* @param array $data
* @return bool|int
* @return bool
* @throws MWException
*/
public function verify( OATHUser $user, array $data ) {
public function verify( OATHUser $user, array $data ): bool {
if ( !isset( $data['token'] ) ) {
return false;
}
$key = $user->getFirstKey();
if ( !( $key instanceof TOTPKey ) ) {
return false;
foreach ( $user->getKeys() as $key ) {
if ( $key instanceof TOTPKey && $key->verify( $data, $user ) ) {
return true;
}
}
return $key->verify( $data, $user );
return false;
}
/**
@ -74,8 +77,14 @@ class TOTP implements IModule {
* @param OATHUser $user
* @return bool
*/
public function isEnabled( OATHUser $user ) {
return $user->getFirstKey() instanceof TOTPKey;
public function isEnabled( OATHUser $user ): bool {
foreach ( $user->getKeys() as $key ) {
if ( $key instanceof TOTPKey ) {
return true;
}
}
return false;
}
/**
@ -90,12 +99,12 @@ class TOTP implements IModule {
OATHUser $user,
OATHUserRepository $repo,
IContextSource $context
) {
$isEnabledForUser = $user->getModule() instanceof self;
if ( $action === OATHManage::ACTION_ENABLE && !$isEnabledForUser ) {
): ?IManageForm {
$hasTOTPKey = $this->isEnabled( $user );
if ( $action === OATHManage::ACTION_ENABLE && !$hasTOTPKey ) {
return new TOTPEnableForm( $user, $repo, $this, $context );
}
if ( $action === OATHManage::ACTION_DISABLE && $isEnabledForUser ) {
if ( $action === OATHManage::ACTION_DISABLE && $hasTOTPKey ) {
return new TOTPDisableForm( $user, $repo, $this, $context );
}
return null;