|
| 1 | +import Foundation |
| 2 | + |
| 3 | + |
| 4 | +/// Failure reasons from decoding a JWT |
| 5 | +public enum InvalidToken : Printable { |
| 6 | + /// Decoding the JWT itself failed |
| 7 | + case DecodeError(String) |
| 8 | + |
| 9 | + /// The JWT uses an unsupported algorithm |
| 10 | + case InvalidAlgorithm |
| 11 | + |
| 12 | + /// An invalid key was used when trying to validate a JWT |
| 13 | + case InvalidKey |
| 14 | + |
| 15 | + /// The issued claim has expired |
| 16 | + case ExpiredSignature |
| 17 | + |
| 18 | + /// The issued claim is for the future |
| 19 | + case ImmatureSignature |
| 20 | + |
| 21 | + /// The claim is for the future |
| 22 | + case InvalidIssuedAt |
| 23 | + |
| 24 | + /// The audience of the claim doesn't match |
| 25 | + case InvalidAudience |
| 26 | + |
| 27 | + /// The issuer claim failed to verify |
| 28 | + case InvalidIssuer |
| 29 | + |
| 30 | + /// Returns a readable description of the error |
| 31 | + public var description:String { |
| 32 | + switch self { |
| 33 | + case .DecodeError(let error): |
| 34 | + return "Decode Error: \(error)" |
| 35 | + case .InvalidIssuer: |
| 36 | + return "Invalid Issuer" |
| 37 | + case .ExpiredSignature: |
| 38 | + return "Expired Signature" |
| 39 | + case .ImmatureSignature: |
| 40 | + return "The token is not yet valid (not before claim)" |
| 41 | + case .InvalidIssuedAt: |
| 42 | + return "Issued at claim (iat) is in the future" |
| 43 | + case InvalidAudience: |
| 44 | + return "Invalid Audience" |
| 45 | + case InvalidAlgorithm: |
| 46 | + return "Unsupported Algorithm" |
| 47 | + case InvalidKey: |
| 48 | + return "Invalid Key" |
| 49 | + } |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | + |
| 54 | +/// Result from decoding a JWT |
| 55 | +public enum DecodeResult { |
| 56 | + /// Decoding succeeded |
| 57 | + case Success(Payload) |
| 58 | + |
| 59 | + /// Decoding failed, take a look at the invalid token reason |
| 60 | + case Failure(InvalidToken) |
| 61 | +} |
| 62 | + |
| 63 | + |
| 64 | +/// Decode a JWT |
| 65 | +public func decode(jwt:String, key:String? = nil, verify:Bool = true, audience:String? = nil, issuer:String? = nil) -> DecodeResult { |
| 66 | + switch load(jwt) { |
| 67 | + case let .Success(header, payload, signature, signatureInput): |
| 68 | + if verify { |
| 69 | + if let failure = validateClaims(payload, audience, issuer) ?? verifySignature(header, signatureInput, signature, key) { |
| 70 | + return .Failure(failure) |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + return .Success(payload) |
| 75 | + case .Failure(let failure): |
| 76 | + return .Failure(failure) |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +// MARK: Parsing a JWT |
| 81 | + |
| 82 | +enum LoadResult { |
| 83 | + case Success(header:Payload, payload:Payload, signature:NSData, signatureInput:String) |
| 84 | + case Failure(InvalidToken) |
| 85 | +} |
| 86 | + |
| 87 | +func load(jwt:String) -> LoadResult { |
| 88 | + let segments = jwt.componentsSeparatedByString(".") |
| 89 | + if segments.count != 3 { |
| 90 | + return .Failure(.DecodeError("Not enough segments")) |
| 91 | + } |
| 92 | + |
| 93 | + let headerSegment = segments[0] |
| 94 | + let payloadSegment = segments[1] |
| 95 | + let signatureSegment = segments[2] |
| 96 | + let signatureInput = "\(headerSegment).\(payloadSegment)" |
| 97 | + |
| 98 | + let headerData = base64decode(headerSegment) |
| 99 | + if headerData == nil { |
| 100 | + return .Failure(.DecodeError("Header is not correctly encoded as base64")) |
| 101 | + } |
| 102 | + |
| 103 | + let header = NSJSONSerialization.JSONObjectWithData(headerData!, options: NSJSONReadingOptions(0), error: nil) as? Payload |
| 104 | + if header == nil { |
| 105 | + return .Failure(.DecodeError("Invalid header")) |
| 106 | + } |
| 107 | + |
| 108 | + let payloadData = base64decode(payloadSegment) |
| 109 | + if payloadData == nil { |
| 110 | + return .Failure(.DecodeError("Payload is not correctly encoded as base64")) |
| 111 | + } |
| 112 | + |
| 113 | + let payload = NSJSONSerialization.JSONObjectWithData(payloadData!, options: NSJSONReadingOptions(0), error: nil) as? Payload |
| 114 | + if payload == nil { |
| 115 | + return .Failure(.DecodeError("Invalid payload")) |
| 116 | + } |
| 117 | + |
| 118 | + let signature = base64decode(signatureSegment) |
| 119 | + if signature == nil { |
| 120 | + return .Failure(.DecodeError("Signature is not correctly encoded as base64")) |
| 121 | + } |
| 122 | + |
| 123 | + return .Success(header:header!, payload:payload!, signature:signature!, signatureInput:signatureInput) |
| 124 | +} |
| 125 | + |
| 126 | +// MARK: Signature Verification |
| 127 | + |
| 128 | +func verifySignature(header:Payload, signingInput:String, signature:NSData, key:String?) -> InvalidToken? { |
| 129 | + if let alg = header["alg"] as? String { |
| 130 | + if let algoritm = Algorithm.algorithm(alg, key: key) { |
| 131 | + if algoritm.verify(signingInput, signature: signature) { |
| 132 | + return nil |
| 133 | + } else { |
| 134 | + return .InvalidKey |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + return .InvalidAlgorithm |
| 139 | + } |
| 140 | + |
| 141 | + return .DecodeError("Missing Algorithm") |
| 142 | +} |
0 commit comments