forked from Mirrors/oauth2
oauth2: allow callers to pass arbitrary auth URL parameters
Many OAuth 2.0 implementations support parameters beyond those supported by this library. This change exports a SetParam function for constructing arbitrary key/value parameters. Change-Id: Ice4179e7c5341bbeac8a53e389b32d59415740fa Reviewed-on: https://go-review.googlesource.com/8054 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
This commit is contained in:
parent
11c60b6f71
commit
3046bc76d6
18
oauth2.go
18
oauth2.go
|
@ -79,22 +79,28 @@ var (
|
||||||
// result in your application obtaining a refresh token the
|
// result in your application obtaining a refresh token the
|
||||||
// first time your application exchanges an authorization
|
// first time your application exchanges an authorization
|
||||||
// code for a user.
|
// code for a user.
|
||||||
AccessTypeOnline AuthCodeOption = setParam{"access_type", "online"}
|
AccessTypeOnline AuthCodeOption = SetParam("access_type", "online")
|
||||||
AccessTypeOffline AuthCodeOption = setParam{"access_type", "offline"}
|
AccessTypeOffline AuthCodeOption = SetParam("access_type", "offline")
|
||||||
|
|
||||||
// ApprovalForce forces the users to view the consent dialog
|
// ApprovalForce forces the users to view the consent dialog
|
||||||
// and confirm the permissions request at the URL returned
|
// and confirm the permissions request at the URL returned
|
||||||
// from AuthCodeURL, even if they've already done so.
|
// from AuthCodeURL, even if they've already done so.
|
||||||
ApprovalForce AuthCodeOption = setParam{"approval_prompt", "force"}
|
ApprovalForce AuthCodeOption = SetParam("approval_prompt", "force")
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// An AuthCodeOption is passed to Config.AuthCodeURL.
|
||||||
|
type AuthCodeOption interface {
|
||||||
|
setValue(url.Values)
|
||||||
|
}
|
||||||
|
|
||||||
type setParam struct{ k, v string }
|
type setParam struct{ k, v string }
|
||||||
|
|
||||||
func (p setParam) setValue(m url.Values) { m.Set(p.k, p.v) }
|
func (p setParam) setValue(m url.Values) { m.Set(p.k, p.v) }
|
||||||
|
|
||||||
// An AuthCodeOption is passed to Config.AuthCodeURL.
|
// SetParam builds an AuthCodeOption which passes key/value parameters
|
||||||
type AuthCodeOption interface {
|
// to a provider's authorization endpoint.
|
||||||
setValue(url.Values)
|
func SetParam(key, value string) AuthCodeOption {
|
||||||
|
return setParam{key, value}
|
||||||
}
|
}
|
||||||
|
|
||||||
// AuthCodeURL returns a URL to OAuth 2.0 provider's consent page
|
// AuthCodeURL returns a URL to OAuth 2.0 provider's consent page
|
||||||
|
|
|
@ -61,6 +61,15 @@ func TestAuthCodeURL(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestAuthCodeURL_CustomParam(t *testing.T) {
|
||||||
|
conf := newConf("server")
|
||||||
|
param := SetParam("foo", "bar")
|
||||||
|
url := conf.AuthCodeURL("baz", param)
|
||||||
|
if url != "server/auth?client_id=CLIENT_ID&foo=bar&redirect_uri=REDIRECT_URL&response_type=code&scope=scope1+scope2&state=baz" {
|
||||||
|
t.Errorf("Auth code URL doesn't match the expected, found: %v", url)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestAuthCodeURL_Optional(t *testing.T) {
|
func TestAuthCodeURL_Optional(t *testing.T) {
|
||||||
conf := &Config{
|
conf := &Config{
|
||||||
ClientID: "CLIENT_ID",
|
ClientID: "CLIENT_ID",
|
||||||
|
|
Loading…
Reference in New Issue