-
-
Save kasramp/39b346972ab447fc250c890c28837fce to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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