Skip to content

Commit

Permalink
[fix] added sdjwt vc dpop test
Browse files Browse the repository at this point in the history
  • Loading branch information
dtsiflit committed Dec 4, 2024
1 parent ec332d9 commit 718617e
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 9 deletions.
6 changes: 3 additions & 3 deletions Sources/Extensions/HTTPURLResponse+Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
*/
import Foundation

extension HTTPURLResponse {
public extension HTTPURLResponse {

func valueForHeader(_ header: String) -> String? {
private func valueForHeader(_ header: String) -> String? {
let lowercasedHeader = header.lowercased()
for (key, value) in allHeaderFields {
if let keyString = key as? String, keyString.lowercased() == lowercasedHeader {
Expand All @@ -28,7 +28,7 @@ extension HTTPURLResponse {
}

func containsDpopError() -> Bool {
guard statusCode == 401,
guard statusCode == HTTPStatusCode.unauthorized,
let wwwAuth = valueForHeader("WWW-Authenticate") else {
return false
}
Expand Down
4 changes: 2 additions & 2 deletions Sources/Utilities/RemoteDataAccess/Fetcher.swift
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public struct Fetcher<Element: Decodable>: Fetching {
let (data, response) = try await self.session.data(from: url)

let statusCode = (response as? HTTPURLResponse)?.statusCode ?? 0
if !statusCode.isWithinRange(200...299) {
if !statusCode.isWithinRange(HTTPStatusCode.ok...HTTPStatusCode.imUsed) {
throw FetchError.invalidStatusCode(url, statusCode)
}
let object = try JSONDecoder().decode(Element.self, from: data)
Expand All @@ -108,7 +108,7 @@ public struct Fetcher<Element: Decodable>: Fetching {
let (data, response) = try await self.session.data(from: url)

let statusCode = (response as? HTTPURLResponse)?.statusCode ?? 0
if !statusCode.isWithinRange(200...299) {
if !statusCode.isWithinRange(HTTPStatusCode.ok...HTTPStatusCode.imUsed) {
throw FetchError.invalidStatusCode(url, statusCode)
}

Expand Down
10 changes: 6 additions & 4 deletions Sources/Utilities/RemoteDataAccess/Poster.swift
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public struct Poster: PostingType {
let httpResponse = (response as? HTTPURLResponse)
let headers = httpResponse?.allHeaderFields ?? [:]

if statusCode >= 400 && statusCode < 500 {
if statusCode >= HTTPStatusCode.badRequest && statusCode < HTTPStatusCode.internalServerError {
if let httpResponse,
httpResponse.containsDpopError(),
let dPopNonce = headers["DPoP-Nonce"] as? String {
Expand All @@ -127,7 +127,7 @@ public struct Poster: PostingType {
return .failure(.response(object))
}

} else if statusCode >= 500 && statusCode < 599 {
} else if statusCode >= HTTPStatusCode.internalServerError {
return .failure(.serverError)
}

Expand All @@ -140,7 +140,7 @@ public struct Poster: PostingType {
)
)
} catch {
if statusCode == 200, let string = String(data: data, encoding: .utf8) {
if statusCode == HTTPStatusCode.ok, let string = String(data: data, encoding: .utf8) {
return .failure(.cannotParse(string))
} else {
return .failure(.networkError(error))
Expand All @@ -166,7 +166,9 @@ public struct Poster: PostingType {
do {
let (_, response) = try await self.session.data(for: request)

return .success((response as? HTTPURLResponse)?.statusCode.isWithinRange(200...299) ?? false)
return .success((response as? HTTPURLResponse)?.statusCode.isWithinRange(
HTTPStatusCode.ok...HTTPStatusCode.imUsed
) ?? false)
} catch let error as NSError {
return .failure(.networkError(error))
} catch {
Expand Down
66 changes: 66 additions & 0 deletions Tests/Wallet/VCIFlowWithOffer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,55 @@ class VCIFlowWithOffer: XCTestCase {

XCTAssert(true)
}

func testWithOfferSdJwtDPoP() async throws {

let privateKey = try KeyController.generateECDHPrivateKey()
let publicKey = try KeyController.generateECDHPublicKey(from: privateKey)

let alg = JWSAlgorithm(.ES256)
let publicKeyJWK = try ECPublicKey(
publicKey: publicKey,
additionalParameters: [
"alg": alg.name,
"use": "sig",
"kid": UUID().uuidString
])

let privateKeyProxy: SigningKeyProxy = .secKey(privateKey)
let bindingKey: BindingKey = .jwk(
algorithm: alg,
jwk: publicKeyJWK,
privateKey: privateKeyProxy
)

let user = ActingUser(
username: "tneal",
password: "password"
)

let wallet = Wallet(
actingUser: user,
bindingKeys: [bindingKey],
dPoPConstructor: DPoPConstructor(
algorithm: alg,
jwk: publicKeyJWK,
privateKey: privateKeyProxy
)
)

do {
try await walletInitiatedIssuanceWithOfferSDJWT_DPoP(
wallet: wallet
)
} catch {

XCTExpectFailure()
XCTAssert(false, error.localizedDescription)
}

XCTAssert(true)
}
}

private func walletInitiatedIssuanceWithOfferSdJWT(
Expand Down Expand Up @@ -353,6 +402,23 @@ private func walletInitiatedIssuanceWithOfferMDL_DPoP(
print("--> [ISSUANCE] Issued credential : \(credential)")
}

private func walletInitiatedIssuanceWithOfferSDJWT_DPoP(
wallet: Wallet,
claimSet: ClaimSet? = nil
) async throws {

print("[[Scenario: Offer passed to wallet via url]] ")

let url = "\(CREDENTIAL_ISSUER_PUBLIC_URL)/credentialoffer?credential_offer=\(SdJwtVC_CredentialOffer)"
let credential = try await wallet.issueByCredentialOfferUrl_DPoP(
offerUri: url,
scope: PID_SdJwtVC_config_id,
claimSet: claimSet
)

print("--> [ISSUANCE] Issued credential : \(credential)")
}

private func walletInitiatedIssuanceWithOfferMdoc(
wallet: Wallet,
claimSet: ClaimSet? = nil
Expand Down

0 comments on commit 718617e

Please sign in to comment.