-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathauth.js
More file actions
35 lines (30 loc) · 886 Bytes
/
auth.js
File metadata and controls
35 lines (30 loc) · 886 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
const realms = {
admin: new Set((process.env.GK_ADMIN_SECRETS || "").split(",")),
drink: new Set((process.env.GK_DRINK_SECRETS || "").split(",")),
projects: new Set((process.env.GK_MEMBER_PROJECT_SECRETS || "").split(",")),
};
function generateMiddleware(realm) {
if (!realms[realm]) {
throw new Error("No such realm: " + realm);
}
function authMiddleware(req, res, next) {
if (!req.headers.authorization) {
res.status(403).json({
message: "Secret not provided",
});
return;
}
if (!realms[realm].has(req.headers.authorization)) {
res.status(403).json({
message: "Unknown application! Are you on the wrong realm?",
});
return;
}
next();
}
return authMiddleware;
}
export function checkSecret(realm, value) {
return realms[realm]?.has(value) ?? false;
}
export default generateMiddleware;