Skip to content

Commit cbdaced

Browse files
committed
Add overloaded methods in SignatureVerifier
These are methods that replaces the signature parameter with a SessionToken parameter.
1 parent 85f953b commit cbdaced

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed

Sources/ATCryptography/SignatureVerifier.swift

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,65 @@ public struct SignatureVerifier {
6565

6666
return try await verifySignature(didKey: didKey, data: dataBytes, signature: signatureBytes, options: options)
6767
}
68+
69+
// MARK: - With SessionToken
70+
/// Verifies a digital signature from a session token using a `did:key`.
71+
///
72+
/// This is essentially the same as creating a ``SessionToken`` object, grabbing the signature,
73+
/// and inserting it into ``verifySignature(didKey:data:signature:options:jwtAlgorithm:)``.
74+
///
75+
/// - Parameters:
76+
/// - didKey: The `did:key` string associated with the signer.
77+
/// - data: The original message that was signed.
78+
/// - sessionToken: The session token to verify.
79+
/// - options: Options for signature verification. Optional. Defaults to `nil`.
80+
/// - jwtAlgorithm: The JWT algorithm used. Optional. Defaults to `nil`.
81+
/// - Returns: `true` if the signature is valid, or `false` if not.
82+
///
83+
/// - Throws: An error if the key type is unsupported or the JWT algorithm does not match.
84+
public static func verifySignature(
85+
didKey: String,
86+
data: [UInt8],
87+
sessionToken: SessionToken,
88+
options: VerifyOptions? = nil,
89+
jwtAlgorithm: String? = nil
90+
) async throws -> Bool {
91+
let jwt = sessionToken
92+
93+
guard let signature = jwt.signature else {
94+
throw SignatureVerificationError.invalidEncoding(reason: "Invalid session token.")
95+
}
96+
97+
return try await SignatureVerifier.verifySignature(
98+
didKey: didKey,
99+
data: data,
100+
signature: [UInt8](signature),
101+
options: options,
102+
jwtAlgorithm: jwtAlgorithm
103+
)
104+
}
105+
106+
/// Verifies a digital signature where the data and signature are given as UTF-8 and Base64URL strings.
107+
///
108+
/// This is essentially the same as creating a ``SessionToken`` object, grabbing the signature,
109+
/// and inserting it into ``verifySignatureUTF8(didKey:data:signature:options:)``.
110+
///
111+
/// - Parameters:
112+
/// - didKey: The `did:key` string associated with the signer.
113+
/// - data: The original message in UTF-8 string format.
114+
/// - sessionToken: The session token to verify.
115+
/// - options: Options for signature verification. Optional. Defaults to `nil`.
116+
/// - Returns: `true` if the signature is valid, otherwise `false`.
117+
///
118+
/// - Throws: An error if decoding fails or signature verification fails.
119+
public static func verifySignatureUTF8(didKey: String, data: String, sessionToken: SessionToken, options: VerifyOptions? = nil) async throws -> Bool {
120+
let jwt = sessionToken
121+
122+
guard let signature = jwt.signature,
123+
let encodedSignatureString = String(data: signature, encoding: .utf8) else {
124+
throw SignatureVerificationError.invalidEncoding(reason: "Invalid session token.")
125+
}
126+
127+
return try await verifySignatureUTF8(didKey: didKey, data: data, signature: encodedSignatureString, options: options)
128+
}
68129
}

0 commit comments

Comments
 (0)