How to add query params to google oauth in golang?

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'\n", 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?

Answers


I think you're close. This worked for me:

hostDomainOption := oauth2.SetAuthURLParam("hd", "example.com")

authUrl := oAuthConfig.AuthCodeURL("state",
    oauth2.AccessTypeOffline,
    hostDomainOption)

I think where you might have been stuck is noticing that the AuthCodeURL method is variadic.


Need Your Help

puma hot restarting on_restart function: necessary for Rails apps?

ruby-on-rails-3 puma

In the configuration file example for Puma, it says the following for the on_restart function:

How to cast a Fraction object to Double object?

java casting

When I'm studying Android, I can cast an object type to other type, like this: