Skip to content

Commit

Permalink
Merge pull request #211 from Spazzy757/master
Browse files Browse the repository at this point in the history
chore: make session on app configurable
  • Loading branch information
huandu authored Apr 3, 2023
2 parents d327788 + 8710deb commit 106621b
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,18 @@ type App struct {
// Enable appsecret proof in every API call to facebook.
// Facebook document: https://developers.facebook.com/docs/graph-api/securing-requests
EnableAppsecretProof bool

// The session to send request when parsing tokens or code.
// If it's not set, default session will be used.
session *Session
}

// New creates a new App and sets app id and secret.
func New(appID, appSecret string) *App {
return &App{
AppId: appID,
AppSecret: appSecret,
session: defaultSession,
}
}

Expand All @@ -45,6 +50,11 @@ func (app *App) AppAccessToken() string {
return app.AppId + "|" + app.AppSecret
}

// SetSession is used to overwrite the default session used by the app
func (app *App) SetSession(s *Session) {
app.session = s
}

// ParseSignedRequest parses signed request.
func (app *App) ParseSignedRequest(signedRequest string) (res Result, err error) {
strs := strings.SplitN(signedRequest, ".", 2)
Expand Down Expand Up @@ -124,7 +134,7 @@ func (app *App) ParseCodeInfo(code, machineID string) (token string, expires int
}

var res Result
res, err = defaultSession.sendOauthRequest("/oauth/access_token", Params{
res, err = app.session.sendOauthRequest("/oauth/access_token", Params{
"client_id": app.AppId,
"redirect_uri": app.RedirectUri,
"client_secret": app.AppSecret,
Expand Down Expand Up @@ -172,7 +182,7 @@ func (app *App) ExchangeToken(accessToken string) (token string, expires int, er
}

var res Result
res, err = defaultSession.sendOauthRequest("/oauth/access_token", Params{
res, err = app.session.sendOauthRequest("/oauth/access_token", Params{
"grant_type": "fb_exchange_token",
"client_id": app.AppId,
"client_secret": app.AppSecret,
Expand Down Expand Up @@ -212,7 +222,7 @@ func (app *App) GetCode(accessToken string) (code string, err error) {
}

var res Result
res, err = defaultSession.sendOauthRequest("/oauth/client_code", Params{
res, err = app.session.sendOauthRequest("/oauth/client_code", Params{
"client_id": app.AppId,
"client_secret": app.AppSecret,
"redirect_uri": app.RedirectUri,
Expand Down

0 comments on commit 106621b

Please sign in to comment.