CenterForDigitalHumanities / TPEN-services

Services required by TPEN interfaces in order to interact with data
1 stars 0 forks source link

Implement Auth in Routes #96

Closed thehabes closed 4 months ago

thehabes commented 4 months ago

Determine a way to use the auth middleware to validate the Bearer (User) tokens passed into the routes. If a route does CRUD, it will need to Authorize the token first before continuing into the route logic. If the route determines that the token is not Authorized, the route should return a 401.

cubap commented 4 months ago

Design Policy

The auth0Middleware() is intended to be used inside the router as close to the required action as possible. Discussing how auth and permissions is included in Interfaces is out of scope here.

Express Docs talk about middleware routers and you are already using them in all your router.get('/',arrowFunc) patterns. with the .use() patterns, you just have a series of arguments (or as a single array) that is the order of middleware to use, so just put the auth0Middleware() at the beginning or consider if it is faster to validate the basic request before bothering. Every function after the auth0Middleware() will have req.user available to them.

Testing should not test auth. Testing should not test auth. Just mock auth0Middleware() and include a req.user that is admin or public or whatever is needed to run your test. Do not rely on copied tokens that need to be validated remotely and updated when they expire.