Skip to content

Commit

Permalink
Merge pull request #102 from niscy-eudiw/feature/sd-jwt-vc-meta-data-…
Browse files Browse the repository at this point in the history
…claims

Parse claims from sd-jwt-vc metadata
  • Loading branch information
dtsiflit authored Dec 12, 2024
2 parents a2d316a + 69f2f68 commit 16e691a
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 2 deletions.
30 changes: 30 additions & 0 deletions Sources/Entities/IssuanceFlows/CredentialIssuanceRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,36 @@ public extension MsoMdocClaims {
}
}

public typealias SdJwtVCMetadataClaims = [ClaimName: Claim]
public extension SdJwtVCMetadataClaims {
init(json: JSON) {
var claims = SdJwtVCMetadataClaims()
if let _ = json.dictionaryObject {
var namespaceClaims = [ClaimName: Claim]()
for (claimName, claimJSON) in json.dictionaryValue {
let claim = Claim(
mandatory: claimJSON["mandatory"].bool,
valueType: claimJSON["valuetype"].string,
display: claimJSON["display"].arrayValue.compactMap {
Display(json: $0)
}
)
namespaceClaims[claimName] = claim
}
claims = namespaceClaims
} else if let jsonArray = json.arrayObject {
for element in jsonArray {
if let dictionary = element as? [String: String] {
if let key = dictionary.keys.first {
claims[key] = Claim()
}
}
}
}
self = claims
}
}

public enum CredentialIssuanceRequest {
case single(SingleCredential, IssuanceResponseEncryptionSpec?)
}
Expand Down
36 changes: 34 additions & 2 deletions Sources/Entities/Profiles/SdJwtVcFormat.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public extension SdJwtVcFormat {
public let credentialEncryptionKey: SecKey?
public let credentialResponseEncryptionAlg: JWEAlgorithm?
public let credentialResponseEncryptionMethod: JOSEEncryptionMethod?
public let claimSet: ClaimSet?
public let credentialDefinition: CredentialDefinition
public let requestedCredentialResponseEncryption: RequestedCredentialResponseEncryption
public let credentialIdentifier: CredentialIdentifier?
Expand All @@ -54,6 +55,7 @@ public extension SdJwtVcFormat {
case credentialEncryptionJwk
case credentialResponseEncryptionAlg
case credentialResponseEncryptionMethod
case claimSet
case credentialDefinition
case credentialIdentifier
}
Expand All @@ -65,6 +67,7 @@ public extension SdJwtVcFormat {
credentialEncryptionKey: SecKey? = nil,
credentialResponseEncryptionAlg: JWEAlgorithm? = nil,
credentialResponseEncryptionMethod: JOSEEncryptionMethod? = nil,
claimSet: ClaimSet? = nil,
credentialDefinition: CredentialDefinition,
credentialIdentifier: CredentialIdentifier?
) throws {
Expand All @@ -86,6 +89,7 @@ public extension SdJwtVcFormat {
responseEncryptionAlg: credentialResponseEncryptionAlg,
responseEncryptionMethod: credentialResponseEncryptionMethod
)
self.claimSet = claimSet
}

public init(from decoder: Decoder) throws {
Expand All @@ -106,6 +110,7 @@ public extension SdJwtVcFormat {
try container.encode(credentialResponseEncryptionMethod, forKey: .credentialResponseEncryptionMethod)

try container.encode(credentialDefinition, forKey: .credentialDefinition)
try container.encode(claimSet, forKey: .claimSet)
}

public struct CredentialDefinition: Codable {
Expand Down Expand Up @@ -178,6 +183,7 @@ public extension SdJwtVcFormat {
public let proofTypesSupported: [String: ProofSigningAlgorithmsSupported]?
public let display: [Display]?
public let credentialDefinition: CredentialDefinitionTO
public let claims: [String: Claim]?

enum CodingKeys: String, CodingKey {
case format
Expand All @@ -188,6 +194,7 @@ public extension SdJwtVcFormat {
case proofTypesSupported = "proof_types_supported"
case display
case credentialDefinition = "credential_definition"
case claims
}

public init(
Expand All @@ -198,6 +205,7 @@ public extension SdJwtVcFormat {
credentialSigningAlgValuesSupported: [String]? = nil,
proofTypesSupported: [String: ProofSigningAlgorithmsSupported]? = nil,
display: [Display]? = nil,
claims: [String : Claim]? = nil,
credentialDefinition: CredentialDefinitionTO
) {
self.format = format
Expand All @@ -208,6 +216,7 @@ public extension SdJwtVcFormat {
self.proofTypesSupported = proofTypesSupported
self.display = display
self.credentialDefinition = credentialDefinition
self.claims = claims
}

func toDomain() throws -> SdJwtVcFormat.CredentialConfiguration {
Expand All @@ -220,14 +229,28 @@ public extension SdJwtVcFormat {
let credentialSigningAlgValuesSupported: [String] = self.credentialSigningAlgValuesSupported ?? []
let credentialDefinition = self.credentialDefinition.toDomain()

let claims: SdJwtVCMetadataClaims = claims?.mapValues { claim in
Claim(
mandatory: claim.mandatory ?? false,
valueType: claim.valueType,
display: claim.display?.compactMap {
Display(
name: $0.name,
locale: $0.locale
)
}
)
} ?? [:]

return .init(
scope: scope,
vct: vct,
cryptographicBindingMethodsSupported: bindingMethods,
credentialSigningAlgValuesSupported: credentialSigningAlgValuesSupported,
proofTypesSupported: self.proofTypesSupported,
display: display,
credentialDefinition: credentialDefinition
credentialDefinition: credentialDefinition,
claims: claims
)
}
}
Expand All @@ -239,6 +262,7 @@ public extension SdJwtVcFormat {
public let credentialSigningAlgValuesSupported: [String]
public let proofTypesSupported: [String: ProofSigningAlgorithmsSupported]?
public let display: [Display]
public let claims: SdJwtVCMetadataClaims
public let credentialDefinition: CredentialDefinition

enum CodingKeys: String, CodingKey {
Expand All @@ -249,6 +273,7 @@ public extension SdJwtVcFormat {
case proofTypesSupported = "proof_types_supported"
case display
case credentialDefinition = "credential_definition"
case claims
}

var claimList: [String] {
Expand All @@ -262,7 +287,8 @@ public extension SdJwtVcFormat {
credentialSigningAlgValuesSupported: [String],
proofTypesSupported: [String: ProofSigningAlgorithmsSupported]?,
display: [Display],
credentialDefinition: CredentialDefinition
credentialDefinition: CredentialDefinition,
claims: SdJwtVCMetadataClaims
) {
self.scope = scope
self.vct = vct
Expand All @@ -271,6 +297,7 @@ public extension SdJwtVcFormat {
self.proofTypesSupported = proofTypesSupported
self.display = display
self.credentialDefinition = credentialDefinition
self.claims = claims
}

public init(from decoder: Decoder) throws {
Expand All @@ -285,6 +312,7 @@ public extension SdJwtVcFormat {

display = try container.decode([Display].self, forKey: .display)
credentialDefinition = try container.decode(CredentialDefinition.self, forKey: .credentialDefinition)
claims = try container.decode(SdJwtVCMetadataClaims.self, forKey: .claims)
}

public func encode(to encoder: Encoder) throws {
Expand All @@ -295,6 +323,7 @@ public extension SdJwtVcFormat {
try container.encode(proofTypesSupported, forKey: .proofTypesSupported)
try container.encode(display, forKey: .display)
try container.encode(credentialDefinition, forKey: .credentialDefinition)
try container.encode(claims, forKey: .claims)
}

init(json: JSON) throws {
Expand All @@ -318,7 +347,10 @@ public extension SdJwtVcFormat {
self.display = json["display"].arrayValue.map { json in
Display(json: json)
}

self.credentialDefinition = CredentialDefinition(json: json["credential_definition"])

self.claims = SdJwtVCMetadataClaims(json: json["claims"])
}

func toIssuanceRequest(
Expand Down

0 comments on commit 16e691a

Please sign in to comment.