mirror of
https://github.com/volatiletech/authboss.git
synced 2024-12-12 10:45:11 +02:00
Merge pull request #81 from buu700/fix-gae-context
orian: Fix a problem with not getting the correct Context on App Engine
This commit is contained in:
commit
45dedd0c2d
@ -8,6 +8,7 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"golang.org/x/crypto/bcrypt"
|
"golang.org/x/crypto/bcrypt"
|
||||||
|
"golang.org/x/net/context"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Config holds all the configuration for both authboss and it's modules.
|
// 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
|
// 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.
|
// LogMailer and SMTPMailer, the default is a LogMailer to io.Discard.
|
||||||
Mailer Mailer
|
Mailer Mailer
|
||||||
|
// ContextProvider provides a context for a given request
|
||||||
|
ContextProvider func(*http.Request) context.Context
|
||||||
}
|
}
|
||||||
|
|
||||||
// Defaults sets the configuration's default values.
|
// Defaults sets the configuration's default values.
|
||||||
@ -163,4 +166,7 @@ func (c *Config) Defaults() {
|
|||||||
|
|
||||||
c.LogWriter = NewDefaultLogger()
|
c.LogWriter = NewDefaultLogger()
|
||||||
c.Mailer = LogMailer(ioutil.Discard)
|
c.Mailer = LogMailer(ioutil.Discard)
|
||||||
|
c.ContextProvider = func(req *http.Request) context.Context {
|
||||||
|
return context.TODO()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -287,14 +287,14 @@ func AssetNames() []string {
|
|||||||
|
|
||||||
// _bindata is a table, holding each asset generator, mapped to its name.
|
// _bindata is a table, holding each asset generator, mapped to its name.
|
||||||
var _bindata = map[string]func() (*asset, error){
|
var _bindata = map[string]func() (*asset, error){
|
||||||
"confirm_email.html.tpl": confirm_emailHtmlTpl,
|
"confirm_email.html.tpl": confirm_emailHtmlTpl,
|
||||||
"confirm_email.txt.tpl": confirm_emailTxtTpl,
|
"confirm_email.txt.tpl": confirm_emailTxtTpl,
|
||||||
"login.html.tpl": loginHtmlTpl,
|
"login.html.tpl": loginHtmlTpl,
|
||||||
"recover.html.tpl": recoverHtmlTpl,
|
"recover.html.tpl": recoverHtmlTpl,
|
||||||
"recover_complete.html.tpl": recover_completeHtmlTpl,
|
"recover_complete.html.tpl": recover_completeHtmlTpl,
|
||||||
"recover_email.html.tpl": recover_emailHtmlTpl,
|
"recover_email.html.tpl": recover_emailHtmlTpl,
|
||||||
"recover_email.txt.tpl": recover_emailTxtTpl,
|
"recover_email.txt.tpl": recover_emailTxtTpl,
|
||||||
"register.html.tpl": registerHtmlTpl,
|
"register.html.tpl": registerHtmlTpl,
|
||||||
}
|
}
|
||||||
|
|
||||||
// AssetDir returns the file names below a certain
|
// AssetDir returns the file names below a certain
|
||||||
@ -336,15 +336,16 @@ type bintree struct {
|
|||||||
Func func() (*asset, error)
|
Func func() (*asset, error)
|
||||||
Children map[string]*bintree
|
Children map[string]*bintree
|
||||||
}
|
}
|
||||||
|
|
||||||
var _bintree = &bintree{nil, map[string]*bintree{
|
var _bintree = &bintree{nil, map[string]*bintree{
|
||||||
"confirm_email.html.tpl": &bintree{confirm_emailHtmlTpl, map[string]*bintree{}},
|
"confirm_email.html.tpl": &bintree{confirm_emailHtmlTpl, map[string]*bintree{}},
|
||||||
"confirm_email.txt.tpl": &bintree{confirm_emailTxtTpl, map[string]*bintree{}},
|
"confirm_email.txt.tpl": &bintree{confirm_emailTxtTpl, map[string]*bintree{}},
|
||||||
"login.html.tpl": &bintree{loginHtmlTpl, map[string]*bintree{}},
|
"login.html.tpl": &bintree{loginHtmlTpl, map[string]*bintree{}},
|
||||||
"recover.html.tpl": &bintree{recoverHtmlTpl, map[string]*bintree{}},
|
"recover.html.tpl": &bintree{recoverHtmlTpl, map[string]*bintree{}},
|
||||||
"recover_complete.html.tpl": &bintree{recover_completeHtmlTpl, 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.html.tpl": &bintree{recover_emailHtmlTpl, map[string]*bintree{}},
|
||||||
"recover_email.txt.tpl": &bintree{recover_emailTxtTpl, map[string]*bintree{}},
|
"recover_email.txt.tpl": &bintree{recover_emailTxtTpl, map[string]*bintree{}},
|
||||||
"register.html.tpl": &bintree{registerHtmlTpl, map[string]*bintree{}},
|
"register.html.tpl": &bintree{registerHtmlTpl, map[string]*bintree{}},
|
||||||
}}
|
}}
|
||||||
|
|
||||||
// RestoreAsset restores an asset under the given directory
|
// RestoreAsset restores an asset under the given directory
|
||||||
@ -393,4 +394,3 @@ func _filePath(dir, name string) string {
|
|||||||
cannonicalName := strings.Replace(name, "\\", "/", -1)
|
cannonicalName := strings.Replace(name, "\\", "/", -1)
|
||||||
return filepath.Join(append([]string{dir}, strings.Split(cannonicalName, "/")...)...)
|
return filepath.Join(append([]string{dir}, strings.Split(cannonicalName, "/")...)...)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -3,6 +3,7 @@ package authboss
|
|||||||
import (
|
import (
|
||||||
"net/url"
|
"net/url"
|
||||||
|
|
||||||
|
"golang.org/x/net/context"
|
||||||
"golang.org/x/oauth2"
|
"golang.org/x/oauth2"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -35,5 +36,5 @@ database/driver.Valuer.
|
|||||||
type OAuth2Provider struct {
|
type OAuth2Provider struct {
|
||||||
OAuth2Config *oauth2.Config
|
OAuth2Config *oauth2.Config
|
||||||
AdditionalParams url.Values
|
AdditionalParams url.Values
|
||||||
Callback func(oauth2.Config, *oauth2.Token) (Attributes, error)
|
Callback func(context.Context, oauth2.Config, *oauth2.Token) (Attributes, error)
|
||||||
}
|
}
|
||||||
|
@ -168,12 +168,12 @@ func (o *OAuth2) oauthCallback(ctx *authboss.Context, w http.ResponseWriter, r *
|
|||||||
|
|
||||||
// Get the code
|
// Get the code
|
||||||
code := r.FormValue("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 {
|
if err != nil {
|
||||||
return fmt.Errorf("Could not validate oauth2 code: %v", err)
|
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 {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -143,7 +143,7 @@ func TestOAuthSuccess(t *testing.T) {
|
|||||||
Expiry: expiry,
|
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{
|
return authboss.Attributes{
|
||||||
authboss.StoreOAuth2UID: "uid",
|
authboss.StoreOAuth2UID: "uid",
|
||||||
authboss.StoreEmail: "email",
|
authboss.StoreEmail: "email",
|
||||||
|
@ -4,6 +4,7 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
|
"golang.org/x/net/context"
|
||||||
"golang.org/x/oauth2"
|
"golang.org/x/oauth2"
|
||||||
"gopkg.in/authboss.v0"
|
"gopkg.in/authboss.v0"
|
||||||
)
|
)
|
||||||
@ -22,8 +23,8 @@ type googleMeResponse struct {
|
|||||||
var clientGet = (*http.Client).Get
|
var clientGet = (*http.Client).Get
|
||||||
|
|
||||||
// Google is a callback appropriate for use with Google's OAuth2 configuration.
|
// Google is a callback appropriate for use with Google's OAuth2 configuration.
|
||||||
func Google(cfg oauth2.Config, token *oauth2.Token) (authboss.Attributes, error) {
|
func Google(ctx context.Context, cfg oauth2.Config, token *oauth2.Token) (authboss.Attributes, error) {
|
||||||
client := cfg.Client(oauth2.NoContext, token)
|
client := cfg.Client(ctx, token)
|
||||||
resp, err := clientGet(client, googleInfoEndpoint)
|
resp, err := clientGet(client, googleInfoEndpoint)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@ -49,8 +50,8 @@ type facebookMeResponse struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Facebook is a callback appropriate for use with Facebook's OAuth2 configuration.
|
// Facebook is a callback appropriate for use with Facebook's OAuth2 configuration.
|
||||||
func Facebook(cfg oauth2.Config, token *oauth2.Token) (authboss.Attributes, error) {
|
func Facebook(ctx context.Context, cfg oauth2.Config, token *oauth2.Token) (authboss.Attributes, error) {
|
||||||
client := cfg.Client(oauth2.NoContext, token)
|
client := cfg.Client(ctx, token)
|
||||||
resp, err := clientGet(client, facebookInfoEndpoint)
|
resp, err := clientGet(client, facebookInfoEndpoint)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
@ -7,6 +7,7 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"golang.org/x/net/context"
|
||||||
"golang.org/x/oauth2"
|
"golang.org/x/oauth2"
|
||||||
"gopkg.in/authboss.v0"
|
"gopkg.in/authboss.v0"
|
||||||
)
|
)
|
||||||
@ -31,7 +32,7 @@ func TestGoogle(t *testing.T) {
|
|||||||
Expiry: time.Now().Add(60 * time.Minute),
|
Expiry: time.Now().Add(60 * time.Minute),
|
||||||
}
|
}
|
||||||
|
|
||||||
user, err := Google(cfg, tok)
|
user, err := Google(context.TODO(), cfg, tok)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
}
|
}
|
||||||
@ -64,7 +65,7 @@ func TestFacebook(t *testing.T) {
|
|||||||
Expiry: time.Now().Add(60 * time.Minute),
|
Expiry: time.Now().Add(60 * time.Minute),
|
||||||
}
|
}
|
||||||
|
|
||||||
user, err := Facebook(cfg, tok)
|
user, err := Facebook(context.TODO(), cfg, tok)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user