Skip to content

Commit 48cd4d6

Browse files
committed
refactor: Deprecate encode without claims: label
1 parent f5c79ff commit 48cd4d6

File tree

3 files changed

+58
-40
lines changed

3 files changed

+58
-40
lines changed

JWT.xcodeproj/project.pbxproj

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@
1818
2777940C1DF22BE400573F3E /* JOSEHeader.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2777940A1DF22BE400573F3E /* JOSEHeader.swift */; };
1919
2777940D1DF22BE400573F3E /* JOSEHeader.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2777940A1DF22BE400573F3E /* JOSEHeader.swift */; };
2020
2777940E1DF22BE400573F3E /* JOSEHeader.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2777940A1DF22BE400573F3E /* JOSEHeader.swift */; };
21+
277794101DF22D0D00573F3E /* Encode.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2777940F1DF22D0D00573F3E /* Encode.swift */; };
22+
277794111DF22D0D00573F3E /* Encode.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2777940F1DF22D0D00573F3E /* Encode.swift */; };
23+
277794121DF22D0D00573F3E /* Encode.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2777940F1DF22D0D00573F3E /* Encode.swift */; };
24+
277794131DF22D0D00573F3E /* Encode.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2777940F1DF22D0D00573F3E /* Encode.swift */; };
2125
279D63A21AD07FFF0024E2BC /* JWT.h in Headers */ = {isa = PBXBuildFile; fileRef = 279D63A11AD07FFF0024E2BC /* JWT.h */; settings = {ATTRIBUTES = (Public, ); }; };
2226
279D63A81AD07FFF0024E2BC /* JWT.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 279D639C1AD07FFF0024E2BC /* JWT.framework */; };
2327
279D63AF1AD07FFF0024E2BC /* JWTTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 279D63AE1AD07FFF0024E2BC /* JWTTests.swift */; };
@@ -77,6 +81,7 @@
7781
/* Begin PBXFileReference section */
7882
277794041DF221F800573F3E /* ClaimSet.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ClaimSet.swift; sourceTree = "<group>"; };
7983
2777940A1DF22BE400573F3E /* JOSEHeader.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = JOSEHeader.swift; sourceTree = "<group>"; };
84+
2777940F1DF22D0D00573F3E /* Encode.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Encode.swift; sourceTree = "<group>"; };
8085
279D639C1AD07FFF0024E2BC /* JWT.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = JWT.framework; sourceTree = BUILT_PRODUCTS_DIR; };
8186
279D63A01AD07FFF0024E2BC /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
8287
279D63A11AD07FFF0024E2BC /* JWT.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = JWT.h; sourceTree = "<group>"; };
@@ -211,6 +216,7 @@
211216
520A71131C469F010005C709 /* Base64.swift */,
212217
520A71141C469F010005C709 /* Claims.swift */,
213218
520A71151C469F010005C709 /* Decode.swift */,
219+
2777940F1DF22D0D00573F3E /* Encode.swift */,
214220
520A71161C469F010005C709 /* JWT.swift */,
215221
);
216222
path = Sources;
@@ -472,6 +478,7 @@
472478
520A71181C469F010005C709 /* Claims.swift in Sources */,
473479
520A711A1C469F010005C709 /* JWT.swift in Sources */,
474480
520A71191C469F010005C709 /* Decode.swift in Sources */,
481+
277794101DF22D0D00573F3E /* Encode.swift in Sources */,
475482
2777940B1DF22BE400573F3E /* JOSEHeader.swift in Sources */,
476483
277794051DF221F800573F3E /* ClaimSet.swift in Sources */,
477484
520A71171C469F010005C709 /* Base64.swift in Sources */,
@@ -493,6 +500,7 @@
493500
CD9B62171C7753D8005D4844 /* Claims.swift in Sources */,
494501
CD9B62181C7753D8005D4844 /* JWT.swift in Sources */,
495502
CD9B62191C7753D8005D4844 /* Decode.swift in Sources */,
503+
277794111DF22D0D00573F3E /* Encode.swift in Sources */,
496504
2777940C1DF22BE400573F3E /* JOSEHeader.swift in Sources */,
497505
277794061DF221F800573F3E /* ClaimSet.swift in Sources */,
498506
CD9B621A1C7753D8005D4844 /* Base64.swift in Sources */,
@@ -506,6 +514,7 @@
506514
CD9B62291C7753EC005D4844 /* Claims.swift in Sources */,
507515
CD9B622A1C7753EC005D4844 /* JWT.swift in Sources */,
508516
CD9B622B1C7753EC005D4844 /* Decode.swift in Sources */,
517+
277794121DF22D0D00573F3E /* Encode.swift in Sources */,
509518
2777940D1DF22BE400573F3E /* JOSEHeader.swift in Sources */,
510519
277794071DF221F800573F3E /* ClaimSet.swift in Sources */,
511520
CD9B622C1C7753EC005D4844 /* Base64.swift in Sources */,
@@ -519,6 +528,7 @@
519528
CD9B623B1C7753FB005D4844 /* Claims.swift in Sources */,
520529
CD9B623C1C7753FB005D4844 /* JWT.swift in Sources */,
521530
CD9B623D1C7753FB005D4844 /* Decode.swift in Sources */,
531+
277794131DF22D0D00573F3E /* Encode.swift in Sources */,
522532
2777940E1DF22BE400573F3E /* JOSEHeader.swift in Sources */,
523533
277794081DF221F800573F3E /* ClaimSet.swift in Sources */,
524534
CD9B623E1C7753FB005D4844 /* Base64.swift in Sources */,

Sources/Encode.swift

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*** Encode a set of claims
2+
- parameter claims: The set of claims
3+
- parameter algorithm: The algorithm to sign the payload with
4+
- returns: The JSON web token as a String
5+
*/
6+
public func encode(claims: ClaimSet, algorithm: Algorithm) -> String {
7+
func encodeJSON(_ payload: [String: Any]) -> String? {
8+
if let data = try? JSONSerialization.data(withJSONObject: payload) {
9+
return base64encode(data)
10+
}
11+
12+
return nil
13+
}
14+
15+
let header = encodeJSON(["typ": "JWT", "alg": algorithm.description])!
16+
let payload = encodeJSON(claims.claims)!
17+
let signingInput = "\(header).\(payload)"
18+
let signature = algorithm.sign(signingInput)
19+
return "\(signingInput).\(signature)"
20+
}
21+
22+
/*** Encode a dictionary of claims
23+
- parameter claims: The dictionary of claims
24+
- parameter algorithm: The algorithm to sign the payload with
25+
- returns: The JSON web token as a String
26+
*/
27+
public func encode(claims: [String: Any], algorithm: Algorithm) -> String {
28+
return encode(claims: ClaimSet(claims: claims), algorithm: algorithm)
29+
}
30+
31+
32+
/// Encode a set of claims using the builder pattern
33+
public func encode(_ algorithm: Algorithm, closure: ((ClaimSetBuilder) -> ())) -> String {
34+
let builder = ClaimSetBuilder()
35+
closure(builder)
36+
return encode(claims: builder.claims, algorithm: algorithm)
37+
}
38+
39+
40+
/*** Encode a payload
41+
- parameter payload: The payload to sign
42+
- parameter algorithm: The algorithm to sign the payload with
43+
- returns: The JSON web token as a String
44+
*/
45+
@available(*, deprecated, message: "use encode(claims: algorithm:) instead")
46+
public func encode(_ payload: Payload, algorithm: Algorithm) -> String {
47+
return encode(claims: ClaimSet(claims: payload), algorithm: algorithm)
48+
}

Sources/JWT.swift

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -64,43 +64,3 @@ public enum Algorithm : CustomStringConvertible {
6464
return sign(message) == base64encode(signature)
6565
}
6666
}
67-
68-
// MARK: Encoding
69-
70-
/*** Encode a set of claims
71-
- parameter claims: The ClaiMSet to sign
72-
- parameter algorithm: The algorithm to sign the payload with
73-
- returns: The JSON web token as a String
74-
*/
75-
public func encode(claims: ClaimSet, algorithm: Algorithm) -> String {
76-
func encodeJSON(_ payload: [String: Any]) -> String? {
77-
if let data = try? JSONSerialization.data(withJSONObject: payload) {
78-
return base64encode(data)
79-
}
80-
81-
return nil
82-
}
83-
84-
let header = encodeJSON(["typ": "JWT", "alg": algorithm.description])!
85-
let payload = encodeJSON(claims.claims)!
86-
let signingInput = "\(header).\(payload)"
87-
let signature = algorithm.sign(signingInput)
88-
return "\(signingInput).\(signature)"
89-
}
90-
91-
92-
/*** Encode a payload
93-
- parameter payload: The payload to sign
94-
- parameter algorithm: The algorithm to sign the payload with
95-
- returns: The JSON web token as a String
96-
*/
97-
public func encode(_ payload: Payload, algorithm: Algorithm) -> String {
98-
return encode(claims: ClaimSet(claims: payload), algorithm: algorithm)
99-
}
100-
101-
/// Encode a set of claims using the builder pattern
102-
public func encode(_ algorithm: Algorithm, closure: ((ClaimSetBuilder) -> ())) -> String {
103-
let builder = ClaimSetBuilder()
104-
closure(builder)
105-
return encode(claims: builder.claims, algorithm: algorithm)
106-
}

0 commit comments

Comments
 (0)