Search code examples
nginxluajwt

How to sign a JWT with a private key in LUA


I'm using the kubernetes nginx ingress controller

FROM k8s.gcr.io/ingress-nginx/controller:v0.40.2

trying to sign a JWT in my nginx.tmpl

I use lua-resty-jwt, it seems to be the commonly used lib for that

RUN luarocks install lua-resty-jwt

nginx.tmpl snippet :

local now = ngx.time()
local jwt = {
   header = {
      typ = "JWT",
      alg = "RS256",
      kid = "kid"
   },
   payload = {
      iss = "nginx",
      env = "dev",
      sub = "sub",
      client_id = "client_id",
      exp = now + 60,
   }
}

local private_key = "${privateKeyPemString}"
local signed_jwt = require("resty.jwt").sign(private_key, jwt)

When I try it, I receive the following error : lua entry thread aborted: runtime error: ./lualib/resty/jwt.lua:517: attempt to index local 'jwt_obj' (a nil value)

What am I doing wrong ? As far as I understand lua, I declared a local jwt which is a lua table, so not nil, am I right ?


Solution

  • I finally ended with creating a new method in openidc.lua, which apparently contains all necessary stuff to call jwt.lua. Not sure what's missing from calling it directly from nginx.tmpl though.