Created
June 7, 2020 08:21
-
-
Save masalib/af143b50eb994e54a60a1b67c73880b8 to your computer and use it in GitHub Desktop.
DenoでJWTの認証(ブログ説明用のソース)
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 { config } from "https://deno.land/x/dotenv/mod.ts"; | |
import users from "../users.ts"; | |
import { makeJwt, setExpiration, Jose, Payload } from "https://deno.land/x/djwt/create.ts" | |
import { validateJwt } from "https://deno.land/x/djwt/validate.ts" | |
import { Context } from "https://deno.land/x/oak/mod.ts"; | |
import * as bcrypt from "https://deno.land/x/bcrypt/mod.ts"; | |
const key = "xxxxxxsome-random-secret-keyxxxxxxxxx"; | |
const header: Jose = { | |
alg: "HS256", | |
typ: "JWT", | |
} | |
// @desc login | |
// @route POST /login | |
const login = async ({ request, response }: { request: any , response: any }) => { | |
if (!request.hasBody){ | |
response.status = 400 | |
response.body = { | |
success: false, | |
msg: 'No Data' | |
} | |
}else { | |
const body = await request.body() | |
console.log(body) | |
console.log(body.value.username) | |
console.log(body.value.password) | |
//const hash = await bcrypt.hash(body.value.password); | |
//console.log(hash) | |
for (const user of users) { | |
//if (body.value.username === user.username && body.value.password === user.password) { | |
if (body.value.username === user.username && await bcrypt.compare(body.value.password, user.password)) { | |
const payload: Payload = { | |
iss: user.username, | |
//exp: setExpiration(new Date().getTime() + 60000), //サンプルだと60秒だった。これは短いので伸ばす | |
exp: setExpiration(new Date().getTime() + 3600000), | |
} | |
// Create JWT and send it to user | |
const jwt = makeJwt({key, header, payload}); | |
if (jwt) { | |
response.status = 200; | |
response.body = { | |
id: user.id, | |
username: user.username, | |
jwt, | |
} | |
} else { | |
response.status = 500; | |
response.body = { | |
message: 'Internal server error' | |
} | |
} | |
return; | |
} | |
} | |
//DBに認証して失敗した場合 | |
response.status = 422; | |
response.body = { | |
message: 'Invalid username or password' | |
}; | |
} | |
} | |
// @desc auth | |
// @route POST /auth | |
const auth = async ({ request, response }: { request: any , response: any }) => { | |
response.status = 200 | |
response.body = { | |
success: true, | |
data:"auth success" | |
} | |
} | |
const authMiddleware = async (ctx: Context, next: any) => { | |
console.log("authMiddleware start") | |
const headers: Headers = ctx.request.headers; | |
// Taking JWT from Authorization header and comparing if it is valid JWT token, if YES - we continue, | |
// otherwise we return with status code 401 | |
const authorization = headers.get('Authorization') | |
console.log("authMiddleware authorization:" + authorization) | |
if (!authorization) { | |
console.log("authorization Noting" ) | |
ctx.response.status = 401; | |
return; | |
} | |
const jwt = authorization.split(' ')[1]; | |
console.log("jwt:" + jwt ) | |
if (!jwt) { | |
console.log("jwt Noting" ) | |
ctx.response.status = 401; | |
return; | |
} | |
if (await validateJwt(jwt, key, {isThrowing: false})){ | |
await next(); | |
return; | |
} | |
console.log("validateJwt false" ) | |
ctx.response.status = 401; | |
ctx.response.body = {message: 'Invalid jwt token'}; | |
} | |
export {login ,auth,authMiddleware} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment