Passed
Push — master ( aeea93...08b2ed )
by Tim
05:13 queued 03:26
created

AAGUID   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 93
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 27
c 2
b 0
f 0
dl 0
loc 93
rs 10
wmc 9
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\Module\webauthn\WebAuthn;
6
7
use SimpleSAML\Logger;
8
use SimpleSAML\Utils\Config as SSPConfig;
9
10
/**
11
 * Class AAGUID
12
 *
13
 * @package SimpleSAML\Module\webauthn\WebAuthn
14
 */
15
class AAGUID
16
{
17
    /**
18
     * The name of the configuration file where we should expect the AAGUID dictionary.
19
     */
20
    public const string AAGUID_CONFIG_FILE = 'webauthn-aaguid.json';
0 ignored issues
show
Bug introduced by
A parse error occurred: Syntax error, unexpected T_STRING, expecting '=' on line 20 at column 24
Loading history...
21
22
23
    /**
24
     * The actual dictionary containing all known tokens.
25
     *
26
     * @var array
27
     */
28
    protected array $dictionary = [];
29
30
    /**
31
     * The singleton instance.
32
     *
33
     * @var \SimpleSAML\Module\webauthn\WebAuthn\AAGUID
34
     */
35
    protected static AAGUID $instance;
36
37
38
    /**
39
     * AAGUID constructor.
40
     */
41
    protected function __construct()
42
    {
43
        $config = new SSPConfig();
44
        $path = $config->getConfigDir() . '/' . self::AAGUID_CONFIG_FILE;
45
        if (!file_exists($path)) {
46
            Logger::warning("Missing AAGUID configuration file ($path). No device will be recognized.");
47
            return;
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
    public static function getInstance(): self
65
    {
66
        if (!isset(self::$instance)) {
67
            self::$instance = new self();
68
        }
69
        return self::$instance;
70
    }
71
72
73
    /**
74
     * Determine if an AAGUID is known
75
     *
76
     * @param string $aaguid The AAGUID that we want to check.
77
     * @return bool True if we know about this token, false otherwise.
78
     */
79
    public function hasToken(string $aaguid): bool
80
    {
81
        $lowerAaguid = strtolower($aaguid);
82
        if (array_key_exists($lowerAaguid, $this->dictionary)) {
83
            return true;
84
        } else {
85
            Logger::info("AAGUID $lowerAaguid not found in dictionary, device is unknown.");
86
            return false;
87
        }
88
    }
89
90
91
    /**
92
     * Get the information for a given AAGUID.
93
     *
94
     * @param string $aaguid The AAGUID we want to get.
95
     * @return array An array containing information about the given AAGUID, or an empty array if that AAGUID is
96
     * unknown.
97
     */
98
    public function get(string $aaguid): array
99
    {
100
        if (!$this->hasToken($aaguid)) {
101
            return [];
102
        }
103
        return $this->dictionary[$aaguid];
104
    }
105
}
106