0

我有一个通过 go-swagger 生成的 api。我正在尝试进行会话检查,它没有像我预期的那样触发。我遵循了一个在 github 中找到的示例,但似乎对我不起作用。

我的代码:

// Applies when the "X-Session-Key" header is set
    api.SessionKeyHeaderAuth = func(token string) (interface{}, error) {
    // test the token
    success := routeHandler.HandleSessionHeaderKey(token)
    if success{
        return nil, nil
    }
    //We are pessimistic, if they aren't successful then we return a 401
    api.Logger("Access attempt with incorrect api key auth: %s", token)
    return nil, errors.New(401, "incorrect api key auth")
    }

我的 Yaml(对于我正在卷曲的端点):

/auth/logout:
  post:
    summary: Logs in the user
    consumes:
      - application/x-www-form-urlencoded
    operationId: authLogoutUser
    tags:
      - auth
    description:
      Allow users to log out and their session will be terminated
    produces:
      - application/json
    parameters:
      - in: header
        name: X-Session-Key
        type: string
        required: true
      - in: header
        name: X-Profile-Key
        type: string
        required: true
    responses:
      200:
       description: Login Success
       headers:
        ProfileKeyHeader:
          type: string
          description: The key for the profile data
        SessionKeyHeader:
          type: string
          description: The key for the session data
      400:
       description: Whether the user is not found or error while login, decided on a generic login failure error
       schema:
        $ref: 'definitions.yaml#/definitions/Error'
      429:
       description: Too many requests and being throttled
       schema:
        $ref: 'definitions.yaml#/definitions/Error'
      500:
        description: Too many requests and being throttled
        schema:
          $ref: 'definitions.yaml#/definitions/Error'

任何帮助了解我做错了什么都将不胜感激。

4

1 回答 1

0

所以,我是个白痴……

问题是我忘记在我的招摇 yaml 中添加安全性。一旦我这样做了,我的函数就会被调用。

operationId: authLogoutUser
        tags:
          - auth
        description:
          Allow users to log out and their session will be terminated
        produces:
          - application/json
        security:
          - SessionKeyHeader: []
于 2018-08-28T14:27:51.967 回答