summaryrefslogtreecommitdiff
path: root/app.ts
diff options
context:
space:
mode:
authorhaturatu <taro@eyes4you.org>2024-08-09 20:57:38 +0900
committerhaturatu <taro@eyes4you.org>2024-08-09 20:57:38 +0900
commit5a73bb414fafca464eb6a0b3ff305bb98d3ece9c (patch)
tree9d2b5cdbf65a1f5a1a03fd3f47eef219f8ea4bea /app.ts
first commit
Diffstat (limited to 'app.ts')
-rw-r--r--app.ts84
1 files changed, 84 insertions, 0 deletions
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 });
+