@@ -19,7 +19,7 @@ import Foundation
19
19
20
20
public enum WebAuthn {
21
21
public static func validateAssertion( _ data: AssertionCredential , challengeProvided: String , publicKey: P256 . Signing . PublicKey , logger: Logger ) throws {
22
- guard let clientObjectData = Data ( base64Encoded : data. response. clientDataJSON) else {
22
+ guard let clientObjectData = data. response. clientDataJSON. base64URLDecodedData else {
23
23
throw WebAuthnError . badRequestData
24
24
}
25
25
let clientObject = try JSONDecoder ( ) . decode ( ClientDataObject . self, from: clientObjectData)
@@ -28,20 +28,12 @@ public enum WebAuthn {
28
28
}
29
29
let clientDataJSONHash = SHA256 . hash ( data: clientObjectData)
30
30
31
- var base64AssertionString = data. response. authenticatorData. replacingOccurrences ( of: " - " , with: " + " ) . replacingOccurrences ( of: " _ " , with: " / " )
32
- while base64AssertionString. count % 4 != 0 {
33
- base64AssertionString = base64AssertionString. appending ( " = " )
34
- }
35
- guard let authenticatorData = Data ( base64Encoded: base64AssertionString) else {
31
+ guard let authenticatorData = data. response. authenticatorData. base64URLDecodedData else {
36
32
throw WebAuthnError . badRequestData
37
33
}
38
34
let signedData = authenticatorData + clientDataJSONHash
39
35
40
- var base64SignatureString = data. response. signature. replacingOccurrences ( of: " - " , with: " + " ) . replacingOccurrences ( of: " _ " , with: " / " )
41
- while base64SignatureString. count % 4 != 0 {
42
- base64SignatureString = base64SignatureString. appending ( " = " )
43
- }
44
- guard let signatureData = Data ( base64Encoded: base64SignatureString) else {
36
+ guard let signatureData = data. response. signature. base64URLDecodedData else {
45
37
throw WebAuthnError . badRequestData
46
38
}
47
39
let signature = try P256 . Signing. ECDSASignature ( derRepresentation: signatureData)
@@ -51,7 +43,7 @@ public enum WebAuthn {
51
43
}
52
44
53
45
public static func parseRegisterCredentials( _ data: RegisterWebAuthnCredentialData , challengeProvided: String , origin: String , logger: Logger ) throws -> Credential {
54
- guard let clientObjectData = Data ( base64Encoded : data. response. clientDataJSON) else {
46
+ guard let clientObjectData = data. response. clientDataJSON. base64URLDecodedData else {
55
47
throw WebAuthnError . badRequestData
56
48
}
57
49
let clientObject = try JSONDecoder ( ) . decode ( ClientDataObject . self, from: clientObjectData)
@@ -64,11 +56,8 @@ public enum WebAuthn {
64
56
guard origin == clientObject. origin else {
65
57
throw WebAuthnError . validationError
66
58
}
67
- var base64AttestationString = data. response. attestationObject. replacingOccurrences ( of: " - " , with: " + " ) . replacingOccurrences ( of: " _ " , with: " / " )
68
- while base64AttestationString. count % 4 != 0 {
69
- base64AttestationString = base64AttestationString. appending ( " = " )
70
- }
71
- guard let attestationData = Data ( base64Encoded: base64AttestationString) else {
59
+
60
+ guard let attestationData = data. response. attestationObject. base64URLDecodedData else {
72
61
throw WebAuthnError . badRequestData
73
62
}
74
63
guard let decodedAttestationObject = try CBOR . decode ( [ UInt8] ( attestationData) ) else {
@@ -184,4 +173,4 @@ public enum WebAuthn {
184
173
}
185
174
return credentialsData
186
175
}
187
- }
176
+ }
0 commit comments