1
0
mirror of https://github.com/volatiletech/authboss.git synced 2025-02-13 13:58:38 +02:00

Fix a problem with not getting the correct Context when running on Google App Engine

This commit is contained in:
orian 2015-09-11 16:03:05 +02:00 committed by Ryan Lester
parent 9cff57b284
commit 231e4b2283
6 changed files with 20 additions and 11 deletions

View File

@ -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()
}
}

View File

@ -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)
}

View File

@ -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
}

View File

@ -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",

View File

@ -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

View File

@ -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)
}