Skip to content

Commit

Permalink
Add method to convert String to Data
Browse files Browse the repository at this point in the history
This method originally existed for testing purpose. However, it was
removed when merging a PR(#2) before the introduction of Github Actions.
Therefore, we re-introduced the method to pass the testcases.
  • Loading branch information
kimdora committed Sep 19, 2024
1 parent b39ade4 commit 2507cb9
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 10 deletions.
7 changes: 6 additions & 1 deletion Sources/WebAuthn/Utility/Extension/String.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,18 @@ import Foundation

extension String {
func base64UrlToData() -> Data? {
var base64 = self.replacingOccurrences(of: "-", with: "+").replacingOccurrences(of: "_", with: "/")
var base64 = self.replacingOccurrences(of: "-", with: "+")
.replacingOccurrences(of: "_", with: "/")
if base64.count % 4 != 0 {
base64.append(String(repeating: "=", count: 4 - base64.count % 4))
}
return Data(base64Encoded: base64, options: .ignoreUnknownCharacters)
}

func toData() -> Data {
return Data(self.utf8)
}

func toSHA256() -> Data {
return Data(self.utf8).toSHA256()
}
Expand Down
6 changes: 3 additions & 3 deletions Tests/WebAuthnTests/AuthenticatorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ final class AuthenticatorTests: XCTestCase {
) async -> Result<AuthenticatorMakeCredentialResult, WebAuthnError> {
let rpEntity = PublicKeyCredentialRpEntity(id: rpId, name: rpId)
let userEntity = PublicKeyCredentialUserEntity(id: username, name: username, displayName: username)
return await authn.makeCredential(hash: "ios:test-create".toSHA256()!,
return await authn.makeCredential(hash: "ios:test-create".toSHA256(),
rpEntity: rpEntity,
userEntity: userEntity,
credTypesAndPubKeyAlgs: credTypesAndPubKeyAlgs,
Expand All @@ -53,7 +53,7 @@ final class AuthenticatorTests: XCTestCase {
_ extensions: AuthenticatorExtensionsInput? = nil
) async -> Result<AuthenticatorGetAssertionResult, WebAuthnError> {
return await authn.getAssertion(rpId: rpId,
hash: "ios:test-get".toSHA256()!,
hash: "ios:test-get".toSHA256(),
allowCredentialDescriptorList: allowedList,
extensions: extensions)
}
Expand Down Expand Up @@ -210,7 +210,7 @@ final class AuthenticatorTests: XCTestCase {
func testGetAssertionWithLoadFailedAtCredSrcStorage() async throws {
let db = MockCredentialSourceStorageWithLoadFailed()
let authn = BiometricAuthenticator(db, ks, la)
let notAllowedCredId = "suspicious credential id".toData()!.toBase64Url()
let notAllowedCredId = "suspicious credential id".toData().toBase64Url()
do {
let notAllowedList = [PublicKeyCredentialDescriptor(type: "public-key", id: notAllowedCredId, transports: [.itn])]
_ = try await authenticate(authn, defaultRpId, notAllowedList).get()
Expand Down
2 changes: 1 addition & 1 deletion Tests/WebAuthnTests/CommonTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ final class CommonTests: XCTestCase {
}

func testBase64UrlToData() {
let data = "data".toData()!
let data = "data".toData()
let base64Url = data.toBase64Url()
let dataFromBase64Url = base64Url.base64UrlToData()
XCTAssertNotNil(dataFromBase64Url)
Expand Down
2 changes: 1 addition & 1 deletion Tests/WebAuthnTests/KeyHelperTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ final class KeyHelperTests: XCTestCase {
func testSignAndVerify() throws {
// Sign
let privateKey = try generatePublicPrivateKeyPair(es256Type, es256Length).get()
let secret = "this is a secret".toData()!
let secret = "this is a secret".toData()
let signature = try sign(privateKey, .ecdsaSignatureMessageX962SHA256, secret).get()
// Verify
let publicKey = getPublicKey(privateKey)
Expand Down
8 changes: 4 additions & 4 deletions Tests/WebAuthnTests/ModelTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,31 +28,31 @@ final class ModelTests: XCTestCase {
let extsOut = AuthenticatorExtensionsOutput()

func testNoneAttestationObjectWithoutAttestedCredentialDataAndExtension() throws {
let authData = AuthenticatorData(rpId.toSHA256()!, true, true, UInt32(0), nil, nil)
let authData = AuthenticatorData(rpId.toSHA256(), true, true, UInt32(0), nil, nil)
let attestationObject = NoneAttestationObject(authData: authData.toData())
let encoded = try! attestationObject.toCBOR().get()
let decoded = try! NoneAttestationObject.decode(encoded).get()
XCTAssertEqual(decoded.authData, authData.toData())
}

func testNoneAttestationObjectWithoutAttestedCredentialData() throws {
let authData = AuthenticatorData(rpId.toSHA256()!, true, true, UInt32(0), nil, extsOut)
let authData = AuthenticatorData(rpId.toSHA256(), true, true, UInt32(0), nil, extsOut)
let attestationObject = NoneAttestationObject(authData: authData.toData())
let encoded = try! attestationObject.toCBOR().get()
let decoded = try! NoneAttestationObject.decode(encoded).get()
XCTAssertEqual(decoded.authData, authData.toData())
}

func testNoneAttestationObjectWithoutExtension() throws {
let authData = AuthenticatorData(rpId.toSHA256()!, true, true, UInt32(0), attestedCredData, nil)
let authData = AuthenticatorData(rpId.toSHA256(), true, true, UInt32(0), attestedCredData, nil)
let attestationObject = NoneAttestationObject(authData: authData.toData())
let encoded = try! attestationObject.toCBOR().get()
let decoded = try! NoneAttestationObject.decode(encoded).get()
XCTAssertEqual(decoded.authData, authData.toData())
}

func testNoneAttestationObject() throws {
let authData = AuthenticatorData(rpId.toSHA256()!, true, true, UInt32(0), attestedCredData, extsOut)
let authData = AuthenticatorData(rpId.toSHA256(), true, true, UInt32(0), attestedCredData, extsOut)
let attestationObject = NoneAttestationObject(authData: authData.toData())
let encoded = try! attestationObject.toCBOR().get()
let decoded = try! NoneAttestationObject.decode(encoded).get()
Expand Down

0 comments on commit 2507cb9

Please sign in to comment.