dsvjmc0907 2016-02-22 15:59
浏览 65
已采纳

什么时候在oauth2中随机化身份验证代码/状态?

According to the docs at https://www.godoc.org/golang.org/x/oauth2#Config.AuthCodeURL

...State is a token to protect the user from CSRF attacks. You must always provide a non-zero string...

and at http://tools.ietf.org/html/rfc6749#section-10.12

...any request sent to the redirection URI endpoint to include a value that binds the request...

Yet this is specifically at the part in the flow when there is no session data, i.e. the user has not logged in and the auth code is only generated upon showing the anonymous page.

How then can this value be randomized and compared upon callback? Is it a static value randomized per server?

  • 写回答

1条回答 默认 最新

  • douyi9705 2016-02-22 16:32
    关注

    state

    RECOMMENDED. An opaque value used by the client to maintain state between the request and callback. The authorization server includes this value when redirecting the user-agent back to the client. The parameter SHOULD be used for preventing cross-site request forgery as described in Section 10.12.

    RFC 6749

    You use state to identify that the callback from the authorization server matches the request sent. If there wasn't state a attacker could just call your callback url with a random access token that you didn't request. With state you know that the called callback is in response to the request you made.

    So you randomize state per request that you sent and track it until you receive the matching callback. It can be anything you want as long as it can't be guessed.

    A simple approach would be leveraging rand.Reader and base64 encoding the result:

    func state(n int) (string, error) {
        data := make([]byte, n)
        if _, err := io.ReadFull(rand.Reader, data); err != nil {
            return "", err
        }
        return base64.StdEncoding.EncodeToString(data), nil
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 eda:门禁系统设计
  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页
  • ¥15 376.1电表主站通信协议下发指令全被否认问题
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥15 复杂网络,变滞后传递熵,FDA
  • ¥20 csv格式数据集预处理及模型选择
  • ¥15 部分网页页面无法显示!
  • ¥15 怎样解决power bi 中设置管理聚合,详细信息表和详细信息列显示灰色,而不能选择相应的内容呢?
  • ¥15 QTOF MSE数据分析
  • ¥15 平板录音机录音问题解决