Skip to content

Commit 50d8236

Browse files
committed
refactor: Simplify signature verification
1 parent 5ada2f3 commit 50d8236

File tree

1 file changed

+9
-10
lines changed

1 file changed

+9
-10
lines changed

Sources/Decode.swift

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -103,17 +103,16 @@ func load(_ jwt:String) throws -> (header: Payload, payload: Payload, signature:
103103

104104
// MARK: Signature Verification
105105

106-
func verifySignature(_ algorithms:[Algorithm], header:Payload, signingInput:String, signature:Data) throws {
107-
if let alg = header["alg"] as? String {
108-
let matchingAlgorithms = algorithms.filter { algorithm in algorithm.description == alg }
109-
let results = matchingAlgorithms.map { algorithm in algorithm.verify(signingInput, signature: signature) }
110-
let successes = results.filter { $0 }
111-
if successes.count > 0 {
112-
return
113-
}
106+
func verifySignature(_ algorithms: [Algorithm], header: Payload, signingInput: String, signature: Data) throws {
107+
guard let alg = header["alg"] as? String else {
108+
throw InvalidToken.decodeError("Missing Algorithm")
109+
}
114110

111+
let verifiedAlgorithms = algorithms
112+
.filter { algorithm in algorithm.description == alg }
113+
.filter { algorithm in algorithm.verify(signingInput, signature: signature) }
114+
115+
if verifiedAlgorithms.isEmpty {
115116
throw InvalidToken.invalidAlgorithm
116117
}
117-
118-
throw InvalidToken.decodeError("Missing Algorithm")
119118
}

0 commit comments

Comments
 (0)