|
|
|
@ -25,25 +25,29 @@ type
|
|
|
|
|
JwtClaims* = object
|
|
|
|
|
rawB64: string
|
|
|
|
|
json: JsonNode
|
|
|
|
|
iss: Option[string]
|
|
|
|
|
sub: Option[string]
|
|
|
|
|
aud: Option[string]
|
|
|
|
|
exp: Option[DateTime]
|
|
|
|
|
nbf: Option[DateTime]
|
|
|
|
|
iat: Option[DateTime]
|
|
|
|
|
iss: Option[string]
|
|
|
|
|
jti: Option[string]
|
|
|
|
|
nbf: Option[DateTime]
|
|
|
|
|
sid: Option[string]
|
|
|
|
|
sub: Option[string]
|
|
|
|
|
typ: Option[string]
|
|
|
|
|
|
|
|
|
|
# Public read-only accessors to JwtClaims members
|
|
|
|
|
# -----------------------------------------------
|
|
|
|
|
|
|
|
|
|
func rawB64*(c: JwtClaims): string = c.rawB64
|
|
|
|
|
func iss*(c: JwtClaims): Option[string] = c.iss
|
|
|
|
|
func sub*(c: JwtClaims): Option[string] = c.sub
|
|
|
|
|
func aud*(c: JwtClaims): Option[string] = c.aud
|
|
|
|
|
func exp*(c: JwtClaims): Option[DateTime] = c.exp
|
|
|
|
|
func nbf*(c: JwtClaims): Option[DateTime] = c.nbf
|
|
|
|
|
func iat*(c: JwtClaims): Option[DateTime] = c.iat
|
|
|
|
|
func iss*(c: JwtClaims): Option[string] = c.iss
|
|
|
|
|
func jti*(c: JwtClaims): Option[string] = c.jti
|
|
|
|
|
func nbf*(c: JwtClaims): Option[DateTime] = c.nbf
|
|
|
|
|
func sid*(c: JwtClaims): Option[string] = c.sid
|
|
|
|
|
func sub*(c: JwtClaims): Option[string] = c.sub
|
|
|
|
|
func typ*(c: JwtClaims): Option[string] = c.typ
|
|
|
|
|
|
|
|
|
|
func `[]`*(c: JwtClaims, key: string): Option[JsonNode] =
|
|
|
|
|
## Generic accessor to claim values by name
|
|
|
|
@ -61,13 +65,15 @@ proc initJwtClaims*(n: JsonNode): JwtClaims =
|
|
|
|
|
return JwtClaims(
|
|
|
|
|
rawB64: b64UrlEncode($n),
|
|
|
|
|
json: n,
|
|
|
|
|
iss: n.optStrVal("iss"),
|
|
|
|
|
sub: n.optStrVal("sub"),
|
|
|
|
|
aud: n.optStrVal("aud"),
|
|
|
|
|
exp: n.optNumericDate("exp"),
|
|
|
|
|
nbf: n.optNumericDate("nbf"),
|
|
|
|
|
iat: n.optNumericDate("iat"),
|
|
|
|
|
jti: n.optStrVal("jtu"))
|
|
|
|
|
iss: n.optStrVal("iss"),
|
|
|
|
|
jti: n.optStrVal("jtu"),
|
|
|
|
|
nbf: n.optNumericDate("nbf"),
|
|
|
|
|
sid: n.optStrVal("sid"),
|
|
|
|
|
sub: n.optStrVal("sub"),
|
|
|
|
|
typ: n.optStrVal("typ"))
|
|
|
|
|
|
|
|
|
|
proc initJwtClaims*(encoded: string): JwtClaims =
|
|
|
|
|
## Parse a Base64url-encoded set of claims into a JwtClaims object
|
|
|
|
@ -77,13 +83,15 @@ proc initJwtClaims*(encoded: string): JwtClaims =
|
|
|
|
|
result.rawB64 = encoded
|
|
|
|
|
|
|
|
|
|
proc initJwtClaims*(
|
|
|
|
|
iss: Option[string] = none[string](),
|
|
|
|
|
sub: Option[string] = none[string](),
|
|
|
|
|
aud: Option[string] = none[string](),
|
|
|
|
|
exp: Option[DateTime] = none[DateTime](),
|
|
|
|
|
nbf: Option[DateTime] = none[DateTime](),
|
|
|
|
|
iat: Option[DateTime] = none[DateTime](),
|
|
|
|
|
iss: Option[string] = none[string](),
|
|
|
|
|
jti: Option[string] = none[string](),
|
|
|
|
|
nbf: Option[DateTime] = none[DateTime](),
|
|
|
|
|
sid: Option[string] = none[string](),
|
|
|
|
|
sub: Option[string] = none[string](),
|
|
|
|
|
typ: Option[string] = none[string](),
|
|
|
|
|
moreClaims: seq[tuple[k, v: string]] = @[]
|
|
|
|
|
): JwtClaims =
|
|
|
|
|
## Convenience method to initialize a new JwtClaims object by specifying
|
|
|
|
@ -99,12 +107,14 @@ proc initJwtClaims*(
|
|
|
|
|
let json = newJObject()
|
|
|
|
|
for claim in moreClaims: json[claim.k] = %claim.v
|
|
|
|
|
|
|
|
|
|
if iss.isSome: json["iss"] = %iss.get
|
|
|
|
|
if sub.isSome: json["sub"] = %sub.get
|
|
|
|
|
if aud.isSome: json["aud"] = %aud.get
|
|
|
|
|
if exp.isSome: json["exp"] = %toNumericDate(exp.get)
|
|
|
|
|
if nbf.isSome: json["nbf"] = %toNumericDate(nbf.get)
|
|
|
|
|
if iat.isSome: json["iat"] = %toNumericDate(iat.get)
|
|
|
|
|
if iss.isSome: json["iss"] = %iss.get
|
|
|
|
|
if jti.isSome: json["jti"] = %jti.get
|
|
|
|
|
if nbf.isSome: json["nbf"] = %toNumericDate(nbf.get)
|
|
|
|
|
if sid.isSome: json["sid"] = %sid.get
|
|
|
|
|
if sub.isSome: json["sub"] = %sub.get
|
|
|
|
|
if typ.isSome: json["typ"] = %typ.get
|
|
|
|
|
|
|
|
|
|
return initJwtClaims(json)
|
|
|
|
|