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

@tus/server: add upload id to onIncomingRequest #516

Merged

Conversation

fenos
Copy link
Collaborator

@fenos fenos commented Nov 10, 2023

This PR makes the onIncomingRequest hook more useful by adding the uploadId as the 3rd parameter.
99% of cases you'd like to use this hook to perform authorization logic. However is always very common to allow check for permission specifically for certain upload-id.

For example:

  • Can this user add chunks to this upload?
  • Can this user view this asset?

By adding the upload-id we can implement custom logic on a per asset basis

Comment on lines -156 to -161
try {
await this.options.onIncomingRequest?.(req, res)
} catch (err) {
return onError(err)
}

Copy link
Collaborator

Choose a reason for hiding this comment

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

What if we only add id to this callback? This way we don't need to edit every handler.

    try {
      const id = this.handlers.PATCH.getFileIdFromRequest(req)
      await this.options.onIncomingRequest?.(req, res, id !== false ? id : undefined)
    } catch (err) {
      return onError(err)
    }

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I can do that - the only thing is that it will be called twice this way

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Also, this approach wouldn't work for the POST endpoint, which creates the URL

@Murderlon Murderlon changed the title fix: add upload-id to onIncomingRequest @tus/server: add upload id to onIncomingRequest Nov 20, 2023
Copy link
Member

@Murderlon Murderlon left a comment

Choose a reason for hiding this comment

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

I think I'm okay with duplication here instead of trying to make the (wrong) abstraction. Do others feel strongly?

@fenos
Copy link
Collaborator Author

fenos commented Nov 27, 2023

What's the status on this? @mitjap @Murderlon
I agree that instead of doing the wrong abstraction we can call them in all handlers.

If we ever are going to restructure the code at some point we can move them in a centralised place

@Murderlon Murderlon merged commit 6cf2786 into tus:main Nov 27, 2023
@fenos fenos deleted the feat/add-useful-info-to-incoming-request-hook branch November 27, 2023 13:29
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.

3 participants