1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | namespace SilverStripe\TOTP; |
||
6 | |||
7 | use SilverStripe\Core\Config\Configurable; |
||
8 | use SilverStripe\Core\Environment; |
||
9 | use SilverStripe\Core\Injector\Injector; |
||
10 | use SilverStripe\Core\Manifest\ModuleLoader; |
||
11 | use SilverStripe\MFA\Method\Handler\VerifyHandlerInterface; |
||
12 | use SilverStripe\MFA\Method\Handler\RegisterHandlerInterface; |
||
13 | use SilverStripe\MFA\Method\MethodInterface; |
||
14 | use SilverStripe\View\Requirements; |
||
15 | |||
16 | /** |
||
17 | * Enables time-based one-time password (TOTP) authentication for the silverstripe/mfa module. |
||
18 | */ |
||
19 | class Method implements MethodInterface |
||
20 | { |
||
21 | use Configurable; |
||
22 | |||
23 | /** |
||
24 | * The TOTP code length |
||
25 | * |
||
26 | * @config |
||
27 | * @var int |
||
28 | */ |
||
29 | private static $code_length = 6; |
||
0 ignored issues
–
show
introduced
by
![]() |
|||
30 | |||
31 | public function getName(): string |
||
32 | { |
||
33 | return _t(__CLASS__ . '.NAME', 'Authenticator app'); |
||
34 | } |
||
35 | |||
36 | public function getURLSegment(): string |
||
37 | { |
||
38 | return 'totp'; |
||
39 | } |
||
40 | |||
41 | public function getVerifyHandler(): VerifyHandlerInterface |
||
42 | { |
||
43 | return Injector::inst()->create(VerifyHandler::class); |
||
44 | } |
||
45 | |||
46 | public function getRegisterHandler(): RegisterHandlerInterface |
||
47 | { |
||
48 | return Injector::inst()->create(RegisterHandler::class); |
||
49 | } |
||
50 | |||
51 | public function getThumbnail(): string |
||
52 | { |
||
53 | return ModuleLoader::getModule('silverstripe/totp-authenticator') |
||
54 | ->getResource('client/dist/images/totp.svg') |
||
55 | ->getURL(); |
||
56 | } |
||
57 | |||
58 | public function applyRequirements(): void |
||
59 | { |
||
60 | Requirements::javascript('silverstripe/totp-authenticator: client/dist/js/bundle.js'); |
||
61 | Requirements::css('silverstripe/totp-authenticator: client/dist/styles/bundle.css'); |
||
62 | } |
||
63 | |||
64 | /** |
||
65 | * TOTP authentication is only available if the required environment variable is set to enable encryption. |
||
66 | * |
||
67 | * @return bool |
||
68 | */ |
||
69 | public function isAvailable(): bool |
||
70 | { |
||
71 | return !empty(Environment::getEnv('SS_MFA_SECRET_KEY')); |
||
72 | } |
||
73 | |||
74 | public function getUnavailableMessage(): string |
||
75 | { |
||
76 | return _t(__CLASS__ . '.NOT_CONFIGURED', 'This method has not been configured yet.'); |
||
77 | } |
||
78 | |||
79 | /** |
||
80 | * Get the length of the TOTP code |
||
81 | * |
||
82 | * @return int |
||
83 | */ |
||
84 | public function getCodeLength(): int |
||
85 | { |
||
86 | return (int) $this->config()->get('code_length'); |
||
87 | } |
||
88 | } |
||
89 |