How to Create JSON Web Token in Deno with CryptoKey, and djwt

Novri Anto
3 min readSep 24, 2022

--

JWT Key Logo

With the help of CryptoKey API, we’ll create JSON Web Token on Deno Runtime. But, what is CryptoKey used for? Well, we’re gonna use that to generate our JWT Key.

This is a huge difference from Node.js Runtime. In Node.js, we use public and private keys that we can generate by using a terminal or from an online generator and store it inside a .env file. Well, in Deno we have to use CryptoKey to generate our Key.

I guess you already know what JWT is, and why you use it. So, without any further useless sentences, let’s create JSON Web Token.

I’ve created a boilerplate for the Deno server using Oak with non-jwt. If you don’t know what Oak is, it’s just a popular, fast, small framework inspired by koa. So, Oak is a version of Koa but for Deno.

Dependencies

I changed some of the dependencies variable names, to make them more understandable.

Dependencies

First and First

Let’s create the jwt core logic. So, create jwt.ts file inside utils directory, or wherever you want to put that thing.

Alright, let’s define an interface for our token payload.

Interface For JWT Payload

Next, let’s create the class. I’ll call it JsonWebToken. Define a property to store the CryptoKey, I’ll call it key.

Define a property inside the JSON Web Token Class

Next, let’s create a function to generate the CryptoKey. It’s pretty simple tough, don’t ya worry!

Generate the CryptoKey Func

See, easy right? Alright, let’s continue to the next function, which is a function to set the imported CryptoKey to the key property that we created previously.

Confuse?

Alright, alright. I know, you must be confused with theimported CryptoKey, or exported CryptoKey. Hope this Image below explains everything for ya.

Generate the CryptoKey and export it as base64 encoded in order to store it in .env file
Import the key and store it in the class property

Continuing from the previous break. Let’s create the set function.

Import and set the exported CyrptoKey to the key property

Here’s the logic of the function:

  1. Get the exported key from .env file, throw an error if the key is undefined
  2. Since the key in .env is base64 encoded, so we have to convert it to its original value, using TextDecoder and base64 decoder.
  3. Then, convert that string into ArrayBuffer format, so that we can use it in the CryptoKey import function.
  4. Set the imported key (CryptoKey) to the class property.

Next, let’s create the exportKey function.

Export CryptoKey to base64 encoded

Here’s the logic of the function:

  1. Generate a CryptoKey that we’re going to export
  2. Use the CryptoKey import function to convert the CryptoKey into ArrayBuffer type.
  3. In order to convert it to base64 encoded, we have to convert the ArrayBuffer to a string.
  4. The last one encodes the string to base64 encoded and return it.

Nothing hard, right?

Let’s carry on.

The last 2 functions that we’re going to create are sign, and verify jwt. Let’s start with signfirst.

Sign JWT Function

createJWT function is required 2parameters, first the JWT Header, the second one is the JWT payload. As you can see, I added an expiresIn property into the payload in order to check if the jwt is expired or not.

Move on to the next function, which is verify function.

Verify JWT Function

The logic of the function is pretty simple. First, throw an error if the hasn’t been set yet, and then run the verifyJWT function from the djwt module to verify the function, finally check if the expires in whether the token is valid or not.

Initialize the Class in a variable

Full Code

Full Code

The last step is to create a new file inside src directory to print the exported CryptoKey which later we can store in the .env file.

After you stored it, then in the main.ts file, let’s call the setKey function to set the key in the class property every time the app is running.

Set the key in the class property every time the app is running

That’s It!

So, that’s how I create JSON Web Token for my Deno Web Server. If you have a better idea, please let me know. I’m listening, and I’m happy to hear from the community🤘🚀.

The full code is on my Github.

--

--