Passed
Push — master ( ee8c83...2ef946 )
by Jaime Pérez
04:32 queued 02:10
created

AAGUID::__construct()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 10
c 0
b 0
f 0
nc 3
nop 0
dl 0
loc 15
rs 9.9332
1
<?php
2
3
namespace SimpleSAML\Module\webauthn\WebAuthn;
4
5
use SimpleSAML\Logger;
6
use SimpleSAML\Utils\Config as SSPConfig;
7
8
9
/**
10
 * Class AAGUID
11
 *
12
 * @package SimpleSAML\Module\webauthn\WebAuthn
13
 */
14
class AAGUID
15
{
16
17
    /**
18
     * The actual dictionary containing all known tokens.
19
     *
20
     * @var array
21
     */
22
    protected $dictionary = [];
23
24
25
    /**
26
     * The name of the configuration file where we should expect the AAGUID dictionary.
27
     */
28
    public const AAGUID_CONFIG_FILE = 'webauthn-aaguid.json';
29
30
31
    /**
32
     * The singleton instance.
33
     *
34
     * @var AAGUID
35
     */
36
    protected static $instance;
37
38
39
    /**
40
     * AAGUID constructor.
41
     */
42
    protected function __construct()
43
    {
44
        $path = SSPConfig::getConfigDir().'/'.self::AAGUID_CONFIG_FILE;
45
        if (!file_exists($path)) {
46
            Logger::warning('Missing "webauthn_tokens.json" configuration file. No device will be recognized.');
47
            return null;
48
        }
49
50
        $data = file_get_contents($path);
51
        $json = json_decode($data, true);
52
        if (!is_array($json)) {
53
            // there was probably an error decoding the config, log the error and pray for the best
54
            Logger::warning('Broken configuration file "'.$path.'": could not JSON-decode it.');
55
        } else {
56
            $this->dictionary = $json;
57
        }
58
    }
59
60
61
    /**
62
     * Get the singleton instance of the AAGUID dictionary.
63
     *
64
     * @return AAGUID
65
     */
66
    public static function getInstance()
67
    {
68
        if (self::$instance === null) {
69
            self::$instance = new self();
70
        }
71
        return self::$instance;
72
    }
73
74
75
    /**
76
     * Determine if an AAGUID is known
77
     *
78
     * @param string $aaguid The AAGUID that we want to check.
79
     *
80
     * @return bool True if we know about this token, false otherwise.
81
     */
82
    public function hasToken(string $aaguid): bool
83
    {
84
        return array_key_exists(strtolower($aaguid), $this->dictionary);
85
    }
86
87
88
    /**
89
     * Get the information for a given AAGUID.
90
     *
91
     * @param string $aaguid The AAGUID we want to get.
92
     *
93
     * @return array An array containing information about the given AAGUID, or an empty array if that AAGUID is
94
     * unknown.
95
     */
96
    public function get(string $aaguid): array
97
    {
98
        if (!$this->hasToken($aaguid)) {
99
            return [];
100
        }
101
        return $this->dictionary[$aaguid];
102
    }
103
}
104