forked from elliotBraem/efizzybot
-
Notifications
You must be signed in to change notification settings - Fork 7
Upgrade staging #184
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Upgrade staging #184
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
6f7509a
moderation handle platform_user_id and near account id
elliotBraem a70baa4
standardize activity
elliotBraem cd04520
fmt
elliotBraem 6d30be2
delete tests
elliotBraem b02e5c6
fix shared types
elliotBraem e03f9bb
activity leaderboard
elliotBraem bf21834
throw not error
elliotBraem 26c2b29
fix services
elliotBraem bce477e
feat: use tanstack table for leaderboard (#183)
itexpert120 76c37da
Merge branch 'main' into staging
elliotBraem ccc02ec
fix: mobile layout improvement feed page (#185)
itexpert120 d38e1cc
Refactors create feed flow, steps use router (#188)
elliotBraem f68e1cc
Implements JWT auth flow with 7 day tokens (#187)
elliotBraem 7b58d30
clean up
elliotBraem f797c5a
fix error types
elliotBraem bfb2e6c
import error
elliotBraem f8e6c2c
fix auth requests
elliotBraem a52e094
fix: toast colors not correct (#189)
itexpert120 37a29a3
fix feed
elliotBraem 2123450
fix zod type
elliotBraem 83c1045
fix @ and config
elliotBraem 765eb74
fmt
elliotBraem File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,54 +1,31 @@ | ||
| import { Context, MiddlewareHandler, Next } from "hono"; | ||
| import { verify } from "near-sign-verify"; | ||
| import { verify } from "hono/jwt"; | ||
| import { getCookie } from "hono/cookie"; | ||
|
|
||
| export function createAuthMiddleware(): MiddlewareHandler { | ||
| return async (c: Context, next: Next) => { | ||
| const method = c.req.method; | ||
| const token = getCookie(c, "token"); | ||
| let accountId: string | null = null; | ||
|
|
||
| if (method === "GET") { | ||
| const nearAccountHeader = c.req.header("X-Near-Account"); | ||
| if ( | ||
| nearAccountHeader && | ||
| nearAccountHeader.toLowerCase() !== "anonymous" | ||
| ) { | ||
| accountId = nearAccountHeader; | ||
| if (token) { | ||
| const secret = process.env.JWT_SECRET; | ||
| if (!secret) { | ||
| console.error("JWT_SECRET is not set."); | ||
| c.status(500); | ||
| return c.json({ error: "Internal Server Error" }); | ||
| } | ||
| try { | ||
| const decodedPayload = await verify(token, secret); | ||
| if (decodedPayload && typeof decodedPayload.sub === "string") { | ||
| accountId = decodedPayload.sub; | ||
| } | ||
| } catch (error) { | ||
| // Invalid token, proceed as anonymous | ||
| console.warn("JWT verification failed:", error); | ||
| } | ||
| // If header is missing or "anonymous", accountId remains null | ||
| c.set("accountId", accountId); | ||
| await next(); | ||
| return; | ||
| } | ||
|
|
||
| // For non-GET requests (POST, PUT, DELETE, PATCH, etc.) | ||
| const authHeader = c.req.header("Authorization"); | ||
| if (!authHeader || !authHeader.startsWith("Bearer ")) { | ||
| c.status(401); | ||
| return c.json({ | ||
| error: "Unauthorized", | ||
| details: "Missing or malformed Authorization header.", | ||
| }); | ||
| } | ||
|
|
||
| const token = authHeader.substring(7); // Remove "Bearer " | ||
|
|
||
| try { | ||
| const verificationResult = await verify(token, { | ||
| expectedRecipient: "curatefun.near", | ||
| requireFullAccessKey: false, | ||
| nonceMaxAge: 300000, // 5 mins | ||
| }); | ||
|
|
||
| accountId = verificationResult.accountId; | ||
| c.set("accountId", accountId); | ||
| await next(); | ||
| } catch (error) { | ||
| console.error("Token verification error:", error); | ||
| c.status(401); | ||
| return c.json({ | ||
| error: "Unauthorized", | ||
| details: "Invalid token signature or recipient.", | ||
| }); | ||
| } | ||
| c.set("accountId", accountId); | ||
| await next(); | ||
| }; | ||
| } |
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| import { Hono } from "hono"; | ||
| import { zValidator } from "@hono/zod-validator"; | ||
| import { z } from "zod"; | ||
| import { AuthService } from "../../services/auth.service"; | ||
| import { Env } from "../../types/app"; | ||
| import { setCookie } from "hono/cookie"; | ||
|
|
||
| export const authRoutes = new Hono<Env>(); | ||
|
|
||
| const CreateAuthRequestSchema = z.object({ | ||
| accountId: z.string(), | ||
| }); | ||
|
|
||
| const VerifyAuthRequestSchema = z.object({ | ||
| token: z.string(), | ||
| accountId: z.string(), | ||
| }); | ||
|
|
||
| authRoutes.post( | ||
| "/initiate-login", | ||
| zValidator("json", CreateAuthRequestSchema), | ||
| async (c) => { | ||
| const payload = c.req.valid("json"); | ||
| const sp = c.var.sp; | ||
| const authService = sp.getService<AuthService>("authService"); | ||
| const result = await authService.createAuthRequest(payload); | ||
| return c.json(result); | ||
| }, | ||
| ); | ||
|
|
||
| authRoutes.post( | ||
| "/verify-login", | ||
| zValidator("json", VerifyAuthRequestSchema), | ||
| async (c) => { | ||
| const payload = c.req.valid("json"); | ||
| const sp = c.var.sp; | ||
| const authService = sp.getService<AuthService>("authService"); | ||
| try { | ||
| const { jwt } = await authService.verifyAuthRequest(payload); | ||
| setCookie(c, "token", jwt, { | ||
| httpOnly: true, | ||
| secure: process.env.NODE_ENV === "production", | ||
| sameSite: "Strict", | ||
| path: "/", | ||
| maxAge: 60 * 60 * 24 * 7, // 7 days | ||
| }); | ||
| return c.json({ success: true }); | ||
| } catch (error: unknown) { | ||
| c.status(401); | ||
| return c.json({ | ||
| success: false, | ||
| error: error instanceof Error ? error.message : "Authentication failed", | ||
| }); | ||
| } | ||
| }, | ||
| ); | ||
|
|
||
| authRoutes.post("/logout", async (c) => { | ||
| setCookie(c, "token", "", { | ||
| httpOnly: true, | ||
| secure: process.env.NODE_ENV === "production", | ||
| sameSite: "Strict", | ||
| path: "/", | ||
| maxAge: 0, | ||
| }); | ||
| return c.json({ success: true }); | ||
| }); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Avoid exposing internal error details in authentication responses.
For security reasons, authentication endpoints should return generic error messages to prevent information leakage about the system's internal state.
Apply this diff to return a generic error message:
} catch (error: unknown) { c.status(401); return c.json({ success: false, - error: error instanceof Error ? error.message : "Authentication failed", + error: "Authentication failed", }); }📝 Committable suggestion
🤖 Prompt for AI Agents