Skip to content

Commit ac38b6f

Browse files
committed
allow validation with headers, enforce same site on login cookies
1 parent 0a4c22c commit ac38b6f

2 files changed

Lines changed: 14 additions & 7 deletions

File tree

astro-app/src/pages/api/login.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { APIRoute } from "astro";
1+
import type { APIRoute, AstroCookieSetOptions } from "astro";
22
import { isHex } from "viem";
33
import { corsHeaders } from "../../lib/cors";
44

@@ -22,9 +22,9 @@ export const POST: APIRoute = async ({ request, cookies }) => {
2222
const cookieOptions = {
2323
httpOnly: true,
2424
secure: true,
25-
sameSite: "none",
25+
sameSite: "strict",
2626
path: "/",
27-
} as const;
27+
} as AstroCookieSetOptions;
2828
cookies.set("xnode_auth_user", user, cookieOptions);
2929
if (user?.startsWith("eth:")) {
3030
const signature = body.signature;

astro-app/src/pages/api/validate.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,21 @@ export const GET: APIRoute = async ({ request, cookies }) => {
2727

2828
const ip = request.headers.get("X-Forwarded-For");
2929

30-
let requestedUser = cookies.get("xnode_auth_user")?.value;
30+
let requestedUser =
31+
cookies.get("xnode_auth_user")?.value ??
32+
request.headers.get("Xnode-Auth-User") ??
33+
undefined;
3134
if (requestedUser?.startsWith("eth:")) {
32-
const signature = cookies.get("xnode_auth_signature")?.value;
33-
const timestamp = cookies.get("xnode_auth_timestamp")?.value;
34-
35+
const signature =
36+
cookies.get("xnode_auth_signature")?.value ??
37+
request.headers.get("Xnode-Auth-Signature");
3538
if (!isHex(signature)) {
3639
throw new Error(`Signature ${signature} is not valid hex.`);
3740
}
41+
42+
const timestamp =
43+
cookies.get("xnode_auth_timestamp")?.value ??
44+
request.headers.get("Xnode-Auth-Timestamp");
3845
if (!timestamp || isNaN(Number(timestamp))) {
3946
// add checks if timestamp in the future or too far in the past
4047
throw new Error(`Timestamp ${timestamp} is not a valid number.`);

0 commit comments

Comments
 (0)