Skip to content

Commit

Permalink
Experimental App Engine changes for Facebook
Browse files Browse the repository at this point in the history
  • Loading branch information
oyvindsk committed Oct 8, 2015
1 parent 456c8ad commit 6e8230b
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
8 changes: 7 additions & 1 deletion providers/facebook/facebook.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (

"github.com/markbates/goth"
"golang.org/x/oauth2"
"golang.org/x/net/context"
)

const (
Expand All @@ -30,6 +31,8 @@ func New(clientKey, secret, callbackURL string, scopes ...string) *Provider {
Secret: secret,
CallbackURL: callbackURL,
}
p.ContextProvider = func() context.Context { return context.TODO() }
p.ClientProvider = func() *http.Client { return http.DefaultClient }
p.config = newConfig(p, scopes)
return p
}
Expand All @@ -39,6 +42,8 @@ type Provider struct {
ClientKey string

This comment has been minimized.

Copy link
@Ahtreek

Ahtreek Jul 17, 2017

fix

Secret string
CallbackURL string
ContextProvider func() context.Context
ClientProvider func() *http.Client
config *oauth2.Config
}

Expand Down Expand Up @@ -67,7 +72,8 @@ func (p *Provider) FetchUser(session goth.Session) (goth.User, error) {
Provider: p.Name(),
}

response, err := http.Get(endpointProfile + "&access_token=" + url.QueryEscape(sess.AccessToken))
client := p.ClientProvider()
response, err := client.Get(endpointProfile + "&access_token=" + url.QueryEscape(sess.AccessToken))
if err != nil {
return user, err
}
Expand Down
4 changes: 2 additions & 2 deletions providers/facebook/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"errors"

"github.com/markbates/goth"
"golang.org/x/oauth2"
)

// Session stores data during the auth process with Facebook.
Expand All @@ -25,7 +24,8 @@ func (s Session) GetAuthURL() (string, error) {
// Authorize the session with Facebook and return the access token to be stored for future use.
func (s *Session) Authorize(provider goth.Provider, params goth.Params) (string, error) {
p := provider.(*Provider)
token, err := p.config.Exchange(oauth2.NoContext, params.Get("code"))
ctx := p.ContextProvider()
token, err := p.config.Exchange(ctx, params.Get("code"))
if err != nil {
return "", err
}
Expand Down

2 comments on commit 6e8230b

@oyvindsk
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Used like this:

func init(){
    fb := facebook.New("", "", "http://localhost:8080/auth/facebook/callback", )
    fb.ContextProvider = getCurrentContext
    fb.ClientProvider = getHttpClient
    goth.UseProviders(
        fb,
    )
}

var httpClient *http.Client
var currentContext context.Context

func getHttpClient() *http.Client {
    return urlfetch.Client(getCurrentContext())
}

func getCurrentContext() context.Context {
    return currentContext
}


// .. 


func rootHandler(res http.ResponseWriter, req *http.Request) {
    c := appengine.NewContext(req)
    // I *think* the context changes for each request so we have to constantly update it =[
    currentContext = c
    //..
}

func otherHandler(res http.ResponseWriter, req *http.Request) {
    c := appengine.NewContext(req)
    currentContext = c
    //..
}

@oyvindsk
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be much nicer if we could supply http.Request to the ContextProvider.

Please sign in to comment.