Skip to content

Commit

Permalink
pkg/oauth2: dependency inject counter interface
Browse files Browse the repository at this point in the history
  • Loading branch information
s-urbaniak committed Jun 13, 2019
1 parent b10ffcf commit 0b82f70
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
7 changes: 4 additions & 3 deletions pkg/oauth2/token_source.go
Expand Up @@ -7,14 +7,15 @@ import (
"sync"
"time"

"github.com/prometheus/client_golang/prometheus"
"golang.org/x/oauth2"
)

type passwordCredentialsTokenSource struct {
ctx context.Context
cfg *oauth2.Config
username, password string
grantsCounter func()
grantsCounter prometheus.Counter

mu sync.Mutex // protects the fields below
refreshToken *oauth2.Token
Expand All @@ -34,7 +35,7 @@ type passwordCredentialsTokenSource struct {
// using the given resource owner and password.
//
// It is safe for concurrent use.
func NewPasswordCredentialsTokenSource(ctx context.Context, cfg *oauth2.Config, grantsCounter func(), username, password string) *passwordCredentialsTokenSource {
func NewPasswordCredentialsTokenSource(ctx context.Context, cfg *oauth2.Config, grantsCounter prometheus.Counter, username, password string) *passwordCredentialsTokenSource {
return &passwordCredentialsTokenSource{
ctx: ctx,
username: username,
Expand Down Expand Up @@ -83,7 +84,7 @@ func (c *passwordCredentialsTokenSource) Token() (*oauth2.Token, error) {
}

func (c *passwordCredentialsTokenSource) passwordCredentialsToken() (*oauth2.Token, error) {
c.grantsCounter()
c.grantsCounter.Inc()

tok, err := c.cfg.PasswordCredentialsToken(c.ctx, c.username, c.password)
if err != nil {
Expand Down
5 changes: 3 additions & 2 deletions pkg/oauth2/token_source_test.go
Expand Up @@ -10,6 +10,8 @@ import (
"testing"
"time"

"github.com/prometheus/client_golang/prometheus"

"golang.org/x/oauth2"
)

Expand Down Expand Up @@ -80,8 +82,7 @@ func TestPasswordCredentialsTokenSource(t *testing.T) {
},
)

nop := func() {}
src := NewPasswordCredentialsTokenSource(ctx, conf, nop, username, password)
src := NewPasswordCredentialsTokenSource(ctx, conf, prometheus.NewCounter(prometheus.CounterOpts{}), username, password)
return src, tr
}
}
Expand Down

0 comments on commit 0b82f70

Please sign in to comment.