Passed
Push — master ( 6e13b4...351d10 )
by Robbie
06:46 queued 10s
created

Method   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 15
dl 0
loc 68
rs 10
c 1
b 0
f 0
wmc 9

9 Methods

Rating   Name   Duplication   Size   Complexity  
A applyRequirements() 0 4 1
A getCodeLength() 0 3 1
A getRegisterHandler() 0 3 1
A getThumbnail() 0 5 1
A getVerifyHandler() 0 3 1
A getName() 0 3 1
A isAvailable() 0 3 1
A getUnavailableMessage() 0 3 1
A getURLSegment() 0 3 1
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\VerifyHandlerInterface;
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;
0 ignored issues
show
introduced by
The private property $code_length is not used, and could be removed.
Loading history...
28
29
    public function getName(): string
30
    {
31
        return _t(__CLASS__ . '.NAME', 'Authenticator app');
32
    }
33
34
    public function getURLSegment(): string
35
    {
36
        return 'totp';
37
    }
38
39
    public function getVerifyHandler(): VerifyHandlerInterface
40
    {
41
        return Injector::inst()->create(VerifyHandler::class);
42
    }
43
44
    public function getRegisterHandler(): RegisterHandlerInterface
45
    {
46
        return Injector::inst()->create(RegisterHandler::class);
47
    }
48
49
    public function getThumbnail(): string
50
    {
51
        return ModuleLoader::getModule('silverstripe/totp-authenticator')
52
            ->getResource('client/dist/images/totp.svg')
53
            ->getURL();
54
    }
55
56
    public function applyRequirements(): void
57
    {
58
        Requirements::javascript('silverstripe/totp-authenticator: client/dist/js/bundle.js');
59
        Requirements::css('silverstripe/totp-authenticator: client/dist/styles/bundle.css');
60
    }
61
62
    /**
63
     * TOTP authentication is only available if the required environment variable is set to enable encryption.
64
     *
65
     * @return bool
66
     */
67
    public function isAvailable(): bool
68
    {
69
        return !empty(Environment::getEnv('SS_MFA_SECRET_KEY'));
70
    }
71
72
    public function getUnavailableMessage(): string
73
    {
74
        return _t(__CLASS__ . '.NOT_CONFIGURED', 'This method has not been configured yet.');
75
    }
76
77
    /**
78
     * Get the length of the TOTP code
79
     *
80
     * @return int
81
     */
82
    public function getCodeLength(): int
83
    {
84
        return (int) $this->config()->get('code_length');
85
    }
86
}
87