• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

hyperledger / identus-edge-agent-sdk-swift / 11914030434

19 Nov 2024 01:25PM UTC coverage: 42.668% (-0.07%) from 42.738%
11914030434

Pull #179

github

web-flow
Merge 3e60ae2e6 into e8bf5e9ea
Pull Request #179: chore: update coverage badge

5479 of 12841 relevant lines covered (42.67%)

99.88 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

13.85
/EdgeAgentSDK/Pollux/Sources/Operation/JWT/CreateJWTCredentialRequest.swift
1
import Combine
2
import Domain
3
import Foundation
4
import JSONWebAlgorithms
5
import JSONWebKey
6
import JSONWebToken
7
import JSONWebSignature
8

9
private struct Schema: Codable {
10
    let name: String
11
    let version: String
12
    let attrNames: [String]
13
    let issuerId: String
14
}
15

16
struct CreateJWTCredentialRequest {
17
    static func create(didStr: String, key: ExportableKey, offerData: Data) async throws -> String {
×
18
        let jsonObject = try JSONSerialization.jsonObject(with: offerData)
×
19
        guard
×
20
            let domain = findValue(forKey: "domain", in: jsonObject),
×
21
            let challenge = findValue(forKey: "challenge", in: jsonObject)
×
22
        else { throw PolluxError.offerDoesntProvideEnoughInformation }
×
23

×
24
        let keyJWK = key.jwk
×
25
        let claims = ClaimsRequestSignatureJWT(
×
26
            iss: didStr,
×
27
            sub: nil,
×
28
            aud: [domain],
×
29
            exp: nil,
×
30
            nbf: nil,
×
31
            iat: nil,
×
32
            jti: nil,
×
33
            nonce: challenge,
×
34
            vp: .init(context: .init([
×
35
                "https://www.w3.org/2018/presentations/v1"
×
36
            ]), type: .init([
×
37
                "VerifiablePresentation"
×
38
            ]))
×
39
        )
×
40

×
41
        ES256KSigner.invertedBytesR_S = true
×
42

×
43
        guard let kty = JWK.KeyType(rawValue: keyJWK.kty) else { throw PolluxError.invalidPrismDID }
×
44
        let jwt = try JWT.signed(
×
45
            payload: claims,
×
46
            protectedHeader: DefaultJWSHeaderImpl(
×
47
                algorithm: .ES256K,
×
48
                keyID: keyJWK.kid
×
49
            ),
×
50
            key: JSONWebKey.JWK(
×
51
                keyType: kty,
×
52
                keyID: keyJWK.kid,
×
53
                x: keyJWK.x.flatMap { Data(fromBase64URL: $0) },
×
54
                y: keyJWK.y.flatMap { Data(fromBase64URL: $0) },
×
55
                d: keyJWK.d.flatMap { Data(fromBase64URL: $0) }
×
56
            )
×
57
        )
×
58

×
59
        ES256KSigner.invertedBytesR_S = false
×
60
        return jwt.jwtString
×
61
    }
×
62
}
63

64
struct ClaimsRequestSignatureJWT: JWTRegisteredFieldsClaims, Codable {
65
    struct VerifiablePresentation: Codable {
66
        enum CodingKeys: String, CodingKey {
67
            case context = "@context"
68
            case type = "type"
69
        }
70

71
        let context: Set<String>
72
        let type: Set<String>
73
    }
74

75
    let iss: String?
76
    let sub: String?
77
    let aud: [String]?
78
    let exp: Date?
79
    let nbf: Date?
80
    let iat: Date?
81
    let jti: String?
82
    let nonce: String
83
    let vp: VerifiablePresentation
84

85
    func validateExtraClaims() throws {}
×
86
}
87

88

89
// TODO: This function is not the most appropriate but will do the job now to change later.
90
func findValue(forKey key: String, in json: Any) -> String? {
8✔
91
    if let dict = json as? [String: Any] {
8✔
92
        if let value = dict[key] {
8✔
93
            return value as? String
4✔
94
        }
4✔
95
        for (_, subJson) in dict {
4✔
96
            if let foundValue = findValue(forKey: key, in: subJson) {
4✔
97
                return foundValue
4✔
98
            }
4✔
99
        }
×
100
    } else if let array = json as? [Any] {
×
101
        for subJson in array {
×
102
            if let foundValue = findValue(forKey: key, in: subJson) {
×
103
                return foundValue
×
104
            }
×
105
        }
×
106
    }
×
107
    return nil
×
108
}
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc