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

feat: Use roles in API #2198

Merged
merged 5 commits into from
Sep 19, 2023
Merged

feat: Use roles in API #2198

merged 5 commits into from
Sep 19, 2023

Conversation

DafyddLlyr
Copy link
Contributor

@DafyddLlyr DafyddLlyr commented Sep 3, 2023

What does this PR do?

@github-actions
Copy link

github-actions bot commented Sep 3, 2023

Removed vultr server and associated DNS entries

@@ -107,7 +101,7 @@ const publishFlow = async (
{
data: flattenedFlow,
flow_id: req.params.flowId,
publisher_id: parseInt(req.user.sub, 10),
publisher_id: parseInt(req.user!.sub!, 10),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The next PR will handle this a bit more elegantly than forcing !

*/
export const useRoleAuth: UseRoleAuth =
(authRoles) => async (req, res, next) => {
useJWT(req, res, () => {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I spent a while trying to get these two middlewares a little less tightly coupled. The Express docs have a nice example of using an array of middleware - https://expressjs.com/en/guide/using-middleware.html

The issue I was hitting was keeping the return types for req, res, next outside this file when passing in the role argument to the middleware / function which returns a middleware array.

In reality they are tightly coupled - we can't check roles without decoding and validating the JWT, but I don't think this reads particularly clearly.

Comment on lines +162 to +164
"teamViewer",
"teamEditor",
"platformAdmin",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These arrays indicate that any of these roles will be valid here. See comment on inherited roles here - #2182 (comment)

In future, PlanX roles could be less hierarchical so this approach would still work there also.

Comment on lines +146 to +148
`Authentication error: User ${userId} does have have any of the roles [${authRoles.join(
", ",
)}] which are required to access ${req.path}`,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can probably drop this in future but it could prove handy for debugging any initial issues we have implementing roles.

.post("/flows/1/move/new-team")
.set(authHeader({ role: "teamViewer" }))
.expect(403);
});
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these feel very explicit & readable 👍

if (!req.user?.sub)
next({ status: 401, message: "User ID missing from JWT" });

app.get("/me", usePlatformAdminAuth, async function (req, res, next) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will any signed in user (including non-platform admins) still be able to call /me if we're using this auth method here? if not, do we want that to be possible? thinking forward to the /me endpoint perhaps underpinning a frontend user store & ability to display your use rdetails in the editor?

Copy link
Contributor Author

@DafyddLlyr DafyddLlyr Sep 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah this is a good catch - I added usePlatformAdminAuth as I was viewing this as a nice way for devs to check their status.

I'll make it accessible to any logged on user again on a follow up PR 👍

For a user store, we could populate it a number of ways -

  • Calling /me
  • Calling $admin.user.getByEmail() directly
  • Enriching the JWT with all required user details

I think you're right here - /me may be the simplest method

@DafyddLlyr DafyddLlyr merged commit 03e24e2 into main Sep 19, 2023
@DafyddLlyr DafyddLlyr deleted the dp/use-roles-in-api branch September 19, 2023 13:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants