| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace Webauthn\CeremonyStep; |
| 6 |
|
| 7 |
use Cose\Algorithm\Manager; |
| 8 |
use Cose\Algorithm\Signature\ECDSA\ES256; |
| 9 |
use Cose\Algorithm\Signature\RSA\RS256; |
| 10 |
use Webauthn\AttestationStatement\AttestationStatementSupportManager; |
| 11 |
use Webauthn\AttestationStatement\NoneAttestationStatementSupport; |
| 12 |
use Webauthn\AuthenticationExtensions\ExtensionOutputCheckerHandler; |
| 13 |
use Webauthn\Counter\CounterChecker; |
| 14 |
use Webauthn\Counter\ThrowExceptionIfInvalid; |
| 15 |
use Webauthn\MetadataService\CertificateChain\CertificateChainValidator; |
| 16 |
use Webauthn\MetadataService\MetadataStatementRepository; |
| 17 |
use Webauthn\MetadataService\StatusReportRepository; |
| 18 |
|
| 19 |
final class CeremonyStepManagerFactory |
| 20 |
{ |
| 21 |
private CounterChecker $counterChecker; |
| 22 |
|
| 23 |
private Manager $algorithmManager; |
| 24 |
|
| 25 |
private null|MetadataStatementRepository $metadataStatementRepository = null; |
| 26 |
|
| 27 |
private null|StatusReportRepository $statusReportRepository = null; |
| 28 |
|
| 29 |
private null|CertificateChainValidator $certificateChainValidator = null; |
| 30 |
|
| 31 |
private null|TopOriginValidator $topOriginValidator = null; |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
private null|array $securedRelyingPartyId = null; |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
private null|array $allowedOrigins = null; |
| 42 |
|
| 43 |
private bool $allowSubdomains = false; |
| 44 |
|
| 45 |
private AttestationStatementSupportManager $attestationStatementSupportManager; |
| 46 |
|
| 47 |
private ExtensionOutputCheckerHandler $extensionOutputCheckerHandler; |
| 48 |
|
| 49 |
public function __construct() |
| 50 |
{ |
| 51 |
$this->counterChecker = new ThrowExceptionIfInvalid(); |
| 52 |
$this->algorithmManager = Manager::create()->add(ES256::create(), RS256::create()); |
| 53 |
$this->attestationStatementSupportManager = new AttestationStatementSupportManager([ |
| 54 |
new NoneAttestationStatementSupport(), |
| 55 |
]); |
| 56 |
$this->extensionOutputCheckerHandler = new ExtensionOutputCheckerHandler(); |
| 57 |
} |
| 58 |
|
| 59 |
public function setCounterChecker(CounterChecker $counterChecker): void |
| 60 |
{ |
| 61 |
$this->counterChecker = $counterChecker; |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
public function setSecuredRelyingPartyId(array $securedRelyingPartyId): void |
| 69 |
{ |
| 70 |
$this->securedRelyingPartyId = $securedRelyingPartyId; |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
public function setAllowedOrigins(array $allowedOrigins, bool $allowSubdomains = false): void |
| 77 |
{ |
| 78 |
$this->allowedOrigins = $allowedOrigins; |
| 79 |
$this->allowSubdomains = $allowSubdomains; |
| 80 |
} |
| 81 |
|
| 82 |
public function setExtensionOutputCheckerHandler(ExtensionOutputCheckerHandler $extensionOutputCheckerHandler): void |
| 83 |
{ |
| 84 |
$this->extensionOutputCheckerHandler = $extensionOutputCheckerHandler; |
| 85 |
} |
| 86 |
|
| 87 |
public function setAttestationStatementSupportManager( |
| 88 |
AttestationStatementSupportManager $attestationStatementSupportManager |
| 89 |
): void { |
| 90 |
$this->attestationStatementSupportManager = $attestationStatementSupportManager; |
| 91 |
} |
| 92 |
|
| 93 |
public function setAlgorithmManager(Manager $algorithmManager): void |
| 94 |
{ |
| 95 |
$this->algorithmManager = $algorithmManager; |
| 96 |
} |
| 97 |
|
| 98 |
public function enableMetadataStatementSupport( |
| 99 |
MetadataStatementRepository $metadataStatementRepository, |
| 100 |
StatusReportRepository $statusReportRepository, |
| 101 |
CertificateChainValidator $certificateChainValidator |
| 102 |
): void { |
| 103 |
$this->metadataStatementRepository = $metadataStatementRepository; |
| 104 |
$this->statusReportRepository = $statusReportRepository; |
| 105 |
$this->certificateChainValidator = $certificateChainValidator; |
| 106 |
} |
| 107 |
|
| 108 |
public function enableCertificateChainValidator(CertificateChainValidator $certificateChainValidator): void |
| 109 |
{ |
| 110 |
$this->certificateChainValidator = $certificateChainValidator; |
| 111 |
} |
| 112 |
|
| 113 |
public function enableTopOriginValidator(TopOriginValidator $topOriginValidator): void |
| 114 |
{ |
| 115 |
$this->topOriginValidator = $topOriginValidator; |
| 116 |
} |
| 117 |
|
| 118 |
public function requestCeremony(): CeremonyStepManager |
| 119 |
{ |
| 120 |
|
| 121 |
return new CeremonyStepManager([ |
| 122 |
new CheckAllowedCredentialList(), |
| 123 |
new CheckUserHandle(), |
| 124 |
new CheckClientDataCollectorType(), |
| 125 |
new CheckChallenge(), |
| 126 |
$this->allowedOrigins === null ? new CheckOrigin( |
| 127 |
$this->securedRelyingPartyId ?? [] |
| 128 |
) : new CheckAllowedOrigins( |
| 129 |
$this->allowedOrigins, |
| 130 |
$this->allowSubdomains, |
| 131 |
$this->securedRelyingPartyId ?? [] |
| 132 |
), |
| 133 |
new CheckTopOrigin($this->topOriginValidator), |
| 134 |
new CheckRelyingPartyIdIdHash(), |
| 135 |
new CheckUserWasPresent(), |
| 136 |
new CheckUserVerification(), |
| 137 |
new CheckBackupBitsAreConsistent(), |
| 138 |
new CheckExtensions($this->extensionOutputCheckerHandler), |
| 139 |
new CheckSignature($this->algorithmManager), |
| 140 |
new CheckCounter($this->counterChecker), |
| 141 |
]); |
| 142 |
} |
| 143 |
|
| 144 |
public function creationCeremony(): CeremonyStepManager |
| 145 |
{ |
| 146 |
return $this->buildCreationCeremony(true); |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
public function conditionalCreateCeremony(): CeremonyStepManager |
| 159 |
{ |
| 160 |
return $this->buildCreationCeremony(false); |
| 161 |
} |
| 162 |
|
| 163 |
private function buildCreationCeremony(bool $requireUserPresence): CeremonyStepManager |
| 164 |
{ |
| 165 |
$metadataStatementChecker = new CheckMetadataStatement(); |
| 166 |
if ($this->certificateChainValidator !== null) { |
| 167 |
$metadataStatementChecker->enableCertificateChainValidator($this->certificateChainValidator); |
| 168 |
} |
| 169 |
if ($this->metadataStatementRepository !== null && $this->statusReportRepository !== null && $this->certificateChainValidator !== null) { |
| 170 |
$metadataStatementChecker->enableMetadataStatementSupport( |
| 171 |
$this->metadataStatementRepository, |
| 172 |
$this->statusReportRepository, |
| 173 |
$this->certificateChainValidator, |
| 174 |
); |
| 175 |
} |
| 176 |
|
| 177 |
|
| 178 |
return new CeremonyStepManager([ |
| 179 |
new CheckClientDataCollectorType(), |
| 180 |
new CheckChallenge(), |
| 181 |
$this->allowedOrigins === null ? new CheckOrigin( |
| 182 |
$this->securedRelyingPartyId ?? [] |
| 183 |
) : new CheckAllowedOrigins( |
| 184 |
$this->allowedOrigins, |
| 185 |
$this->allowSubdomains, |
| 186 |
$this->securedRelyingPartyId ?? [] |
| 187 |
), |
| 188 |
new CheckTopOrigin($this->topOriginValidator), |
| 189 |
new CheckRelyingPartyIdIdHash(), |
| 190 |
new CheckUserWasPresent($requireUserPresence), |
| 191 |
new CheckUserVerification(), |
| 192 |
new CheckBackupBitsAreConsistent(), |
| 193 |
new CheckAlgorithm(), |
| 194 |
new CheckExtensions($this->extensionOutputCheckerHandler), |
| 195 |
new CheckAttestationFormatIsKnownAndValid($this->attestationStatementSupportManager), |
| 196 |
new CheckHasAttestedCredentialData(), |
| 197 |
$metadataStatementChecker, |
| 198 |
new CheckCredentialId(), |
| 199 |
]); |
| 200 |
} |
| 201 |
} |