Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

expose decoding strategy of JSONDecoder for claims #122

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 37 additions & 1 deletion Sources/SwiftJWT/JWTDecoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ public class JWTDecoder: BodyDecoder {

let keyIDToVerifier: (String) -> JWTVerifier?
let jwtVerifier: JWTVerifier?
public var claimsKeyDecodingStrategy : JSONDecoder.KeyDecodingStrategy = .useDefaultKeys
public var claimsDateDecodingStrategy : JSONDecoder.DateDecodingStrategy = .deferredToDate
public var claimsDataDecodingStrategy : JSONDecoder.DataDecodingStrategy = .base64
public var claimsNonConformingFloatDecodingStrategy : JSONDecoder.NonConformingFloatDecodingStrategy = .throw

// MARK: Initializers

Expand Down Expand Up @@ -84,6 +88,10 @@ public class JWTDecoder: BodyDecoder {

// Decode the JWT headers and claims data into a _JWTDecoder.
let decoder = _JWTDecoder(header: headerData, claims: claimsData)
decoder.claimsKeyDecodingStrategy = self.claimsKeyDecodingStrategy
decoder.claimsDateDecodingStrategy = self.claimsDateDecodingStrategy
decoder.claimsDataDecodingStrategy = self.claimsDataDecodingStrategy
decoder.claimsNonConformingFloatDecodingStrategy = self.claimsNonConformingFloatDecodingStrategy
let jwt = try decoder.decode(type)

let _jwtVerifier: JWTVerifier
Expand Down Expand Up @@ -156,14 +164,26 @@ fileprivate class _JWTDecoder: Decoder {

var userInfo: [CodingUserInfoKey : Any] = [:]

var claimsKeyDecodingStrategy : JSONDecoder.KeyDecodingStrategy = .useDefaultKeys

var claimsDateDecodingStrategy : JSONDecoder.DateDecodingStrategy = .deferredToDate

var claimsDataDecodingStrategy : JSONDecoder.DataDecodingStrategy = .base64

var claimsNonConformingFloatDecodingStrategy : JSONDecoder.NonConformingFloatDecodingStrategy = .throw

// Call the Codable Types init from decoder function.
public func decode<T: Decodable>(_ type: T.Type) throws -> T {
return try type.init(from: self)
}

// JWT should only be a Keyed container
func container<Key : CodingKey>(keyedBy type: Key.Type) throws -> KeyedDecodingContainer<Key> {
let container = _JWTKeyedDecodingContainer<Key>(decoder: self, header: header, claims: claims)
var container = _JWTKeyedDecodingContainer<Key>(decoder: self, header: header, claims: claims)
container.claimsKeyDecodingStrategy = claimsKeyDecodingStrategy
container.claimsDateDecodingStrategy = claimsDateDecodingStrategy
container.claimsDataDecodingStrategy = claimsDataDecodingStrategy
container.claimsNonConformingFloatDecodingStrategy = claimsNonConformingFloatDecodingStrategy
return KeyedDecodingContainer(container)
}

Expand All @@ -189,6 +209,14 @@ private struct _JWTKeyedDecodingContainer<Key: CodingKey>: KeyedDecodingContaine

var codingPath: [CodingKey]

var claimsKeyDecodingStrategy : JSONDecoder.KeyDecodingStrategy

var claimsDateDecodingStrategy : JSONDecoder.DateDecodingStrategy

var claimsDataDecodingStrategy : JSONDecoder.DataDecodingStrategy

var claimsNonConformingFloatDecodingStrategy : JSONDecoder.NonConformingFloatDecodingStrategy

public var allKeys: [Key]
{
#if swift(>=4.1)
Expand All @@ -203,6 +231,10 @@ private struct _JWTKeyedDecodingContainer<Key: CodingKey>: KeyedDecodingContaine
self.header = header
self.claims = claims
self.codingPath = decoder.codingPath
self.claimsKeyDecodingStrategy = .useDefaultKeys
self.claimsDateDecodingStrategy = .deferredToDate
self.claimsDataDecodingStrategy = .base64
self.claimsNonConformingFloatDecodingStrategy = .throw
}

public func contains(_ key: Key) -> Bool {
Expand All @@ -224,6 +256,10 @@ private struct _JWTKeyedDecodingContainer<Key: CodingKey>: KeyedDecodingContaine
return decodedHeader
} else
if key.stringValue == "claims" {
jsonDecoder.keyDecodingStrategy = self.claimsKeyDecodingStrategy
jsonDecoder.dateDecodingStrategy = self.claimsDateDecodingStrategy
jsonDecoder.dataDecodingStrategy = self.claimsDataDecodingStrategy
jsonDecoder.nonConformingFloatDecodingStrategy = self.claimsNonConformingFloatDecodingStrategy
return try jsonDecoder.decode(type, from: claims)
} else {
throw DecodingError.keyNotFound(key, DecodingError.Context(codingPath: codingPath, debugDescription: "value not found for provided key"))
Expand Down