@@ -36,130 +36,130 @@ |
||
| 36 | 36 | |
| 37 | 37 | class BackupCodesProvider implements IProvider, IProvidesPersonalSettings { |
| 38 | 38 | |
| 39 | - /** @var string */ |
|
| 40 | - private $appName; |
|
| 41 | - |
|
| 42 | - /** @var BackupCodeStorage */ |
|
| 43 | - private $storage; |
|
| 44 | - |
|
| 45 | - /** @var IL10N */ |
|
| 46 | - private $l10n; |
|
| 47 | - |
|
| 48 | - /** @var AppManager */ |
|
| 49 | - private $appManager; |
|
| 50 | - /** @var IInitialStateService */ |
|
| 51 | - private $initialStateService; |
|
| 52 | - |
|
| 53 | - /** |
|
| 54 | - * @param string $appName |
|
| 55 | - * @param BackupCodeStorage $storage |
|
| 56 | - * @param IL10N $l10n |
|
| 57 | - * @param AppManager $appManager |
|
| 58 | - */ |
|
| 59 | - public function __construct(string $appName, |
|
| 60 | - BackupCodeStorage $storage, |
|
| 61 | - IL10N $l10n, |
|
| 62 | - AppManager $appManager, |
|
| 63 | - IInitialStateService $initialStateService) { |
|
| 64 | - $this->appName = $appName; |
|
| 65 | - $this->l10n = $l10n; |
|
| 66 | - $this->storage = $storage; |
|
| 67 | - $this->appManager = $appManager; |
|
| 68 | - $this->initialStateService = $initialStateService; |
|
| 69 | - } |
|
| 70 | - |
|
| 71 | - /** |
|
| 72 | - * Get unique identifier of this 2FA provider |
|
| 73 | - * |
|
| 74 | - * @return string |
|
| 75 | - */ |
|
| 76 | - public function getId(): string { |
|
| 77 | - return 'backup_codes'; |
|
| 78 | - } |
|
| 79 | - |
|
| 80 | - /** |
|
| 81 | - * Get the display name for selecting the 2FA provider |
|
| 82 | - * |
|
| 83 | - * @return string |
|
| 84 | - */ |
|
| 85 | - public function getDisplayName(): string { |
|
| 86 | - return $this->l10n->t('Backup code'); |
|
| 87 | - } |
|
| 88 | - |
|
| 89 | - /** |
|
| 90 | - * Get the description for selecting the 2FA provider |
|
| 91 | - * |
|
| 92 | - * @return string |
|
| 93 | - */ |
|
| 94 | - public function getDescription(): string { |
|
| 95 | - return $this->l10n->t('Use backup code'); |
|
| 96 | - } |
|
| 97 | - |
|
| 98 | - /** |
|
| 99 | - * Get the template for rending the 2FA provider view |
|
| 100 | - * |
|
| 101 | - * @param IUser $user |
|
| 102 | - * @return Template |
|
| 103 | - */ |
|
| 104 | - public function getTemplate(IUser $user): Template { |
|
| 105 | - return new Template('twofactor_backupcodes', 'challenge'); |
|
| 106 | - } |
|
| 107 | - |
|
| 108 | - /** |
|
| 109 | - * Verify the given challenge |
|
| 110 | - * |
|
| 111 | - * @param IUser $user |
|
| 112 | - * @param string $challenge |
|
| 113 | - * @return bool |
|
| 114 | - */ |
|
| 115 | - public function verifyChallenge(IUser $user, string $challenge): bool { |
|
| 116 | - return $this->storage->validateCode($user, $challenge); |
|
| 117 | - } |
|
| 118 | - |
|
| 119 | - /** |
|
| 120 | - * Decides whether 2FA is enabled for the given user |
|
| 121 | - * |
|
| 122 | - * @param IUser $user |
|
| 123 | - * @return boolean |
|
| 124 | - */ |
|
| 125 | - public function isTwoFactorAuthEnabledForUser(IUser $user): bool { |
|
| 126 | - return $this->storage->hasBackupCodes($user); |
|
| 127 | - } |
|
| 128 | - |
|
| 129 | - /** |
|
| 130 | - * Determine whether backup codes should be active or not |
|
| 131 | - * |
|
| 132 | - * Backup codes only make sense if at least one 2FA provider is active, |
|
| 133 | - * hence this method checks all enabled apps on whether they provide 2FA |
|
| 134 | - * functionality or not. If there's at least one app, backup codes are |
|
| 135 | - * enabled on the personal settings page. |
|
| 136 | - * |
|
| 137 | - * @param IUser $user |
|
| 138 | - * @return boolean |
|
| 139 | - */ |
|
| 140 | - public function isActive(IUser $user): bool { |
|
| 141 | - $appIds = array_filter($this->appManager->getEnabledAppsForUser($user), function($appId) { |
|
| 142 | - return $appId !== $this->appName; |
|
| 143 | - }); |
|
| 144 | - foreach ($appIds as $appId) { |
|
| 145 | - $info = $this->appManager->getAppInfo($appId); |
|
| 146 | - if (isset($info['two-factor-providers']) && count($info['two-factor-providers']) > 0) { |
|
| 147 | - return true; |
|
| 148 | - } |
|
| 149 | - } |
|
| 150 | - return false; |
|
| 151 | - } |
|
| 152 | - |
|
| 153 | - /** |
|
| 154 | - * @param IUser $user |
|
| 155 | - * |
|
| 156 | - * @return IPersonalProviderSettings |
|
| 157 | - */ |
|
| 158 | - public function getPersonalSettings(IUser $user): IPersonalProviderSettings { |
|
| 159 | - $state = $this->storage->getBackupCodesState($user); |
|
| 160 | - $this->initialStateService->provideInitialState($this->appName, 'state', $state); |
|
| 161 | - return new Personal(); |
|
| 162 | - } |
|
| 39 | + /** @var string */ |
|
| 40 | + private $appName; |
|
| 41 | + |
|
| 42 | + /** @var BackupCodeStorage */ |
|
| 43 | + private $storage; |
|
| 44 | + |
|
| 45 | + /** @var IL10N */ |
|
| 46 | + private $l10n; |
|
| 47 | + |
|
| 48 | + /** @var AppManager */ |
|
| 49 | + private $appManager; |
|
| 50 | + /** @var IInitialStateService */ |
|
| 51 | + private $initialStateService; |
|
| 52 | + |
|
| 53 | + /** |
|
| 54 | + * @param string $appName |
|
| 55 | + * @param BackupCodeStorage $storage |
|
| 56 | + * @param IL10N $l10n |
|
| 57 | + * @param AppManager $appManager |
|
| 58 | + */ |
|
| 59 | + public function __construct(string $appName, |
|
| 60 | + BackupCodeStorage $storage, |
|
| 61 | + IL10N $l10n, |
|
| 62 | + AppManager $appManager, |
|
| 63 | + IInitialStateService $initialStateService) { |
|
| 64 | + $this->appName = $appName; |
|
| 65 | + $this->l10n = $l10n; |
|
| 66 | + $this->storage = $storage; |
|
| 67 | + $this->appManager = $appManager; |
|
| 68 | + $this->initialStateService = $initialStateService; |
|
| 69 | + } |
|
| 70 | + |
|
| 71 | + /** |
|
| 72 | + * Get unique identifier of this 2FA provider |
|
| 73 | + * |
|
| 74 | + * @return string |
|
| 75 | + */ |
|
| 76 | + public function getId(): string { |
|
| 77 | + return 'backup_codes'; |
|
| 78 | + } |
|
| 79 | + |
|
| 80 | + /** |
|
| 81 | + * Get the display name for selecting the 2FA provider |
|
| 82 | + * |
|
| 83 | + * @return string |
|
| 84 | + */ |
|
| 85 | + public function getDisplayName(): string { |
|
| 86 | + return $this->l10n->t('Backup code'); |
|
| 87 | + } |
|
| 88 | + |
|
| 89 | + /** |
|
| 90 | + * Get the description for selecting the 2FA provider |
|
| 91 | + * |
|
| 92 | + * @return string |
|
| 93 | + */ |
|
| 94 | + public function getDescription(): string { |
|
| 95 | + return $this->l10n->t('Use backup code'); |
|
| 96 | + } |
|
| 97 | + |
|
| 98 | + /** |
|
| 99 | + * Get the template for rending the 2FA provider view |
|
| 100 | + * |
|
| 101 | + * @param IUser $user |
|
| 102 | + * @return Template |
|
| 103 | + */ |
|
| 104 | + public function getTemplate(IUser $user): Template { |
|
| 105 | + return new Template('twofactor_backupcodes', 'challenge'); |
|
| 106 | + } |
|
| 107 | + |
|
| 108 | + /** |
|
| 109 | + * Verify the given challenge |
|
| 110 | + * |
|
| 111 | + * @param IUser $user |
|
| 112 | + * @param string $challenge |
|
| 113 | + * @return bool |
|
| 114 | + */ |
|
| 115 | + public function verifyChallenge(IUser $user, string $challenge): bool { |
|
| 116 | + return $this->storage->validateCode($user, $challenge); |
|
| 117 | + } |
|
| 118 | + |
|
| 119 | + /** |
|
| 120 | + * Decides whether 2FA is enabled for the given user |
|
| 121 | + * |
|
| 122 | + * @param IUser $user |
|
| 123 | + * @return boolean |
|
| 124 | + */ |
|
| 125 | + public function isTwoFactorAuthEnabledForUser(IUser $user): bool { |
|
| 126 | + return $this->storage->hasBackupCodes($user); |
|
| 127 | + } |
|
| 128 | + |
|
| 129 | + /** |
|
| 130 | + * Determine whether backup codes should be active or not |
|
| 131 | + * |
|
| 132 | + * Backup codes only make sense if at least one 2FA provider is active, |
|
| 133 | + * hence this method checks all enabled apps on whether they provide 2FA |
|
| 134 | + * functionality or not. If there's at least one app, backup codes are |
|
| 135 | + * enabled on the personal settings page. |
|
| 136 | + * |
|
| 137 | + * @param IUser $user |
|
| 138 | + * @return boolean |
|
| 139 | + */ |
|
| 140 | + public function isActive(IUser $user): bool { |
|
| 141 | + $appIds = array_filter($this->appManager->getEnabledAppsForUser($user), function($appId) { |
|
| 142 | + return $appId !== $this->appName; |
|
| 143 | + }); |
|
| 144 | + foreach ($appIds as $appId) { |
|
| 145 | + $info = $this->appManager->getAppInfo($appId); |
|
| 146 | + if (isset($info['two-factor-providers']) && count($info['two-factor-providers']) > 0) { |
|
| 147 | + return true; |
|
| 148 | + } |
|
| 149 | + } |
|
| 150 | + return false; |
|
| 151 | + } |
|
| 152 | + |
|
| 153 | + /** |
|
| 154 | + * @param IUser $user |
|
| 155 | + * |
|
| 156 | + * @return IPersonalProviderSettings |
|
| 157 | + */ |
|
| 158 | + public function getPersonalSettings(IUser $user): IPersonalProviderSettings { |
|
| 159 | + $state = $this->storage->getBackupCodesState($user); |
|
| 160 | + $this->initialStateService->provideInitialState($this->appName, 'state', $state); |
|
| 161 | + return new Personal(); |
|
| 162 | + } |
|
| 163 | 163 | |
| 164 | 164 | } |
| 165 | 165 | |
@@ -30,61 +30,61 @@ |
||
| 30 | 30 | |
| 31 | 31 | class InitialStateService implements IInitialStateService { |
| 32 | 32 | |
| 33 | - /** @var ILogger */ |
|
| 34 | - private $logger; |
|
| 33 | + /** @var ILogger */ |
|
| 34 | + private $logger; |
|
| 35 | 35 | |
| 36 | - /** @var string[][] */ |
|
| 37 | - private $states = []; |
|
| 36 | + /** @var string[][] */ |
|
| 37 | + private $states = []; |
|
| 38 | 38 | |
| 39 | - /** @var Closure[][] */ |
|
| 40 | - private $lazyStates = []; |
|
| 39 | + /** @var Closure[][] */ |
|
| 40 | + private $lazyStates = []; |
|
| 41 | 41 | |
| 42 | - public function __construct(ILogger $logger) { |
|
| 43 | - $this->logger = $logger; |
|
| 44 | - } |
|
| 42 | + public function __construct(ILogger $logger) { |
|
| 43 | + $this->logger = $logger; |
|
| 44 | + } |
|
| 45 | 45 | |
| 46 | - public function provideInitialState(string $appName, string $key, $data): void { |
|
| 47 | - // Scalars and JsonSerializable are fine |
|
| 48 | - if (is_scalar($data) || $data instanceof \JsonSerializable || is_array($data)) { |
|
| 49 | - if (!isset($this->states[$appName])) { |
|
| 50 | - $this->states[$appName] = []; |
|
| 51 | - } |
|
| 52 | - $this->states[$appName][$key] = json_encode($data); |
|
| 53 | - return; |
|
| 54 | - } |
|
| 46 | + public function provideInitialState(string $appName, string $key, $data): void { |
|
| 47 | + // Scalars and JsonSerializable are fine |
|
| 48 | + if (is_scalar($data) || $data instanceof \JsonSerializable || is_array($data)) { |
|
| 49 | + if (!isset($this->states[$appName])) { |
|
| 50 | + $this->states[$appName] = []; |
|
| 51 | + } |
|
| 52 | + $this->states[$appName][$key] = json_encode($data); |
|
| 53 | + return; |
|
| 54 | + } |
|
| 55 | 55 | |
| 56 | - $this->logger->warning('Invalid data provided to provideInitialState by ' . $appName); |
|
| 57 | - } |
|
| 56 | + $this->logger->warning('Invalid data provided to provideInitialState by ' . $appName); |
|
| 57 | + } |
|
| 58 | 58 | |
| 59 | - public function provideLazyInitialState(string $appName, string $key, Closure $closure): void { |
|
| 60 | - if (!isset($this->lazyStates[$appName])) { |
|
| 61 | - $this->lazyStates[$appName] = []; |
|
| 62 | - } |
|
| 63 | - $this->lazyStates[$appName][$key] = $closure; |
|
| 64 | - } |
|
| 59 | + public function provideLazyInitialState(string $appName, string $key, Closure $closure): void { |
|
| 60 | + if (!isset($this->lazyStates[$appName])) { |
|
| 61 | + $this->lazyStates[$appName] = []; |
|
| 62 | + } |
|
| 63 | + $this->lazyStates[$appName][$key] = $closure; |
|
| 64 | + } |
|
| 65 | 65 | |
| 66 | - /** |
|
| 67 | - * Invoke all callbacks to populate the `states` property |
|
| 68 | - */ |
|
| 69 | - private function invokeLazyStateCallbacks(): void { |
|
| 70 | - foreach ($this->lazyStates as $app => $lazyStates) { |
|
| 71 | - foreach ($lazyStates as $key => $lazyState) { |
|
| 72 | - $this->provideInitialState($app, $key, $lazyState()); |
|
| 73 | - } |
|
| 74 | - } |
|
| 75 | - $this->lazyStates = []; |
|
| 76 | - } |
|
| 66 | + /** |
|
| 67 | + * Invoke all callbacks to populate the `states` property |
|
| 68 | + */ |
|
| 69 | + private function invokeLazyStateCallbacks(): void { |
|
| 70 | + foreach ($this->lazyStates as $app => $lazyStates) { |
|
| 71 | + foreach ($lazyStates as $key => $lazyState) { |
|
| 72 | + $this->provideInitialState($app, $key, $lazyState()); |
|
| 73 | + } |
|
| 74 | + } |
|
| 75 | + $this->lazyStates = []; |
|
| 76 | + } |
|
| 77 | 77 | |
| 78 | - public function getInitialStates(): array { |
|
| 79 | - $this->invokeLazyStateCallbacks(); |
|
| 78 | + public function getInitialStates(): array { |
|
| 79 | + $this->invokeLazyStateCallbacks(); |
|
| 80 | 80 | |
| 81 | - $appStates = []; |
|
| 82 | - foreach ($this->states as $app => $states) { |
|
| 83 | - foreach ($states as $key => $value) { |
|
| 84 | - $appStates["$app-$key"] = $value; |
|
| 85 | - } |
|
| 86 | - } |
|
| 87 | - return $appStates; |
|
| 88 | - } |
|
| 81 | + $appStates = []; |
|
| 82 | + foreach ($this->states as $app => $states) { |
|
| 83 | + foreach ($states as $key => $value) { |
|
| 84 | + $appStates["$app-$key"] = $value; |
|
| 85 | + } |
|
| 86 | + } |
|
| 87 | + return $appStates; |
|
| 88 | + } |
|
| 89 | 89 | |
| 90 | 90 | } |
@@ -53,7 +53,7 @@ |
||
| 53 | 53 | return; |
| 54 | 54 | } |
| 55 | 55 | |
| 56 | - $this->logger->warning('Invalid data provided to provideInitialState by ' . $appName); |
|
| 56 | + $this->logger->warning('Invalid data provided to provideInitialState by '.$appName); |
|
| 57 | 57 | } |
| 58 | 58 | |
| 59 | 59 | public function provideLazyInitialState(string $appName, string $key, Closure $closure): void { |
@@ -30,31 +30,31 @@ |
||
| 30 | 30 | * @since 16.0.0 |
| 31 | 31 | */ |
| 32 | 32 | interface IInitialStateService { |
| 33 | - /** |
|
| 34 | - * Allows an app to provide its initial state to the template system. |
|
| 35 | - * Use this if you know your initial state sill be used for example if |
|
| 36 | - * you are in the render function of you controller. |
|
| 37 | - * |
|
| 38 | - * @since 16.0.0 |
|
| 39 | - * |
|
| 40 | - * @param string $appName |
|
| 41 | - * @param string $key |
|
| 42 | - * @param bool|int|float|string|array|\JsonSerializable $data |
|
| 43 | - */ |
|
| 44 | - public function provideInitialState(string $appName, string $key, $data): void; |
|
| 33 | + /** |
|
| 34 | + * Allows an app to provide its initial state to the template system. |
|
| 35 | + * Use this if you know your initial state sill be used for example if |
|
| 36 | + * you are in the render function of you controller. |
|
| 37 | + * |
|
| 38 | + * @since 16.0.0 |
|
| 39 | + * |
|
| 40 | + * @param string $appName |
|
| 41 | + * @param string $key |
|
| 42 | + * @param bool|int|float|string|array|\JsonSerializable $data |
|
| 43 | + */ |
|
| 44 | + public function provideInitialState(string $appName, string $key, $data): void; |
|
| 45 | 45 | |
| 46 | - /** |
|
| 47 | - * Allows an app to provide its initial state via a lazy method. |
|
| 48 | - * This will call the closure when the template is being generated. |
|
| 49 | - * Use this if your app is injected into pages. Since then the render method |
|
| 50 | - * is not called explicitly. But we do not want to load the state on webdav |
|
| 51 | - * requests for example. |
|
| 52 | - * |
|
| 53 | - * @since 16.0.0 |
|
| 54 | - * |
|
| 55 | - * @param string $appName |
|
| 56 | - * @param string $key |
|
| 57 | - * @param Closure $closure returns a primitive or an object that implements JsonSerializable |
|
| 58 | - */ |
|
| 59 | - public function provideLazyInitialState(string $appName, string $key, Closure $closure): void; |
|
| 46 | + /** |
|
| 47 | + * Allows an app to provide its initial state via a lazy method. |
|
| 48 | + * This will call the closure when the template is being generated. |
|
| 49 | + * Use this if your app is injected into pages. Since then the render method |
|
| 50 | + * is not called explicitly. But we do not want to load the state on webdav |
|
| 51 | + * requests for example. |
|
| 52 | + * |
|
| 53 | + * @since 16.0.0 |
|
| 54 | + * |
|
| 55 | + * @param string $appName |
|
| 56 | + * @param string $key |
|
| 57 | + * @param Closure $closure returns a primitive or an object that implements JsonSerializable |
|
| 58 | + */ |
|
| 59 | + public function provideLazyInitialState(string $appName, string $key, Closure $closure): void; |
|
| 60 | 60 | } |