Skip to content

Can't pass security validation when using oapi validation middleware #221

Closed

Description

@bandirsen

I use this library on sever side, while on client side do manually since the client is for mobile platform.

This is my openapi spec :
I follow openapi documentation to define security scheme

paths:
  /admins:
    get:
      description: Get AdminList
      security:
        - bearerAuth: []
      operationId: getAdminList
    
components:
  securitySchemes:
    bearerAuth:
      type: http
      scheme: bearer
      bearerFormat: JWT

And in my request I put Authorization header on request
Authorization: Bearer ---random JWT token ----

But I always get 'Security requirements failed' from validation middleware (the JWT token was checked and valid)
Am I missing something ?

PS: also it return http error 403, while above documentation recommending to return http 401 error for unauthorized request.

Activity

  1. kevinduffey commented on Aug 22, 2020

    @kevinduffey

    Not sure this helps.. but you define auth globally in components, and you are applying it to the endpoint. You don't need to apply it to the endpoint since you are defining it globally as well. Try removing the security from the /admins endpoint.

  2. bandirsen commented on Aug 22, 2020

    @bandirsen
    Author

    @kevinduffey

    As fas as I understand from this documentation, global or per-operation(per-method) security, both require "securitySchemes" on components to define security schemes.

    If we want to globaly security we put "security: " tag at same level as paths/component, for example:

    paths:
      /admins:
        get:
        ...
    components:
      securitySchemes:
        bearerAuth:
          type: http
         .....
    security:
      bearerAuth: []
    

    if we want per-operation (per-method) security we put 'security:' under method name like I do

  3. kevinduffey commented on Aug 22, 2020

    @kevinduffey

    That sounds right.. but your first example you had it on the method.. though that should be valid too.

  4. zaccari commented on Aug 26, 2020

    @zaccari

    I'm seeing this error as well, with the global security set (not at the path level):

    paths:
      /models:
        post:
        ...
    components:
      securitySchemes:
        bearerAuth:
          type: http
          scheme: bearer
          bearerFormat: JWT
    security:
      - bearerAuth: []

    Removing the global security lets the validation pass:

    curl -X POST "http://127.0.0.1:8090/models" -H "Authorization: Bearer a503faf9-45b5-4fec-8334-337284a66ea4" -H  "accept: application/json" -H  "Content-Type: application/json" -d "{\"name\":\"string\",\"type\":\"string\",\"options\":\"string\",\"parentId\":\"string\"}"

    I'm looking into the issue now, just wanted to make a note of it here for reference

  5. zaccari commented on Aug 26, 2020

    @zaccari

    @bandirsen I was able to resolve the issue by providing a custom validator function to the options, like so:

    	validatorOptions := &oapimiddleware.Options{}
    
    	validatorOptions.Options.AuthenticationFunc = func(c context.Context, input *oapifilter.AuthenticationInput) error {
    		fmt.Println(">>>> INSIDE AuthenticationFunc")
    		return nil
    	}
    
    	e.Use(oapimiddleware.OapiRequestValidatorWithOptions(swagger, validatorOptions))

    If I don't pass a validator function, I get the Security requirements failed error

  6. bandirsen commented on Aug 26, 2020

    @bandirsen
    Author

    @zaccari

    In my case, I can't use global security, there are some my end-points that didn't requires authorization(jwt token), something like user registration etc, because of that, I solve my problem by creating custom oapi validation middleware based on current oapi validation middleware but I remove security validation part, and keeping the input validation part.

    And then I create another custom middleware, based on echo jwtAuth middleware for token validation + user info extraction. and finally put that middleware to intercept request to oapi path that require authorization, so I do security validation and user info extraction from that jwt token in one step.

  7. silenzium commented on Apr 5, 2023

    @silenzium

    @bandirsen

    You can exclude your custom routes with the Skipper like this:

    validatorOptions := &oapiMiddleware.Options{
        Skipper: func(c echo.Context) bool {
            return !strings.HasPrefix(c.Request().URL.Path, "/api/")
        },
    }
    e.Use(oapiMiddleware.OapiRequestValidatorWithOptions(swagger, validatorOptions))

    This will skip validation for routes that don't start with /api/.

  8. mromaszewicz commented on Feb 7, 2026

    @mromaszewicz
    Member

    This code has changed quite a bit since this bug was filed, and I think you can achieve everything you want. As described above, a Skipper can be used to omit the validation in some API's, but you can also create a global security schema which requires JWT's and then on a per-operation basis, you can remove the security requirement by setting the security to nil, eg security: null

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