48 lines
1.1 KiB
Nim
48 lines
1.1 KiB
Nim
import std/json, std/options, std/strutils, std/times
|
|
|
|
import ./claims, ./joseheader, ./jwe, ./jws
|
|
|
|
export claims
|
|
|
|
type
|
|
JwtKind = enum jkJWE, jkJWS
|
|
|
|
JWT* = object
|
|
claims: JwtClaims
|
|
case kind: JwtKind
|
|
of jkJWE: jwe: JWE
|
|
of jkJWS: jws: JWS
|
|
|
|
## Public read-only accessors to JWT members
|
|
## -----------------------------------------
|
|
|
|
func claims*(jwt: JWT): JwtClaims = jwt.claims
|
|
|
|
func header*(jwt: JWT): JoseHeader =
|
|
case jwt.kind:
|
|
of jkJWS: return jwt.jws.header
|
|
of jkJWE: return jwt.jwe.header
|
|
|
|
func `$`*(jwt: JWT): string =
|
|
result = jwt.header.rawB64 & "." & jwt.claims.rawB64 & "."
|
|
if jwt.signature.isSome: result &= jwt.signature.get
|
|
|
|
proc initJWT*(encoded: string): JWT =
|
|
let parts = encoded.split('.')
|
|
|
|
result = JWT(
|
|
signature: if parts.len > 2: some(parts[2])
|
|
else: none[string](),
|
|
header: initJoseHeader(parts[0]),
|
|
claims: initJwtClaims(parts[1]))
|
|
|
|
proc initJWT*(header: JoseHeader, claims: JwtClaims): JWT =
|
|
result = JWT(
|
|
header: header,
|
|
claims: claims,
|
|
signature: none[string]())
|
|
|
|
# proc verify*(jwt: JWT): bool =
|
|
|
|
# proc sign*(jwt: JWT, key: JWK): JWT =
|