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

Question: arguments-aware authorization possible? #1513

Closed
1 of 4 tasks
ondrejpar opened this issue Nov 15, 2023 · 5 comments
Closed
1 of 4 tasks

Question: arguments-aware authorization possible? #1513

ondrejpar opened this issue Nov 15, 2023 · 5 comments

Comments

@ondrejpar
Copy link

Hi, I have a controller similar to this:

@Route('/api')
class PetshopController extends Controller {
  @Get('/{shopId}/pets')
  getPets(@Query() shopId: string) { ... }
  @Post('/{shopId}/pets')
  addPet(@Query() shopId: string) { ... }
  ...ten more methods that use shopId
}

I need to verify (authorize) that caller can access the shop identified by shopId. The authorization is quite complex and involves database.

I can add await authorizeShopAccess(shopId) to the beginning of every method, but is there a way to write this only once? As far as I can tell, the middlewares don't have access to path parameters (or arguments in general) and @Security only handles authentication, not authorization.

Sorting

  • I'm submitting a ...

    • bug report
    • feature request
    • support request
  • I confirm that I

    • used the search to make sure that a similar issue hasn't already been submit
Copy link

Hello there ondrejpar 👋

Thank you for opening your very first issue in this project.

We will try to get back to you as soon as we can.👀

@WoH
Copy link
Collaborator

WoH commented Nov 15, 2023

the middlewares don't have access to path parameters (or arguments in general)

But the request object

@ondrejpar
Copy link
Author

@WoH well, yes, but I would have to parse the path again and keep patterns in sync at two different places - not DRY.

@WoH
Copy link
Collaborator

WoH commented Nov 15, 2023

Then you probably want your own Decorator.
(Albeit lose the OpenAPI response docs)

@ondrejpar
Copy link
Author

Did that and works perfectly. I created my own Authorizer (the code is a bit complex for Typescript 4, I think it could be simplified for Typescript 5) which uses Proxy to intercept all calls to the annotated class and performs authorization. In fact, it's all completely independent on TSOA.
Thanks for help.

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

No branches or pull requests

2 participants