TOTPAware   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
c 1
b 0
f 0
dl 0
loc 27
rs 10
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getTotp() 0 7 2
A getEncryptionKey() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SilverStripe\TOTP;
6
7
use OTPHP\TOTP;
8
use OTPHP\TOTPInterface;
9
use RuntimeException;
10
use SilverStripe\Core\Environment;
11
use SilverStripe\MFA\Store\StoreInterface;
12
13
trait TOTPAware
14
{
15
    /**
16
     * Gets the encryption key to use from environment variables. This is generated by default on the Common Web
17
     * Platform, but can be defined as a custom value if required.
18
     *
19
     * @return string
20
     */
21
    protected function getEncryptionKey(): string
22
    {
23
        return (string) Environment::getEnv('SS_MFA_SECRET_KEY');
24
    }
25
26
    /**
27
     * Get an instance of the TOTP handler service. The secret must already be defined and set to the StoreInterface.
28
     *
29
     * @param StoreInterface $store
30
     * @return TOTPInterface
31
     * @throws RuntimeException
32
     */
33
    protected function getTotp(StoreInterface $store): TOTPInterface
34
    {
35
        $state = $store->getState();
36
        if (!isset($state['secret'])) {
37
            throw new RuntimeException('TOTP secret is not available in the StoreInterface');
38
        }
39
        return TOTP::create($state['secret']);
40
    }
41
}
42