-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #78 from okta/tschaub_cache-config
Tschaub cache config
- Loading branch information
Showing
8 changed files
with
257 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,23 @@ | ||
# Changelog | ||
|
||
## v1.1.2 | ||
## v1.2.0 (February 16, 2022) | ||
|
||
### Updates | ||
|
||
- Only `alg` and `kid` claims in a JWT header are considered during verification. | ||
* Customizable resource cache. Thanks, [@tschaub](https://github.com/tschaub)! | ||
|
||
|
||
## v1.1.3 | ||
|
||
### Updates | ||
|
||
- Fixed edge cause with `aud` claim that would not find Auth0 being JWTs valid (thank you @awrenn). | ||
- Updated readme with testing notes. | ||
- Ran `gofumpt` on code for clean up. | ||
- Ran `gofumpt` on code for clean up. | ||
|
||
## v1.1.2 | ||
|
||
### Updates | ||
|
||
- Only `alg` and `kid` claims in a JWT header are considered during verification. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package utils | ||
|
||
import ( | ||
"sync" | ||
"time" | ||
|
||
"github.com/patrickmn/go-cache" | ||
) | ||
|
||
// Cacher is a read-only cache interface. | ||
// | ||
// Get returns the value associated with the given key. | ||
type Cacher interface { | ||
Get(string) (interface{}, error) | ||
} | ||
|
||
type defaultCache struct { | ||
cache *cache.Cache | ||
lookup func(string) (interface{}, error) | ||
mutex *sync.Mutex | ||
} | ||
|
||
func (c *defaultCache) Get(key string) (interface{}, error) { | ||
c.mutex.Lock() | ||
defer c.mutex.Unlock() | ||
|
||
if value, found := c.cache.Get(key); found { | ||
return value, nil | ||
} | ||
|
||
value, err := c.lookup(key) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
c.cache.SetDefault(key, value) | ||
return value, nil | ||
} | ||
|
||
// defaultCache implements the Cacher interface | ||
var _ Cacher = (*defaultCache)(nil) | ||
|
||
// NewDefaultCache returns cache with a 5 minute expiration. | ||
func NewDefaultCache(lookup func(string) (interface{}, error)) (Cacher, error) { | ||
return &defaultCache{ | ||
cache: cache.New(5*time.Minute, 10*time.Minute), | ||
lookup: lookup, | ||
mutex: &sync.Mutex{}, | ||
}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package utils_test | ||
|
||
import ( | ||
"fmt" | ||
|
||
jwtverifier "github.com/okta/okta-jwt-verifier-golang" | ||
"github.com/okta/okta-jwt-verifier-golang/utils" | ||
) | ||
|
||
// ForeverCache caches values forever | ||
type ForeverCache struct { | ||
values map[string]interface{} | ||
lookup func(string) (interface{}, error) | ||
} | ||
|
||
// Get returns the value for the given key | ||
func (c *ForeverCache) Get(key string) (interface{}, error) { | ||
value, ok := c.values[key] | ||
if ok { | ||
return value, nil | ||
} | ||
value, err := c.lookup(key) | ||
if err != nil { | ||
return nil, err | ||
} | ||
c.values[key] = value | ||
return value, nil | ||
} | ||
|
||
// ForeverCache implements the read-only Cacher interface | ||
var _ utils.Cacher = (*ForeverCache)(nil) | ||
|
||
// NewForeverCache takes a lookup function and returns a cache | ||
func NewForeverCache(lookup func(string) (interface{}, error)) (utils.Cacher, error) { | ||
return &ForeverCache{ | ||
values: map[string]interface{}{}, | ||
lookup: lookup, | ||
}, nil | ||
} | ||
|
||
// Example demonstrating how the JwtVerifier can be configured with a custom Cache function. | ||
func Example() { | ||
jwtVerifierSetup := jwtverifier.JwtVerifier{ | ||
Cache: NewForeverCache, | ||
// other fields here | ||
} | ||
|
||
verifier := jwtVerifierSetup.New() | ||
fmt.Println(verifier) | ||
} |
Oops, something went wrong.