11<?php
22
33namespace Onetech \WebAuthn \Attestation ;
4+
5+ use Onetech \WebAuthn \Attestation \Format \FormatBase ;
46use Onetech \WebAuthn \WebAuthnException ;
57use Onetech \WebAuthn \CBOR \CborDecoder ;
68use Onetech \WebAuthn \Binary \ByteBuffer ;
911 * @author Lukas Buchs
1012 * @license https://github.com/lbuchs/WebAuthn/blob/master/LICENSE MIT
1113 */
12- class AttestationObject {
13- private $ _authenticatorData ;
14- private $ _attestationFormat ;
15- private $ _attestationFormatName ;
14+ class AttestationObject
15+ {
16+ private AuthenticatorData $ _authenticatorData ;
17+ private FormatBase $ _attestationFormat ;
18+ private string $ _attestationFormatName ;
1619
17- public function __construct ($ binary , $ allowedFormats ) {
20+ /**
21+ * @throws WebAuthnException
22+ */
23+ public function __construct ($ binary , $ allowedFormats )
24+ {
1825 $ enc = CborDecoder::decode ($ binary );
1926 // validation
2027 if (!\is_array ($ enc ) || !\array_key_exists ('fmt ' , $ enc ) || !is_string ($ enc ['fmt ' ])) {
@@ -34,59 +41,64 @@ public function __construct($binary , $allowedFormats) {
3441
3542 // Format ok?
3643 if (!in_array ($ this ->_attestationFormatName , $ allowedFormats )) {
37- throw new WebAuthnException ('invalid atttestation format: ' . $ this ->_attestationFormatName , WebAuthnException::INVALID_DATA );
44+ throw new WebAuthnException ('invalid attestation format: ' . $ this ->_attestationFormatName , WebAuthnException::INVALID_DATA );
3845 }
3946
4047
41- switch ($ this ->_attestationFormatName ) {
42- case 'android-key ' : $ this -> _attestationFormat = new Format \AndroidKey ($ enc , $ this ->_authenticatorData ); break ;
43- case 'android-safetynet ' : $ this -> _attestationFormat = new Format \AndroidSafetyNet ($ enc , $ this ->_authenticatorData ); break ;
44- case 'apple ' : $ this -> _attestationFormat = new Format \Apple ($ enc , $ this ->_authenticatorData ); break ;
45- case 'fido-u2f ' : $ this -> _attestationFormat = new Format \U2f ($ enc , $ this ->_authenticatorData ); break ;
46- case 'none ' : $ this -> _attestationFormat = new Format \None ($ enc , $ this ->_authenticatorData ); break ;
47- case 'packed ' : $ this -> _attestationFormat = new Format \Packed ($ enc , $ this ->_authenticatorData ); break ;
48- case 'tpm ' : $ this -> _attestationFormat = new Format \Tpm ($ enc , $ this ->_authenticatorData ); break ;
49- default : throw new WebAuthnException ('invalid attestation format: ' . $ enc ['fmt ' ], WebAuthnException::INVALID_DATA );
50- }
48+ $ this -> _attestationFormat = match ($ this ->_attestationFormatName ) {
49+ 'android-key ' => new Format \AndroidKey ($ enc , $ this ->_authenticatorData ),
50+ 'android-safetynet ' => new Format \AndroidSafetyNet ($ enc , $ this ->_authenticatorData ),
51+ 'apple ' => new Format \Apple ($ enc , $ this ->_authenticatorData ),
52+ 'fido-u2f ' => new Format \U2f ($ enc , $ this ->_authenticatorData ),
53+ 'none ' => new Format \None ($ enc , $ this ->_authenticatorData ),
54+ 'packed ' => new Format \Packed ($ enc , $ this ->_authenticatorData ),
55+ 'tpm ' => new Format \Tpm ($ enc , $ this ->_authenticatorData ),
56+ default => throw new WebAuthnException ('invalid attestation format: ' . $ enc ['fmt ' ], WebAuthnException::INVALID_DATA ),
57+ };
5158 }
5259
5360 /**
5461 * returns the attestation format name
5562 * @return string
5663 */
57- public function getAttestationFormatName () {
64+ public function getAttestationFormatName (): string
65+ {
5866 return $ this ->_attestationFormatName ;
5967 }
6068
6169 /**
6270 * returns the attestation format class
63- * @return Format\ FormatBase
71+ * @return FormatBase
6472 */
65- public function getAttestationFormat () {
73+ public function getAttestationFormat (): FormatBase
74+ {
6675 return $ this ->_attestationFormat ;
6776 }
6877
6978 /**
7079 * returns the attestation public key in PEM format
7180 * @return AuthenticatorData
7281 */
73- public function getAuthenticatorData () {
82+ public function getAuthenticatorData (): AuthenticatorData
83+ {
7484 return $ this ->_authenticatorData ;
7585 }
7686
7787 /**
7888 * returns the certificate chain as PEM
7989 * @return string|null
8090 */
81- public function getCertificateChain () {
91+ public function getCertificateChain (): ?string
92+ {
8293 return $ this ->_attestationFormat ->getCertificateChain ();
8394 }
8495
8596 /**
8697 * return the certificate issuer as string
8798 * @return string
8899 */
89- public function getCertificateIssuer () {
100+ public function getCertificateIssuer (): string
101+ {
90102 $ pem = $ this ->getCertificatePem ();
91103 $ issuer = '' ;
92104 if ($ pem ) {
@@ -115,7 +127,8 @@ public function getCertificateIssuer() {
115127 * return the certificate subject as string
116128 * @return string
117129 */
118- public function getCertificateSubject () {
130+ public function getCertificateSubject (): string
131+ {
119132 $ pem = $ this ->getCertificatePem ();
120133 $ subject = '' ;
121134 if ($ pem ) {
@@ -144,7 +157,8 @@ public function getCertificateSubject() {
144157 * returns the key certificate in PEM format
145158 * @return string
146159 */
147- public function getCertificatePem () {
160+ public function getCertificatePem (): string
161+ {
148162 return $ this ->_attestationFormat ->getCertificatePem ();
149163 }
150164
@@ -154,7 +168,8 @@ public function getCertificatePem() {
154168 * @return bool
155169 * @throws WebAuthnException
156170 */
157- public function validateAttestation ($ clientDataHash ) {
171+ public function validateAttestation (string $ clientDataHash ): bool
172+ {
158173 return $ this ->_attestationFormat ->validateAttestation ($ clientDataHash );
159174 }
160175
@@ -164,16 +179,18 @@ public function validateAttestation($clientDataHash) {
164179 * @return boolean
165180 * @throws WebAuthnException
166181 */
167- public function validateRootCertificate ($ rootCas ) {
182+ public function validateRootCertificate (array $ rootCas ): bool
183+ {
168184 return $ this ->_attestationFormat ->validateRootCertificate ($ rootCas );
169185 }
170186
171187 /**
172188 * checks if the RpId-Hash is valid
173- * @param string$rpIdHash
189+ * @param string $rpIdHash
174190 * @return bool
175191 */
176- public function validateRpIdHash ($ rpIdHash ) {
192+ public function validateRpIdHash (string $ rpIdHash ): bool
193+ {
177194 return $ rpIdHash === $ this ->_authenticatorData ->getRpIdHash ();
178195 }
179196}
0 commit comments