forked from manolingam/the-valhalla
-
Notifications
You must be signed in to change notification settings - Fork 0
feat: retained member sessions #6
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
Changes from all commits
Commits
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 |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| import { NextResponse } from "next/server"; | ||
|
|
||
| import { clearAuthCookies, getSameOrigin } from "../../shared/session"; | ||
|
|
||
| export async function POST(request: Request) { | ||
| if (!getSameOrigin(request)) { | ||
| return NextResponse.json( | ||
| { error: "Invalid request origin" }, | ||
| { status: 403 }, | ||
| ); | ||
| } | ||
|
|
||
| const response = NextResponse.json( | ||
| { authenticated: false }, | ||
| { headers: { "Cache-Control": "no-store" } }, | ||
| ); | ||
| clearAuthCookies(response); | ||
| return response; | ||
| } |
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,96 @@ | ||
| import { NextResponse } from "next/server"; | ||
| import { getAddress, isAddress } from "viem"; | ||
| import { createSiweMessage, generateSiweNonce } from "viem/siwe"; | ||
| import { gnosis } from "viem/chains"; | ||
|
|
||
| import { | ||
| authRateLimitResponse, | ||
| checkAuthGlobalBudget, | ||
| checkAuthRateLimit, | ||
| } from "../../shared/authRateLimit"; | ||
| import { | ||
| createChallengeToken, | ||
| getMessageDigest, | ||
| getSameOrigin, | ||
| setChallengeCookie, | ||
| } from "../../shared/session"; | ||
|
|
||
| type MessageRequestBody = { | ||
| address: string; | ||
| chainId: number; | ||
| }; | ||
|
|
||
| function isMessageRequestBody(value: unknown): value is MessageRequestBody { | ||
| if (!value || typeof value !== "object") return false; | ||
|
|
||
| const candidate = value as Partial<MessageRequestBody>; | ||
| return ( | ||
| typeof candidate.address === "string" && | ||
| isAddress(candidate.address) && | ||
| candidate.chainId === gnosis.id | ||
| ); | ||
| } | ||
|
|
||
| export async function POST(request: Request) { | ||
| const origin = getSameOrigin(request); | ||
| if (!origin) { | ||
| return NextResponse.json( | ||
| { error: "Invalid request origin" }, | ||
| { status: 403 }, | ||
| ); | ||
| } | ||
|
|
||
| let body: MessageRequestBody; | ||
|
|
||
| try { | ||
| const parsed = (await request.json()) as unknown; | ||
| if (!isMessageRequestBody(parsed)) { | ||
| return NextResponse.json( | ||
| { error: "Connect a wallet on Gnosis Chain to continue." }, | ||
| { status: 400 }, | ||
| ); | ||
| } | ||
| body = parsed; | ||
| } catch { | ||
| return NextResponse.json({ error: "Invalid JSON" }, { status: 400 }); | ||
| } | ||
|
|
||
| const address = getAddress(body.address); | ||
| const rateLimit = checkAuthRateLimit("message", address, 30); | ||
| if (!rateLimit.allowed) { | ||
| return authRateLimitResponse(rateLimit.retryAfterSeconds); | ||
| } | ||
|
|
||
| const globalLimit = checkAuthGlobalBudget("message", 300); | ||
| if (!globalLimit.allowed) { | ||
| return authRateLimitResponse(globalLimit.retryAfterSeconds); | ||
| } | ||
|
|
||
| const nonce = generateSiweNonce(); | ||
| const now = new Date(); | ||
| const originUrl = new URL(origin); | ||
| const message = createSiweMessage({ | ||
| address, | ||
| chainId: gnosis.id, | ||
| domain: originUrl.host, | ||
| expirationTime: new Date(now.getTime() + 5 * 60 * 1000), | ||
| issuedAt: now, | ||
| nonce, | ||
| scheme: originUrl.protocol.slice(0, -1), | ||
| statement: "Sign in to the RaidGuild Guild Archive.", | ||
| uri: origin, | ||
| version: "1", | ||
| }); | ||
| const challengeToken = await createChallengeToken({ | ||
| address, | ||
| messageDigest: getMessageDigest(message), | ||
| nonce, | ||
| }); | ||
| const response = NextResponse.json( | ||
| { message }, | ||
| { headers: { "Cache-Control": "no-store" } }, | ||
| ); | ||
|
|
||
| setChallengeCookie(response, challengeToken); | ||
| return response; | ||
| } | ||
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,59 @@ | ||
| import { NextResponse } from "next/server"; | ||
|
|
||
| import { | ||
| authRateLimitResponse, | ||
| checkAuthRateLimit, | ||
| } from "../../shared/authRateLimit"; | ||
| import { | ||
| isEligibleMemberAddress, | ||
| logServerError, | ||
| } from "../../shared/memberAuth"; | ||
| import { | ||
| clearSessionCookie, | ||
| createSessionToken, | ||
| readSessionAddress, | ||
| setSessionCookie, | ||
| } from "../../shared/session"; | ||
|
|
||
| export async function GET() { | ||
| const address = await readSessionAddress(); | ||
|
|
||
| if (!address) { | ||
| const response = NextResponse.json( | ||
| { authenticated: false }, | ||
| { headers: { "Cache-Control": "no-store" } }, | ||
| ); | ||
| clearSessionCookie(response); | ||
| return response; | ||
| } | ||
|
|
||
| try { | ||
| const rateLimit = checkAuthRateLimit("session", address, 60); | ||
| if (!rateLimit.allowed) { | ||
| return authRateLimitResponse(rateLimit.retryAfterSeconds); | ||
| } | ||
|
|
||
| if (!(await isEligibleMemberAddress(address))) { | ||
| const response = NextResponse.json( | ||
| { authenticated: false }, | ||
| { headers: { "Cache-Control": "no-store" } }, | ||
| ); | ||
| clearSessionCookie(response); | ||
| return response; | ||
| } | ||
|
|
||
| const refreshedToken = await createSessionToken(address); | ||
| const response = NextResponse.json( | ||
| { authenticated: true, address }, | ||
| { headers: { "Cache-Control": "no-store" } }, | ||
| ); | ||
| setSessionCookie(response, refreshedToken); | ||
| return response; | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| } catch (error: unknown) { | ||
| logServerError("Error restoring wallet session", error); | ||
| return NextResponse.json( | ||
| { error: "Unable to restore your session right now." }, | ||
| { status: 500, headers: { "Cache-Control": "no-store" } }, | ||
| ); | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.