Skip to content

Commit

Permalink
cleanup of imports, addition of logger interface, ability to change h…
Browse files Browse the repository at this point in the history
…ttp.Client (#5)

* cleanup of imports, addition of logger interface, ability to change http.Client

* change way of working with default logging
  • Loading branch information
imle authored Jun 7, 2021
1 parent 31d0e3e commit 47b1356
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 16 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,11 @@ go get github.com/s12v/[email protected]
package main

import (
"log"
"time"

"github.com/s12v/go-jwks"
"github.com/square/go-jose"
"time"
"log"
)

func main() {
Expand Down
3 changes: 2 additions & 1 deletion cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ package jwks

import (
"fmt"
"github.com/patrickmn/go-cache"
"time"

"github.com/patrickmn/go-cache"
)

type Cache interface {
Expand Down
3 changes: 2 additions & 1 deletion cache_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package jwks

import (
"github.com/patrickmn/go-cache"
"testing"
"time"

"github.com/patrickmn/go-cache"
)

func TestDefaultCache_Get(t *testing.T) {
Expand Down
6 changes: 3 additions & 3 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package jwks

import (
"fmt"
"time"

"github.com/square/go-jose"
"golang.org/x/sync/semaphore"
"log"
"time"
)

type JWKSClient interface {
Expand Down Expand Up @@ -62,7 +62,7 @@ func (c *jWKSClient) GetKey(keyId string, use string) (jwk *jose.JSONWebKey, err
go func() {
defer c.sem.Release(1)
if _, err := c.refreshKey(keyId, use); err != nil {
log.Printf("unable to refresh key: %v", err)
logger.Printf("unable to refresh key: %v", err)
}
}()
}
Expand Down
4 changes: 2 additions & 2 deletions client_mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ func (c *jWKSClientMock) GetKey(keyId string, use string) (*jose.JSONWebKey, err
return mockKey(c.secret), nil
}

func mockKey(secret string) (*jose.JSONWebKey) {
func mockKey(secret string) *jose.JSONWebKey {
return &jose.JSONWebKey{
KeyID: "key1",
Algorithm: "HS256",
Key: []byte(secret),
Key: []byte(secret),
}
}
5 changes: 3 additions & 2 deletions client_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package jwks

import (
"github.com/square/go-jose"
"testing"
"time"

"github.com/square/go-jose"
)

func TestJWKSClient_GetKey(t *testing.T) {
Expand Down Expand Up @@ -40,7 +41,7 @@ func TestJWKSClient_GetKeyWithPrefetch(t *testing.T) {
keyId,
&cacheEntry{
refresh: 0,
jwk: &mockJwk,
jwk: &mockJwk,
},
time.Unix(0, 0),
)
Expand Down
22 changes: 22 additions & 0 deletions logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package jwks

import (
"log"
)

type jwksLogger interface {
Printf(format string, v ...interface{})
}

// This is necessary to work around go1.12 requirement
type defaultLogger struct{}

func (defaultLogger) Printf(format string, v ...interface{}) {
log.Printf(format, v...)
}

var logger jwksLogger = defaultLogger{}

func SetLogger(l jwksLogger) {
logger = l
}
14 changes: 9 additions & 5 deletions source.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package jwks
import (
"encoding/json"
"fmt"
"github.com/square/go-jose"
"log"
"net/http"

"github.com/square/go-jose"
)

type JWKSSource interface {
Expand All @@ -17,15 +17,19 @@ type WebSource struct {
jwksUri string
}

func NewWebSource(jwksUri string) *WebSource {
func NewWebSource(jwksUri string, client *http.Client) *WebSource {
if client == nil {
client = new(http.Client)
}

return &WebSource{
client: new(http.Client),
client: client,
jwksUri: jwksUri,
}
}

func (s *WebSource) JSONWebKeySet() (*jose.JSONWebKeySet, error) {
log.Printf("Fetchng JWKS from %s", s.jwksUri)
logger.Printf("Fetching JWKS from %s", s.jwksUri)
resp, err := s.client.Get(s.jwksUri)
if err != nil {
return nil, err
Expand Down

0 comments on commit 47b1356

Please sign in to comment.