Fail during conf init if auth or token URL is not valid.

This commit is contained in:
Burcu Dogan
2014-07-20 16:56:38 -07:00
parent 7935ece62b
commit 70bd497612
5 changed files with 35 additions and 41 deletions

View File

@@ -78,16 +78,19 @@ type Options struct {
// NewConfig creates a generic OAuth 2.0 configuration that talks
// to an OAuth 2.0 provider specified with authURL and tokenURL.
func NewConfig(opts *Options, authURL, tokenURL string) (*Config, error) {
conf := &Config{
opts: opts,
authURL: authURL,
tokenURL: tokenURL,
func NewConfig(opts *Options, authURL, tokenURL string) (conf *Config, err error) {
var aURL, tURL *url.URL
if aURL, err = url.Parse(authURL); err != nil {
return
}
if err := conf.validate(); err != nil {
if tURL, err = url.Parse(tokenURL); err != nil {
return
}
conf = &Config{opts: opts, authURL: aURL, tokenURL: tURL}
if err = conf.validate(); err != nil {
return nil, err
}
return conf, nil
return
}
// Config represents the configuration of an OAuth 2.0 consumer client.
@@ -95,17 +98,22 @@ type Config struct {
opts *Options
// AuthURL is the URL the user will be directed to
// in order to grant access.
authURL string
authURL *url.URL
// TokenURL is the URL used to retrieve OAuth tokens.
tokenURL string
tokenURL *url.URL
}
// AuthCodeURL returns a URL to OAuth 2.0 provider's consent page
// that asks for permissions for the required scopes explicitly.
func (c *Config) AuthCodeURL(state string) (authURL string, err error) {
u, err := url.Parse(c.authURL)
if err != nil {
return
func (c *Config) AuthCodeURL(state string) (authURL string) {
u := url.URL{
Scheme: c.authURL.Scheme,
Opaque: c.authURL.Opaque,
User: c.authURL.User,
Host: c.authURL.Host,
Path: c.authURL.Path,
RawQuery: c.authURL.RawQuery,
Fragment: c.authURL.Fragment,
}
q := url.Values{
"response_type": {"code"},
@@ -121,7 +129,7 @@ func (c *Config) AuthCodeURL(state string) (authURL string, err error) {
} else {
u.RawQuery += "&" + q
}
return u.String(), nil
return u.String()
}
// NewTransport creates a new authorizable transport. It doesn't
@@ -176,14 +184,6 @@ func (c *Config) validate() error {
if c.opts.RedirectURL == "" {
return errors.New("A redirect URL should be provided.")
}
// TODO(jbd): Validate the URLs. Maybe convert them to URL
// objects on construction.
if c.authURL == "" {
return errors.New("An auth URL should be provided.")
}
if c.tokenURL == "" {
return errors.New("A token URL should be provided.")
}
return nil
}
@@ -206,7 +206,7 @@ func (c *Config) exchange(exchangeCode string) (*Token, error) {
func (c *Config) updateToken(tok *Token, v url.Values) error {
v.Set("client_id", c.opts.ClientID)
v.Set("client_secret", c.opts.ClientSecret)
r, err := http.DefaultClient.PostForm(c.tokenURL, v)
r, err := http.DefaultClient.PostForm(c.tokenURL.String(), v)
if err != nil {
return err
}