Skip to content

Commit

Permalink
[fix] moved jws extension
Browse files Browse the repository at this point in the history
  • Loading branch information
dtsiflit committed Aug 29, 2024
1 parent 1d206b0 commit df53d4c
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 89 deletions.
82 changes: 0 additions & 82 deletions Sources/Entities/Validated/ValidatedSiopOpenId4VPRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -507,88 +507,6 @@ private extension ValidatedSiopOpenId4VPRequest {
}
}

private extension JWS {

// Function to convert Unix timestamp to Date
func dateFromUnixTimestamp(_ timestamp: Any) -> Date? {
if let timestampInt = timestamp as? Int {
return Date(timeIntervalSince1970: TimeInterval(timestampInt))
} else if let timestampDouble = timestamp as? Double {
return Date(timeIntervalSince1970: timestampDouble)
}
return nil
}

func verifierAttestationClaims() throws -> VerifierAttestationClaims {

let payload = payload.data()
guard let json = try JSONSerialization.jsonObject(
with: payload,
options: []
) as? [String: Any] else {
throw ValidatedAuthorizationError.validationError("Invalid JWS payload")
}

guard
let cnf = json["cnf"] as? [String: Any],
let jwkDict = cnf["jwk"] as? [String: Any],
let jwk = convertJSONToPublicKey(json: jwkDict)
else {
throw ValidatedAuthorizationError.validationError("Cannot locate cnf/jwk in payload")
}

return VerifierAttestationClaims(
iss: try tryExtract(JWTClaimNames.issuer, from: json),
sub: try tryExtract(JWTClaimNames.subject, from: json),
iat: try tryExtract(JWTClaimNames.issuedAt, from: json, converter: dateFromUnixTimestamp),
exp: try tryExtract(JWTClaimNames.expirationTime, from: json, converter: dateFromUnixTimestamp),
verifierPubJwk: jwk,
redirectUris: try tryExtract("redirect_uris", from: json),
responseUris: try tryExtract("response_uris", from: json)
)
}

// Function to convert JSON to ECPublicKey or RSAPublicKey
func convertJSONToPublicKey(json: [String: Any]) -> JWK? {
guard let kty = json["kty"] as? String else {
return nil
}

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

// Function to convert JSON to ECPublicKey
func convertJSONToECPublicKey(json: [String: Any]) -> ECPublicKey? {
guard
let x = json["x"] as? String,
let y = json["y"] as? String,
let crv = json["crv"] as? String,
let curve = ECCurveType(rawValue: crv)
else {
return nil
}
return ECPublicKey(crv: curve, x: x, y: y)
}

// Function to convert JSON to RSAPublicKey
func convertJSONToRSAPublicKey(json: [String: Any]) -> RSAPublicKey? {
guard
let n = json["n"] as? String,
let e = json["e"] as? String
else {
return nil
}
return RSAPublicKey(modulus: n, exponent: e)
}
}

// Protocol to verify JWT claims
private protocol JWTClaimsSetVerifier {
func verify(claimsSet: JWTClaimsSet) throws
Expand Down
99 changes: 99 additions & 0 deletions Sources/Utilities/Extensions/JWS+Extensions.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/*
* Copyright (c) 2023 European Commission
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import Foundation
import JOSESwift

internal extension JWS {

// Function to convert Unix timestamp to Date
func dateFromUnixTimestamp(_ timestamp: Any) -> Date? {
if let timestampInt = timestamp as? Int {
return Date(timeIntervalSince1970: TimeInterval(timestampInt))
} else if let timestampDouble = timestamp as? Double {
return Date(timeIntervalSince1970: timestampDouble)
}
return nil
}

func verifierAttestationClaims() throws -> VerifierAttestationClaims {

let payload = payload.data()
guard let json = try JSONSerialization.jsonObject(
with: payload,
options: []
) as? [String: Any] else {
throw ValidatedAuthorizationError.validationError("Invalid JWS payload")
}

guard
let cnf = json["cnf"] as? [String: Any],
let jwkDict = cnf["jwk"] as? [String: Any],
let jwk = convertJSONToPublicKey(json: jwkDict)
else {
throw ValidatedAuthorizationError.validationError("Cannot locate cnf/jwk in payload")
}

return VerifierAttestationClaims(
iss: try tryExtract(JWTClaimNames.issuer, from: json),
sub: try tryExtract(JWTClaimNames.subject, from: json),
iat: try tryExtract(JWTClaimNames.issuedAt, from: json, converter: dateFromUnixTimestamp),
exp: try tryExtract(JWTClaimNames.expirationTime, from: json, converter: dateFromUnixTimestamp),
verifierPubJwk: jwk,
redirectUris: try tryExtract("redirect_uris", from: json),
responseUris: try tryExtract("response_uris", from: json)
)
}

// Function to convert JSON to ECPublicKey or RSAPublicKey
func convertJSONToPublicKey(json: [String: Any]) -> JWK? {
guard let kty = json["kty"] as? String else {
return nil
}

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

// Function to convert JSON to ECPublicKey
func convertJSONToECPublicKey(json: [String: Any]) -> ECPublicKey? {
guard
let x = json["x"] as? String,
let y = json["y"] as? String,
let crv = json["crv"] as? String,
let curve = ECCurveType(rawValue: crv)
else {
return nil
}
return ECPublicKey(crv: curve, x: x, y: y)
}

// Function to convert JSON to RSAPublicKey
func convertJSONToRSAPublicKey(json: [String: Any]) -> RSAPublicKey? {
guard
let n = json["n"] as? String,
let e = json["e"] as? String
else {
return nil
}
return RSAPublicKey(modulus: n, exponent: e)
}
}
8 changes: 1 addition & 7 deletions Tests/DID/DIDTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,7 @@ final class DIDTests: XCTestCase {

private let sampleDidJwk =
"""
did:jwk:
eyJraWQiOiJ1cm46aWV0ZjpwYXJhbXM6b2F1dGg6andrLXRodW1icHJpbnQ6c2hhLTI
1Njpnc0w0VTRxX1J6VFhRckpwQUNnZGkwb1lCdUV1QjNZNWZFanhDd1NPUFlBIiwia3
R5IjoiRUMiLCJjcnYiOiJQLTM4NCIsImFsZyI6IkVTMzg0IiwieCI6ImEtRWV5T2hlR
UNWcDJqRkdVRTNqR0RCNlAzVV80S0lyZHRzTU9RQXFQN0NBMlVvV3NERG1nOWdJUVhi
OEthd0ciLCJ5Ijoib3cxWDJ6VFVRaG12elY4NnpHdGhKc0xLeDE2MmhmSmxmN1p0OTF
YUnZBTzRScE4zR2RGaVl3Tmc0NXJWUmlUcSJ9
did:jwk:eyJrdHkiOiJFQyIsInVzZSI6InNpZyIsImtleV9vcHMiOlsic2lnbiJdLCJhbGciOiJFUzI1NiIsImtpZCI6IjhjMWQwZGQ1LTAxZjItNGMxOS04MjQwLTI0ZDQ3NWVkY2I5NCIsImNydiI6IlAtMjU2IiwieCI6Ik1VZzM5Mmk2OFNOaEFWYmlWRnJHQ2FyeTZIZzFkeFpZNk1OY1VwNVNULVkiLCJ5Ijoid243NTJSX3BnRWxlVWtNYmF0M0hmRDE3LUdhZTRLRHIwRUxyVU1HZnFWSSJ9
""".replacingOccurrences(of: "\n", with: "")
.trimmingCharacters(in: .whitespacesAndNewlines)
.replacingOccurrences(of: " ", with: "")
Expand Down

0 comments on commit df53d4c

Please sign in to comment.