Skip to content

Instantly share code, notes, and snippets.

@kasramp

kasramp/index.js Secret

Created June 3, 2020 11:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kasramp/39b346972ab447fc250c890c28837fce to your computer and use it in GitHub Desktop.
Save kasramp/39b346972ab447fc250c890c28837fce to your computer and use it in GitHub Desktop.
import { validateJwt } from "https://deno.land/x/djwt/validate.ts";
import { makeJwt, setExpiration } from "https://deno.land/x/djwt/create.ts";
import { Application, Router } from "https://deno.land/x/oak/mod.ts";
const expirationMinute = 10;
const key = "dummy_key"
const getTokenExpirationAsMilliseconds = () => {
return new Date().getTime() + expirationMinute * 60 * 1000;
}
const payload = {
iss: "dummy_issuer",
exp: setExpiration(getTokenExpirationAsMilliseconds()),
}
const header = {
alg: "HS256",
typ: "JWT",
}
const generateToken = () => {
return makeJwt({ key, header, payload })
}
const validateToken = (token) => {
return validateJwt(token, key, { isThrowing: false });
}
const router = new Router();
const app = new Application();
router
.get("/generate", (context) => {
context.response.body = { token: generateToken() };
})
.post("/validate", async (context) => {
const { token } = (await context.request.body(true)).value;
context.response.body = await validateToken(token) ? { isValid: true } : { isValid: false };
});
app.use(router.routes());
app.use(router.allowedMethods());
await app.listen({ port: 8080 });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment