SHARE
TWEET

Untitled

a guest Oct 21st, 2019 89 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import jwt, times, json, tables
  2.  
  3. var secret = "secret"
  4.  
  5. proc sign*(userId: string): string =
  6.   var token = toJWT(%*{
  7.     "header": {
  8.       "alg": "HS256",
  9.       "typ": "JWT"
  10.     },
  11.     "claims": {
  12.       "userId": userId,
  13.       "exp": (getTime() + 1.days).toSeconds().int
  14.     }
  15.   })
  16.  
  17.   token.sign(secret)
  18.  
  19.   result = $token
  20.  
  21. proc verify*(token: string): bool =
  22.   try:
  23.     let jwtToken = token.toJWT()
  24.     result = jwtToken.verify(secret)
  25.   except InvalidToken:
  26.     result = false
  27.  
  28. proc decode*(token: string): string =
  29.   let jwt = token.toJWT()
  30.   result = $jwt.claims["userId"].node.str
  31.  
  32.  
  33. var mytoken = sign("poop55")
  34. echo mytoken
  35.  
  36. if mytoken.verify():
  37.   echo "Token verifyed, now decoding"
  38.   echo "ID riped form token is: ", mytoken.decode()
  39.   echo "Now go copy it to https://jwt.io/ and its invalid signature for some reson, tryed both base64 encoded and not "
  40.  
  41. #FOR JWT I USED https://github.com/yglukhov/nim-jwt DUNO ANY ELSE :)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top