Skip to content
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

Fix admin verification #161

Merged
merged 2 commits into from
Oct 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 14 additions & 26 deletions services/gateway/src/auth/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,9 @@ export const setupIsLoggedIn = (app : Express, routes : any[]) => {

export const setupUserIdMatch = (app : Express, routes : any[]) => {
routes.forEach(r => {
r.user_match_required_methods.forEach((method : string) => {
applyMiddleware(r.url + "/:uid", method, app,
function(req : express.Request, res : express.Response, next : express.NextFunction) {
const idToken = req.get(userIdTokenHeader);
app.use(r.url, function(req : express.Request, res : express.Response, next : express.NextFunction) {
if (r.user_match_required_methods.includes(req.method)) {
const idToken = req.get(userIdTokenHeader);
const paramUid = req.params.uid;
if (!idToken || !paramUid) {
res.redirect(redirectLink)
Expand All @@ -48,17 +47,19 @@ export const setupUserIdMatch = (app : Express, routes : any[]) => {
res.status(500).send("A server-side error occurred! Contact the admin for help.");
});
}
});
} else {
// Skip
next();
}
});
});
}

export const setupAdmin = (app : Express, routes : any[]) => {
// If admin access is required, check that the firebase ID token has an admin claim
routes.forEach(r => {
r.admin_required_methods.forEach((method : string) => {
applyMiddleware(r.url, method, app,
function(req : express.Request, res : express.Response, next : express.NextFunction) {
app.use(r.url, function(req : express.Request, res : express.Response, next : express.NextFunction) {
if (r.admin_required_methods.includes(req.method)) {
// Pass in the user as a header of the request
const idToken = req.get(userIdTokenHeader);
if (!idToken) {
Expand All @@ -75,23 +76,10 @@ export const setupAdmin = (app : Express, routes : any[]) => {
res.status(500).send("A server-side error occurred! Contact the admin for help.");
})
}
})
} else {
// Skip
next();
}
});
})
});
};

const applyMiddleware = (url : string, method : string, app : Express, func : any) => {
switch(method) {
case "GET":
app.get(url, func);
break;
case "POST":
app.post(url, func);
break;
case "PUT":
app.put(url, func);
break;
case "DELETE":
app.delete(url, func)
}
}
16 changes: 2 additions & 14 deletions services/gateway/src/proxied_routes/proxied_routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,16 @@ export const proxied_routes: ProxiedRoute[] = [
},
{
url: "/api/admin-service",
admin_required_methods: ["GET, POST, PUT, DELETE"], // All routes in admin service can only be accessed by admins
admin_required_methods: ["GET", "POST", "PUT", "DELETE"], // All routes in admin service can only be accessed by admins
user_match_required_methods: [], // No need for exact user match here
proxy: {
target: adminServiceAddress,
changeOrigin: true,
pathRewrite: {
"^/api/admin-service": "",
},
},
},
{
url: "/api/question-service",
admin_required_methods: ["POST, PUT, DELETE"], // Only admins can create, update or delete questions
admin_required_methods: ["POST", "PUT", "DELETE"], // Only admins can create, update or delete questions
user_match_required_methods: [], // No need for exact user match here
proxy: {
target: questionServiceAddress,
Expand All @@ -61,13 +58,4 @@ export const proxied_routes: ProxiedRoute[] = [
changeOrigin: true,
},
},
{
url: "/api/question-service",
admin_required_methods: ["POST, PUT, DELETE"], // All routes in admin service can only be accessed by admins
user_match_required_methods: [], // No need for exact user match here
proxy: {
target: questionServiceAddress,
changeOrigin: true,
},
},
];