1<?php
2
3declare(strict_types=1);
4
5/*
6 * The MIT License (MIT)
7 *
8 * Copyright (c) 2014-2021 Spomky-Labs
9 *
10 * This software may be modified and distributed under the terms
11 * of the MIT license.  See the LICENSE file for details.
12 */
13
14namespace Webauthn;
15
16use function ord;
17use Webauthn\AuthenticationExtensions\AuthenticationExtensionsClientOutputs;
18
19/**
20 * @see https://www.w3.org/TR/webauthn/#sec-authenticator-data
21 */
22class AuthenticatorData
23{
24    private const FLAG_UP = 0b00000001;
25    private const FLAG_RFU1 = 0b00000010;
26    private const FLAG_UV = 0b00000100;
27    private const FLAG_RFU2 = 0b00111000;
28    private const FLAG_AT = 0b01000000;
29    private const FLAG_ED = 0b10000000;
30    /**
31     * @var string
32     */
33    protected $authData;
34
35    /**
36     * @var string
37     */
38    protected $rpIdHash;
39
40    /**
41     * @var string
42     */
43    protected $flags;
44
45    /**
46     * @var int
47     */
48    protected $signCount;
49
50    /**
51     * @var AttestedCredentialData|null
52     */
53    protected $attestedCredentialData;
54
55    /**
56     * @var AuthenticationExtensionsClientOutputs|null
57     */
58    protected $extensions;
59
60    public function __construct(string $authData, string $rpIdHash, string $flags, int $signCount, ?AttestedCredentialData $attestedCredentialData, ?AuthenticationExtensionsClientOutputs $extensions)
61    {
62        $this->rpIdHash = $rpIdHash;
63        $this->flags = $flags;
64        $this->signCount = $signCount;
65        $this->attestedCredentialData = $attestedCredentialData;
66        $this->extensions = $extensions;
67        $this->authData = $authData;
68    }
69
70    public function getAuthData(): string
71    {
72        return $this->authData;
73    }
74
75    public function getRpIdHash(): string
76    {
77        return $this->rpIdHash;
78    }
79
80    public function isUserPresent(): bool
81    {
82        return 0 !== (ord($this->flags) & self::FLAG_UP) ? true : false;
83    }
84
85    public function isUserVerified(): bool
86    {
87        return 0 !== (ord($this->flags) & self::FLAG_UV) ? true : false;
88    }
89
90    public function hasAttestedCredentialData(): bool
91    {
92        return 0 !== (ord($this->flags) & self::FLAG_AT) ? true : false;
93    }
94
95    public function hasExtensions(): bool
96    {
97        return 0 !== (ord($this->flags) & self::FLAG_ED) ? true : false;
98    }
99
100    public function getReservedForFutureUse1(): int
101    {
102        return ord($this->flags) & self::FLAG_RFU1;
103    }
104
105    public function getReservedForFutureUse2(): int
106    {
107        return ord($this->flags) & self::FLAG_RFU2;
108    }
109
110    public function getSignCount(): int
111    {
112        return $this->signCount;
113    }
114
115    public function getAttestedCredentialData(): ?AttestedCredentialData
116    {
117        return $this->attestedCredentialData;
118    }
119
120    public function getExtensions(): ?AuthenticationExtensionsClientOutputs
121    {
122        return null !== $this->extensions && $this->hasExtensions() ? $this->extensions : null;
123    }
124}
125