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 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器