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