| 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 WP_DEFENDER_VENDOR\Webauthn\TokenBinding; |
| 15 |
|
| 16 |
use function array_key_exists; |
| 17 |
use WP_DEFENDER_VENDOR\Assert\Assertion; |
| 18 |
use WP_DEFENDER_VENDOR\Base64Url\Base64Url; |
| 19 |
use function WP_DEFENDER_VENDOR\Safe\sprintf; |
| 20 |
|
| 21 |
class TokenBinding |
| 22 |
{ |
| 23 |
public const TOKEN_BINDING_STATUS_PRESENT = 'present'; |
| 24 |
public const TOKEN_BINDING_STATUS_SUPPORTED = 'supported'; |
| 25 |
public const TOKEN_BINDING_STATUS_NOT_SUPPORTED = 'not-supported'; |
| 26 |
|
| 27 |
/** |
| 28 |
* @var string |
| 29 |
*/ |
| 30 |
private $status; |
| 31 |
|
| 32 |
/** |
| 33 |
* @var string|null |
| 34 |
*/ |
| 35 |
private $id; |
| 36 |
|
| 37 |
public function __construct(string $status, ?string $id) |
| 38 |
{ |
| 39 |
Assertion::false(self::TOKEN_BINDING_STATUS_PRESENT === $status && null === $id, 'The member "id" is required when status is "present"'); |
| 40 |
$this->status = $status; |
| 41 |
$this->id = $id; |
| 42 |
} |
| 43 |
|
| 44 |
/** |
| 45 |
* @param mixed[] $json |
| 46 |
*/ |
| 47 |
public static function createFormArray(array $json): self |
| 48 |
{ |
| 49 |
Assertion::keyExists($json, 'status', 'The member "status" is required'); |
| 50 |
$status = $json['status']; |
| 51 |
Assertion::inArray( |
| 52 |
$status, |
| 53 |
self::getSupportedStatus(), |
| 54 |
sprintf('The member "status" is invalid. Supported values are: %s', implode(', ', self::getSupportedStatus())) |
| 55 |
); |
| 56 |
$id = array_key_exists('id', $json) ? Base64Url::decode($json['id']) : null; |
| 57 |
|
| 58 |
return new self($status, $id); |
| 59 |
} |
| 60 |
|
| 61 |
public function getStatus(): string |
| 62 |
{ |
| 63 |
return $this->status; |
| 64 |
} |
| 65 |
|
| 66 |
public function getId(): ?string |
| 67 |
{ |
| 68 |
return $this->id; |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* @return string[] |
| 73 |
*/ |
| 74 |
private static function getSupportedStatus(): array |
| 75 |
{ |
| 76 |
return [ |
| 77 |
self::TOKEN_BINDING_STATUS_PRESENT, |
| 78 |
self::TOKEN_BINDING_STATUS_SUPPORTED, |
| 79 |
self::TOKEN_BINDING_STATUS_NOT_SUPPORTED, |
| 80 |
]; |
| 81 |
} |
| 82 |
} |
| 83 |
|