ausocean / cloud

GNU General Public License v3.0
1 stars 1 forks source link

Bench/Broadcast: 502 Error when trying to regenerate a token #163

Closed ao-david closed 5 days ago

ao-david commented 5 days ago

This issue is caused by the implementation of pattern matching by this function:

// genToken redirects the user to an authorisation page for generation of an
// authorisation token.
func genToken(w http.ResponseWriter, r *http.Request, config *oauth2.Config, url string) {
    config.RedirectURL = "https://" + r.Host + youtubeCredsRedirect

    http.HandleFunc(
        youtubeCredsRedirect,
        func(w http.ResponseWriter, r *http.Request) {
            code := r.FormValue("code")
            tok, err := config.Exchange(context.Background(), code)
            if err != nil {
                log.Printf("could not exchange token: %v", err)
            }

            if production {
                err = saveTokObj(context.Background(), tok, url)
            } else {
                err = saveTokFile(tok, url)
            }

            if err != nil {
                log.Printf("could not save new token: %v", err)
            }

            completionRedirect := "https://" + r.Host + "/admin/broadcast"
            http.Redirect(w, r, completionRedirect, http.StatusSeeOther)
        },
    )

    authURL := config.AuthCodeURL("state-token", oauth2.AccessTypeOffline)
    http.Redirect(w, r, authURL, http.StatusSeeOther)
}

This was implemented like this to allow for the closure to access the config whilst still matching the pattern required for a http handler. image

ao-david commented 5 days ago

Addressed in #164