Skip to content
Merged
Show file tree
Hide file tree
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
41 changes: 13 additions & 28 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -1,33 +1,18 @@
name: CI

on:
push:
schedule:
- cron: '0 0 * * *'
branches:
- master
pull_request:
workflow_dispatch:

jobs:
Test:
if: |
!contains(github.event.head_commit.message, '[skip ci]')
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest]
nim-channel: [stable, devel]

name: ${{ matrix.os }}-${{ matrix.nim-channel }}
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v2

- name: Setup nim
uses: jiro4989/setup-nim-action@v1
with:
nim-version: ${{ matrix.nim-channel }}

- name: Test
shell: bash
run: |
nim --version
nimble install -dy
nimble test
build:
uses: status-im/nimbus-common-workflow/.github/workflows/common.yml@main
with:
nim-versions: '["version-2-0", "version-2-2", "devel"]'
nimble-version: 1b4deb9be9fca7b8a21576a6c0de85f1ae7c30de
test-command: |
nimble dump --solve
nimble list --installed --version
env NIMLANG=c nimble test
6 changes: 3 additions & 3 deletions jwt.nim
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import json, strutils, tables, times
import bearssl
import jwt/[claims, jose, utils, crypto]
import ./jwt/[claims, jose, utils, crypto]

type
InvalidToken* = object of ValueError
Expand All @@ -9,7 +9,7 @@ type
headerB64: string
claimsB64: string
header*: JsonNode
claims*: TableRef[string, Claim]
claims*: OrderedTableRef[string, Claim]
signature*: seq[byte]

export claims
Expand All @@ -22,7 +22,7 @@ proc splitToken(s: string): seq[string] =
result = parts

proc initJWT*(
header: JsonNode, claims: TableRef[string, Claim], signature: seq[byte] = @[]
header: JsonNode, claims: OrderedTableRef[string, Claim], signature: seq[byte] = @[]
): JWT =
JWT(
headerB64: header.toBase64,
Expand Down
29 changes: 26 additions & 3 deletions jwt.nimble
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,29 @@ description = "JSON Web Tokens for Nim"
license = "MIT"

# Deps
requires "nim >= 0.19.0"
requires "bearssl"
requires "https://github.com/vacp2p/bearssl_pkey_decoder#21dd3710df9345ed2ad8bf8f882761e07863b8e0"
requires "nim >= 1.6.0"
requires "bearssl >= 0.2.7"
requires "https://github.com/vacp2p/bearssl_pkey_decoder#d34aa46bf9d0a3ffff810fbd3c4d2fa024eb9368"

let nimc = getEnv("NIMC", "nim") # Which nim compiler to use
let lang = getEnv("NIMLANG", "c") # Which backend (c/cpp/js)
let flags = getEnv("NIMFLAGS", "") # Extra flags for the compiler
let verbose = getEnv("V", "") notin ["", "0"]

from os import quoteShell

let cfg =
" --styleCheck:usages --styleCheck:error" &
(if verbose: "" else: " --verbosity:0 --hints:off") & " --outdir:build " &
quoteShell("--nimcache:build/nimcache/$projectName")

proc build(args, path: string) =
exec nimc & " " & lang & " " & cfg & " " & flags & " " & args & " " & path

proc run(args, path: string) =
build args & " --mm:refc -r", path
build args & " --mm:orc -r", path

task test, "Run all tests":
for threads in ["--threads:off", "--threads:on"]:
run threads, "tests/test_all"
10 changes: 5 additions & 5 deletions jwt/claims.nim
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ type

proc newClaims*(
claims: varargs[tuple[key: string, val: Claim]]
): TableRef[string, Claim] =
result = newTable[string, Claim](claims)
): OrderedTableRef[string, Claim] =
result = newOrderedTable[string, Claim](claims)

proc newClaim*(k: ClaimKind, node: JsonNode): Claim =
new result
Expand Down Expand Up @@ -109,7 +109,7 @@ proc newJTI*(j: JsonNode): Claim =
proc newJTI*(s: string): Claim =
return newJTI(%s)

proc toClaims*(j: JsonNode): TableRef[string, Claim] =
proc toClaims*(j: JsonNode): OrderedTableRef[string, Claim] =
result = newClaims()

for claimKey, claimNode in j:
Expand All @@ -134,11 +134,11 @@ proc toClaims*(j: JsonNode): TableRef[string, Claim] =
proc `%`*(c: Claim): JsonNode =
result = c.node

proc `%`*(claims: TableRef[string, Claim]): JsonNode =
proc `%`*(claims: OrderedTableRef[string, Claim]): JsonNode =
result = newJObject()
for k, v in claims:
result[k] = %v

proc toBase64*(claims: TableRef[string, Claim]): string =
proc toBase64*(claims: OrderedTableRef[string, Claim]): string =
let asJson = %claims
result = encodeUrlSafe($asJson)
8 changes: 4 additions & 4 deletions jwt/crypto.nim
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ proc invalidPemKey() =

proc pemDecoderLoop(
pem: string,
prc: proc(ctx: pointer, pbytes: pointer, nbytes: uint) {.bearSslFunc.},
prc: proc(ctx: pointer, pbytes: pointer, nbytes: csize_t) {.bearSslFunc.},
ctx: pointer,
) =
var pemCtx: PemDecoderContext
Expand Down Expand Up @@ -55,7 +55,7 @@ proc decodeFromPem(pkCtx: var PkeyDecoderContext, pem: string) =
pkeyDecoderInit(addr pkCtx)
pemDecoderLoop(
pem,
cast[proc(ctx: pointer, pbytes: pointer, nbytes: uint) {.bearSslFunc.}](pkeyDecoderPush),
cast[proc(ctx: pointer, pbytes: pointer, nbytes: csize_t) {.bearSslFunc.}](pkeyDecoderPush),
addr pkCtx,
)
if pkeyDecoderLastError(addr pkCtx) != 0:
Expand Down Expand Up @@ -96,7 +96,7 @@ proc bearSignRSPem*(

proc bearVerifyRSPem*(
data, key: string,
sig: openarray[byte],
sig: openArray[byte],
alg: ptr HashClass,
hashOid: cstring,
hashLen: int,
Expand Down Expand Up @@ -152,7 +152,7 @@ proc bearSignECPem*(data, key: string, alg: ptr HashClass): seq[byte] =
result.setLen(sz)

proc bearVerifyECPem*(
data, key: string, sig: openarray[byte], alg: ptr HashClass, hashLen: int
data, key: string, sig: openArray[byte], alg: ptr HashClass, hashLen: int
): bool =
# Step 1. Extract EC Pub key from `key` in PEM format
var pkCtx: PkeyDecoderContext
Expand Down
4 changes: 2 additions & 2 deletions jwt/utils.nim
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ proc checkKeysExists*(node: JsonNode, keys: varargs[string]) =
if not node.hasKey(key):
raise newException(KeyError, "$# is not present." % key)

proc encodeUrlSafe*(s: openarray[byte]): string =
proc encodeUrlSafe*(s: openArray[byte]): string =
when (NimMajor >= 1 and (NimMinor >= 1 or NimPatch >= 2)) or NimMajor >= 2:
result = base64.encode(s)
else:
Expand All @@ -20,7 +20,7 @@ proc encodeUrlSafe*(s: openarray[byte]): string =
result.setLen(result.len - 1)
result = result.replace('+', '-').replace('/', '_')

proc encodeUrlSafe*(s: openarray[char]): string {.inline.} =
proc encodeUrlSafe*(s: openArray[char]): string {.inline.} =
encodeUrlSafe(s.toOpenArrayByte(s.low, s.high))

proc decodeUrlSafeAsString*(s: string): string =
Expand Down
2 changes: 1 addition & 1 deletion tests/t_claims.nim
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import json, unittest
import jwt
import ../jwt

suite "Claim ops":
test "Create claims from JSON":
Expand Down
4 changes: 2 additions & 2 deletions tests/t_jwt.nim
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import json, times, unittest
import jwt
import ../jwt

proc getToken(claims: JsonNode = newJObject(), header: JsonNode = newJObject()): JWT =
for k, v in %*{"alg": "HS512", "typ": "JWT"}:
Expand All @@ -10,7 +10,7 @@ proc getToken(claims: JsonNode = newJObject(), header: JsonNode = newJObject()):

proc tokenWithAlg(alg: string): JWT =
let header = %*{"typ": "JWT", "alg": alg}
let claims = %*{"sub": "1234567890", "name": "John Doe", "iat": 1516239022}
let claims = %*{"name": "John Doe", "sub": "1234567890", "iat": 1516239022}
initJWT(header.toHeader, claims.toClaims)

proc signedHSToken(alg: string): JWT =
Expand Down
5 changes: 5 additions & 0 deletions tests/test_all.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{.warning[UnusedImport]: off.}

import
./t_claims,
./t_jwt
Loading