From fc87491745ad9456ccae7effa3f380a9b603bc02 Mon Sep 17 00:00:00 2001 From: Jeremy Jacobson Date: Tue, 15 Nov 2016 23:22:25 -0800 Subject: [PATCH] Fix issues with Linux. Add tests and Dockerfile for testing on Linux. --- .gitignore | 4 ++++ Dockerfile | 15 +++++++++++++++ Sources/Base64.swift | 17 +++++++++-------- Sources/JWT.swift | 12 ++++++------ Tests/JWTTests/JWTTests.swift | 33 +++++++++++++++++++++++++++++++++ Tests/LinuxMain.swift | 6 ++++++ 6 files changed, 73 insertions(+), 14 deletions(-) create mode 100644 Dockerfile create mode 100644 Tests/JWTTests/JWTTests.swift create mode 100644 Tests/LinuxMain.swift diff --git a/.gitignore b/.gitignore index 2295c9c..d5d6874 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,6 @@ xcuserdata/ *.xccheckout +.DS_Store +/.build +/Packages +/*.xcodeproj \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..e3a9e48 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,15 @@ +FROM swiftdocker/swift + +RUN apt-get update && \ + apt-get install -y openssl libssl-dev && \ + apt-get clean + +WORKDIR /App/ + +ADD ./Package.swift /App/ +RUN swift package fetch + +ADD ./Sources /App/Sources +ADD ./Tests /App/Tests + +CMD ["swift", "test"] \ No newline at end of file diff --git a/Sources/Base64.swift b/Sources/Base64.swift index 32b18cf..220ae0a 100644 --- a/Sources/Base64.swift +++ b/Sources/Base64.swift @@ -3,12 +3,13 @@ import Foundation /// URI Safe base64 encode func base64encode(_ input:Data) -> String { - let data = input.base64EncodedData(options: NSData.Base64EncodingOptions(rawValue: 0)) - let string = NSString(data: data, encoding: String.Encoding.utf8.rawValue) as! String + let data = input.base64EncodedData() + let string = String(data: data, encoding: .utf8)! + return string - .replacingOccurrences(of: "+", with: "-", options: NSString.CompareOptions(rawValue: 0), range: nil) - .replacingOccurrences(of: "/", with: "_", options: NSString.CompareOptions(rawValue: 0), range: nil) - .replacingOccurrences(of: "=", with: "", options: NSString.CompareOptions(rawValue: 0), range: nil) + .replacingOccurrences(of: "+", with: "-") + .replacingOccurrences(of: "/", with: "_") + .replacingOccurrences(of: "=", with: "") } /// URI Safe base64 decode @@ -21,8 +22,8 @@ func base64decode(_ input:String) -> Data? { ending = String(repeating: "=", count: amount) } - let base64 = input.replacingOccurrences(of: "-", with: "+", options: NSString.CompareOptions(rawValue: 0), range: nil) - .replacingOccurrences(of: "_", with: "/", options: NSString.CompareOptions(rawValue: 0), range: nil) + ending + let base64 = input.replacingOccurrences(of: "-", with: "+") + .replacingOccurrences(of: "_", with: "/") + ending - return Data(base64Encoded: base64, options: NSData.Base64DecodingOptions(rawValue: 0)) + return Data(base64Encoded: base64) } diff --git a/Sources/JWT.swift b/Sources/JWT.swift index 84f4809..078957f 100644 --- a/Sources/JWT.swift +++ b/Sources/JWT.swift @@ -81,7 +81,7 @@ public func encode(_ payload:Payload, algorithm:Algorithm) -> String { return nil } - let header = encodeJSON(["typ": "JWT" as AnyObject, "alg": algorithm.description as AnyObject])! + let header = encodeJSON(["typ": "JWT", "alg": algorithm.description])! let payload = encodeJSON(payload)! let signingInput = "\(header).\(payload)" let signature = algorithm.sign(signingInput) @@ -96,7 +96,7 @@ open class PayloadBuilder { return payload["iss"] as? String } set { - payload["iss"] = newValue as AnyObject? + payload["iss"] = newValue } } @@ -105,7 +105,7 @@ open class PayloadBuilder { return payload["aud"] as? String } set { - payload["aud"] = newValue as AnyObject? + payload["aud"] = newValue } } @@ -118,7 +118,7 @@ open class PayloadBuilder { return nil } set { - payload["exp"] = newValue?.timeIntervalSince1970 as AnyObject? + payload["exp"] = newValue?.timeIntervalSince1970 } } @@ -131,7 +131,7 @@ open class PayloadBuilder { return nil } set { - payload["nbf"] = newValue?.timeIntervalSince1970 as AnyObject? + payload["nbf"] = newValue?.timeIntervalSince1970 } } @@ -144,7 +144,7 @@ open class PayloadBuilder { return nil } set { - payload["iat"] = newValue?.timeIntervalSince1970 as AnyObject? + payload["iat"] = newValue?.timeIntervalSince1970 } } diff --git a/Tests/JWTTests/JWTTests.swift b/Tests/JWTTests/JWTTests.swift new file mode 100644 index 0000000..0434ad0 --- /dev/null +++ b/Tests/JWTTests/JWTTests.swift @@ -0,0 +1,33 @@ +import XCTest +import Foundation +@testable import JWT + +class JWTTests: XCTestCase { + static var allTests: [(String, (JWTTests) -> () throws -> ())] = [ + ("testEncode", testEncode), + ("testDecode", testDecode) + ] + + func testEncode() throws { + let payload: Payload = ["hello": "world"] + guard let secret = "secret".data(using: .utf8) else { + XCTFail("Could not encoded string into UTF8") + return + } + + _ = JWT.encode(payload, algorithm: .hs256(secret)) + } + + func testDecode() throws { + let payload: Payload = ["hello": "world"] + guard let secret = "secret".data(using: .utf8) else { + XCTFail("Could not encoded string into UTF8") + return + } + + let token = JWT.encode(payload, algorithm: .hs256(secret)) + let payloadActual = try JWT.decode(token, algorithm: .hs256(secret)) + + XCTAssertEqual(payload["hello"] as? String, payloadActual["hello"] as? String) + } +} diff --git a/Tests/LinuxMain.swift b/Tests/LinuxMain.swift new file mode 100644 index 0000000..7a9ddcf --- /dev/null +++ b/Tests/LinuxMain.swift @@ -0,0 +1,6 @@ +import XCTest +@testable import JWTTests + +XCTMain([ + testCase(JWTTests.allTests) +]) \ No newline at end of file