| 1 |
<?php |
| 2 |
|
| 3 |
declare(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 |
|
| 14 |
namespace Webauthn\MetadataService; |
| 15 |
|
| 16 |
use Assert\Assertion; |
| 17 |
use JsonSerializable; |
| 18 |
use function Safe\sprintf; |
| 19 |
|
| 20 |
class VerificationMethodDescriptor implements JsonSerializable |
| 21 |
{ |
| 22 |
public const USER_VERIFY_PRESENCE = 0x00000001; |
| 23 |
public const USER_VERIFY_FINGERPRINT = 0x00000002; |
| 24 |
public const USER_VERIFY_PASSCODE = 0x00000004; |
| 25 |
public const USER_VERIFY_VOICEPRINT = 0x00000008; |
| 26 |
public const USER_VERIFY_FACEPRINT = 0x00000010; |
| 27 |
public const USER_VERIFY_LOCATION = 0x00000020; |
| 28 |
public const USER_VERIFY_EYEPRINT = 0x00000040; |
| 29 |
public const USER_VERIFY_PATTERN = 0x00000080; |
| 30 |
public const USER_VERIFY_HANDPRINT = 0x00000100; |
| 31 |
public const USER_VERIFY_NONE = 0x00000200; |
| 32 |
public const USER_VERIFY_ALL = 0x00000400; |
| 33 |
|
| 34 |
/** |
| 35 |
* @var int |
| 36 |
*/ |
| 37 |
private $userVerification; |
| 38 |
|
| 39 |
/** |
| 40 |
* @var CodeAccuracyDescriptor|null |
| 41 |
*/ |
| 42 |
private $caDesc; |
| 43 |
|
| 44 |
/** |
| 45 |
* @var BiometricAccuracyDescriptor|null |
| 46 |
*/ |
| 47 |
private $baDesc; |
| 48 |
|
| 49 |
/** |
| 50 |
* @var PatternAccuracyDescriptor|null |
| 51 |
*/ |
| 52 |
private $paDesc; |
| 53 |
|
| 54 |
public function __construct(int $userVerification, ?CodeAccuracyDescriptor $caDesc = null, ?BiometricAccuracyDescriptor $baDesc = null, ?PatternAccuracyDescriptor $paDesc = null) |
| 55 |
{ |
| 56 |
Assertion::greaterOrEqualThan($userVerification, 0, Utils::logicException('The parameter "userVerification" is invalid')); |
| 57 |
$this->userVerification = $userVerification; |
| 58 |
$this->caDesc = $caDesc; |
| 59 |
$this->baDesc = $baDesc; |
| 60 |
$this->paDesc = $paDesc; |
| 61 |
} |
| 62 |
|
| 63 |
public function getUserVerification(): int |
| 64 |
{ |
| 65 |
return $this->userVerification; |
| 66 |
} |
| 67 |
|
| 68 |
public function userPresence(): bool |
| 69 |
{ |
| 70 |
return 0 !== ($this->userVerification & self::USER_VERIFY_PRESENCE); |
| 71 |
} |
| 72 |
|
| 73 |
public function fingerprint(): bool |
| 74 |
{ |
| 75 |
return 0 !== ($this->userVerification & self::USER_VERIFY_FINGERPRINT); |
| 76 |
} |
| 77 |
|
| 78 |
public function passcode(): bool |
| 79 |
{ |
| 80 |
return 0 !== ($this->userVerification & self::USER_VERIFY_PASSCODE); |
| 81 |
} |
| 82 |
|
| 83 |
public function voicePrint(): bool |
| 84 |
{ |
| 85 |
return 0 !== ($this->userVerification & self::USER_VERIFY_VOICEPRINT); |
| 86 |
} |
| 87 |
|
| 88 |
public function facePrint(): bool |
| 89 |
{ |
| 90 |
return 0 !== ($this->userVerification & self::USER_VERIFY_FACEPRINT); |
| 91 |
} |
| 92 |
|
| 93 |
public function location(): bool |
| 94 |
{ |
| 95 |
return 0 !== ($this->userVerification & self::USER_VERIFY_LOCATION); |
| 96 |
} |
| 97 |
|
| 98 |
public function eyePrint(): bool |
| 99 |
{ |
| 100 |
return 0 !== ($this->userVerification & self::USER_VERIFY_EYEPRINT); |
| 101 |
} |
| 102 |
|
| 103 |
public function pattern(): bool |
| 104 |
{ |
| 105 |
return 0 !== ($this->userVerification & self::USER_VERIFY_PATTERN); |
| 106 |
} |
| 107 |
|
| 108 |
public function handprint(): bool |
| 109 |
{ |
| 110 |
return 0 !== ($this->userVerification & self::USER_VERIFY_HANDPRINT); |
| 111 |
} |
| 112 |
|
| 113 |
public function none(): bool |
| 114 |
{ |
| 115 |
return 0 !== ($this->userVerification & self::USER_VERIFY_NONE); |
| 116 |
} |
| 117 |
|
| 118 |
public function all(): bool |
| 119 |
{ |
| 120 |
return 0 !== ($this->userVerification & self::USER_VERIFY_ALL); |
| 121 |
} |
| 122 |
|
| 123 |
public function getCaDesc(): ?CodeAccuracyDescriptor |
| 124 |
{ |
| 125 |
return $this->caDesc; |
| 126 |
} |
| 127 |
|
| 128 |
public function getBaDesc(): ?BiometricAccuracyDescriptor |
| 129 |
{ |
| 130 |
return $this->baDesc; |
| 131 |
} |
| 132 |
|
| 133 |
public function getPaDesc(): ?PatternAccuracyDescriptor |
| 134 |
{ |
| 135 |
return $this->paDesc; |
| 136 |
} |
| 137 |
|
| 138 |
public static function createFromArray(array $data): self |
| 139 |
{ |
| 140 |
$data = Utils::filterNullValues($data); |
| 141 |
Assertion::keyExists($data, 'userVerification', Utils::logicException('The parameter "userVerification" is missing')); |
| 142 |
Assertion::integer($data['userVerification'], Utils::logicException('The parameter "userVerification" is invalid')); |
| 143 |
foreach (['caDesc', 'baDesc', 'paDesc'] as $key) { |
| 144 |
if (isset($data[$key])) { |
| 145 |
Assertion::isArray($data[$key], Utils::logicException(sprintf('Invalid parameter "%s"', $key))); |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
return new self( |
| 150 |
$data['userVerification'], |
| 151 |
isset($data['caDesc']) ? CodeAccuracyDescriptor::createFromArray($data['caDesc']) : null, |
| 152 |
isset($data['baDesc']) ? BiometricAccuracyDescriptor::createFromArray($data['baDesc']) : null, |
| 153 |
isset($data['paDesc']) ? PatternAccuracyDescriptor::createFromArray($data['paDesc']) : null |
| 154 |
); |
| 155 |
} |
| 156 |
|
| 157 |
public function jsonSerialize(): array |
| 158 |
{ |
| 159 |
$data = [ |
| 160 |
'userVerification' => $this->userVerification, |
| 161 |
'caDesc' => null === $this->caDesc ? null : $this->caDesc->jsonSerialize(), |
| 162 |
'baDesc' => null === $this->baDesc ? null : $this->baDesc->jsonSerialize(), |
| 163 |
'paDesc' => null === $this->paDesc ? null : $this->paDesc->jsonSerialize(), |
| 164 |
]; |
| 165 |
|
| 166 |
return Utils::filterNullValues($data); |
| 167 |
} |
| 168 |
} |
| 169 |
|