Skip to content

Commit acb7a81

Browse files
committed
clientDataJSON is Base64URL encoded
1 parent aa3ccea commit acb7a81

File tree

2 files changed

+32
-18
lines changed

2 files changed

+32
-18
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the WebAuthn Swift open source project
4+
//
5+
// Copyright (c) 2022 the WebAuthn Swift project authors
6+
// Licensed under Apache License v2.0
7+
//
8+
// See LICENSE.txt for license information
9+
// See CONTRIBUTORS.txt for the list of WebAuthn Swift project authors
10+
//
11+
// SPDX-License-Identifier: Apache-2.0
12+
//
13+
//===----------------------------------------------------------------------===//
14+
15+
import Foundation
16+
17+
extension String {
18+
var base64URLDecodedData: Data? {
19+
var result = self.replacingOccurrences(of: "-", with: "+").replacingOccurrences(of: "_", with: "/")
20+
while result.count % 4 != 0 {
21+
result = result.appending("=")
22+
}
23+
return Data(base64Encoded: result)
24+
}
25+
}

Sources/WebAuthn/WebAuthn.swift

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import Foundation
1919

2020
public enum WebAuthn {
2121
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 {
2323
throw WebAuthnError.badRequestData
2424
}
2525
let clientObject = try JSONDecoder().decode(ClientDataObject.self, from: clientObjectData)
@@ -28,20 +28,12 @@ public enum WebAuthn {
2828
}
2929
let clientDataJSONHash = SHA256.hash(data: clientObjectData)
3030

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 {
3632
throw WebAuthnError.badRequestData
3733
}
3834
let signedData = authenticatorData + clientDataJSONHash
3935

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 {
4537
throw WebAuthnError.badRequestData
4638
}
4739
let signature = try P256.Signing.ECDSASignature(derRepresentation: signatureData)
@@ -51,7 +43,7 @@ public enum WebAuthn {
5143
}
5244

5345
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 {
5547
throw WebAuthnError.badRequestData
5648
}
5749
let clientObject = try JSONDecoder().decode(ClientDataObject.self, from: clientObjectData)
@@ -64,11 +56,8 @@ public enum WebAuthn {
6456
guard origin == clientObject.origin else {
6557
throw WebAuthnError.validationError
6658
}
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 {
7261
throw WebAuthnError.badRequestData
7362
}
7463
guard let decodedAttestationObject = try CBOR.decode([UInt8](attestationData)) else {
@@ -184,4 +173,4 @@ public enum WebAuthn {
184173
}
185174
return credentialsData
186175
}
187-
}
176+
}

0 commit comments

Comments
 (0)