TOTPAware::getEncryptionKey()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
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