Skip to content

securitySchemes/bearerAuth usage unclear.  #177

Closed

Description

@utx0

Hey guys,

I have been using this package for some time on a number of public API with great joy. However, I now have a need to set up JWT Bearer token auth on a number of API endpoints and I am finding that the usage patterns for this arent clear from reading the documentation or the code. I would really love some guidance on how this is meant to be implemented.

As a starting point I have defined the following test api endpoint in my openapi.json spec as follows;

components:
  responses:
    UnauthorizedError:
      description: Acces token is missing or invalid
  securitySchemes:
    bearerAuth:
      type: http
      scheme: bearer
      bearerFormat: JWT
paths:
  "/v1/jwt":
    get:
      security:
        - bearerAuth: []
      operationId: GetJWT
      summary: Get JWT info
      description: JWT Test interface
      responses:
        "200":
          description: Return info on the JWT
        "401":
          $ref: '#/components/responses/UnauthorizedError'

In turn, this generated the following in the codegen (Which for the life of me I can't see how this really archives anything useful?):

// GetJWT converts echo context to params.
func (w *ServerInterfaceWrapper) GetJWT(ctx echo.Context) error {
	var err error

	ctx.Set("bearerAuth.Scopes", []string{""})

	// Invoke the callback with all the unmarshalled arguments
	err = w.Handler.GetJWT(ctx)
	return err
}

Be great to know what is now required to turn a simple handler (as follows) into one that requires AUTH?

func (h *Handlers) GetDocs(ctx echo.Context) error {
	return ctx.File("public/rest/v1/api.html")
}

Activity

  1. mitar commented on May 22, 2021

    @mitar
    Contributor

    Why is the code []string{""} and not []string{} for empty list?

  2. mitar commented on May 25, 2021

    @mitar
    Contributor

    So I made this work by making a custom middleware which validates the access token and checks that it has all the required scopes. And I get required scopes in the context from the code generated by this package.

  3. zeako commented on Jul 5, 2021

    @zeako

    @mitar that's a good workaround I found myself using, but.. if we want to use OapiRequestValidator as in the examples no matter what we do the call will always fail

  4. debuggerpk commented on Dec 31, 2022

    @debuggerpk

    So I made this work by making a custom middleware which validates the access token and checks that it has all the required scopes. And I get required scopes in the context from the code generated by this package.

    Can you please guide on this more .. I am trying to write my own middleware, but i cannot find the scopes that are set. please see #911

  5. mitar commented on Dec 31, 2022

    @mitar
    Contributor

    My middleware is just a wrapper around a handler.

  6. debuggerpk commented on Dec 31, 2022

    @debuggerpk

    correct me if i am wrong, but wouldn't that require writing your own template for code generation? ..

    right now, the code that is being generated is

    func (w *ServerInterfaceWrapper) ValidateAPIKey(ctx echo.Context) error {
    	var err error
    
    	ctx.Set(APIKeyAuthScopes, []string{""})
    
    	// Invoke the callback with all the unmarshalled arguments
    	err = w.Handler.ValidateAPIKey(ctx)
    	return err
    }

    we want the ServerInterfaceWrapper to be

    type ServerInterfaceWrapper struct {
    	Handler    ServerInterface
    	Middleware echo.MiddlewareFunc
    }

    and the actual function to generate

    func (w *ServerInterfaceWrapper) ValidateAPIKey(ctx echo.Context) error {
    	var err error
    
    	ctx.Set(APIKeyAuthScopes, []string{""})
    
    	// Invoke the callback with all the unmarshalled arguments
    	err = w.Middleware(w.Handler.ValidateAPIKey(ctx))
    	return err
    }

    Thanks for taking a peak.

  7. mitar commented on Dec 31, 2022

    @mitar
    Contributor

    I am using Chi. There, generated code looks like:

    	ctx = context.WithValue(ctx, AccessTokenScopes, []string{"foobar"})
    
    	var handler = func(w http.ResponseWriter, r *http.Request) {
    		siw.Handler.GetFoobar(w, r)
    	}
    
    	for _, middleware := range siw.HandlerMiddlewares {
    		handler = middleware(handler)
    	}
    
    	handler(w, r.WithContext(ctx))

    So middlewares are called after context is set.

  8. mromaszewicz commented on Feb 7, 2026

    @mromaszewicz
    Member

    The README now has a dedicated "Implementing security" section with documentation on Security Schemes, Security Requirements, validation middleware, and the securityprovider package. There is also an authenticated API example at examples/authenticated-api/echo.

    The original ask for guidance on bearer auth usage is well-addressed by the current documentation.

    Closing as completed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions