Cookie不会与对Go Server的后续REST调用一起返回

问题描述 投票:0回答:1

我正在研究OAuth2身份验证,并设置了一个通过Github进行身份验证的服务器。我遵循了this example,并使其能够正常工作。我想继续提出一些建议,并实现基本的会话令牌系统,并从服务器进行Github调用,而不是将授权令牌发送给客户端。

这里是我稍作修改的/ oauth / redirect处理程序

func oauthRedirectHandler(w http.ResponseWriter, r *http.Request) {
    fmt.Println("/oauth/redirect")

    err := r.ParseForm()
    if err != nil {
        fmt.Fprintf(os.Stdout, "could not parse query: %+v", err)
        w.WriteHeader(http.StatusBadRequest)
    }

    code := r.FormValue("code")

    reqURL := fmt.Sprintf("https://github.com/login/oauth/access_token?client_id=%s&client_secret=%s&code=%s", clientID, clientSecret, code)
    req, err := http.NewRequest(http.MethodPost, reqURL, nil)
    if err != nil {
        fmt.Fprintf(os.Stdout, "could not create HTTP request: %v", err)
        w.WriteHeader(http.StatusBadRequest)
    }

    req.Header.Set("accept", "application/json")

    res, err := httpClient.Do(req)
    if err != nil {
        fmt.Fprintf(os.Stdout, "could not send HTTP request: %+v", err)
        w.WriteHeader(http.StatusInternalServerError)
    }
    defer res.Body.Close()

    var t oAuthAccessResponse
    if err := json.NewDecoder(res.Body).Decode(&t); err != nil {
        fmt.Fprintf(os.Stdout, "could not parse JSON response: %+v", err)
        w.WriteHeader(http.StatusBadRequest)
    }

        newSession := sessionTracker{
        AccessToken: accessToken,
        TimeOut:     time.Now().Add(time.Minute * 15),
    }

    sessionToken := uuid.New().String()

    sessionTrackerCache[sessionToken] = newSession

    http.SetCookie(w, &http.Cookie{
        Name:    sessionTokenConst,
        Value:   sessionToken,
        Expires: newSession.TimeOut,
        Domain:  "localhost",
    })

    http.Redirect(w, r, "/welcome.html", http.StatusFound)
}

它重定向到带有包含我的SessionToken ID的cookie的欢迎页面。

这是我的welcomeHandler

func welcomeHandler(w http.ResponseWriter, req *http.Request) {
    fmt.Println("/welcome")

    cookie, err := req.Cookie(sessionTokenConst)
    if err != nil {
        fmt.Fprintf(os.Stdout, "no cookie attached: %+v", err)
        w.WriteHeader(http.StatusBadRequest)
        return
    } 

    dat, err := ioutil.ReadFile("./public/welcome.html")
    if err != nil {
        fmt.Fprintf(os.Stdout, "could not read welcome page: %+v", err)
        w.WriteHeader(http.StatusInternalServerError)
    }

    fmt.Fprintf(w, string(dat))
}

观察浏览器的网络选项卡,我向Github进行身份验证,并且服务器能够看到授权令牌。 oauthRedirectHandler末尾的重定向响应包含带有SessionID的cookie。我的问题在于,浏览器似乎并未将GET调用上的令牌附加到/welcome.html。我可以在浏览器和welcomeHandler中确认这一点。

全部都在本地托管。

我不确定这是否是我的服务器,浏览器出现的问题,还是我了解到在Cookie到期日期不正确之前,浏览器是否将Cookie应用于将来的任何客户端请求。

感谢您的任何帮助!

go cookies oauth-2.0 session-cookies setcookie
1个回答
0
投票

浏览器将cookie路径默认为请求路径。将路径设置为“ /”,以使Cookie在整个网站上都可用。

http.SetCookie(w, &http.Cookie{
    Name:    sessionTokenConst,
    Value:   sessionToken,
    Path:    "/", // <--- add this line
    Expires: newSession.TimeOut,
    Domain:  "localhost",
})
© www.soinside.com 2019 - 2024. All rights reserved.