forked from Mirrors/oauth2
Merge pull request #15 from stacktic/dropbox
Making AccessType, ApprovalPrompt and RedirectURL optional
This commit is contained in:
commit
33dee1ee8d
35
oauth2.go
35
oauth2.go
|
@ -108,15 +108,22 @@ type Config struct {
|
||||||
// that asks for permissions for the required scopes explicitly.
|
// that asks for permissions for the required scopes explicitly.
|
||||||
func (c *Config) AuthCodeURL(state string) (authURL string) {
|
func (c *Config) AuthCodeURL(state string) (authURL string) {
|
||||||
u := *c.authURL
|
u := *c.authURL
|
||||||
q := url.Values{
|
vals := url.Values{
|
||||||
"response_type": {"code"},
|
"response_type": {"code"},
|
||||||
"client_id": {c.opts.ClientID},
|
"client_id": {c.opts.ClientID},
|
||||||
"redirect_uri": {c.opts.RedirectURL},
|
|
||||||
"scope": {strings.Join(c.opts.Scopes, " ")},
|
"scope": {strings.Join(c.opts.Scopes, " ")},
|
||||||
"state": {state},
|
"state": {state},
|
||||||
"access_type": {c.opts.AccessType},
|
}
|
||||||
"approval_prompt": {c.opts.ApprovalPrompt},
|
if c.opts.AccessType != "" {
|
||||||
}.Encode()
|
vals.Set("access_type", c.opts.AccessType)
|
||||||
|
}
|
||||||
|
if c.opts.ApprovalPrompt != "" {
|
||||||
|
vals.Set("approval_prompt", c.opts.ApprovalPrompt)
|
||||||
|
}
|
||||||
|
if c.opts.RedirectURL != "" {
|
||||||
|
vals.Set("redirect_uri", c.opts.RedirectURL)
|
||||||
|
}
|
||||||
|
q := vals.Encode()
|
||||||
if u.RawQuery == "" {
|
if u.RawQuery == "" {
|
||||||
u.RawQuery = q
|
u.RawQuery = q
|
||||||
} else {
|
} else {
|
||||||
|
@ -172,11 +179,6 @@ func (c *Config) validate() error {
|
||||||
if c.opts.ClientSecret == "" {
|
if c.opts.ClientSecret == "" {
|
||||||
return errors.New("A client secret should be provided.")
|
return errors.New("A client secret should be provided.")
|
||||||
}
|
}
|
||||||
// TODO(jbd): Are redirect URIs allowed to be a
|
|
||||||
// non-value string in the spec?
|
|
||||||
if c.opts.RedirectURL == "" {
|
|
||||||
return errors.New("A redirect URL should be provided.")
|
|
||||||
}
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -184,12 +186,17 @@ func (c *Config) validate() error {
|
||||||
// to retrieve a new access token.
|
// to retrieve a new access token.
|
||||||
func (c *Config) exchange(exchangeCode string) (*Token, error) {
|
func (c *Config) exchange(exchangeCode string) (*Token, error) {
|
||||||
token := &Token{}
|
token := &Token{}
|
||||||
err := c.updateToken(token, url.Values{
|
vals := url.Values{
|
||||||
"grant_type": {"authorization_code"},
|
"grant_type": {"authorization_code"},
|
||||||
"redirect_uri": {c.opts.RedirectURL},
|
|
||||||
"scope": {strings.Join(c.opts.Scopes, " ")},
|
|
||||||
"code": {exchangeCode},
|
"code": {exchangeCode},
|
||||||
})
|
}
|
||||||
|
if len(c.opts.Scopes) != 0 {
|
||||||
|
vals.Set("scope", strings.Join(c.opts.Scopes, " "))
|
||||||
|
}
|
||||||
|
if c.opts.RedirectURL != "" {
|
||||||
|
vals.Set("redirect_uri", c.opts.RedirectURL)
|
||||||
|
}
|
||||||
|
err := c.updateToken(token, vals)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue