| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace SpomkyLabs\Pki\X509\AttributeCertificate; |
| 6 |
|
| 7 |
use SpomkyLabs\Pki\X501\ASN1\Attribute; |
| 8 |
use SpomkyLabs\Pki\X501\ASN1\AttributeType; |
| 9 |
use SpomkyLabs\Pki\X501\ASN1\AttributeValue\AttributeValue; |
| 10 |
use SpomkyLabs\Pki\X501\ASN1\Collection\SequenceOfAttributes; |
| 11 |
use SpomkyLabs\Pki\X509\AttributeCertificate\Attribute\AccessIdentityAttributeValue; |
| 12 |
use SpomkyLabs\Pki\X509\AttributeCertificate\Attribute\AuthenticationInfoAttributeValue; |
| 13 |
use SpomkyLabs\Pki\X509\AttributeCertificate\Attribute\ChargingIdentityAttributeValue; |
| 14 |
use SpomkyLabs\Pki\X509\AttributeCertificate\Attribute\GroupAttributeValue; |
| 15 |
use SpomkyLabs\Pki\X509\AttributeCertificate\Attribute\RoleAttributeValue; |
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
final class Attributes extends SequenceOfAttributes |
| 24 |
{ |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
private const MAP_OID_TO_CLASS = [ |
| 33 |
AccessIdentityAttributeValue::OID => AccessIdentityAttributeValue::class, |
| 34 |
AuthenticationInfoAttributeValue::OID => AuthenticationInfoAttributeValue::class, |
| 35 |
ChargingIdentityAttributeValue::OID => ChargingIdentityAttributeValue::class, |
| 36 |
GroupAttributeValue::OID => GroupAttributeValue::class, |
| 37 |
AttributeType::OID_ROLE => RoleAttributeValue::class, |
| 38 |
]; |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
public static function fromAttributeValues(AttributeValue ...$values): static |
| 46 |
{ |
| 47 |
return static::create(...array_map(static fn (AttributeValue $value) => $value->toAttribute(), $values)); |
| 48 |
} |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
public function hasAccessIdentity(): bool |
| 54 |
{ |
| 55 |
return $this->has(AccessIdentityAttributeValue::OID); |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
public function accessIdentity(): AccessIdentityAttributeValue |
| 62 |
{ |
| 63 |
return $this->firstOf(AccessIdentityAttributeValue::OID)->first(); |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
public function hasAuthenticationInformation(): bool |
| 70 |
{ |
| 71 |
return $this->has(AuthenticationInfoAttributeValue::OID); |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
public function authenticationInformation(): AuthenticationInfoAttributeValue |
| 78 |
{ |
| 79 |
return $this->firstOf(AuthenticationInfoAttributeValue::OID)->first(); |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
public function hasChargingIdentity(): bool |
| 86 |
{ |
| 87 |
return $this->has(ChargingIdentityAttributeValue::OID); |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
public function chargingIdentity(): ChargingIdentityAttributeValue |
| 94 |
{ |
| 95 |
return $this->firstOf(ChargingIdentityAttributeValue::OID)->first(); |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
public function hasGroup(): bool |
| 102 |
{ |
| 103 |
return $this->has(GroupAttributeValue::OID); |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
public function group(): GroupAttributeValue |
| 110 |
{ |
| 111 |
return $this->firstOf(GroupAttributeValue::OID)->first(); |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
public function hasRole(): bool |
| 118 |
{ |
| 119 |
return $this->has(AttributeType::OID_ROLE); |
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
public function role(): RoleAttributeValue |
| 126 |
{ |
| 127 |
return $this->firstOf(AttributeType::OID_ROLE)->first(); |
| 128 |
} |
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
public function roles(): array |
| 136 |
{ |
| 137 |
return array_merge( |
| 138 |
[], |
| 139 |
...array_map(static fn (Attribute $attr) => $attr->values(), $this->allOf(AttributeType::OID_ROLE)) |
| 140 |
); |
| 141 |
} |
| 142 |
|
| 143 |
protected static function _castAttributeValues(Attribute $attribute): Attribute |
| 144 |
{ |
| 145 |
$oid = $attribute->oid(); |
| 146 |
if (isset(self::MAP_OID_TO_CLASS[$oid])) { |
| 147 |
return $attribute->castValues(self::MAP_OID_TO_CLASS[$oid]); |
| 148 |
} |
| 149 |
return $attribute; |
| 150 |
} |
| 151 |
} |