CI (SIAX Cloud) / integration (push) Skipped
CI (SIAX Cloud) / sonar (push) Skipped
masterplan-lock / check (push) Successful in 3s
CI (SIAX Cloud) / sonar (pull_request) Skipped
masterplan-lock / check (pull_request) Successful in 5s
Security Scan (SIAX Cloud) / security (push) Successful in 14s
CI (SIAX Cloud) / security (push) Successful in 21s
Security Scan (SIAX Cloud) / security (pull_request) Successful in 19s
CI (SIAX Cloud) / security (pull_request) Successful in 23s
CI (SIAX Cloud) / integration (pull_request) Successful in 28s
CI (SIAX Cloud) / quality (push) Successful in 48s
CI (SIAX Cloud) / quality (pull_request) Successful in 45s
The portal had no way to obtain a verified principal: /api/0pt/* requires a Bearer token, but the browser only had the dev-only HMAC cookie. - GET /api/auth/login: Authorization Code + PKCE against Zitadel. - GET /api/auth/callback: code exchange, id_token verification (issuer, audience, nonce) and a signed session cookie with sub + tenant. - middleware accepts the signed session as a verified principal; Bearer wins. - dev login POST is refused in production. - Sign-in link in the portal shell.
96 lines
3.2 KiB
TypeScript
96 lines
3.2 KiB
TypeScript
import { timingSafeEqual } from 'node:crypto';
|
|
import { NextRequest, NextResponse } from 'next/server';
|
|
import { signSession, SESSION_COOKIE } from '@/lib/auth';
|
|
import {
|
|
OIDC_STATE_COOKIE,
|
|
appBaseUrl,
|
|
authorizeUrl,
|
|
createOidcState,
|
|
createPkce,
|
|
readOidcConfig,
|
|
} from '@/lib/oidc';
|
|
|
|
export const runtime = 'nodejs';
|
|
|
|
function safeNext(value: string | null): string {
|
|
if (!value) return '/portal/dokument';
|
|
if (!value.startsWith('/') || value.startsWith('//')) return '/portal/dokument';
|
|
return value;
|
|
}
|
|
|
|
/**
|
|
* GET /api/auth/login — start the Zitadel OIDC Authorization Code + PKCE flow.
|
|
* Redirects back to /api/auth/callback, which sets the verified session cookie.
|
|
*/
|
|
export async function GET(request: NextRequest) {
|
|
const config = readOidcConfig();
|
|
if (!config) {
|
|
return NextResponse.json(
|
|
{
|
|
error: {
|
|
code: 'auth_not_configured',
|
|
message: 'Zitadel OIDC is not configured (ZITADEL_OIDC_CLIENT_ID/SECRET)',
|
|
},
|
|
},
|
|
{ status: 503 },
|
|
);
|
|
}
|
|
const next = safeNext(request.nextUrl.searchParams.get('next'));
|
|
const { state, nonce } = createOidcState();
|
|
const { verifier, challenge } = createPkce();
|
|
const response = NextResponse.redirect(authorizeUrl(config, state, nonce, challenge));
|
|
response.cookies.set(OIDC_STATE_COOKIE, JSON.stringify({ state, nonce, verifier, next, ts: Date.now() }), {
|
|
httpOnly: true,
|
|
sameSite: 'lax',
|
|
secure: appBaseUrl().startsWith('https://'),
|
|
path: '/',
|
|
maxAge: 600,
|
|
});
|
|
return response;
|
|
}
|
|
|
|
// POST /api/auth/login — guarded dev login (scaffold). Requires AUTH_SECRET +
|
|
// a matching DEV_LOGIN_KEY. Never available in production; real login is GET.
|
|
export async function POST(req: NextRequest) {
|
|
if (process.env.NODE_ENV === 'production') {
|
|
return NextResponse.json(
|
|
{ error: { code: 'forbidden', message: 'Dev-login is disabled in production' } },
|
|
{ status: 403 },
|
|
);
|
|
}
|
|
const { email, key } = await req.json().catch(() => ({ email: undefined, key: undefined }));
|
|
if (!process.env.AUTH_SECRET) {
|
|
return NextResponse.json(
|
|
{ error: { code: 'auth_not_configured', message: 'AUTH_SECRET saknas' } },
|
|
{ status: 501 },
|
|
);
|
|
}
|
|
const devKey = process.env.DEV_LOGIN_KEY;
|
|
// Constant-time compare: a plain !== leaks the number of matching leading
|
|
// bytes via response timing, letting an attacker recover DEV_LOGIN_KEY
|
|
// byte-by-byte instead of an all-or-nothing guess.
|
|
const keyBuf = Buffer.from(typeof key === 'string' ? key : '');
|
|
const devKeyBuf = Buffer.from(devKey ?? '');
|
|
const keysMatch =
|
|
Boolean(devKey) && keyBuf.length === devKeyBuf.length && timingSafeEqual(keyBuf, devKeyBuf);
|
|
if (!keysMatch) {
|
|
return NextResponse.json(
|
|
{ error: { code: 'forbidden', message: 'Dev-login kräver giltig nyckel' } },
|
|
{ status: 403 },
|
|
);
|
|
}
|
|
if (!email) {
|
|
return NextResponse.json({ error: { code: 'bad_request', message: 'email krävs' } }, { status: 400 });
|
|
}
|
|
const token = await signSession({ email, exp: Date.now() + 7 * 24 * 60 * 60 * 1000 });
|
|
const res = NextResponse.json({ ok: true, email });
|
|
res.cookies.set(SESSION_COOKIE, token, {
|
|
httpOnly: true,
|
|
sameSite: 'lax',
|
|
secure: true,
|
|
path: '/',
|
|
maxAge: 60 * 60 * 24 * 7,
|
|
});
|
|
return res;
|
|
}
|