Skip to content

Commit

Permalink
Go-vcr fix.
Browse files Browse the repository at this point in the history
Note that you must be careful to use fixed data (nothing randomly generated) for
go-vcr to really work
  • Loading branch information
kraney committed Sep 5, 2020
1 parent 4014c68 commit 2f615e4
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,9 @@ func main() {
This TokenSource is also designed to facilitate use of [GoVCR](https://github.com/seborama/govcr) for testing.

```golang
var ts *spauth.TokenSource = spauth.NewTokenSource(*clientid, *clientsecret)
ts.Client = govcr.NewVCR(testname, &govcr.VCRConfig{
httpclient := govcr.NewVCR(testname, &govcr.VCRConfig{
CassettePath: "./testdata/recordings",
}).Client
var ts *spauth.TokenSource = spauth.NewTokenSource(*clientid, *clientsecret, spauth.HTTPClientOption(httpclient))
auth := context.WithValue(context.Background(), stacks.ContextOAuth2, ts)
```
22 changes: 18 additions & 4 deletions pkg/oauth2/tokensource.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,39 @@ import (
// TokenSource is an oauth2 token source for stackpath
type TokenSource struct {
req user.IdentityGetAccessTokenRequest
Client *http.Client
client *http.Client
}

// Option is used to pass options to the client during creation
type Option func(*TokenSource)

// HTTPClientOption returns an option telling the Client to use the given http.Client
func HTTPClientOption(client *http.Client) Option {
return func(sps *TokenSource) {
sps.client = client
}
}

// NewTokenSource returns a new token source for stackpath
func NewTokenSource(clientid string, clientsecret string) *TokenSource {
func NewTokenSource(clientid string, clientsecret string, options ...Option) oauth2.TokenSource {
creds := "client_credentials"
return &TokenSource{
src := &TokenSource{
req: user.IdentityGetAccessTokenRequest{
ClientId: &clientid,
ClientSecret: &clientsecret,
GrantType: &creds,
},
}
for _, opt := range options {
opt(src)
}
return oauth2.ReuseTokenSource(nil, src)
}

// Token returns a token
func (sps *TokenSource) Token() (*oauth2.Token, error) {
usercfg := user.NewConfiguration()
usercfg.HTTPClient = sps.Client
usercfg.HTTPClient = sps.client
userclient := user.NewAPIClient(usercfg)
idresp, _, err := userclient.AuthenticationApi.GetAccessToken(context.Background()).IdentityGetAccessTokenRequest(sps.req).Execute()
if err != nil {
Expand Down

0 comments on commit 2f615e4

Please sign in to comment.