Skip to content

Commit a3423e6

Browse files
committed
Migrate to Swift 2.0
1 parent 486140c commit a3423e6

File tree

10 files changed

+84
-97
lines changed

10 files changed

+84
-97
lines changed

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
language: objective-c
2+
osx_image: xcode7
23
before_install:
34
- gem install cocoapods --no-document
45
- gem install xcpretty --no-document

JSONWebToken.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ Pod::Spec.new do |spec|
1111
spec.ios.deployment_target = '8.0'
1212
spec.osx.deployment_target = '10.9'
1313
spec.requires_arc = true
14-
spec.dependency 'CryptoSwift', '0.0.10'
14+
spec.dependency 'CryptoSwift', '0.0.14'
1515
spec.module_name = 'JWT'
1616
end
1717

JWT.xcodeproj/project.pbxproj

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,6 @@
171171
279D63981AD07FFF0024E2BC /* Frameworks */,
172172
279D63991AD07FFF0024E2BC /* Headers */,
173173
279D639A1AD07FFF0024E2BC /* Resources */,
174-
842FB8EA008161B653B5AD81 /* Embed Pods Frameworks */,
175174
6D3D4069FD3A7DC06168A6A2 /* Copy Pods Resources */,
176175
);
177176
buildRules = (
@@ -210,6 +209,8 @@
210209
279D63931AD07FFF0024E2BC /* Project object */ = {
211210
isa = PBXProject;
212211
attributes = {
212+
LastSwiftMigration = 0700;
213+
LastSwiftUpdateCheck = 0700;
213214
LastUpgradeCheck = 0620;
214215
ORGANIZATIONNAME = Cocode;
215216
TargetAttributes = {
@@ -302,21 +303,6 @@
302303
shellScript = "diff \"${PODS_ROOT}/../Podfile.lock\" \"${PODS_ROOT}/Manifest.lock\" > /dev/null\nif [[ $? != 0 ]] ; then\n cat << EOM\nerror: The sandbox is not in sync with the Podfile.lock. Run 'pod install' or update your CocoaPods installation.\nEOM\n exit 1\nfi\n";
303304
showEnvVarsInLog = 0;
304305
};
305-
842FB8EA008161B653B5AD81 /* Embed Pods Frameworks */ = {
306-
isa = PBXShellScriptBuildPhase;
307-
buildActionMask = 2147483647;
308-
files = (
309-
);
310-
inputPaths = (
311-
);
312-
name = "Embed Pods Frameworks";
313-
outputPaths = (
314-
);
315-
runOnlyForDeploymentPostprocessing = 0;
316-
shellPath = /bin/sh;
317-
shellScript = "\"${SRCROOT}/Pods/Target Support Files/Pods-JWT/Pods-JWT-frameworks.sh\"\n";
318-
showEnvVarsInLog = 0;
319-
};
320306
E68E26141F4DF11E3638A2F0 /* Embed Pods Frameworks */ = {
321307
isa = PBXShellScriptBuildPhase;
322308
buildActionMask = 2147483647;

JWT/Base64.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,26 @@ import Foundation
33

44
/// URI Safe base64 encode
55
func base64encode(input:NSData) -> String {
6-
let data = input.base64EncodedDataWithOptions(NSDataBase64EncodingOptions(0))
6+
let data = input.base64EncodedDataWithOptions(NSDataBase64EncodingOptions(rawValue: 0))
77
let string = NSString(data: data, encoding: NSUTF8StringEncoding) as! String
88
return string
9-
.stringByReplacingOccurrencesOfString("+", withString: "-", options: NSStringCompareOptions(0), range: nil)
10-
.stringByReplacingOccurrencesOfString("/", withString: "_", options: NSStringCompareOptions(0), range: nil)
11-
.stringByReplacingOccurrencesOfString("=", withString: "", options: NSStringCompareOptions(0), range: nil)
9+
.stringByReplacingOccurrencesOfString("+", withString: "-", options: NSStringCompareOptions(rawValue: 0), range: nil)
10+
.stringByReplacingOccurrencesOfString("/", withString: "_", options: NSStringCompareOptions(rawValue: 0), range: nil)
11+
.stringByReplacingOccurrencesOfString("=", withString: "", options: NSStringCompareOptions(rawValue: 0), range: nil)
1212
}
1313

1414
/// URI Safe base64 decode
1515
func base64decode(input:String) -> NSData? {
16-
let rem = count(input) % 4
16+
let rem = input.characters.count % 4
1717

1818
var ending = ""
1919
if rem > 0 {
2020
let amount = 4 - rem
2121
ending = String(count: amount, repeatedValue: Character("="))
2222
}
2323

24-
let base64 = input.stringByReplacingOccurrencesOfString("-", withString: "+", options: NSStringCompareOptions(0), range: nil)
25-
.stringByReplacingOccurrencesOfString("_", withString: "/", options: NSStringCompareOptions(0), range: nil) + ending
24+
let base64 = input.stringByReplacingOccurrencesOfString("-", withString: "+", options: NSStringCompareOptions(rawValue: 0), range: nil)
25+
.stringByReplacingOccurrencesOfString("_", withString: "/", options: NSStringCompareOptions(rawValue: 0), range: nil) + ending
2626

27-
return NSData(base64EncodedString: base64, options: NSDataBase64DecodingOptions(0))
27+
return NSData(base64EncodedString: base64, options: NSDataBase64DecodingOptions(rawValue: 0))
2828
}

JWT/Claims.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
import Foundation
22

33
func validateClaims(payload:Payload, audience:String?, issuer:String?) -> InvalidToken? {
4-
return validateIssuer(payload, issuer) ?? validateAudience(payload, audience) ??
5-
validateDate(payload, "exp", .OrderedAscending, .ExpiredSignature, "Expiration time claim (exp) must be an integer") ??
6-
validateDate(payload, "nbf", .OrderedDescending, .ImmatureSignature, "Not before claim (nbf) must be an integer") ??
7-
validateDate(payload, "iat", .OrderedDescending, .InvalidIssuedAt, "Issued at claim (iat) must be an integer")
4+
return validateIssuer(payload, issuer: issuer) ?? validateAudience(payload, audience: audience) ??
5+
validateDate(payload, key: "exp", comparison: .OrderedAscending, failure: .ExpiredSignature, decodeError: "Expiration time claim (exp) must be an integer") ??
6+
validateDate(payload, key: "nbf", comparison: .OrderedDescending, failure: .ImmatureSignature, decodeError: "Not before claim (nbf) must be an integer") ??
7+
validateDate(payload, key: "iat", comparison: .OrderedDescending, failure: .InvalidIssuedAt, decodeError: "Issued at claim (iat) must be an integer")
88
}
99

1010
func validateAudience(payload:Payload, audience:String?) -> InvalidToken? {
1111
if let audience = audience {
1212
if let aud = payload["aud"] as? [String] {
13-
if !contains(aud, audience) {
13+
if !aud.contains(audience) {
1414
return .InvalidAudience
1515
}
1616
} else if let aud = payload["aud"] as? String {
@@ -45,7 +45,7 @@ func validateDate(payload:Payload, key:String, comparison:NSComparisonResult, fa
4545
if date.compare(NSDate()) == comparison {
4646
return failure
4747
}
48-
} else if let timestamp:AnyObject = payload[key] {
48+
} else if payload[key] != nil {
4949
return .DecodeError(decodeError)
5050
}
5151

JWT/Decode.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -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
7575
public 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

127127
func 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
}

JWT/JWT.swift

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import CryptoSwift
44
public typealias Payload = [String:AnyObject]
55

66
/// The supported Algorithms
7-
public enum Algorithm : Printable {
7+
public enum Algorithm : CustomStringConvertible {
88
/// No Algorithm, i-e, insecure
99
case None
1010

@@ -19,7 +19,7 @@ public enum Algorithm : Printable {
1919

2020
static func algorithm(name:String, key:String?) -> Algorithm? {
2121
if name == "none" {
22-
if let key = key {
22+
if key != nil {
2323
return nil // We don't allow nil when we configured a key
2424
}
2525
return Algorithm.None
@@ -40,11 +40,11 @@ public enum Algorithm : Printable {
4040
switch self {
4141
case .None:
4242
return "none"
43-
case .HS256(let key):
43+
case .HS256:
4444
return "HS256"
45-
case .HS384(let key):
45+
case .HS384:
4646
return "HS384"
47-
case .HS512(let key):
47+
case .HS512:
4848
return "HS512"
4949
}
5050
}
@@ -64,13 +64,13 @@ public enum Algorithm : Printable {
6464
return ""
6565

6666
case .HS256(let key):
67-
return signHS(key, .sha256)
67+
return signHS(key, variant: .sha256)
6868

6969
case .HS384(let key):
70-
return signHS(key, .sha384)
70+
return signHS(key, variant: .sha384)
7171

7272
case .HS512(let key):
73-
return signHS(key, .sha512)
73+
return signHS(key, variant: .sha512)
7474
}
7575
}
7676

@@ -83,13 +83,13 @@ public enum Algorithm : Printable {
8383
// MARK: Encoding
8484

8585
/*** Encode a payload
86-
:param: payload The payload to sign
87-
:param: algorithm The algorithm to sign the payload with
88-
:returns: The JSON web token as a String
86+
- parameter payload: The payload to sign
87+
- parameter algorithm: The algorithm to sign the payload with
88+
- returns: The JSON web token as a String
8989
*/
9090
public func encode(payload:Payload, algorithm:Algorithm) -> String {
9191
func encodeJSON(payload:Payload) -> String? {
92-
if let data = NSJSONSerialization.dataWithJSONObject(payload, options: NSJSONWritingOptions(0), error: nil) {
92+
if let data = try? NSJSONSerialization.dataWithJSONObject(payload, options: NSJSONWritingOptions(rawValue: 0)) {
9393
return base64encode(data)
9494
}
9595

@@ -176,5 +176,5 @@ public class PayloadBuilder {
176176
public func encode(algorithm:Algorithm, closure:(PayloadBuilder -> ())) -> String {
177177
let builder = PayloadBuilder()
178178
closure(builder)
179-
return encode(builder.payload, algorithm)
179+
return encode(builder.payload, algorithm: algorithm)
180180
}

0 commit comments

Comments
 (0)