Completed
Push — master ( 0d1193...76ec59 )
by
unknown
15s
created

TOTPAware::getTotp()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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