470,810 Members | 1,266 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,810 developers. It's quick & easy.

How do i create and validate Json Web Token in Deno?

7 Nibble
How do i create and validate Json Web Token in Deno?
Oct 16 '20 #1
3 4676
The Internet standard used to construct tokens for an application is JSON Web Token. JSON data is owned by these tokens and is cryptographically signed. JWT is a good way of sending information between parties securely. And it is possible to sign JWTs, because you can be sure that the senders are who they claim they are. And you can also check that the content has not been tampered with, as the signature is created using the header and the payload.

JWT can contain user information in the payload and can be used to authenticate the user in the session as well. First, let's set up a Deno server to accept requests, since we are using the Oak system for it, as you can see below, it is very simple and few lines of codes.

// index.ts
import { Application, Router } from "https://deno.land/x/oak/mod.ts";

const router = new Router();
router
.get("/", (context) => {
context.response.body = "JWT Example!";
})

const app = new Application();
app.use(router.routes());
app.use(router.allowedMethods());

await app.listen({ port: 8000 });

Let's import djwt functions to generate JWT token once our programme is ready to accept request, we can use a secret key in the below code, expiry time for JWT token in 1 hour from the time programme will run and we are using HS256 algorithm.

You can now get a brand new token on http:/localhost:8000 / generate by adding the below code to index.ts and upgrading the router as shown below.

// index.ts
...

import { makeJwt, setExpiration, Jose, Payload } from "https://deno.land/x/djwt/create.ts";

const key = "secret-key";
const payload: Payload = {
iss: "Jon Doe",
exp: setExpiration(new Date().getTime() + 60000),
};
const header: Jose = {
alg: "HS256",
typ: "JWT",
};


const router = new Router();
router
.get("/", (context) => {
context.response.body = "JWT Example!";
})
.get("/generate", (context) => {
context.response.body = makeJwt({ header, payload, key }) + "\n";
})


I hope this would be helpful to you :)
Oct 16 '20 #2
Ishan Shah
47 32bit
To generate the JWT token in Deno
First, set up a Deno server to accept requests for it, we will use the Oak framework which is quite simple and a few lines of codes as following :

Expand|Select|Wrap|Line Numbers
  1. //index.ts file code
  2. import { Application, Router } from "https://deno.land/x/oak/mod.ts";
  3.  
  4. const router = new Router();
  5. router
  6.   .get("/", (context) => {
  7.     context.response.body = "JWT Demo";
  8.   })
  9.  
  10. const app = new Application();
  11. app.use(router.routes());
  12. app.use(router.allowedMethods());
  13.  
  14. await app.listen({ port: 8000 });
  15.  
Add the following code in index.ts and update the router as shown below, we can now get a brand new token on http;//localhost:800/generate

Expand|Select|Wrap|Line Numbers
  1. // index.ts file code
  2. ...
  3.  
  4. import { makeJwt, setExpiration, Jose, Payload } from "https://deno.land/x/djwt/create.ts";
  5.  
  6. const key = "secret-key";
  7. const payload: Payload = {
  8.   iss: "Amit",
  9.   exp: setExpiration(new Date().getTime() + 60000),
  10. };
  11. const header: Jose = {
  12.   alg: "HS256",
  13.   typ: "JWT",
  14. };
  15.  
  16.  
  17. const router = new Router();
  18. router
  19.   .get("/", (context) => {
  20.     context.response.body = "JWT Demo";
  21.   })
  22.   .get("/generate", (context) => {
  23.     context.response.body = makeJwt({ header, payload, key }) + "\n";
  24.   })
  25.  
Validating a JWT token

After you get a JWT token you can validate the token by validateJwt function in djwt, let's import the validateJwt and add one more route/validate/:token

Now you can confirm any token by passing it to a route like-http://localhost:800/validate/jwt_token
where jwt_token is a place holder, so it's mandatory to replace it with a real JWT token

Expand|Select|Wrap|Line Numbers
  1. // index.ts file code
  2. ...
  3.  
  4. import { validateJwt } from "https://deno.land/x/djwt/validate.ts";
  5.  
  6. ...
  7.  
  8. router
  9.   .get("/", (context) => {
  10.     context.response.body = "JWT Demo";
  11.   })
  12.   .get("/generate", (context) => {
  13.     context.response.body = makeJwt({ header, payload, key }) + "\n";
  14.   })
  15.   .get("/validate/:token", async (context) => {
  16.     if ( context.params && context.params.token && (await validateJwt(context.params.token, key)).isValid) {
  17.       context.response.body = "Correct JWT\n";
  18.     } else {
  19.       context.response.body = "Incorrecct JWT\n";
  20.     }
  21.   });
  22.  
  23. ...
  24.  
  25.  
  26.  
  27.  
  28.  
  29.  
  30. ...
Jan 1 '21 #3
scode
1 Bit
you can check this youtube tutorial because it is explained here:

https://www.youtube.com/watch?v=9W_BUMeMQI8
May 6 '21 #4

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

20 posts views Thread by Luke Matuszewski | last post: by
8 posts views Thread by MD | last post: by
5 posts views Thread by Tom Cole | last post: by
4 posts views Thread by pistacchio | last post: by
1 post views Thread by SirCodesALot | last post: by
Claus Mygind
3 posts views Thread by Claus Mygind | last post: by
reply views Thread by mihailmihai484 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.