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
JSON Web Key Support #410
Merged
Merged
JSON Web Key Support #410
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
1a64f64
Add basic JWKS support
danikarik 8355e05
Add Test_tokenVerifierJWT_VerifyConnectTokenWithJWK
danikarik d882f3e
Merge remote-tracking branch 'upstream/master'
danikarik c78f6cf
Minor fixes
danikarik 19eb3a6
Fix test cases for Test_tokenVerifierJWT_VerifyConnectTokenWithJWK
danikarik 24f6666
Cleanup modules
danikarik 9e82732
Add jwks package
danikarik 140f649
Replace JWKS client ti internal implementation
danikarik 2046e90
Cleanup
danikarik 3214c63
Minor tweaks
danikarik File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
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,22 @@ | ||
package jwks | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
) | ||
|
||
var ( | ||
// ErrEmptyKeyID raises when input kid is empty. | ||
ErrEmptyKeyID = errors.New("cache: empty kid") | ||
// ErrCacheNotFound raises when cache value not found. | ||
ErrCacheNotFound = errors.New("cache: value not found") | ||
// ErrInvalidValue raises when type conversion to JWK has been failed. | ||
ErrInvalidValue = errors.New("cache: invalid value") | ||
) | ||
|
||
// Cache works with cache layer. | ||
type Cache interface { | ||
Add(ctx context.Context, key *JWK) error | ||
Get(ctx context.Context, kid string) (*JWK, error) | ||
Len(ctx context.Context) (int, error) | ||
} |
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,139 @@ | ||
package jwks | ||
|
||
import ( | ||
"context" | ||
"sync" | ||
"time" | ||
) | ||
|
||
type item struct { | ||
sync.RWMutex | ||
data *JWK | ||
expiration *time.Time | ||
} | ||
|
||
func (i *item) touch(d time.Duration) { | ||
i.Lock() | ||
exp := time.Now().Add(d) | ||
i.expiration = &exp | ||
i.Unlock() | ||
} | ||
|
||
func (i *item) expired() bool { | ||
i.RLock() | ||
res := true | ||
if i.expiration != nil { | ||
res = i.expiration.Before(time.Now()) | ||
} | ||
i.RUnlock() | ||
return res | ||
} | ||
|
||
// TTLCache is a TTL bases in-memory cache. | ||
type TTLCache struct { | ||
mu sync.RWMutex | ||
ttl time.Duration | ||
stop chan struct{} | ||
items map[string]*item | ||
} | ||
|
||
// NewTTLCache returns a new instance of ttl cache. | ||
func NewTTLCache(ttl time.Duration) *TTLCache { | ||
cache := &TTLCache{ | ||
ttl: ttl, | ||
stop: make(chan struct{}), | ||
items: make(map[string]*item), | ||
} | ||
cache.run() | ||
return cache | ||
} | ||
|
||
func (tc *TTLCache) cleanup() { | ||
tc.mu.Lock() | ||
for key, item := range tc.items { | ||
if item.expired() { | ||
delete(tc.items, key) | ||
} | ||
} | ||
tc.mu.Unlock() | ||
} | ||
|
||
func (tc *TTLCache) run() { | ||
d := tc.ttl | ||
if d < time.Second { | ||
d = time.Second | ||
} | ||
|
||
ticker := time.Tick(d) | ||
go func() { | ||
for { | ||
select { | ||
case <-ticker: | ||
tc.cleanup() | ||
case <-tc.stop: | ||
return | ||
} | ||
} | ||
}() | ||
} | ||
|
||
// Add item into cache. | ||
func (tc *TTLCache) Add(_ context.Context, key *JWK) error { | ||
tc.mu.Lock() | ||
item := &item{data: key} | ||
item.touch(tc.ttl) | ||
tc.items[key.Kid] = item | ||
tc.mu.Unlock() | ||
return nil | ||
} | ||
|
||
// Get item by key. | ||
func (tc *TTLCache) Get(_ context.Context, kid string) (*JWK, error) { | ||
tc.mu.RLock() | ||
item, ok := tc.items[kid] | ||
if !ok || item.expired() { | ||
tc.mu.RUnlock() | ||
return nil, ErrCacheNotFound | ||
} | ||
item.touch(tc.ttl) | ||
tc.mu.RUnlock() | ||
return item.data, nil | ||
} | ||
|
||
// Remove item by key. | ||
func (tc *TTLCache) Remove(_ context.Context, kid string) error { | ||
tc.mu.Lock() | ||
delete(tc.items, kid) | ||
tc.mu.Unlock() | ||
return nil | ||
} | ||
|
||
// Contains checks item on existence. | ||
func (tc *TTLCache) Contains(_ context.Context, kid string) (bool, error) { | ||
tc.mu.RLock() | ||
_, ok := tc.items[kid] | ||
tc.mu.RUnlock() | ||
return ok, nil | ||
} | ||
|
||
// Len returns current size of cache. | ||
func (tc *TTLCache) Len(_ context.Context) (int, error) { | ||
tc.mu.RLock() | ||
n := len(tc.items) | ||
tc.mu.RUnlock() | ||
return n, nil | ||
} | ||
|
||
// Purge deletes all items. | ||
func (tc *TTLCache) Purge(_ context.Context) error { | ||
tc.mu.Lock() | ||
tc.items = map[string]*item{} | ||
tc.mu.Unlock() | ||
return nil | ||
} | ||
|
||
// Stop cleanup process. | ||
func (tc *TTLCache) Stop(_ context.Context) error { | ||
tc.stop <- struct{}{} | ||
return nil | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a way to stop cache at the moment?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://github.com/centrifugal/centrifugo/pull/410/files#diff-f8bd2f65ee773d1c71e5c047d159b86f1729df216e4f3f30fefb59de2368eb74R136