Skip to content

Commit 8e72a33

Browse files
committed
chore: Align code style
1 parent e36d4f0 commit 8e72a33

File tree

8 files changed

+32
-39
lines changed

8 files changed

+32
-39
lines changed

Package.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@ import PackageDescription
33
let package = Package(
44
name: "JWT",
55
dependencies: [
6-
.Package(url: "https://github.com/krzyzanowskim/CryptoSwift", versions: Version(0, 6, 1) ..< Version(0, 7, 0))
6+
.Package(url: "https://github.com/krzyzanowskim/CryptoSwift", versions: Version(0, 6, 1) ..< Version(0, 7, 0)),
77
]
88
)

Sources/Base64.swift

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import Foundation
22

3-
43
/// URI Safe base64 encode
5-
func base64encode(_ input:Data) -> String {
4+
func base64encode(_ input: Data) -> String {
65
let data = input.base64EncodedData(options: NSData.Base64EncodingOptions(rawValue: 0))
76
let string = String(data: data, encoding: .utf8)!
87
return string
@@ -12,7 +11,7 @@ func base64encode(_ input:Data) -> String {
1211
}
1312

1413
/// URI Safe base64 decode
15-
func base64decode(_ input:String) -> Data? {
14+
func base64decode(_ input: String) -> Data? {
1615
let rem = input.characters.count % 4
1716

1817
var ending = ""

Sources/Claims.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import Foundation
22

3-
func validateDate(_ payload:Payload, key:String, comparison:ComparisonResult, failure:InvalidToken, decodeError:String) throws {
3+
func validateDate(_ payload: Payload, key: String, comparison: ComparisonResult, failure: InvalidToken, decodeError: String) throws {
44
if payload[key] == nil {
55
return
66
}

Sources/Decode.swift

+3-7
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 : CustomStringConvertible, Error {
5+
public enum InvalidToken: CustomStringConvertible, Error {
66
/// Decoding the JWT itself failed
77
case decodeError(String)
88

@@ -25,7 +25,7 @@ public enum InvalidToken : CustomStringConvertible, Error {
2525
case invalidIssuer
2626

2727
/// Returns a readable description of the error
28-
public var description:String {
28+
public var description: String {
2929
switch self {
3030
case .decodeError(let error):
3131
return "Decode Error: \(error)"
@@ -58,30 +58,26 @@ public func decode(_ jwt: String, algorithms: [Algorithm], verify: Bool = true,
5858
return claims
5959
}
6060

61-
6261
/// Decode a JWT
6362
public func decode(_ jwt: String, algorithm: Algorithm, verify: Bool = true, audience: String? = nil, issuer: String? = nil) throws -> ClaimSet {
6463
return try decode(jwt, algorithms: [algorithm], verify: verify, audience: audience, issuer: issuer)
6564
}
6665

67-
6866
/// Decode a JWT
6967
@available(*, deprecated, message: "use decode that returns a ClaimSet instead")
7068
public func decode(_ jwt: String, algorithms: [Algorithm], verify: Bool = true, audience: String? = nil, issuer: String? = nil) throws -> Payload {
7169
return try decode(jwt, algorithms: algorithms, verify: verify, audience: audience, issuer: issuer).claims
7270
}
7371

74-
7572
/// Decode a JWT
7673
@available(*, deprecated, message: "use decode that returns a ClaimSet instead")
7774
public func decode(_ jwt: String, algorithm: Algorithm, verify: Bool = true, audience: String? = nil, issuer: String? = nil) throws -> Payload {
7875
return try decode(jwt, algorithms: [algorithm], verify: verify, audience: audience, issuer: issuer).claims
7976
}
8077

81-
8278
// MARK: Parsing a JWT
8379

84-
func load(_ jwt:String) throws -> (header: JOSEHeader, payload: ClaimSet, signature: Data, signatureInput: String) {
80+
func load(_ jwt: String) throws -> (header: JOSEHeader, payload: ClaimSet, signature: Data, signatureInput: String) {
8581
let segments = jwt.components(separatedBy: ".")
8682
if segments.count != 3 {
8783
throw InvalidToken.decodeError("Not enough segments")

Sources/Encode.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public func encode(claims: [String: Any], algorithm: Algorithm) -> String {
3030

3131

3232
/// Encode a set of claims using the builder pattern
33-
public func encode(_ algorithm: Algorithm, closure: ((ClaimSetBuilder) -> ())) -> String {
33+
public func encode(_ algorithm: Algorithm, closure: ((ClaimSetBuilder) -> Void)) -> String {
3434
let builder = ClaimSetBuilder()
3535
closure(builder)
3636
return encode(claims: builder.claims, algorithm: algorithm)

Sources/JWT.swift

+6-6
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import CryptoSwift
44
public typealias Payload = [String: Any]
55

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

@@ -17,7 +17,7 @@ public enum Algorithm : CustomStringConvertible {
1717
/// HMAC using SHA-512 hash algorithm
1818
case hs512(Data)
1919

20-
public var description:String {
20+
public var description: String {
2121
switch self {
2222
case .none:
2323
return "none"
@@ -31,10 +31,10 @@ public enum Algorithm : CustomStringConvertible {
3131
}
3232

3333
/// Sign a message using the algorithm
34-
func sign(_ message:String) -> String {
35-
func signHS(_ key: Data, variant:CryptoSwift.HMAC.Variant) -> String {
34+
func sign(_ message: String) -> String {
35+
func signHS(_ key: Data, variant: CryptoSwift.HMAC.Variant) -> String {
3636
let messageData = message.data(using: String.Encoding.utf8, allowLossyConversion: false)!
37-
let mac = HMAC(key: key.bytes, variant:variant)
37+
let mac = HMAC(key: key.bytes, variant: variant)
3838
let result: [UInt8]
3939
do {
4040
result = try mac.authenticate(messageData.bytes)
@@ -60,7 +60,7 @@ public enum Algorithm : CustomStringConvertible {
6060
}
6161

6262
/// Verify a signature for a message using the algorithm
63-
func verify(_ message:String, signature:Data) -> Bool {
63+
func verify(_ message: String, signature: Data) -> Bool {
6464
return sign(message) == base64encode(signature)
6565
}
6666
}

Tests/JWTTests/JWTTests.swift

+15-15
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class EncodeTests: XCTestCase {
3333
class PayloadTests: XCTestCase {
3434
func testIssuer() {
3535
_ = JWT.encode(.none) { builder in
36-
builder.issuer = "fuller.li"
36+
builder.issuer = "fuller.li"
3737
XCTAssertEqual(builder.issuer, "fuller.li")
3838
XCTAssertEqual(builder["iss"] as? String, "fuller.li")
3939
}
@@ -106,26 +106,26 @@ class DecodeTests: XCTestCase {
106106

107107
func testDisablingVerify() {
108108
let jwt = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.e30.2_8pWJfyPup0YwOXK7g9Dn0cF1E3pdn299t4hSeJy5w"
109-
assertSuccess(try decode(jwt, algorithm: .none, verify:false, issuer:"fuller.li"))
109+
assertSuccess(try decode(jwt, algorithm: .none, verify: false, issuer: "fuller.li"))
110110
}
111111

112112
// MARK: Issuer claim
113113

114114
func testSuccessfulIssuerValidation() {
115115
let jwt = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJmdWxsZXIubGkifQ.d7B7PAQcz1E6oNhrlxmHxHXHgg39_k7X7wWeahl8kSQ"
116-
assertSuccess(try decode(jwt, algorithm: .hs256("secret".data(using: .utf8)!), issuer:"fuller.li")) { payload in
116+
assertSuccess(try decode(jwt, algorithm: .hs256("secret".data(using: .utf8)!), issuer: "fuller.li")) { payload in
117117
XCTAssertEqual(payload as! [String: String], ["iss": "fuller.li"])
118118
}
119119
}
120120

121121
func testIncorrectIssuerValidation() {
122122
let jwt = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJmdWxsZXIubGkifQ.wOhJ9_6lx-3JGJPmJmtFCDI3kt7uMAMmhHIslti7ryI"
123-
assertFailure(try decode(jwt, algorithm: .hs256("secret".data(using: .utf8)!), issuer:"querykit.org"))
123+
assertFailure(try decode(jwt, algorithm: .hs256("secret".data(using: .utf8)!), issuer: "querykit.org"))
124124
}
125125

126126
func testMissingIssuerValidation() {
127127
let jwt = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.e30.2_8pWJfyPup0YwOXK7g9Dn0cF1E3pdn299t4hSeJy5w"
128-
assertFailure(try decode(jwt, algorithm: .hs256("secret".data(using: .utf8)!), issuer:"fuller.li"))
128+
assertFailure(try decode(jwt, algorithm: .hs256("secret".data(using: .utf8)!), issuer: "fuller.li"))
129129
}
130130

131131
// MARK: Expiration claim
@@ -147,7 +147,7 @@ class DecodeTests: XCTestCase {
147147
XCTAssertEqual(payload as! [String: Int], ["exp": 1728188491])
148148
}
149149
}
150-
150+
151151
func testUnexpiredClaimString() {
152152
// If this just started failing, hello 2024!
153153
let jwt = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOiIxNzI4MTg4NDkxIn0.y4w7lNLrfRRPzuNUfM-ZvPkoOtrTU_d8ZVYasLdZGpk"
@@ -164,7 +164,7 @@ class DecodeTests: XCTestCase {
164164
XCTAssertEqual(payload as! [String: Int], ["nbf": 1428189720])
165165
}
166166
}
167-
167+
168168
func testNotBeforeClaimString() {
169169
let jwt = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYmYiOiIxNDI4MTg5NzIwIn0.qZsj36irdmIAeXv6YazWDSFbpuxHtEh4Deof5YTpnVI"
170170
assertSuccess(try decode(jwt, algorithm: .hs256("secret".data(using: .utf8)!))) { payload in
@@ -215,34 +215,34 @@ class DecodeTests: XCTestCase {
215215

216216
func testAudiencesClaim() {
217217
let jwt = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhdWQiOlsibWF4aW5lIiwia2F0aWUiXX0.-PKvdNLCClrWG7CvesHP6PB0-vxu-_IZcsYhJxBy5JM"
218-
assertSuccess(try decode(jwt, algorithm: .hs256("secret".data(using: .utf8)!), audience:"maxine")) { payload in
218+
assertSuccess(try decode(jwt, algorithm: .hs256("secret".data(using: .utf8)!), audience: "maxine")) { payload in
219219
XCTAssertEqual(payload.count, 1)
220220
XCTAssertEqual(payload["aud"] as! [String], ["maxine", "katie"])
221221
}
222222
}
223223

224224
func testAudienceClaim() {
225225
let jwt = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhdWQiOiJreWxlIn0.dpgH4JOwueReaBoanLSxsGTc7AjKUvo7_M1sAfy_xVE"
226-
assertSuccess(try decode(jwt, algorithm: .hs256("secret".data(using: .utf8)!), audience:"kyle")) { payload in
226+
assertSuccess(try decode(jwt, algorithm: .hs256("secret".data(using: .utf8)!), audience: "kyle")) { payload in
227227
XCTAssertEqual(payload as! [String: String], ["aud": "kyle"])
228228
}
229229
}
230230

231231
func testMismatchAudienceClaim() {
232232
let jwt = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhdWQiOiJreWxlIn0.VEB_n06pTSLlTXPFkc46ARADJ9HXNUBUPo3VhL9RDe4" // kyle
233-
assertFailure(try decode(jwt, algorithm: .hs256("secret".data(using: .utf8)!), audience:"maxine"))
233+
assertFailure(try decode(jwt, algorithm: .hs256("secret".data(using: .utf8)!), audience: "maxine"))
234234
}
235235

236236
func testMissingAudienceClaim() {
237237
let jwt = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.e30.2_8pWJfyPup0YwOXK7g9Dn0cF1E3pdn299t4hSeJy5w"
238-
assertFailure(try decode(jwt, algorithm: .hs256("secret".data(using: .utf8)!), audience:"kyle"))
238+
assertFailure(try decode(jwt, algorithm: .hs256("secret".data(using: .utf8)!), audience: "kyle"))
239239
}
240240

241241
// MARK: Signature verification
242242

243243
func testNoneAlgorithm() {
244244
let jwt = "eyJhbGciOiJub25lIiwidHlwIjoiSldUIn0.eyJ0ZXN0IjoiaW5nIn0."
245-
assertSuccess(try decode(jwt, algorithm:.none)) { payload in
245+
assertSuccess(try decode(jwt, algorithm: .none)) { payload in
246246
XCTAssertEqual(payload as! [String: String], ["test": "ing"])
247247
}
248248
}
@@ -274,7 +274,7 @@ class DecodeTests: XCTestCase {
274274

275275
// MARK: Helpers
276276

277-
func assertSuccess(_ decoder: @autoclosure () throws -> Payload, closure:((Payload) -> ())? = nil) {
277+
func assertSuccess(_ decoder: @autoclosure () throws -> Payload, closure: ((Payload) -> Void)? = nil) {
278278
do {
279279
let payload = try decoder()
280280
closure?(payload)
@@ -283,7 +283,7 @@ func assertSuccess(_ decoder: @autoclosure () throws -> Payload, closure:((Paylo
283283
}
284284
}
285285

286-
func assertFailure(_ decoder: @autoclosure () throws -> Payload, closure:((InvalidToken) -> ())? = nil) {
286+
func assertFailure(_ decoder: @autoclosure () throws -> Payload, closure: ((InvalidToken) -> Void)? = nil) {
287287
do {
288288
_ = try decoder()
289289
XCTFail("Decoding succeeded, expected a failure.")
@@ -294,7 +294,7 @@ func assertFailure(_ decoder: @autoclosure () throws -> Payload, closure:((Inval
294294
}
295295
}
296296

297-
func assertDecodeError(_ decoder:@autoclosure () throws -> Payload, error:String) {
297+
func assertDecodeError(_ decoder: @autoclosure () throws -> Payload, error: String) {
298298
assertFailure(try decoder()) { failure in
299299
switch failure {
300300
case .decodeError(let decodeError):

Tests/LinuxMain.swift

+3-5
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,16 @@ import XCTest
33

44

55
extension EncodeTests {
6-
static var allTests : [(String, (EncodeTests) -> () throws -> Void)] {
6+
static var allTests: [(String, (EncodeTests) -> Void throws -> Void)] {
77
return [
88
("testEncodingJWT", testEncodingJWT),
99
("testEncodingWithBuilder", testEncodingWithBuilder),
1010
]
1111
}
1212
}
1313

14-
1514
extension DecodeTests {
16-
static var allTests : [(String, (DecodeTests) -> () throws -> Void)] {
15+
static var allTests: [(String, (DecodeTests) -> Void throws -> Void)] {
1716
return [
1817
("testDecodingValidJWT", testDecodingValidJWT),
1918
("testFailsToDecodeInvalidStringWithoutThreeSegments", testFailsToDecodeInvalidStringWithoutThreeSegments),
@@ -46,9 +45,8 @@ extension DecodeTests {
4645
}
4746
}
4847

49-
5048
extension PayloadTests {
51-
static var allTests : [(String, (PayloadTests) -> () throws -> Void)] {
49+
static var allTests: [(String, (PayloadTests) -> Void throws -> Void)] {
5250
return [
5351
("testIssuer", testIssuer),
5452
("testAudience", testAudience),

0 commit comments

Comments
 (0)