Skip to content

feat: Add bearer token http middleware [3/4]#1124

Merged
jcscottiii merged 1 commit intomainfrom
jcscottiii/create-auth-middleware
Feb 4, 2025
Merged

feat: Add bearer token http middleware [3/4]#1124
jcscottiii merged 1 commit intomainfrom
jcscottiii/create-auth-middleware

Conversation

@jcscottiii
Copy link
Collaborator

This middleware adds support for Bearer token authentication in your API. It extracts the Bearer token from the Authorization header and passes it to an Authenticator interface for verification. The implementation of the Authenticator interface is provided separately.

Key features

  • Extracts Bearer token: The middleware extracts the Bearer token from the Authorization header if present.
  • Passes token to authenticator: It passes the extracted token to an Authenticator interface, which is responsible for verifying the token and returning a user object if the authentication is successful.
  • Handles missing or invalid tokens: The middleware handles cases where the Authorization header is missing or malformed by returning appropriate error responses.
  • Customizable error responses: The errorFn parameter allows customization of the error response returned when authentication fails.

How to use it

  1. Implement the Authenticator interface to define how the Bearer token is verified and a user object is returned.
  2. Create an instance of the BearerTokenAuthenticationMiddleware using your Authenticator implementation and an errorFn to handle authentication errors.
  3. Apply the middleware to your API routes.

Additional resources

Important notes

  • This middleware assumes that the route requires authentication if the BearerAuthScopes field is set in the request context.
  • You are responsible for setting the ctxKey in the request context when authentication is needed.

This is a split up of #1118

@jcscottiii jcscottiii changed the title feat: Add bearer token http middleware feat: Add bearer token http middleware [3/4] Feb 3, 2025
@jcscottiii jcscottiii force-pushed the jcscottiii/gcip_authenticator branch from 048cb0e to 77c4af8 Compare February 4, 2025 14:09
@jcscottiii jcscottiii force-pushed the jcscottiii/create-auth-middleware branch from 4bc502b to 4f2db1e Compare February 4, 2025 14:10
Base automatically changed from jcscottiii/gcip_authenticator to main February 4, 2025 14:48
This middleware adds support for Bearer token authentication in your API. It extracts the Bearer token from the Authorization header and passes it to an `Authenticator` interface for verification. The implementation of the `Authenticator` interface is provided separately.

Key features

* Extracts Bearer token: The middleware extracts the Bearer token from the Authorization header if present.
* Passes token to authenticator: It passes the extracted token to an `Authenticator` interface, which is responsible for verifying the token and returning a user object if the authentication is successful.
* Handles missing or invalid tokens: The middleware handles cases where the Authorization header is missing or malformed by returning appropriate error responses.
* Customizable error responses: The `errorFn` parameter allows customization of the error response returned when authentication fails.

How to use it

1. Implement the `Authenticator` interface to define how the Bearer token is verified and a user object is returned.
2. Create an instance of the `BearerTokenAuthenticationMiddleware` using your `Authenticator` implementation and an `errorFn` to handle authentication errors.
3. Apply the middleware to your API routes.

Additional resources

* OpenAPI specification: https://swagger.io/docs/specification/v3_0/authentication/bearer-authentication/
* Google Cloud Endpoints documentation: https://cloud.google.com/endpoints/docs/openapi/authenticating-users-custom

Important notes

* This middleware assumes that the route requires authentication if the `BearerAuthScopes` field is set in the request context.
* You are responsible for setting the `ctxKey` in the request context when authentication is needed.
@jcscottiii jcscottiii force-pushed the jcscottiii/create-auth-middleware branch from 4f2db1e to 05e72f4 Compare February 4, 2025 14:58
@jcscottiii jcscottiii added this pull request to the merge queue Feb 4, 2025
Merged via the queue into main with commit c026b91 Feb 4, 2025
6 checks passed
@jcscottiii jcscottiii deleted the jcscottiii/create-auth-middleware branch February 4, 2025 20:22
@DanielRyanSmith DanielRyanSmith mentioned this pull request Feb 5, 2025
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.

2 participants