Skip to content

Commit f1acfc9

Browse files
committed
Add overloaded methods in P256 and K256Operations
1 parent 3af079b commit f1acfc9

File tree

2 files changed

+100
-0
lines changed

2 files changed

+100
-0
lines changed

Sources/ATCryptography/k256/K256Operations.swift

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,4 +84,54 @@ public struct K256Operations {
8484
return false
8585
}
8686
}
87+
88+
// MARK: - With SessionToken
89+
90+
/// Verifies a DID-based signature from a session token.
91+
///
92+
/// A ``SessionToken`` instance must be created before calling this method. The reason for this instead
93+
/// of the method itself having to call the method is to help in ensuring the session token was
94+
/// actually used.
95+
///
96+
/// - Parameters:
97+
/// - did: The DID of the signer.
98+
/// - data: The original message that was signed.
99+
/// - sessionToken: The session token containing the signature to verify.
100+
/// - options: Optional verification settings. Optional. Defaults to `nil`.
101+
/// - Returns: `true` if the signature is valid, otherwise `false`.
102+
///
103+
/// - Throws: An error if the DID is not a valid k256 `did:key`.
104+
public static func verifyDIDSignature(did: String, data: [UInt8], sessionToken: SessionToken, options: VerifyOptions? = nil) async throws -> Bool {
105+
let jwt = sessionToken
106+
107+
guard let signature = jwt.signature else {
108+
throw SignatureVerificationError.invalidEncoding(reason: "No valid signature found in the provided session token.")
109+
}
110+
111+
return try await verifyDIDSignature(did: did, data: data, signature: [UInt8](signature), options: options)
112+
}
113+
114+
/// Verifies a k256 signature from a session token.
115+
///
116+
/// A ``SessionToken`` instance must be created before calling this method. The reason for this instead
117+
/// of the method itself having to call the method is to help in ensuring the session token was
118+
/// actually used.
119+
///
120+
/// - Parameters:
121+
/// - publicKey: The public key in raw bytes.
122+
/// - data: The original message that was signed.
123+
/// - signature: The signature to verify.
124+
/// - options: Options for signature verification. Optional. Defaults to `nil`.
125+
/// - Returns: `true` if the signature is valid, or `false` if not.
126+
///
127+
/// - Throws: An error if signature verification fails.
128+
public static func verifySignature(publicKey: [UInt8], data: [UInt8], sessionToken: SessionToken, options: VerifyOptions? = nil) async throws -> Bool {
129+
let jwt = sessionToken
130+
131+
guard let signature = jwt.signature else {
132+
throw SignatureVerificationError.invalidEncoding(reason: "No valid signature found in the provided session token.")
133+
}
134+
135+
return try await K256Operations.verifySignature(publicKey: publicKey, data: data, signature: [UInt8](signature), options: options)
136+
}
87137
}

Sources/ATCryptography/p256/P256Operations.swift

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,4 +96,54 @@ public struct P256Operations {
9696
return false
9797
}
9898
}
99+
100+
// MARK: - With SessionToken
101+
102+
/// Verifies a DID-based signature from a session token.
103+
///
104+
/// A ``SessionToken`` instance must be created before calling this method. The reason for this instead
105+
/// of the method itself having to call the method is to help in ensuring the session token was
106+
/// actually used.
107+
///
108+
/// - Parameters:
109+
/// - did: The DID of the signer.
110+
/// - data: The original message that was signed.
111+
/// - sessionToken: The session token containing the signature to verify.
112+
/// - options: Optional verification settings. Optional. Defaults to `nil`.
113+
/// - Returns: `true` if the signature is valid, otherwise `false`.
114+
///
115+
/// - Throws: An error if the DID is not a valid p256 `did:key`.
116+
public static func verifyDIDSignature(did: String, data: [UInt8], sessionToken: SessionToken, options: VerifyOptions? = nil) async throws -> Bool {
117+
let jwt = sessionToken
118+
119+
guard let signature = jwt.signature else {
120+
throw SignatureVerificationError.invalidEncoding(reason: "No valid signature found in the provided session token.")
121+
}
122+
123+
return try await P256Operations.verifyDIDSignature(did: did, data: data, signature: [UInt8](signature), options: options)
124+
}
125+
126+
/// Verifies a p256 signature from a session token.
127+
///
128+
/// A ``SessionToken`` instance must be created before calling this method. The reason for this instead
129+
/// of the method itself having to call the method is to help in ensuring the session token was
130+
/// actually used.
131+
///
132+
/// - Parameters:
133+
/// - publicKey: The public key in raw bytes.
134+
/// - data: The original message that was signed.
135+
/// - sessionToken: The session token containing the signature to verify.
136+
/// - options: Options for signature verification. Optional. Defaults to `nil`.
137+
/// - Returns: `true` if the signature is valid, or `false` if not.
138+
///
139+
/// - Throws: An error if signature verification fails.
140+
public static func verifySignature(publicKey: [UInt8], data: [UInt8], sessionToken: SessionToken, options: VerifyOptions? = nil) async throws -> Bool {
141+
let jwt = sessionToken
142+
143+
guard let signature = jwt.signature else {
144+
throw SignatureVerificationError.invalidEncoding(reason: "No valid signature found in the provided session token.")
145+
}
146+
147+
return try await P256Operations.verifySignature(publicKey: publicKey, data: data, signature: [UInt8](signature), options: options)
148+
}
99149
}

0 commit comments

Comments
 (0)