@@ -2,7 +2,7 @@ import Foundation
22
33
44/// Failure reasons from decoding a JWT
5- public enum InvalidToken : Printable {
5+ public enum InvalidToken : CustomStringConvertible {
66 /// Decoding the JWT itself failed
77 case DecodeError( String )
88
@@ -60,7 +60,7 @@ public func decode(jwt:String, algorithms:[Algorithm], verify:Bool = true, audie
6060 switch load ( jwt) {
6161 case let . Success( header, payload, signature, signatureInput) :
6262 if verify {
63- if let failure = validateClaims ( payload, audience, issuer) ?? verifySignature ( algorithms, header, signatureInput, signature) {
63+ if let failure = validateClaims ( payload, audience: audience , issuer: issuer ) ?? verifySignature ( algorithms, header: header , signingInput : signatureInput, signature : signature) {
6464 return . Failure( failure)
6565 }
6666 }
@@ -73,7 +73,7 @@ public func decode(jwt:String, algorithms:[Algorithm], verify:Bool = true, audie
7373
7474/// Decode a JWT
7575public func decode( jwt: String , algorithm: Algorithm , verify: Bool = true , audience: String ? = nil , issuer: String ? = nil ) -> DecodeResult {
76- return decode ( jwt, [ algorithm] , verify: verify, audience: audience, issuer: issuer)
76+ return decode ( jwt, algorithms : [ algorithm] , verify: verify, audience: audience, issuer: issuer)
7777}
7878
7979// MARK: Parsing a JWT
@@ -99,7 +99,7 @@ func load(jwt:String) -> LoadResult {
9999 return . Failure( . DecodeError( " Header is not correctly encoded as base64 " ) )
100100 }
101101
102- let header = NSJSONSerialization . JSONObjectWithData ( headerData!, options: NSJSONReadingOptions ( 0 ) , error : nil ) as? Payload
102+ let header = ( try ? NSJSONSerialization . JSONObjectWithData ( headerData!, options: NSJSONReadingOptions ( rawValue : 0 ) ) ) as? Payload
103103 if header == nil {
104104 return . Failure( . DecodeError( " Invalid header " ) )
105105 }
@@ -109,7 +109,7 @@ func load(jwt:String) -> LoadResult {
109109 return . Failure( . DecodeError( " Payload is not correctly encoded as base64 " ) )
110110 }
111111
112- let payload = NSJSONSerialization . JSONObjectWithData ( payloadData!, options: NSJSONReadingOptions ( 0 ) , error : nil ) as? Payload
112+ let payload = ( try ? NSJSONSerialization . JSONObjectWithData ( payloadData!, options: NSJSONReadingOptions ( rawValue : 0 ) ) ) as? Payload
113113 if payload == nil {
114114 return . Failure( . DecodeError( " Invalid payload " ) )
115115 }
@@ -126,9 +126,9 @@ func load(jwt:String) -> LoadResult {
126126
127127func verifySignature( algorithms: [ Algorithm ] , header: Payload , signingInput: String , signature: NSData ) -> InvalidToken ? {
128128 if let alg = header [ " alg " ] as? String {
129- let matchingAlgorithms = filter ( algorithms) { algorithm in algorithm. description == alg }
130- let results = map ( matchingAlgorithms) { algorithm in algorithm. verify ( signingInput, signature: signature) }
131- let successes = filter ( results) { $0 }
129+ let matchingAlgorithms = algorithms. filter { algorithm in algorithm. description == alg }
130+ let results = matchingAlgorithms. map { algorithm in algorithm. verify ( signingInput, signature: signature) }
131+ let successes = results. filter { $0 }
132132 if successes. count > 0 {
133133 return nil
134134 }
0 commit comments