Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 2 commits into from
Jun 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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/go-jwks@v0.2.1
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