Repository navigation
feat: move auth to Coolify Postgres #88
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
Show all changes
5 commits
Select commit
Hold shift + click to select a range
5810762
feat: move auth to app-owned postgres
Skeyelab b1cb967
ci: remove neon auth build env
Skeyelab bca647b
fix: harden app-owned auth
Skeyelab 29948b0
fix: use db-backed auth throttling
Skeyelab bdaf2c1
fix: make auth rate limit increments atomic
Skeyelab 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
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
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,3 +1,7 @@ | ||
| import { auth } from '@/app/lib/auth/server'; | ||
| export function GET() { | ||
| return Response.json({ ok: false, error: 'Unknown auth endpoint' }, { status: 404 }); | ||
| } | ||
|
|
||
| export const { GET, POST } = auth.handler(); | ||
| export function POST() { | ||
| return Response.json({ ok: false, error: 'Unknown auth endpoint' }, { status: 404 }); | ||
| } |
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,47 @@ | ||
| import { NextResponse } from 'next/server'; | ||
|
|
||
| import { SESSION_COOKIE_NAME } from '@/app/lib/auth/constants'; | ||
|
|
||
| export type JsonBody = Record<string, unknown>; | ||
|
|
||
| export async function readJson(request: Request): Promise<JsonBody | null> { | ||
| try { | ||
| const body = await request.json(); | ||
| return typeof body === 'object' && body !== null ? (body as JsonBody) : null; | ||
| } catch { | ||
| return null; | ||
| } | ||
| } | ||
|
|
||
| export function stringField(body: JsonBody | null, key: string) { | ||
| const value = body?.[key]; | ||
| return typeof value === 'string' ? value : ''; | ||
| } | ||
|
|
||
| export function okWithSession(data: unknown, token: string, expiresAt: Date) { | ||
| const response = NextResponse.json({ ok: true, data }); | ||
| response.cookies.set({ | ||
| name: SESSION_COOKIE_NAME, | ||
| value: token, | ||
| httpOnly: true, | ||
| sameSite: 'lax', | ||
| secure: process.env.NODE_ENV === 'production', | ||
| path: '/', | ||
| expires: expiresAt, | ||
| }); | ||
| return response; | ||
| } | ||
|
|
||
| export function clearSessionResponse() { | ||
| const response = NextResponse.json({ ok: true }); | ||
| response.cookies.set({ | ||
| name: SESSION_COOKIE_NAME, | ||
| value: '', | ||
| httpOnly: true, | ||
| sameSite: 'lax', | ||
| secure: process.env.NODE_ENV === 'production', | ||
| path: '/', | ||
| maxAge: 0, | ||
| }); | ||
| 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,8 @@ | ||
| import { requestPasswordReset } from '@/app/lib/auth/server'; | ||
| import { readJson, stringField } from '../_shared'; | ||
|
|
||
| export async function POST(request: Request) { | ||
| const body = await readJson(request); | ||
| await requestPasswordReset(stringField(body, 'email')); | ||
| return Response.json({ ok: true }); | ||
| } |
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,11 +1,3 @@ | ||
| import { auth } from '@/app/lib/auth/server'; | ||
|
|
||
| const handler = auth.handler(); | ||
|
|
||
| export async function GET(request: Request) { | ||
| return handler.GET(request, { params: Promise.resolve({ path: [] }) }); | ||
| } | ||
|
|
||
| export async function POST(request: Request) { | ||
| return handler.POST(request, { params: Promise.resolve({ path: [] }) }); | ||
| export function GET() { | ||
| return Response.json({ ok: true, service: 'clawproxy-auth' }); | ||
| } |
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,53 @@ | ||
| import { NextResponse } from 'next/server'; | ||
|
|
||
| import { | ||
| checkAuthRateLimit, | ||
| clearAuthRateLimit, | ||
| recordAuthRateLimitAttempt, | ||
| } from '@/app/lib/auth/rate-limit'; | ||
| import { signInWithPassword } from '@/app/lib/auth/server'; | ||
| import { okWithSession, readJson, stringField } from '../_shared'; | ||
|
|
||
| const SIGN_IN_WINDOW_MS = 15 * 60 * 1000; | ||
| const SIGN_IN_MAX_FAILED_ATTEMPTS = 5; | ||
| const SIGN_IN_KIND = 'sign-in'; | ||
|
|
||
| function signInKey(email: string) { | ||
| return `${SIGN_IN_KIND}:${email.trim().toLowerCase()}`; | ||
| } | ||
|
|
||
| function tooManyAttemptsResponse(retryAfterSeconds: number) { | ||
| return NextResponse.json( | ||
| { ok: false, error: 'Too many sign-in attempts' }, | ||
| { | ||
| status: 429, | ||
| headers: { 'retry-after': String(retryAfterSeconds) }, | ||
| } | ||
| ); | ||
| } | ||
|
|
||
| export async function POST(request: Request) { | ||
| const body = await readJson(request); | ||
| const email = stringField(body, 'email'); | ||
| const key = signInKey(email); | ||
|
|
||
| const rateLimit = await checkAuthRateLimit({ | ||
| key, | ||
| kind: SIGN_IN_KIND, | ||
| maxAttempts: SIGN_IN_MAX_FAILED_ATTEMPTS, | ||
| windowMs: SIGN_IN_WINDOW_MS, | ||
| }); | ||
| if (rateLimit.limited) { | ||
| return tooManyAttemptsResponse(rateLimit.retryAfterSeconds); | ||
| } | ||
|
|
||
| const result = await signInWithPassword(email, stringField(body, 'password')); | ||
|
|
||
| if (!result.ok) { | ||
| await recordAuthRateLimitAttempt({ key, kind: SIGN_IN_KIND, windowMs: SIGN_IN_WINDOW_MS }); | ||
| return NextResponse.json({ ok: false, error: result.error }, { status: result.status ?? 401 }); | ||
| } | ||
|
|
||
| await clearAuthRateLimit(key); | ||
| return okWithSession({ user: result.user }, result.token, result.expiresAt); | ||
| } | ||
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,15 @@ | ||
| import { SESSION_COOKIE_NAME } from '@/app/lib/auth/constants'; | ||
| import { signOutSession } from '@/app/lib/auth/server'; | ||
| import { clearSessionResponse } from '../_shared'; | ||
|
|
||
| export async function POST(request: Request) { | ||
| const token = request.headers | ||
| .get('cookie') | ||
| ?.split(';') | ||
| .map((part) => part.trim()) | ||
| .find((part) => part.startsWith(`${SESSION_COOKIE_NAME}=`)) | ||
| ?.slice(SESSION_COOKIE_NAME.length + 1); | ||
|
|
||
| await signOutSession(token); | ||
| return clearSessionResponse(); | ||
| } |
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,54 @@ | ||
| import { NextResponse } from 'next/server'; | ||
|
|
||
| import { | ||
| checkAuthRateLimit, | ||
| recordAuthRateLimitAttempt, | ||
| } from '@/app/lib/auth/rate-limit'; | ||
| import { signUpWithPassword } from '@/app/lib/auth/server'; | ||
| import { okWithSession, readJson, stringField } from '../_shared'; | ||
|
|
||
| const SIGN_UP_WINDOW_MS = 15 * 60 * 1000; | ||
| const SIGN_UP_MAX_ATTEMPTS = 5; | ||
| const SIGN_UP_KIND = 'sign-up'; | ||
| const SIGN_UP_GLOBAL_KEY = `${SIGN_UP_KIND}:global`; | ||
|
|
||
| function tooManyAttemptsResponse(retryAfterSeconds: number) { | ||
| return NextResponse.json( | ||
| { ok: false, error: 'Too many sign-up attempts' }, | ||
| { | ||
| status: 429, | ||
| headers: { 'retry-after': String(retryAfterSeconds) }, | ||
| } | ||
| ); | ||
| } | ||
|
|
||
| export async function POST(request: Request) { | ||
| const body = await readJson(request); | ||
| const rateLimit = await checkAuthRateLimit({ | ||
| key: SIGN_UP_GLOBAL_KEY, | ||
| kind: SIGN_UP_KIND, | ||
| maxAttempts: SIGN_UP_MAX_ATTEMPTS, | ||
| windowMs: SIGN_UP_WINDOW_MS, | ||
| }); | ||
| if (rateLimit.limited) { | ||
| return tooManyAttemptsResponse(rateLimit.retryAfterSeconds); | ||
| } | ||
|
|
||
| await recordAuthRateLimitAttempt({ | ||
| key: SIGN_UP_GLOBAL_KEY, | ||
| kind: SIGN_UP_KIND, | ||
| windowMs: SIGN_UP_WINDOW_MS, | ||
| }); | ||
|
|
||
| const result = await signUpWithPassword({ | ||
| email: stringField(body, 'email'), | ||
| password: stringField(body, 'password'), | ||
| name: stringField(body, 'name'), | ||
| }); | ||
|
|
||
| if (!result.ok) { | ||
| return NextResponse.json({ ok: false, error: result.error }, { status: result.status ?? 400 }); | ||
| } | ||
|
|
||
| return okWithSession({ user: result.user }, result.token, result.expiresAt); | ||
| } |
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
Oops, something went wrong.
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.
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.
Sign-in/sign-up throttling bypassable via spoofed X-Forwarded-For and per-process stateResolved — no longer flagged as of
29948b0.