Skip to content

Commit 7fc12af

Browse files
committed
pass deny reason to login page
1 parent 8bdadc1 commit 7fc12af

2 files changed

Lines changed: 15 additions & 10 deletions

File tree

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

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@ export const GET: APIRoute = async ({ request, cookies }) => {
1717
try {
1818
const domain = request.headers.get("Host");
1919
if (!domain) {
20-
throw new Error();
20+
throw new Error("Could not determine domain.");
2121
}
2222

2323
const path = request.headers.get("Path");
2424
if (!path) {
25-
throw new Error();
25+
throw new Error("Could not determine domain.");
2626
}
2727

2828
const ip = request.headers.get("X-Forwarded-For");
@@ -33,11 +33,11 @@ export const GET: APIRoute = async ({ request, cookies }) => {
3333
const timestamp = cookies.get("xnode_auth_timestamp")?.value;
3434

3535
if (!isHex(signature)) {
36-
throw new Error();
36+
throw new Error(`Signature ${signature} is not valid hex.`);
3737
}
3838
if (!timestamp || isNaN(Number(timestamp))) {
3939
// add checks if timestamp in the future or too far in the past
40-
throw new Error();
40+
throw new Error(`Timestamp ${timestamp} is not a valid number.`);
4141
}
4242

4343
const validSignature = await verifyXnodeUserEthAddress({
@@ -53,16 +53,17 @@ export const GET: APIRoute = async ({ request, cookies }) => {
5353
requestedUser = undefined;
5454
}
5555

56+
const users = ([] as string[])
57+
.concat(ip ? [`ip:${ip}`] : [])
58+
.concat(requestedUser ? [requestedUser] : []);
5659
const user = await hasAccess({
57-
users: ([] as string[])
58-
.concat(ip ? [`ip:${ip}`] : [])
59-
.concat(requestedUser ? [requestedUser] : []),
60+
users,
6061
domain,
6162
path,
6263
});
6364

6465
if (user === undefined) {
65-
throw new Error();
66+
throw new Error(`Access denied for ${requestedUser}`);
6667
}
6768

6869
return new Response(null, {
@@ -72,7 +73,10 @@ export const GET: APIRoute = async ({ request, cookies }) => {
7273
} catch (err: any) {
7374
return new Response(null, {
7475
status: 401,
75-
headers: corsHeaders(request.headers),
76+
headers: {
77+
"Xnode-Auth-Deny-Reason": err.message,
78+
...corsHeaders(request.headers),
79+
},
7680
});
7781
}
7882
};

nix/nixos-module.nix

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,7 @@ in
324324
extraConfig = ''
325325
auth_request /xnode-auth/api/validate;
326326
auth_request_set $auth_resp_xnode_auth_user $upstream_http_xnode_auth_user;
327+
auth_request_set $auth_resp_xnode_auth_deny_reason $upstream_http_xnode_auth_deny_reason;
327328
proxy_set_header Xnode-Auth-User $auth_resp_xnode_auth_user;
328329
error_page 401 = @login;
329330
'';
@@ -366,7 +367,7 @@ in
366367
'';
367368
};
368369
"@login" = {
369-
return = "302 $scheme://$host${cfg.nginxConfig.subpath}?redirect=$scheme://$host$request_uri";
370+
return = "302 $scheme://$host${cfg.nginxConfig.subpath}?redirect=$scheme://$host$request_uri&rejected=$auth_resp_xnode_auth_deny_reason";
370371
};
371372
}
372373
];

0 commit comments

Comments
 (0)