Skip to content

Commit

Permalink
[fix] updated did lookup method
Browse files Browse the repository at this point in the history
  • Loading branch information
dtsiflit committed Aug 23, 2024
1 parent a246608 commit 3dfe07d
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -102,15 +102,15 @@ public enum AuthorizationResponsePayload: Encodable {
var container = encoder.container(keyedBy: CodingKeys.self)

switch self {
case .siopAuthenticationResponse(let idToken, let state, let nonce):
case .siopAuthenticationResponse(let idToken, let state, _):
try container.encode(state, forKey: .state)
try container.encode(idToken, forKey: .idToken)
case .openId4VPAuthorizationResponse(
let vpToken,
_,
let presentationSubmission,
let state,
let nonce
_
):
try container.encode(presentationSubmission, forKey: .presentationSubmission)
try container.encode(vpToken.value, forKey: .vpToken)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public enum SupportedClientIdScheme {
case preregistered(clients: [String: PreregisteredClient])
case x509SanUri(trust: CertificateTrust)
case x509SanDns(trust: CertificateTrust)
case did(lookup: DIDPublicKeyLookupAgent)
case did(lookup: DIDPublicKeyLookupAgentType)
case verifierAttestation(
trust: Verifier,
clockSkew: TimeInterval = 15.0
Expand Down
4 changes: 3 additions & 1 deletion Sources/Entities/DID/DIDPublicKeyLookupAgent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,6 @@
*/
import Foundation

public typealias DIDPublicKeyLookupAgent = (_ didUrl: DID) -> SecKey?
public protocol DIDPublicKeyLookupAgentType {
func resolveKey(from didUrl: DID) async -> SecKey?
}
30 changes: 15 additions & 15 deletions Sources/Entities/Validated/ValidatedSiopOpenId4VPRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public extension ValidatedSiopOpenId4VPRequest {
walletConfiguration: walletConfiguration
)

let client = try Self.getClient(
let client = try await Self.getClient(
clientId: payloadcClientId,
jwt: jwt,
config: walletConfiguration,
Expand Down Expand Up @@ -144,7 +144,7 @@ public extension ValidatedSiopOpenId4VPRequest {
walletConfiguration: walletConfiguration
)

let client = try Self.getClient(
let client = try await Self.getClient(
clientId: clientId,
jwt: request,
config: walletConfiguration,
Expand Down Expand Up @@ -264,7 +264,7 @@ public extension ValidatedSiopOpenId4VPRequest {
jwt: JWTString,
config: WalletOpenId4VPConfiguration?,
scheme: String?
) throws -> Client {
) async throws -> Client {
guard
let scheme: SupportedClientIdScheme = config?.supportedClientIdSchemes.first(where: { $0.scheme.rawValue == scheme }) ?? config?.supportedClientIdSchemes.first
else {
Expand Down Expand Up @@ -302,7 +302,7 @@ public extension ValidatedSiopOpenId4VPRequest {
)

case .did(let keyLookup):
return try Self.didPublicKeyLookup(
return try await Self.didPublicKeyLookup(
jws: try JWS(compactSerialization: jwt),
clientId: clientId,
keyLookup: keyLookup
Expand Down Expand Up @@ -361,9 +361,9 @@ private extension ValidatedSiopOpenId4VPRequest {
private static func didPublicKeyLookup(
jws: JWS,
clientId: String,
keyLookup: DIDPublicKeyLookupAgent
) throws -> Client {

keyLookup: DIDPublicKeyLookupAgentType
) async throws -> Client {
guard let kid = jws.header.kid else {
throw ValidatedAuthorizationError.validationError("kid not found in JWT header")
}
Expand All @@ -378,8 +378,8 @@ private extension ValidatedSiopOpenId4VPRequest {
guard let clientIdAsDID = DID.parse(clientId) else {
throw ValidatedAuthorizationError.validationError("Invalid DID")
}

guard let publicKey = keyLookup(clientIdAsDID) else {
guard let publicKey = await keyLookup.resolveKey(from: clientIdAsDID) else {
throw ValidatedAuthorizationError.validationError("Unable to extract public key from DID")
}

Expand Down Expand Up @@ -555,12 +555,12 @@ private extension JWS {
}

switch kty {
case "EC":
return convertJSONToECPublicKey(json: json)
case "RSA":
return convertJSONToRSAPublicKey(json: json)
default:
return nil
case "EC":
return convertJSONToECPublicKey(json: json)
case "RSA":
return convertJSONToRSAPublicKey(json: json)
default:
return nil
}
}

Expand Down
22 changes: 14 additions & 8 deletions Tests/VerifierAttestation/VerifierAttestationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ final class VerifierAttestaionTestsTests: XCTestCase {

override func setUpWithError() throws {
}

override func tearDownWithError() throws {
}

Expand All @@ -44,7 +44,7 @@ final class VerifierAttestaionTestsTests: XCTestCase {
verifier: verifier
)

let client = try ValidatedSiopOpenId4VPRequest.getClient(
let client = try await ValidatedSiopOpenId4VPRequest.getClient(
clientId: clientId,
jwt: jwt.compactSerializedString,
config: config,
Expand Down Expand Up @@ -80,12 +80,18 @@ final class VerifierAttestaionTestsTests: XCTestCase {
verifier: verifier
)

XCTAssertThrowsError(try ValidatedSiopOpenId4VPRequest.getClient(
clientId: clientId,
jwt: jwt.compactSerializedString,
config: config,
scheme: "verifier_attestation"
)) { error in
do {
// Attempt to call the async function
_ = try await ValidatedSiopOpenId4VPRequest.getClient(
clientId: clientId,
jwt: jwt.compactSerializedString,
config: config,
scheme: "verifier_attestation"
)

// If no error is thrown, this assertion will fail the test
XCTFail("Expected error to be thrown, but no error was thrown.")
} catch {
guard let joseError = error as? JOSESwiftError else {
XCTAssert(false)
return
Expand Down

0 comments on commit 3dfe07d

Please sign in to comment.