How to Create JSON Web Token in Deno with CryptoKey, and djwt
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.
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.
Next, let’s create the class. I’ll call it JsonWebToken. Define a property to store the CryptoKey, I’ll call it key
.
Next, let’s create a function to generate the CryptoKey. It’s pretty simple tough, don’t ya worry!
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.
Continuing from the previous break. Let’s create the set function.
Here’s the logic of the function:
- Get the
exported key
from.env
file, throw an error if the key is undefined - Since the key in
.env
is base64 encoded, so we have to convert it to its original value, using TextDecoder and base64 decoder. - Then, convert that string into ArrayBuffer format, so that we can use it in the CryptoKey import function.
- Set the
imported key
(CryptoKey) to the class property.
Next, let’s create the exportKey
function.
Here’s the logic of the function:
- Generate a CryptoKey that we’re going to export
- Use the CryptoKey
import
function to convert the CryptoKey into ArrayBuffer type. - In order to convert it to base64 encoded, we have to convert the ArrayBuffer to a string.
- 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 sign
first.
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.
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.
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.
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.