Skip to content

Commit 30cb9ea

Browse files
committed
refactor: Deprecate decoding to dictionary
1 parent 48cd4d6 commit 30cb9ea

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

Sources/Decode.swift

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public enum InvalidToken : CustomStringConvertible, Error {
4747

4848

4949
/// Decode a JWT
50-
func decode(_ jwt: String, algorithms: [Algorithm], verify: Bool = true, audience: String? = nil, issuer: String? = nil) throws -> ClaimSet {
50+
public func decode(_ jwt: String, algorithms: [Algorithm], verify: Bool = true, audience: String? = nil, issuer: String? = nil) throws -> ClaimSet {
5151
let (header, claims, signature, signatureInput) = try load(jwt)
5252

5353
if verify {
@@ -60,12 +60,20 @@ func decode(_ jwt: String, algorithms: [Algorithm], verify: Bool = true, audienc
6060

6161

6262
/// Decode a JWT
63+
public func decode(_ jwt: String, algorithm: Algorithm, verify: Bool = true, audience: String? = nil, issuer: String? = nil) throws -> ClaimSet {
64+
return try decode(jwt, algorithms: [algorithm], verify: verify, audience: audience, issuer: issuer)
65+
}
66+
67+
68+
/// Decode a JWT
69+
@available(*, deprecated, message: "use decode that returns a ClaimSet instead")
6370
public func decode(_ jwt: String, algorithms: [Algorithm], verify: Bool = true, audience: String? = nil, issuer: String? = nil) throws -> Payload {
6471
return try decode(jwt, algorithms: algorithms, verify: verify, audience: audience, issuer: issuer).claims
6572
}
6673

6774

6875
/// Decode a JWT
76+
@available(*, deprecated, message: "use decode that returns a ClaimSet instead")
6977
public func decode(_ jwt: String, algorithm: Algorithm, verify: Bool = true, audience: String? = nil, issuer: String? = nil) throws -> Payload {
7078
return try decode(jwt, algorithms: [algorithm], verify: verify, audience: audience, issuer: issuer).claims
7179
}

Tests/JWTTests/JWTTests.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,13 @@ class PayloadTests: XCTestCase {
8383
}
8484

8585
class DecodeTests: XCTestCase {
86+
func testDecodingValidJWTAsClaimSet() throws {
87+
let jwt = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYW1lIjoiS3lsZSJ9.zxm7xcp1eZtZhp4t-nlw09ATQnnFKIiSN83uG8u6cAg"
88+
89+
let claims: ClaimSet = try JWT.decode(jwt, algorithm: .hs256("secret".data(using: .utf8)!))
90+
XCTAssertEqual(claims["name"] as? String, "Kyle")
91+
}
92+
8693
func testDecodingValidJWT() {
8794
let jwt = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYW1lIjoiS3lsZSJ9.zxm7xcp1eZtZhp4t-nlw09ATQnnFKIiSN83uG8u6cAg"
8895

0 commit comments

Comments
 (0)