2023-06-22 22:12:33 +00:00
|
|
|
// Copyright 2020-2023 the Pinniped contributors. All Rights Reserved.
|
2020-10-17 00:51:40 +00:00
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
|
|
|
package jwks
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"gopkg.in/square/go-jose.v2"
|
|
|
|
)
|
|
|
|
|
|
|
|
type DynamicJWKSProvider interface {
|
2020-12-03 20:34:58 +00:00
|
|
|
SetIssuerToJWKSMap(
|
|
|
|
issuerToJWKSMap map[string]*jose.JSONWebKeySet,
|
|
|
|
issuerToActiveJWKMap map[string]*jose.JSONWebKey,
|
|
|
|
)
|
|
|
|
GetJWKS(issuerName string) (jwks *jose.JSONWebKeySet, activeJWK *jose.JSONWebKey)
|
2020-10-17 00:51:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type dynamicJWKSProvider struct {
|
2020-12-03 20:34:58 +00:00
|
|
|
issuerToJWKSMap map[string]*jose.JSONWebKeySet
|
|
|
|
issuerToActiveJWKMap map[string]*jose.JSONWebKey
|
|
|
|
mutex sync.RWMutex
|
2020-10-17 00:51:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewDynamicJWKSProvider() DynamicJWKSProvider {
|
|
|
|
return &dynamicJWKSProvider{
|
2020-12-03 20:34:58 +00:00
|
|
|
issuerToJWKSMap: map[string]*jose.JSONWebKeySet{},
|
|
|
|
issuerToActiveJWKMap: map[string]*jose.JSONWebKey{},
|
2020-10-17 00:51:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-03 20:34:58 +00:00
|
|
|
func (p *dynamicJWKSProvider) SetIssuerToJWKSMap(
|
|
|
|
issuerToJWKSMap map[string]*jose.JSONWebKeySet,
|
|
|
|
issuerToActiveJWKMap map[string]*jose.JSONWebKey,
|
|
|
|
) {
|
2020-10-17 00:51:40 +00:00
|
|
|
p.mutex.Lock() // acquire a write lock
|
|
|
|
defer p.mutex.Unlock()
|
|
|
|
p.issuerToJWKSMap = issuerToJWKSMap
|
2020-12-03 20:34:58 +00:00
|
|
|
p.issuerToActiveJWKMap = issuerToActiveJWKMap
|
2020-10-17 00:51:40 +00:00
|
|
|
}
|
|
|
|
|
2020-12-03 20:34:58 +00:00
|
|
|
func (p *dynamicJWKSProvider) GetJWKS(issuerName string) (*jose.JSONWebKeySet, *jose.JSONWebKey) {
|
2020-10-17 00:51:40 +00:00
|
|
|
p.mutex.RLock() // acquire a read lock
|
|
|
|
defer p.mutex.RUnlock()
|
2020-12-03 20:34:58 +00:00
|
|
|
return p.issuerToJWKSMap[issuerName], p.issuerToActiveJWKMap[issuerName]
|
2020-10-17 00:51:40 +00:00
|
|
|
}
|