Skip to content

Commit

Permalink
hooks: pre-access fix MR feedbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
sberthier committed Feb 16, 2024
1 parent fb21b4a commit 9433698
Show file tree
Hide file tree
Showing 13 changed files with 134 additions and 133 deletions.
6 changes: 3 additions & 3 deletions docs/hooks.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ The table below provides an overview of all available hooks.
| pre-finish | Yes | after all upload data has been received but before a response is sent. | sending custom data when an upload is finished | Yes |
| post-finish | No | after all upload data has been received and after a response is sent. | post-processing of upload, logging of upload end | Yes |
| post-terminate | No | after an upload has been terminated. | clean up of allocated resources | Yes |
| pre-access | Yes | before an existing upload is access (Head/Get/Patch/Delete). | validation of user authentication | No |
| pre-access | Yes | before an existing upload is access (Head/Get/Patch/Delete/Upload-Concat). | validation of user authentication | No |

Users should be aware of following things:
- If a hook is _blocking_, tusd will wait with further processing until the hook is completed. This is useful for validation and authentication, where further processing should be stopped if the hook determines to do so. However, long execution time may impact the user experience because the upload processing is blocked while the hook executes.
Expand Down Expand Up @@ -113,7 +113,7 @@ Below you can find an annotated, JSON-ish encoded example of a hook request:
"Mode": "read"
// All files info that will be access by http request
// Use an array because of Upload-Concat that may target several files
"Files": [
"Uploads": [
// same as Upload
]
}
Expand Down Expand Up @@ -320,7 +320,7 @@ For example, assume that every upload must belong to a specific user project. Th

### Authenticating Users

User authentication can be achieved by two ways: Either, user tokens can be included in the upload meta data, as described in the above example. Alternatively, traditional header fields, such as `Authorization` or `Cookie` can be used to carry user-identifying information. These header values are also present for the hook requests and are accessible for the `pre-create` and `pre-access` hooks, where the authorization tokens or cookies can be validated to authenticate the user.
User authentication can be achieved by two ways: Either, user tokens can be included in the upload meta data, as described in the above example. Alternatively, traditional header fields, such as `Authorization` or `Cookie` can be used to carry user-identifying information. These header values are also present for the hook requests and are accessible for the `pre-access` hook, where the authorization tokens or cookies can be validated to authenticate the user.

If the authentication is successful, the hook can return an empty hook response to indicate tusd that the upload should continue as normal. If the authentication fails, the hook can instruct tusd to reject the upload and return a custom error response to the client. For example, this is a possible hook response:

Expand Down
36 changes: 18 additions & 18 deletions examples/hooks/grpc/hook_pb2.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions examples/hooks/grpc/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ def InvokeHook(self, hook_request, context):
# Example: Use the pre-access hook to print each upload access
if hook_request.type == 'pre-access':
mode = hook_request.event.access.mode
id = hook_request.event.access.files[0].id
size = hook_request.event.access.files[0].size
id = hook_request.event.access.uploads[0].id
size = hook_request.event.access.uploads[0].size
print(f'Access {id} (mode={mode}, size={size} bytes)')

# Example: Use the post-finish hook to print information about a completed upload,
Expand Down
4 changes: 2 additions & 2 deletions examples/hooks/http/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ def do_POST(self):
# Example: Use the pre-access hook to print each upload access
if hook_request['Type'] == 'pre-access':
mode = hook_request['Event']['Access']['Mode']
id = hook_request['Event']['Access']['Files'][0]['ID']
size = hook_request['Event']['Access']['Files'][0]['Size']
id = hook_request['Event']['Access']['Uploads'][0]['ID']
size = hook_request['Event']['Access']['Uploads'][0]['Size']
print(f'Access {id} (mode={mode}, size={size} bytes)')

# Example: Use the post-finish hook to print information about a completed upload,
Expand Down
4 changes: 2 additions & 2 deletions examples/hooks/plugin/hook_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ func (g *MyHookHandler) InvokeHook(req hooks.HookRequest) (res hooks.HookRespons
// Example: Use the pre-access hook to print each upload access
if req.Type == hooks.HookPreAccess {
mode := req.Event.Access.Mode
id := req.Event.Access.Files[0].ID
size := req.Event.Access.Files[0].Size
id := req.Event.Access.Uploads[0].ID
size := req.Event.Access.Uploads[0].Size

log.Printf("Access %s (mode=%s, size=%d bytes) \n", id, mode, size)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/handler/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ type Config struct {
// more details on its behavior. If you do not want to make any changes, return an empty struct.
PreUploadCreateCallback func(hook HookEvent) (HTTPResponse, FileInfoChanges, error)
// PreUploadAccessCallback will be invoked before accessing an upload, if the
// property is supplied, on Get/Head/Patch/Delete requests.
// property is supplied, on Get/Head/Patch/Delete/Upload-Concat requests.
// If the callback returns no error, the requests will continue.
// If the error is non-nil, the requests will be rejected. This can be used to implement
// authorization.
Expand Down
20 changes: 9 additions & 11 deletions pkg/handler/hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ type AccessInfo struct {

// All files info that will be access by http request
// Use an array because of Upload-Concat that may target several files
Files []FileInfo
Uploads []FileInfo
}

func newHookEvent(c *httpContext, info *FileInfo, accessInfo *AccessInfo) HookEvent {
func newHookEvent(c *httpContext, info *FileInfo) HookEvent {
// The Host header field is not present in the header map, see https://pkg.go.dev/net/http#Request:
// > For incoming requests, the Host header is promoted to the
// > Request.Host field and removed from the Header map.
Expand All @@ -58,10 +58,6 @@ func newHookEvent(c *httpContext, info *FileInfo, accessInfo *AccessInfo) HookEv
if info != nil {
upload = *info
}
var access AccessInfo
if accessInfo != nil {
access = *accessInfo
}
return HookEvent{
Context: c,
Upload: upload,
Expand All @@ -71,13 +67,15 @@ func newHookEvent(c *httpContext, info *FileInfo, accessInfo *AccessInfo) HookEv
RemoteAddr: c.req.RemoteAddr,
Header: c.req.Header,
},
Access: access,
Access: AccessInfo{},
}
}

func newAccessInfo(mode AccessMode, files []FileInfo) AccessInfo {
return AccessInfo{
Mode: mode,
Files: files,
func newHookAccessEvent(c *httpContext, mode AccessMode, uploads []FileInfo) HookEvent {
event := newHookEvent(c, nil)
event.Access = AccessInfo{
Mode: mode,
Uploads: uploads,
}
return event
}
4 changes: 2 additions & 2 deletions pkg/handler/post_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -625,8 +625,8 @@ func TestPost(t *testing.T) {
handler, _ := NewHandler(Config{
StoreComposer: composer,
BasePath: "/files/",
PreUploadCreateCallback: func(event HookEvent) (HTTPResponse, FileInfoChanges, error) {
return HTTPResponse{}, FileInfoChanges{}, ErrAccessRejectedByServer
PreUploadAccessCallback: func(event HookEvent) error {
return ErrAccessRejectedByServer
},
})

Expand Down
Loading

0 comments on commit 9433698

Please sign in to comment.