Skip to content

Commit

Permalink
feat: add MustHaveAuthentication() utility to internal middleware mod…
Browse files Browse the repository at this point in the history
…ule in @observerly/birpc

feat: add MustHaveAuthentication() utility to internal middleware module in @observerly/birpc
  • Loading branch information
michealroberts committed Oct 14, 2024
1 parent 89e9fe1 commit becd236
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions internal/middleware/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,25 @@ func IsAuthenticated(ctx context.Context, req connect.AnyRequest, auth *auth.Cli
}

/*****************************************************************************************************************/

func MustHaveAuthentication[T any](
ctx context.Context,
req connect.AnyRequest,
auth *auth.Client,
handlerFunc func() (*connect.Response[T], error),
) (*connect.Response[T], error) {
isAuthed, err := IsAuthenticated(ctx, req, auth)

if err != nil {
return nil, connect.NewError(connect.CodeInternal, fmt.Errorf("error checking user claims: %w", err))
}

if !isAuthed {
return nil, connect.NewError(connect.CodePermissionDenied, fmt.Errorf("user does not have valid claims"))
}

// Call the actual handler function:
return handlerFunc()
}

/*****************************************************************************************************************/

0 comments on commit becd236

Please sign in to comment.