Skip to content

Commit

Permalink
Allows for for offline mode with refresh token (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
LifeOverIP authored Apr 24, 2022
1 parent e1cf09c commit 5e3a119
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,18 @@ type PersistentStore interface {
// This is not immediately obvious from the documentation.
// See https://developer.tdameritrade.com/content/authentication-faq
type Authenticator struct {
Store PersistentStore
OAuth2 oauth2.Config
Store PersistentStore
OAuth2 oauth2.Config
AuthOpts []oauth2.AuthCodeOption
}

// NewAuthenticator will automatically append @AMER.OAUTHAP to the client ID to save callers hours of frustration.
func NewAuthenticator(store PersistentStore, oauth2 oauth2.Config) *Authenticator {
func NewAuthenticator(store PersistentStore, oauth2 oauth2.Config, opts ...oauth2.AuthCodeOption) *Authenticator {
oauth2.ClientID = oauth2.ClientID + "@AMER.OAUTHAP"
return &Authenticator{
Store: store,
OAuth2: oauth2,
Store: store,
OAuth2: oauth2,
AuthOpts: opts,
}
}

Expand Down Expand Up @@ -75,7 +77,7 @@ func (a *Authenticator) StartOAuth2Flow(w http.ResponseWriter, req *http.Request
return "", err
}

return a.OAuth2.AuthCodeURL(state), nil
return a.OAuth2.AuthCodeURL(state, a.AuthOpts...), nil
}

// FinishOAuth2Flow finishes authenticating a user returning from TD Ameritrade.
Expand Down Expand Up @@ -104,7 +106,7 @@ func (a *Authenticator) FinishOAuth2Flow(ctx context.Context, w http.ResponseWri
if state[0] != expectedState {
return nil, fmt.Errorf("invalid state. expected: '%v', got '%v'", expectedState, state[0])
}
token, err := a.OAuth2.Exchange(ctx, code[0])
token, err := a.OAuth2.Exchange(ctx, code[0], a.AuthOpts...)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 5e3a119

Please sign in to comment.