mirror of
https://github.com/vacp2p/nim-jwt.git
synced 2026-01-09 20:27:56 -05:00
Consistent indent
This commit is contained in:
@@ -7,5 +7,5 @@ license = "MIT"
|
||||
srcDir = "src"
|
||||
|
||||
# Deps
|
||||
requires "nim >= 0.10.3"
|
||||
requires "https://github.com/yglukhov/linktools"
|
||||
requires "nim >= 0.19.0"
|
||||
|
||||
|
||||
53
src/jwt.nim
53
src/jwt.nim
@@ -5,45 +5,42 @@ from private/crypto import nil
|
||||
import private/claims, private/jose, private/utils
|
||||
|
||||
type
|
||||
InvalidToken* = object of Exception
|
||||
InvalidToken* = object of Exception
|
||||
|
||||
JWT* = object
|
||||
headerB64: string
|
||||
claimsB64: string
|
||||
header*: JOSEHeader
|
||||
claims*: TableRef[string, Claim]
|
||||
signature*: string
|
||||
JWT* = object
|
||||
headerB64: string
|
||||
claimsB64: string
|
||||
header*: JOSEHeader
|
||||
claims*: TableRef[string, Claim]
|
||||
signature*: string
|
||||
|
||||
export claims
|
||||
export jose
|
||||
|
||||
|
||||
|
||||
proc splitToken(s: string): seq[string] =
|
||||
let parts = s.split(".")
|
||||
if parts.len != 3:
|
||||
raise newException(InvalidToken, "Invalid token")
|
||||
result = parts
|
||||
let parts = s.split(".")
|
||||
if parts.len != 3:
|
||||
raise newException(InvalidToken, "Invalid token")
|
||||
result = parts
|
||||
|
||||
|
||||
# Load up a b64url string to JWT
|
||||
proc toJWT*(s: string): JWT =
|
||||
var parts = splitToken(s)
|
||||
let
|
||||
headerB64 = parts[0]
|
||||
claimsB64 = parts[1]
|
||||
headerJson = parseJson(decodeUrlSafe(headerB64))
|
||||
claimsJson = parseJson(decodeUrlSafe(claimsB64))
|
||||
signature = decodeUrlSafe(parts[2])
|
||||
|
||||
result = JWT(
|
||||
headerB64: headerB64,
|
||||
claimsB64: claimsB64,
|
||||
header: headerJson.toHeader(),
|
||||
claims: claimsJson.toClaims(),
|
||||
signature: signature
|
||||
)
|
||||
var parts = splitToken(s)
|
||||
let
|
||||
headerB64 = parts[0]
|
||||
claimsB64 = parts[1]
|
||||
headerJson = parseJson(decodeUrlSafe(headerB64))
|
||||
claimsJson = parseJson(decodeUrlSafe(claimsB64))
|
||||
signature = decodeUrlSafe(parts[2])
|
||||
|
||||
result = JWT(
|
||||
headerB64: headerB64,
|
||||
claimsB64: claimsB64,
|
||||
header: headerJson.toHeader(),
|
||||
claims: claimsJson.toClaims(),
|
||||
signature: signature
|
||||
)
|
||||
|
||||
proc toJWT*(node: JsonNode): JWT =
|
||||
let claims = node["claims"].toClaims
|
||||
|
||||
@@ -14,8 +14,6 @@ export EVP_PKEY_RSA
|
||||
const
|
||||
HMAC_MAX_MD_CBLOCK* = 128
|
||||
|
||||
const sslIsOld = true #libHasSymbol(libcrypto, "EVP_MD_CTX_create")
|
||||
|
||||
type
|
||||
EVP_MD* = SslPtr
|
||||
EVP_MD_CTX* = SslPtr
|
||||
@@ -46,15 +44,14 @@ proc PEM_read_bio_PrivateKey*(bp: BIO, x: ptr EVP_PKEY,
|
||||
cb: pointer, u: pointer): EVP_PKEY {.cdecl, importc.}
|
||||
proc EVP_PKEY_free*(p: EVP_PKEY) {.cdecl, importc.}
|
||||
|
||||
when sslIsOld:
|
||||
proc EVP_MD_CTX_create*(): EVP_MD_CTX {.cdecl, importc.}
|
||||
proc EVP_MD_CTX_destroy*(ctx: EVP_MD_CTX) {.cdecl, importc.}
|
||||
else:
|
||||
proc EVP_MD_CTX_create*(): EVP_MD_CTX {.cdecl, importc: "EVP_MD_CTX_new".}
|
||||
proc EVP_MD_CTX_destroy*(ctx: EVP_MD_CTX) {.cdecl, importc: "EVP_MD_CTX_free".}
|
||||
|
||||
proc EVP_DigestSignInit*(ctx: EVP_MD_CTX, pctx: ptr EVP_PKEY_CTX,
|
||||
typ: EVP_MD, e: ENGINE, pkey: EVP_PKEY): cint {.cdecl, importc.}
|
||||
proc EVP_MD_CTX_create*(): EVP_MD_CTX {.cdecl, importc.}
|
||||
proc EVP_MD_CTX_destroy*(ctx: EVP_MD_CTX) {.cdecl, importc.}
|
||||
# some times you will need this instead:
|
||||
#proc EVP_MD_CTX_create*(): EVP_MD_CTX {.cdecl, importc: "EVP_MD_CTX_new".}
|
||||
#proc EVP_MD_CTX_destroy*(ctx: EVP_MD_CTX) {.cdecl, importc: "EVP_MD_CTX_free".}
|
||||
|
||||
proc EVP_DigestSignInit*(ctx: EVP_MD_CTX, pctx: ptr EVP_PKEY_CTX, typ: EVP_MD, e: ENGINE, pkey: EVP_PKEY): cint {.cdecl, importc.}
|
||||
|
||||
proc EVP_DigestSignUpdate*(ctx: EVP_MD_CTX, data: pointer, len: cuint): cint {.cdecl, importc: "EVP_DigestUpdate".}
|
||||
proc EVP_DigestSignFinal*(ctx: EVP_MD_CTX, data: pointer, len: ptr csize): cint {.cdecl, importc.}
|
||||
|
||||
@@ -3,56 +3,56 @@ import json, strutils, tables
|
||||
import utils
|
||||
|
||||
type
|
||||
CryptoException* = object of Exception
|
||||
UnsupportedAlgorithm* = object of CryptoException
|
||||
CryptoException* = object of Exception
|
||||
UnsupportedAlgorithm* = object of CryptoException
|
||||
|
||||
SignatureAlgorithm* = enum
|
||||
NONE
|
||||
HS256
|
||||
HS384
|
||||
HS512
|
||||
RS256
|
||||
RS384
|
||||
RS512
|
||||
ES384
|
||||
SignatureAlgorithm* = enum
|
||||
NONE
|
||||
HS256
|
||||
HS384
|
||||
HS512
|
||||
RS256
|
||||
RS384
|
||||
RS512
|
||||
ES384
|
||||
|
||||
JOSEHeader* = object
|
||||
alg*: SignatureAlgorithm
|
||||
typ*: string
|
||||
JOSEHeader* = object
|
||||
alg*: SignatureAlgorithm
|
||||
typ*: string
|
||||
|
||||
|
||||
proc strToSignatureAlgorithm(s: string): SignatureAlgorithm =
|
||||
try:
|
||||
result = parseEnum[SignatureAlgorithm](s)
|
||||
except ValueError:
|
||||
raise newException(UnsupportedAlgorithm, "$# isn't supported" % s)
|
||||
try:
|
||||
result = parseEnum[SignatureAlgorithm](s)
|
||||
except ValueError:
|
||||
raise newException(UnsupportedAlgorithm, "$# isn't supported" % s)
|
||||
|
||||
|
||||
proc toHeader*(j: JsonNode): JOSEHeader =
|
||||
let algStr = j["alg"].str
|
||||
let algo = strToSignatureAlgorithm(algStr)
|
||||
let algStr = j["alg"].str
|
||||
let algo = strToSignatureAlgorithm(algStr)
|
||||
|
||||
# Check that the keys are present so we dont blow up.
|
||||
utils.checkKeysExists(j, "alg", "typ")
|
||||
# Check that the keys are present so we dont blow up.
|
||||
utils.checkKeysExists(j, "alg", "typ")
|
||||
|
||||
result = JOSEHeader(
|
||||
alg: algo,
|
||||
typ: j["typ"].str
|
||||
)
|
||||
result = JOSEHeader(
|
||||
alg: algo,
|
||||
typ: j["typ"].str
|
||||
)
|
||||
|
||||
|
||||
proc `%`*(alg: SignatureAlgorithm): JsonNode =
|
||||
let s = $alg
|
||||
return %s
|
||||
let s = $alg
|
||||
return %s
|
||||
|
||||
|
||||
proc `%`*(h: JOSEHeader): JsonNode =
|
||||
return %{
|
||||
"alg": %h.alg,
|
||||
"typ": %h.typ
|
||||
}
|
||||
return %{
|
||||
"alg": %h.alg,
|
||||
"typ": %h.typ
|
||||
}
|
||||
|
||||
|
||||
proc toBase64*(h: JOSEHeader): string =
|
||||
let asJson = %h
|
||||
result = encodeUrlSafe($asJson)
|
||||
let asJson = %h
|
||||
result = encodeUrlSafe($asJson)
|
||||
|
||||
@@ -4,25 +4,25 @@ from base64 import nil
|
||||
|
||||
|
||||
type
|
||||
KeyError = object of Exception
|
||||
KeyError = object of Exception
|
||||
|
||||
proc checkJsonNodeKind*(node: JsonNode, kind: JsonNodeKind) =
|
||||
# Check that a given JsonNode has a given kind, raise InvalidClaim if not
|
||||
if node.kind != kind:
|
||||
raise newException(ValueError, "Invalid kind")
|
||||
# Check that a given JsonNode has a given kind, raise InvalidClaim if not
|
||||
if node.kind != kind:
|
||||
raise newException(ValueError, "Invalid kind")
|
||||
|
||||
|
||||
proc checkKeysExists*(node: JsonNode, keys: varargs[string]) =
|
||||
for key in keys:
|
||||
if not node.hasKey(key):
|
||||
raise newException(KeyError, "$# is not present." % key)
|
||||
for key in keys:
|
||||
if not node.hasKey(key):
|
||||
raise newException(KeyError, "$# is not present." % key)
|
||||
|
||||
|
||||
proc encodeUrlSafe*(s: string): string =
|
||||
result = base64.encode(s, newLine="")
|
||||
while result.endsWith("="):
|
||||
result = result.substr(0, result.high-1)
|
||||
result = result.replace('+', '-').replace('/', '_')
|
||||
result = result.replace('+', '-').replace('/', '_')
|
||||
|
||||
|
||||
proc decodeUrlSafe*(s: string): string =
|
||||
|
||||
Reference in New Issue
Block a user