JWTCodable is a JSON Web Tokens decoder framework for iOS in swift.
JWTCodable
is available on CocoaPods:
pod 'JWTCodable'
if you want to use Decoder
only you can install only JWTDecoder
Readme
if you want to use Encoder
only you can install only JWTEncoder
Readme
import JWTCodable
let jwtToken = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c"
Header
{
"alg": "HS256",
"typ": "JWT"
}
Payload
{
"sub": "1234567890",
"name": "John Doe",
"iat": 1516239022
}
Signature
HMACSHA256(
base64UrlEncode(header) + "." +
base64UrlEncode(payload),
your-256-bit-secret)
Create your own Payload object Codable, For example reference from payload JSON
struct Payload: Codable {
var sub: String?
var name: String?
var iat: Int?
}
let dict:[String: Any] = JWTDecoder.shared.decodeDict(jwtToken: jwtToken)
// [String: Any]
let payload:Payload? = JWTDecoder.shared.decodePayload(jwtToken: jwtToken, type: Payload.self)
// Payload?
let jwt:JWT? = JWTDecoder.shared.decode(jwtToken: jwtToken, payloadType: Payload.self)
// JWT<Payload>?
jwt?.header
// value as object JWTHeader?
jwt?.payload
// value as object Payload?
jwt?.signature
// signature value as String?
jwt?.jwtToken
// jwtToken value as String?
let header = JWTHeader(alg: .hs256)
let payload = Payload(sub: "1234567890", name: "John Doe", iat: 1516239022)
let signature = "SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c"
let jwt = JWT<Payload>(header: header, payload: payload, signature: signature)
// Create JWT Object
let jwtToken = JWTEncoder.shared.encode(jwt: jwt)
// Create jwtToken value as String?
- iOS 10.0+
- Xcode 11+
- Swift 5.1+