Skip to content

Commit fc87491

Browse files
committed
Fix issues with Linux. Add tests and Dockerfile for testing on Linux.
1 parent 1e3d591 commit fc87491

File tree

6 files changed

+73
-14
lines changed

6 files changed

+73
-14
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,6 @@
11
xcuserdata/
22
*.xccheckout
3+
.DS_Store
4+
/.build
5+
/Packages
6+
/*.xcodeproj

Dockerfile

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
FROM swiftdocker/swift
2+
3+
RUN apt-get update && \
4+
apt-get install -y openssl libssl-dev && \
5+
apt-get clean
6+
7+
WORKDIR /App/
8+
9+
ADD ./Package.swift /App/
10+
RUN swift package fetch
11+
12+
ADD ./Sources /App/Sources
13+
ADD ./Tests /App/Tests
14+
15+
CMD ["swift", "test"]

Sources/Base64.swift

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

44
/// URI Safe base64 encode
55
func base64encode(_ input:Data) -> String {
6-
let data = input.base64EncodedData(options: NSData.Base64EncodingOptions(rawValue: 0))
7-
let string = NSString(data: data, encoding: String.Encoding.utf8.rawValue) as! String
6+
let data = input.base64EncodedData()
7+
let string = String(data: data, encoding: .utf8)!
8+
89
return string
9-
.replacingOccurrences(of: "+", with: "-", options: NSString.CompareOptions(rawValue: 0), range: nil)
10-
.replacingOccurrences(of: "/", with: "_", options: NSString.CompareOptions(rawValue: 0), range: nil)
11-
.replacingOccurrences(of: "=", with: "", options: NSString.CompareOptions(rawValue: 0), range: nil)
10+
.replacingOccurrences(of: "+", with: "-")
11+
.replacingOccurrences(of: "/", with: "_")
12+
.replacingOccurrences(of: "=", with: "")
1213
}
1314

1415
/// URI Safe base64 decode
@@ -21,8 +22,8 @@ func base64decode(_ input:String) -> Data? {
2122
ending = String(repeating: "=", count: amount)
2223
}
2324

24-
let base64 = input.replacingOccurrences(of: "-", with: "+", options: NSString.CompareOptions(rawValue: 0), range: nil)
25-
.replacingOccurrences(of: "_", with: "/", options: NSString.CompareOptions(rawValue: 0), range: nil) + ending
25+
let base64 = input.replacingOccurrences(of: "-", with: "+")
26+
.replacingOccurrences(of: "_", with: "/") + ending
2627

27-
return Data(base64Encoded: base64, options: NSData.Base64DecodingOptions(rawValue: 0))
28+
return Data(base64Encoded: base64)
2829
}

Sources/JWT.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public func encode(_ payload:Payload, algorithm:Algorithm) -> String {
8181
return nil
8282
}
8383

84-
let header = encodeJSON(["typ": "JWT" as AnyObject, "alg": algorithm.description as AnyObject])!
84+
let header = encodeJSON(["typ": "JWT", "alg": algorithm.description])!
8585
let payload = encodeJSON(payload)!
8686
let signingInput = "\(header).\(payload)"
8787
let signature = algorithm.sign(signingInput)
@@ -96,7 +96,7 @@ open class PayloadBuilder {
9696
return payload["iss"] as? String
9797
}
9898
set {
99-
payload["iss"] = newValue as AnyObject?
99+
payload["iss"] = newValue
100100
}
101101
}
102102

@@ -105,7 +105,7 @@ open class PayloadBuilder {
105105
return payload["aud"] as? String
106106
}
107107
set {
108-
payload["aud"] = newValue as AnyObject?
108+
payload["aud"] = newValue
109109
}
110110
}
111111

@@ -118,7 +118,7 @@ open class PayloadBuilder {
118118
return nil
119119
}
120120
set {
121-
payload["exp"] = newValue?.timeIntervalSince1970 as AnyObject?
121+
payload["exp"] = newValue?.timeIntervalSince1970
122122
}
123123
}
124124

@@ -131,7 +131,7 @@ open class PayloadBuilder {
131131
return nil
132132
}
133133
set {
134-
payload["nbf"] = newValue?.timeIntervalSince1970 as AnyObject?
134+
payload["nbf"] = newValue?.timeIntervalSince1970
135135
}
136136
}
137137

@@ -144,7 +144,7 @@ open class PayloadBuilder {
144144
return nil
145145
}
146146
set {
147-
payload["iat"] = newValue?.timeIntervalSince1970 as AnyObject?
147+
payload["iat"] = newValue?.timeIntervalSince1970
148148
}
149149
}
150150

Tests/JWTTests/JWTTests.swift

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import XCTest
2+
import Foundation
3+
@testable import JWT
4+
5+
class JWTTests: XCTestCase {
6+
static var allTests: [(String, (JWTTests) -> () throws -> ())] = [
7+
("testEncode", testEncode),
8+
("testDecode", testDecode)
9+
]
10+
11+
func testEncode() throws {
12+
let payload: Payload = ["hello": "world"]
13+
guard let secret = "secret".data(using: .utf8) else {
14+
XCTFail("Could not encoded string into UTF8")
15+
return
16+
}
17+
18+
_ = JWT.encode(payload, algorithm: .hs256(secret))
19+
}
20+
21+
func testDecode() throws {
22+
let payload: Payload = ["hello": "world"]
23+
guard let secret = "secret".data(using: .utf8) else {
24+
XCTFail("Could not encoded string into UTF8")
25+
return
26+
}
27+
28+
let token = JWT.encode(payload, algorithm: .hs256(secret))
29+
let payloadActual = try JWT.decode(token, algorithm: .hs256(secret))
30+
31+
XCTAssertEqual(payload["hello"] as? String, payloadActual["hello"] as? String)
32+
}
33+
}

Tests/LinuxMain.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import XCTest
2+
@testable import JWTTests
3+
4+
XCTMain([
5+
testCase(JWTTests.allTests)
6+
])

0 commit comments

Comments
 (0)