Skip to content

Add AccessTokenOption from Improbable soft fork #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions oauth2.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,17 @@ func SetAuthURLParam(key, value string) AuthCodeOption {
return setParam{key, value}
}

// An AccessTokenOption is passed to Config.Exchange.
type AccessTokenOption interface {
setValue(url.Values)
}

// SetAccessTokenOption builds an AccessTokenOption which passes key/value parameters
// to a provider's access token endpoint.
func SetAccessTokenOption(key, value string) AccessTokenOption {
return setParam{key, value}
}

// AuthCodeURL returns a URL to OAuth 2.0 provider's consent page
// that asks for permissions for the required scopes explicitly.
//
Expand Down Expand Up @@ -175,13 +186,17 @@ func (c *Config) PasswordCredentialsToken(ctx context.Context, username, passwor
//
// The code will be in the *http.Request.FormValue("code"). Before
// calling Exchange, be sure to validate FormValue("state").
func (c *Config) Exchange(ctx context.Context, code string) (*Token, error) {
return retrieveToken(ctx, c, url.Values{
func (c *Config) Exchange(ctx context.Context, code string, opts ...AccessTokenOption) (*Token, error) {
v := url.Values{
"grant_type": {"authorization_code"},
"code": {code},
"redirect_uri": internal.CondVal(c.RedirectURL),
"scope": internal.CondVal(strings.Join(c.Scopes, " ")),
})
}
for _, opt := range opts {
opt.setValue(v)
}
return retrieveToken(ctx, c, v)
}

// Client returns an HTTP client using the provided token.
Expand Down
4 changes: 4 additions & 0 deletions token.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,3 +156,7 @@ func retrieveToken(ctx context.Context, c *Config, v url.Values) (*Token, error)
}
return tokenFromInternal(tk), nil
}

func RetrieveToken(ctx context.Context, c *Config, v url.Values) (*Token, error) {
return retrieveToken(ctx, c, v)
}