From 9cff57b28448207b805afcb393e4119587dead95 Mon Sep 17 00:00:00 2001 From: orian Date: Mon, 3 Aug 2015 22:35:43 +0200 Subject: [PATCH 1/3] add Facebook provider for oauth2 --- oauth2/providers.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/oauth2/providers.go b/oauth2/providers.go index dfc74cd..12e72d5 100644 --- a/oauth2/providers.go +++ b/oauth2/providers.go @@ -48,7 +48,7 @@ type facebookMeResponse struct { Name string `json:"name"` } -// Facebook is a callback appropriate for use with Facebook's OAuth2 configuration. +// Google is a callback appropriate for use with Google's OAuth2 configuration. func Facebook(cfg oauth2.Config, token *oauth2.Token) (authboss.Attributes, error) { client := cfg.Client(oauth2.NoContext, token) resp, err := clientGet(client, facebookInfoEndpoint) From 231e4b22835bea85ece45ad68558e1581c88d67c Mon Sep 17 00:00:00 2001 From: orian Date: Fri, 11 Sep 2015 16:03:05 +0200 Subject: [PATCH 2/3] Fix a problem with not getting the correct Context when running on Google App Engine --- config.go | 6 ++++++ oauth2.go | 3 ++- oauth2/oauth2.go | 4 ++-- oauth2/oauth2_test.go | 2 +- oauth2/providers.go | 11 ++++++----- oauth2/providers_test.go | 5 +++-- 6 files changed, 20 insertions(+), 11 deletions(-) diff --git a/config.go b/config.go index 0e4e3b4..3f916c7 100644 --- a/config.go +++ b/config.go @@ -8,6 +8,7 @@ import ( "time" "golang.org/x/crypto/bcrypt" + "golang.org/x/net/context" ) // Config holds all the configuration for both authboss and it's modules. @@ -113,6 +114,8 @@ type Config struct { // Mailer is the mailer being used to send e-mails out. Authboss defines two loggers for use // LogMailer and SMTPMailer, the default is a LogMailer to io.Discard. Mailer Mailer + // ContextProvider provides a context for a given request + ContextProvider func(*http.Request) context.Context } // Defaults sets the configuration's default values. @@ -163,4 +166,7 @@ func (c *Config) Defaults() { c.LogWriter = NewDefaultLogger() c.Mailer = LogMailer(ioutil.Discard) + c.ContextProvider = func(req *http.Request) context.Context { + return context.TODO() + } } diff --git a/oauth2.go b/oauth2.go index 60cdf19..3c2c56f 100644 --- a/oauth2.go +++ b/oauth2.go @@ -4,6 +4,7 @@ import ( "net/url" "golang.org/x/oauth2" + "golang.org/x/net/context" ) /* @@ -35,5 +36,5 @@ database/driver.Valuer. type OAuth2Provider struct { OAuth2Config *oauth2.Config AdditionalParams url.Values - Callback func(oauth2.Config, *oauth2.Token) (Attributes, error) + Callback func(context.Context, oauth2.Config, *oauth2.Token) (Attributes, error) } diff --git a/oauth2/oauth2.go b/oauth2/oauth2.go index b575552..66c1e26 100644 --- a/oauth2/oauth2.go +++ b/oauth2/oauth2.go @@ -168,12 +168,12 @@ func (o *OAuth2) oauthCallback(ctx *authboss.Context, w http.ResponseWriter, r * // Get the code code := r.FormValue("code") - token, err := exchanger(cfg.OAuth2Config, oauth2.NoContext, code) + token, err := exchanger(cfg.OAuth2Config, o.Config.ContextProvider(r), code) if err != nil { return fmt.Errorf("Could not validate oauth2 code: %v", err) } - user, err := cfg.Callback(*cfg.OAuth2Config, token) + user, err := cfg.Callback(o.Config.ContextProvider(r), *cfg.OAuth2Config, token) if err != nil { return err } diff --git a/oauth2/oauth2_test.go b/oauth2/oauth2_test.go index 31b5731..7ea41cf 100644 --- a/oauth2/oauth2_test.go +++ b/oauth2/oauth2_test.go @@ -143,7 +143,7 @@ func TestOAuthSuccess(t *testing.T) { Expiry: expiry, } - fakeCallback := func(_ oauth2.Config, _ *oauth2.Token) (authboss.Attributes, error) { + fakeCallback := func(_ context.Context, _ oauth2.Config, _ *oauth2.Token) (authboss.Attributes, error) { return authboss.Attributes{ authboss.StoreOAuth2UID: "uid", authboss.StoreEmail: "email", diff --git a/oauth2/providers.go b/oauth2/providers.go index 12e72d5..2c6a8f1 100644 --- a/oauth2/providers.go +++ b/oauth2/providers.go @@ -4,6 +4,7 @@ import ( "encoding/json" "net/http" + "golang.org/x/net/context" "golang.org/x/oauth2" "gopkg.in/authboss.v0" ) @@ -22,8 +23,8 @@ type googleMeResponse struct { var clientGet = (*http.Client).Get // Google is a callback appropriate for use with Google's OAuth2 configuration. -func Google(cfg oauth2.Config, token *oauth2.Token) (authboss.Attributes, error) { - client := cfg.Client(oauth2.NoContext, token) +func Google(ctx context.Context, cfg oauth2.Config, token *oauth2.Token) (authboss.Attributes, error) { + client := cfg.Client(ctx, token) resp, err := clientGet(client, googleInfoEndpoint) if err != nil { return nil, err @@ -48,9 +49,9 @@ type facebookMeResponse struct { Name string `json:"name"` } -// Google is a callback appropriate for use with Google's OAuth2 configuration. -func Facebook(cfg oauth2.Config, token *oauth2.Token) (authboss.Attributes, error) { - client := cfg.Client(oauth2.NoContext, token) +// Facebook is a callback appropriate for use with Facebook's OAuth2 configuration. +func Facebook(ctx context.Context, cfg oauth2.Config, token *oauth2.Token) (authboss.Attributes, error) { + client := cfg.Client(ctx, token) resp, err := clientGet(client, facebookInfoEndpoint) if err != nil { return nil, err diff --git a/oauth2/providers_test.go b/oauth2/providers_test.go index 02ee88f..91fe827 100644 --- a/oauth2/providers_test.go +++ b/oauth2/providers_test.go @@ -7,6 +7,7 @@ import ( "testing" "time" + "golang.org/x/net/context" "golang.org/x/oauth2" "gopkg.in/authboss.v0" ) @@ -31,7 +32,7 @@ func TestGoogle(t *testing.T) { Expiry: time.Now().Add(60 * time.Minute), } - user, err := Google(cfg, tok) + user, err := Google(context.TODO(), cfg, tok) if err != nil { t.Error(err) } @@ -64,7 +65,7 @@ func TestFacebook(t *testing.T) { Expiry: time.Now().Add(60 * time.Minute), } - user, err := Facebook(cfg, tok) + user, err := Facebook(context.TODO(), cfg, tok) if err != nil { t.Error(err) } From 8655934e605e1c4c36c549d78ceebb5a39a6261c Mon Sep 17 00:00:00 2001 From: Ryan Lester Date: Tue, 23 Feb 2016 21:28:43 -0800 Subject: [PATCH 3/3] run gofmt --- internal/response/bindata.go | 30 +++++++++++++++--------------- oauth2.go | 2 +- 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/internal/response/bindata.go b/internal/response/bindata.go index 32bd153..c6dd525 100644 --- a/internal/response/bindata.go +++ b/internal/response/bindata.go @@ -287,14 +287,14 @@ func AssetNames() []string { // _bindata is a table, holding each asset generator, mapped to its name. var _bindata = map[string]func() (*asset, error){ - "confirm_email.html.tpl": confirm_emailHtmlTpl, - "confirm_email.txt.tpl": confirm_emailTxtTpl, - "login.html.tpl": loginHtmlTpl, - "recover.html.tpl": recoverHtmlTpl, + "confirm_email.html.tpl": confirm_emailHtmlTpl, + "confirm_email.txt.tpl": confirm_emailTxtTpl, + "login.html.tpl": loginHtmlTpl, + "recover.html.tpl": recoverHtmlTpl, "recover_complete.html.tpl": recover_completeHtmlTpl, - "recover_email.html.tpl": recover_emailHtmlTpl, - "recover_email.txt.tpl": recover_emailTxtTpl, - "register.html.tpl": registerHtmlTpl, + "recover_email.html.tpl": recover_emailHtmlTpl, + "recover_email.txt.tpl": recover_emailTxtTpl, + "register.html.tpl": registerHtmlTpl, } // AssetDir returns the file names below a certain @@ -336,15 +336,16 @@ type bintree struct { Func func() (*asset, error) Children map[string]*bintree } + var _bintree = &bintree{nil, map[string]*bintree{ - "confirm_email.html.tpl": &bintree{confirm_emailHtmlTpl, map[string]*bintree{}}, - "confirm_email.txt.tpl": &bintree{confirm_emailTxtTpl, map[string]*bintree{}}, - "login.html.tpl": &bintree{loginHtmlTpl, map[string]*bintree{}}, - "recover.html.tpl": &bintree{recoverHtmlTpl, map[string]*bintree{}}, + "confirm_email.html.tpl": &bintree{confirm_emailHtmlTpl, map[string]*bintree{}}, + "confirm_email.txt.tpl": &bintree{confirm_emailTxtTpl, map[string]*bintree{}}, + "login.html.tpl": &bintree{loginHtmlTpl, map[string]*bintree{}}, + "recover.html.tpl": &bintree{recoverHtmlTpl, map[string]*bintree{}}, "recover_complete.html.tpl": &bintree{recover_completeHtmlTpl, map[string]*bintree{}}, - "recover_email.html.tpl": &bintree{recover_emailHtmlTpl, map[string]*bintree{}}, - "recover_email.txt.tpl": &bintree{recover_emailTxtTpl, map[string]*bintree{}}, - "register.html.tpl": &bintree{registerHtmlTpl, map[string]*bintree{}}, + "recover_email.html.tpl": &bintree{recover_emailHtmlTpl, map[string]*bintree{}}, + "recover_email.txt.tpl": &bintree{recover_emailTxtTpl, map[string]*bintree{}}, + "register.html.tpl": &bintree{registerHtmlTpl, map[string]*bintree{}}, }} // RestoreAsset restores an asset under the given directory @@ -393,4 +394,3 @@ func _filePath(dir, name string) string { cannonicalName := strings.Replace(name, "\\", "/", -1) return filepath.Join(append([]string{dir}, strings.Split(cannonicalName, "/")...)...) } - diff --git a/oauth2.go b/oauth2.go index 3c2c56f..ecf21f0 100644 --- a/oauth2.go +++ b/oauth2.go @@ -3,8 +3,8 @@ package authboss import ( "net/url" - "golang.org/x/oauth2" "golang.org/x/net/context" + "golang.org/x/oauth2" ) /*