-
Notifications
You must be signed in to change notification settings - Fork 135
Expand file tree
/
Copy pathKey.php
More file actions
109 lines (88 loc) · 2.68 KB
/
Key.php
File metadata and controls
109 lines (88 loc) · 2.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
<?php
declare(strict_types=1);
namespace SimpleSAML\SAML2\Certificate;
use ArrayAccess;
use SimpleSAML\SAML2\Assert\Assert;
use SimpleSAML\SAML2\Certificate\Exception\InvalidKeyUsageException;
use SimpleSAML\SAML2\Exception\InvalidArgumentException;
use function array_key_exists;
use function is_string;
/**
* Simple DTO wrapper for (X509) keys. Implements ArrayAccess
* for easier backwards compatibility.
*/
class Key implements ArrayAccess
{
// Possible key usages
public const string USAGE_SIGNING = 'signing';
public const string USAGE_ENCRYPTION = 'encryption';
/** @var array */
protected array $keyData = [];
/**
* @param array $keyData
*/
public function __construct(array $keyData)
{
// forcing usage of offsetSet
foreach ($keyData as $property => $value) {
$this->offsetSet($property, $value);
}
}
/**
* Whether or not the key is configured to be used for usage given
*
* @throws \SimpleSAML\SAML2\Exception\InvalidArgumentException
*/
public function canBeUsedFor(string $usage): bool
{
Assert::oneOf(
$usage,
[self::USAGE_ENCRYPTION, self::USAGE_SIGNING],
'Invalid key usage given: "%s", usages "%2$s" allowed',
InvalidKeyUsageException::class,
);
return isset($this->keyData[$usage]) && $this->keyData[$usage];
}
/**
* @param mixed $offset
*
* @throws \SimpleSAML\SAML2\Exception\InvalidArgumentException
*/
public function offsetExists(mixed $offset): bool
{
if (!is_string($offset)) {
throw InvalidArgumentException::invalidType('string', $offset);
}
return array_key_exists($offset, $this->keyData);
}
/**
* @throws \SimpleSAML\SAML2\Exception\InvalidArgumentException
*/
public function offsetGet($offset): mixed
{
if (!is_string($offset)) {
throw InvalidArgumentException::invalidType('string', $offset);
}
return $this->keyData[$offset];
}
/**
* @throws \SimpleSAML\SAML2\Exception\InvalidArgumentException
*/
public function offsetSet(mixed $offset, mixed $value): void
{
if (!is_string($offset)) {
throw InvalidArgumentException::invalidType('string', $offset);
}
$this->keyData[$offset] = $value;
}
/**
* @throws \SimpleSAML\SAML2\Exception\InvalidArgumentException
*/
public function offsetUnset(mixed $offset): void
{
if (!is_string($offset)) {
throw InvalidArgumentException::invalidType('string', $offset);
}
unset($this->keyData[$offset]);
}
}