Skip to content

Commit 69866e1

Browse files
committed
refactor: Implement Compact JSON Coders
1 parent 7be4e46 commit 69866e1

File tree

5 files changed

+30
-2
lines changed

5 files changed

+30
-2
lines changed

JWT.xcodeproj/project.pbxproj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
271E10821F90253300B5033C /* JWA.swift in Sources */ = {isa = PBXBuildFile; fileRef = 271E107F1F90253300B5033C /* JWA.swift */; };
1313
271E10831F90253300B5033C /* JWA.swift in Sources */ = {isa = PBXBuildFile; fileRef = 271E107F1F90253300B5033C /* JWA.swift */; };
1414
271E10851F90274A00B5033C /* JOSEHeaderTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 271E10841F90274A00B5033C /* JOSEHeaderTests.swift */; };
15+
271E10891F90334B00B5033C /* CompactJSONEncoder.swift in Sources */ = {isa = PBXBuildFile; fileRef = 271E10881F90334B00B5033C /* CompactJSONEncoder.swift */; };
16+
271E108B1F9034B100B5033C /* CompactJSONDecoder.swift in Sources */ = {isa = PBXBuildFile; fileRef = 271E108A1F9034B100B5033C /* CompactJSONDecoder.swift */; };
1517
273010FF1F33EABA00219C35 /* HMAC.swift in Sources */ = {isa = PBXBuildFile; fileRef = 273010FE1F33EABA00219C35 /* HMAC.swift */; };
1618
273011001F33EABA00219C35 /* HMAC.swift in Sources */ = {isa = PBXBuildFile; fileRef = 273010FE1F33EABA00219C35 /* HMAC.swift */; };
1719
273011011F33EABA00219C35 /* HMAC.swift in Sources */ = {isa = PBXBuildFile; fileRef = 273010FE1F33EABA00219C35 /* HMAC.swift */; };
@@ -69,6 +71,8 @@
6971
/* Begin PBXFileReference section */
7072
271E107F1F90253300B5033C /* JWA.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = JWA.swift; sourceTree = "<group>"; };
7173
271E10841F90274A00B5033C /* JOSEHeaderTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = JOSEHeaderTests.swift; sourceTree = "<group>"; };
74+
271E10881F90334B00B5033C /* CompactJSONEncoder.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CompactJSONEncoder.swift; sourceTree = "<group>"; };
75+
271E108A1F9034B100B5033C /* CompactJSONDecoder.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CompactJSONDecoder.swift; sourceTree = "<group>"; };
7276
273010FE1F33EABA00219C35 /* HMAC.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = HMAC.swift; sourceTree = "<group>"; };
7377
273011041F33FC5F00219C35 /* HMACCommonCrypto.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = HMACCommonCrypto.swift; sourceTree = "<group>"; };
7478
273011091F33FC9100219C35 /* HMACCryptoSwift.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = HMACCryptoSwift.swift; sourceTree = "<group>"; };
@@ -138,6 +142,8 @@
138142
children = (
139143
277794041DF221F800573F3E /* ClaimSet.swift */,
140144
2777940A1DF22BE400573F3E /* JOSEHeader.swift */,
145+
271E10881F90334B00B5033C /* CompactJSONEncoder.swift */,
146+
271E108A1F9034B100B5033C /* CompactJSONDecoder.swift */,
141147
520A71131C469F010005C709 /* Base64.swift */,
142148
520A71141C469F010005C709 /* Claims.swift */,
143149
520A71151C469F010005C709 /* Decode.swift */,
@@ -445,12 +451,14 @@
445451
271E10801F90253300B5033C /* JWA.swift in Sources */,
446452
273011161F34029900219C35 /* HMACCommonCrypto.swift in Sources */,
447453
520A71181C469F010005C709 /* Claims.swift in Sources */,
454+
271E10891F90334B00B5033C /* CompactJSONEncoder.swift in Sources */,
448455
520A711A1C469F010005C709 /* JWT.swift in Sources */,
449456
520A71191C469F010005C709 /* Decode.swift in Sources */,
450457
277794101DF22D0D00573F3E /* Encode.swift in Sources */,
451458
2777940B1DF22BE400573F3E /* JOSEHeader.swift in Sources */,
452459
277794051DF221F800573F3E /* ClaimSet.swift in Sources */,
453460
273010FF1F33EABA00219C35 /* HMAC.swift in Sources */,
461+
271E108B1F9034B100B5033C /* CompactJSONDecoder.swift in Sources */,
454462
520A71171C469F010005C709 /* Base64.swift in Sources */,
455463
);
456464
runOnlyForDeploymentPostprocessing = 0;

Sources/JWT/Base64.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ func base64encode(_ input: Data) -> String {
1212

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

1717
var ending = ""
1818
if rem > 0 {

Sources/JWT/CompactJSONDecoder.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class CompactJSONDecoder: JSONDecoder {
2+
override func decode<T>(_ type: T.Type, from data: Data) throws -> T where T : Decodable {
3+
guard let string = String(data: data, encoding: .ascii) else {
4+
fatalError()
5+
}
6+
7+
return try super.decode(type, from: base64decode(string)!)
8+
}
9+
}

Sources/JWT/CompactJSONEncoder.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class CompactJSONEncoder: JSONEncoder {
2+
override func encode<T : Encodable>(_ value: T) throws -> Data {
3+
return try encodeString(value).data(using: .ascii) ?? Data()
4+
}
5+
6+
func encodeString<T: Encodable>(_ value: T) throws -> String {
7+
return base64encode(try super.encode(value))
8+
}
9+
}

Sources/JWT/Encode.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import Foundation
66
- returns: The JSON web token as a String
77
*/
88
public func encode(claims: ClaimSet, algorithm: Algorithm, headers: [String: String]? = nil) -> String {
9+
let encoder = CompactJSONEncoder()
10+
911
func encodeJSON(_ payload: [String: Any]) -> String? {
1012
if let data = try? JSONSerialization.data(withJSONObject: payload) {
1113
return base64encode(data)
@@ -20,7 +22,7 @@ public func encode(claims: ClaimSet, algorithm: Algorithm, headers: [String: Str
2022
}
2123
headers["alg"] = algorithm.description
2224

23-
let header = encodeJSON(headers)!
25+
let header = try! encoder.encodeString(headers)
2426
let payload = encodeJSON(claims.claims)!
2527
let signingInput = "\(header).\(payload)"
2628
let signature = algorithm.sign(signingInput)

0 commit comments

Comments
 (0)