Link fixes + tests

This commit is contained in:
Yuriy Glukhov
2019-06-13 16:20:50 +03:00
parent 4d15cef10f
commit 4738c5eeaa
10 changed files with 73 additions and 28 deletions

33
.travis.yml Normal file
View File

@@ -0,0 +1,33 @@
language: c
# https://docs.travis-ci.com/user/caching/
#
# Caching the whole nim folder is better than relying on ccache - this way, we
# skip the expensive bootstrap process and linking
cache:
directories:
- nim
os:
- linux
- osx
install:
# check version of remote branch
- "export NIMVER=$(git ls-remote https://github.com/nim-lang/nim.git HEAD | cut -f 1)"
# after building nim, wipe csources to save on cache space
- "{ [ -f nim/$NIMVER/bin/nim ] && [ -f nim/$NIMVER/bin/nimble ] ; } ||
{ rm -rf nim ;
mkdir -p nim ;
git clone --depth=1 https://github.com/nim-lang/nim.git nim/$NIMVER ;
cd nim/$NIMVER ;
sh build_all.sh ;
rm -rf csources ;
cd ../.. ;
}"
- "export PATH=$PWD/nim/$NIMVER/bin:$HOME/.nimble/bin:$PATH"
script:
- nimble install -y
- nimble test

View File

@@ -1,4 +1,4 @@
JWT Implementation for Nim-lang
JWT Implementation for Nim-lang [![Build Status](https://travis-ci.org/yglukhov/nim-jwt.svg?branch=master)](https://travis-ci.org/yglukhov/nim-jwt)
===============================
This is a implementation of JSON Web Tokens for Nim, it allows for the following operations to be performed:

View File

@@ -1,8 +1,8 @@
import future, json, strutils, tables, times, sequtils
import json, strutils, tables, times, sequtils
from private/crypto import nil
from jwt/private/crypto import nil
import private/claims, private/jose, private/utils
import jwt/private/[claims, jose, utils]
type
InvalidToken* = object of Exception
@@ -98,7 +98,7 @@ proc signString*(toSign: string, secret: string, algorithm: SignatureAlgorithm =
return rsSign(crypto.EVP_sha384())
else:
raise newException(UnsupportedAlgorithm, $algorithm & " isn't supported")
result = join(signature.map((i: uint8) => (toHex(BiggestInt(i), 2))), "")
result = join(signature.mapIt(toHex(BiggestInt(it), 2)), "")
# Verify that the token is not tampered with
proc verifySignature*(data: string, signature: string, secret: string): bool =

View File

@@ -8,4 +8,4 @@ srcDir = "src"
# Deps
requires "nim >= 0.19.0"
requires "https://github.com/yglukhov/linktools"

View File

@@ -1,4 +1,4 @@
import future, json, sequtils, strutils, times, tables
import json, sequtils, strutils, times, tables
import utils
@@ -72,7 +72,7 @@ proc newTimeClaim*(k: ClaimKind, i: int64): Claim =
# Returns the claimKeyms value as a time
proc getClaimTime*(c: Claim): Time =
result = fromSeconds(c.node.num)
result = fromUnix(c.node.num)
# NBF
proc newNBF*(s: string): Claim = return newTimeClaim(NBF, s)

View File

@@ -1,9 +1,21 @@
import openssl
import openssl, linktools
# TODO: Linkage flags should probably need more attention because of different
# openssl versions. E.g. DigestSign* functions are not available in old openssl.
when defined(macosx):
const libcrypto = "crypto.35"
else:
const libcrypto = "crypto"
{.passL: "-l" & libcrypto.}
export EVP_PKEY_RSA
const
HMAC_MAX_MD_CBLOCK* = 128
const sslIsOld = libHasSymbol(libcrypto, "EVP_MD_CTX_create")
type
EVP_MD* = SslPtr
EVP_MD_CTX* = SslPtr
@@ -35,7 +47,7 @@ proc PEM_read_bio_PrivateKey*(bp: BIO, x: ptr EVP_PKEY,
proc EVP_PKEY_free*(p: EVP_PKEY) {.cdecl, importc.}
when defined(macosx):
when sslIsOld:
proc EVP_MD_CTX_create*(): EVP_MD_CTX {.cdecl, importc.}
proc EVP_MD_CTX_destroy*(ctx: EVP_MD_CTX) {.cdecl, importc.}
else:

View File

@@ -1,21 +1,21 @@
import json, unittest
import jwt
import ../jwt
suite "Claim ops":
test "Create claims from JSON":
let asJson = %{
"iss": %"jane",
"sub": %"john",
"nbf": %1234,
"iat": %1234,
"exp": %1234,
"jti": %"token-id",
"foo": %{"bar": %1}
}
let claims = asJson.toClaims
let toJson = %claims
let asJson = %{
"iss": %"jane",
"sub": %"john",
"nbf": %1234,
"iat": %1234,
"exp": %1234,
"jti": %"token-id",
"foo": %{"bar": %1}
}
let claims = asJson.toClaims
let toJson = %claims
assert asJson.len == toJson.len
for k, v in asJson:
assert v == toJson[k]
assert asJson.len == toJson.len
for k, v in asJson:
assert v == toJson[k]

View File

@@ -1,7 +1,7 @@
import json, times, unittest
from times import nil
import jwt
import ../jwt
proc getToken(claims: JsonNode = newJObject(), header: JsonNode = newJObject()): JWT =
let claims = claims.toClaims
@@ -32,14 +32,14 @@ suite "Token tests":
test "NBF Check":
let
now = getTime().toSeconds.int + 60
now = getTime().toUnix.int + 60
token = getToken(claims = %{"nbf": %now})
expect(InvalidToken):
token.verifyTimeClaims
test "EXP Check":
let
now = getTime().toSeconds.int - 60
now = getTime().toUnix.int - 60
token = getToken(claims = %{"exp": %now})
expect(InvalidToken):
token.verifyTimeClaims