In my use case I have to add a query param to google oauth redirect URL. I am adding a query param with key as redirect
. I am trying to add in the following way,
var (
googleRedirectURL = "http://127.0.0.1:8080/oauth-callback/google"
oauthCfg = &oauth2.Config{
ClientID: "XXXXXXXXXX",
ClientSecret: "XXXXXXXXXX",
Endpoint: google.Endpoint,
RedirectURL: "http://127.0.0.1:8080/oauth-callback/google",
Scopes: []string{"https://www.googleapis.com/auth/userinfo.profile", "https://www.googleapis.com/auth/userinfo.email"},
}
//random string for oauth2 API calls to protect against CSRF
googleOauthStateString = getUUID()
)
const profileInfoURL = "https://www.googleapis.com/oauth2/v1/userinfo?alt=json"
func HandleGoogleLogin(w http.ResponseWriter, r *http.Request) {
redirect := strings.TrimSpace(r.FormValue("redirect"))
if redirect == "" {
httpErrorf(w, "HandleGoogleLogin() :: Missing redirect value for /login")
return
}
q := url.Values{
"redirect": {redirect},
}.Encode()
//params := '{"redirect": '+redirect+'}'
log.Printf("HandleGoogleLogin() :: redirect %s ", q)
//param := oauth2.SetAuthURLParam("redirect", q)
// url := oauthCfg.AuthCodeURL("state", param)
//append the redirect URL to the request
oauthCfg.RedirectURL = googleRedirectURL
url := oauthCfg.AuthCodeURL("state")
url = oauthCfg.AuthCodeURL(googleOauthStateString, oauth2.AccessTypeOnline)
url = url + "?redirct=" + q
http.Redirect(w, r, url, http.StatusTemporaryRedirect)
}
But this is appending the redirect param to the state param of the url. So when I compare the state code oauthCfg.AuthCodeURL("state")
the value differs. I mean the following check.
state := r.FormValue("state")
log.Printf("HandleGoogleCallback() :: state string %s ", state)
if state != googleOauthStateString {
log.Printf("invalid oauth state, expected '%s', got '%s'
", googleOauthStateString, state)
http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
return
}
I can split the string to get the state value using ?
delimiter. But I thought there must be a standard way of adding query param to redirect url in google oauth. Could someone give some suggestions on this?