From 5a73bb414fafca464eb6a0b3ff305bb98d3ece9c Mon Sep 17 00:00:00 2001 From: haturatu Date: Fri, 9 Aug 2024 20:57:38 +0900 Subject: first commit --- app.ts | 84 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 app.ts (limited to 'app.ts') diff --git a/app.ts b/app.ts new file mode 100644 index 0000000..c80d508 --- /dev/null +++ b/app.ts @@ -0,0 +1,84 @@ +import { Hono } from "https://deno.land/x/hono/mod.ts"; +import { create, verify } from "https://deno.land/x/djwt@v3.0.2/mod.ts"; +import { crypto } from "https://deno.land/std@0.140.0/crypto/mod.ts"; +import { serve } from "https://deno.land/std@0.224.0/http/server.ts"; + +const app = new Hono(); + +// シークレットキーをファイルから読み込む +const loadKey = async () => { + try { + const keyData = await Deno.readFile("secret.key"); + return await crypto.subtle.importKey( + "raw", + keyData, + { name: "HMAC", hash: "SHA-512" }, + true, + ["sign", "verify"] + ); + } catch (error) { + console.error("Error loading key:", error); + throw error; + } +}; + +// ユーザーのログイン処理 +app.post("/login", async (c) => { + try { + const { username, password } = await c.req.json(); + + // ユーザー認証のロジック (ここでは仮の認証を使用) + if (username === "usesr" && password === "password") { + const key = await loadKey(); + + // JWTを生成 + const jwt = await create( + { alg: "HS512", typ: "JWT" }, + { username: username }, + key + ); + + return c.json({ message: "Login successful", token: jwt }); + } else { + return c.json({ message: "Invalid username or password" }, 401); + } + } catch (error) { + console.error("Login error:", error); + return c.json({ message: "Internal Server Error", error: error.message }, 500); + } +}); + +// JWT検証 +app.use("*", async (c, next) => { + if (c.req.method === "POST" && c.req.url.includes("/login")) { + await next(); + return; + } + + try { + const authHeader = c.req.headers.get("Authorization"); + if (!authHeader) { + return c.json({ message: "Authorization header missing" }, 401); + } + + const token = authHeader.replace("Bearer ", ""); + const key = await loadKey(); + const payload = await verify(token, key); + + c.req.state = { payload }; + await next(); + } catch (error) { + console.error("JWT verification error:", error); + return c.json({ message: "Unauthorized", error: error.message }, 401); + } +}); + +app.get("/", (c) => { + return c.json({ message: "Hello, world!" }); +}); + +// Honoサーバーの起動 +const port = 8787; +console.log(`Server running on http://localhost:${port}`); +await serve(app.fetch, { port }); + -- cgit v1.2.3