Skip to content

Commit

Permalink
Fix: Updated filter logic for sdjwt credentials
Browse files Browse the repository at this point in the history
  • Loading branch information
josmilan committed Oct 10, 2024
1 parent 889c97d commit 58eb864
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 28 deletions.
103 changes: 76 additions & 27 deletions Sources/eudiWalletOidcIos/Service/SDJWTService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ public class SDJWTService {
private init() {}

/**
* Calculates the SHA-256 hash of the input string and returns it in base64url encoding.
*
* @param inputString The input string to be hashed.
* @return The SHA-256 hash of the input string in base64url encoding, or null if the input is null.
*/
* Calculates the SHA-256 hash of the input string and returns it in base64url encoding.
*
* @param inputString The input string to be hashed.
* @return The SHA-256 hash of the input string in base64url encoding, or null if the input is null.
*/
public func calculateSHA256Hash(inputString: String?) -> String? {
guard let inputString = inputString,
let inputData = inputString.data(using: .utf8) else {
Expand Down Expand Up @@ -50,28 +50,28 @@ public class SDJWTService {
presentationDefinition: VerificationService.processPresentationDefinition(presentationRequest.presentationDefinition)
)

// let iat = Date()
// let payload =
// ([
// "audience": "\(presentationRequest.clientId ?? "")",
// "issueTime": "\(iat)",
// "nonce": "\(UUID().uuidString)",
// "exp": SDJWTService().calculateSHA256Hash(inputString: processedCredentialWithRequiredDisclosures) ?? ""
// ] as [String : Any]).toString() ?? ""
//
// let header =
// ([
// "algorithm": "ES256",
// "type": "kb_jwt"
// ]).toString() ?? ""
//
// // Create JWT token
// let headerData = Data(header.utf8)
// let payloadData = Data(payload.utf8)
// let unsignedToken = "\(headerData.base64URLEncodedString()).\(payloadData.base64URLEncodedString())"
// let signatureData = try! privateKey.signature(for: unsignedToken.data(using: .utf8)!)
// let signature = signatureData.rawRepresentation
// let idToken = "\(unsignedToken).\(signature.base64URLEncodedString())"
// let iat = Date()
// let payload =
// ([
// "audience": "\(presentationRequest.clientId ?? "")",
// "issueTime": "\(iat)",
// "nonce": "\(UUID().uuidString)",
// "exp": SDJWTService().calculateSHA256Hash(inputString: processedCredentialWithRequiredDisclosures) ?? ""
// ] as [String : Any]).toString() ?? ""
//
// let header =
// ([
// "algorithm": "ES256",
// "type": "kb_jwt"
// ]).toString() ?? ""
//
// // Create JWT token
// let headerData = Data(header.utf8)
// let payloadData = Data(payload.utf8)
// let unsignedToken = "\(headerData.base64URLEncodedString()).\(payloadData.base64URLEncodedString())"
// let signatureData = try! privateKey.signature(for: unsignedToken.data(using: .utf8)!)
// let signature = signatureData.rawRepresentation
// let idToken = "\(unsignedToken).\(signature.base64URLEncodedString())"

return processedCredentialWithRequiredDisclosures
} catch {
Expand Down Expand Up @@ -134,6 +134,55 @@ public class SDJWTService {
guard let jsonData = try? JSONSerialization.data(withJSONObject: object) else { return nil }
return String(data: jsonData, encoding: .utf8)
}
public func updateIssuerJwtWithDisclosuresForFiltering(credential: String?) -> String? {
guard let split = credential?.split(separator: "."), split.count > 1,
let jsonString = "\(split[1])".decodeBase64(),
let jsonObject = UIApplicationUtils.shared.convertStringToDictionary(text: jsonString) else { return nil }

var object = jsonObject

var hashList: [String] = []
let disclosures = getDisclosuresFromSDJWT(credential) ?? []
disclosures.forEach { encodedString in
guard let hash = calculateSHA256Hash(inputString: encodedString) else { return }
hashList.append(hash)
}

object = addDisclosuresToCredentialForFiltering(jsonElement: jsonObject, disclosures: disclosures, hashList: hashList)

guard let jsonData = try? JSONSerialization.data(withJSONObject: object) else { return nil }
return String(data: jsonData, encoding: .utf8)
}
private func addDisclosuresToCredentialForFiltering(jsonElement: [String: Any], disclosures: [String], hashList: [String]) -> [String: Any] {
var modifiedJsonElement = jsonElement

if modifiedJsonElement["_sd"] != nil {
guard let sdList = modifiedJsonElement["_sd"] as? [String] else { return [:] }
for (index, hash) in hashList.enumerated() {
if isStringPresentInJSONArray(jsonArray: sdList, searchString: hash) {

if let disclosure = disclosures[index].decodeBase64() {
let (decodedKey, decodedValue) = extractKeyValue(from: disclosure) ?? ("","" as Any)
if let decodedValue = decodedValue as? [String: Any] {
modifiedJsonElement[decodedKey] = disclosure
} else if let decodedValue = decodedValue as? [Any] {
modifiedJsonElement[decodedKey] = disclosure
} else {
modifiedJsonElement[decodedKey] = disclosure
}
}
}
}
}

for (key, value) in modifiedJsonElement {
if(value is [String: Any]){
modifiedJsonElement[key] = addDisclosuresToCredentialForFiltering(jsonElement: value as! [String : Any], disclosures: disclosures, hashList: hashList)
}
}

return modifiedJsonElement
}

private func addDisclosuresToCredential(jsonElement: [String: Any], disclosures: [String], hashList: [String]) -> [String: Any] {
var modifiedJsonElement = jsonElement
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -821,7 +821,7 @@ public func filterNameSpaces(nameSpacesValue: CBOR, requestedParams: [String]) -

let jsonString: String
if (cred.split(separator: "~").count) > 0 {
jsonString = SDJWTService.shared.updateIssuerJwtWithDisclosures(credential: cred) ?? ""
jsonString = SDJWTService.shared.updateIssuerJwtWithDisclosuresForFiltering(credential: cred) ?? ""
} else if split.count > 1,
let base64Data = Data(base64Encoded: String(split[1]), options: .ignoreUnknownCharacters),
let decodedString = String(data: base64Data, encoding: .utf8) {
Expand Down

0 comments on commit 58eb864

Please sign in to comment.