1<?php
2
3declare(strict_types=1);
4
5/*
6 * The MIT License (MIT)
7 *
8 * Copyright (c) 2014-2019 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 Webauthn\AuthenticationExtensions\AuthenticationExtensionsClientOutputs;
17
18/**
19 * @see https://www.w3.org/TR/webauthn/#sec-authenticator-data
20 */
21class AuthenticatorData
22{
23    /**
24     * @var string
25     */
26    protected $authData;
27
28    /**
29     * @var string
30     */
31    protected $rpIdHash;
32
33    /**
34     * @var string
35     */
36    protected $flags;
37
38    /**
39     * @var int
40     */
41    protected $signCount;
42
43    /**
44     * @var AttestedCredentialData|null
45     */
46    protected $attestedCredentialData;
47
48    /**
49     * @var AuthenticationExtensionsClientOutputs|null
50     */
51    protected $extensions;
52
53    private const FLAG_UP = 0b00000001;
54    private const FLAG_RFU1 = 0b00000010;
55    private const FLAG_UV = 0b00000100;
56    private const FLAG_RFU2 = 0b00111000;
57    private const FLAG_AT = 0b01000000;
58    private const FLAG_ED = 0b10000000;
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