2020-12-10 21:54:02 +00:00
|
|
|
// Copyright 2020 the Pinniped contributors. All Rights Reserved.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
|
|
|
package secret
|
|
|
|
|
2020-12-14 16:32:11 +00:00
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
"sync/atomic"
|
|
|
|
)
|
2020-12-14 15:36:45 +00:00
|
|
|
|
2020-12-10 21:54:02 +00:00
|
|
|
type Cache struct {
|
2020-12-14 16:32:11 +00:00
|
|
|
csrfCookieEncoderHashKey atomic.Value
|
2020-12-16 22:27:09 +00:00
|
|
|
federationDomainCacheMap sync.Map
|
2020-12-10 21:54:02 +00:00
|
|
|
}
|
|
|
|
|
2020-12-14 16:32:11 +00:00
|
|
|
// New returns an empty Cache.
|
|
|
|
func New() *Cache { return &Cache{} }
|
2020-12-10 21:54:02 +00:00
|
|
|
|
2020-12-16 22:27:09 +00:00
|
|
|
type federationDomainCache struct {
|
2020-12-14 16:32:11 +00:00
|
|
|
tokenHMACKey atomic.Value
|
|
|
|
stateEncoderHashKey atomic.Value
|
|
|
|
stateEncoderBlockKey atomic.Value
|
2020-12-10 21:54:02 +00:00
|
|
|
}
|
|
|
|
|
2020-12-14 16:32:11 +00:00
|
|
|
func (c *Cache) GetCSRFCookieEncoderHashKey() []byte {
|
|
|
|
return bytesOrNil(c.csrfCookieEncoderHashKey.Load())
|
2020-12-10 21:54:02 +00:00
|
|
|
}
|
|
|
|
|
2020-12-14 16:32:11 +00:00
|
|
|
func (c *Cache) SetCSRFCookieEncoderHashKey(key []byte) {
|
|
|
|
c.csrfCookieEncoderHashKey.Store(key)
|
2020-12-10 21:54:02 +00:00
|
|
|
}
|
|
|
|
|
2020-12-14 16:32:11 +00:00
|
|
|
func (c *Cache) GetTokenHMACKey(oidcIssuer string) []byte {
|
2020-12-16 22:27:09 +00:00
|
|
|
return bytesOrNil(c.getFederationDomainCache(oidcIssuer).tokenHMACKey.Load())
|
2020-12-10 21:54:02 +00:00
|
|
|
}
|
|
|
|
|
2020-12-14 16:32:11 +00:00
|
|
|
func (c *Cache) SetTokenHMACKey(oidcIssuer string, key []byte) {
|
2020-12-16 22:27:09 +00:00
|
|
|
c.getFederationDomainCache(oidcIssuer).tokenHMACKey.Store(key)
|
2020-12-10 21:54:02 +00:00
|
|
|
}
|
|
|
|
|
2020-12-14 16:32:11 +00:00
|
|
|
func (c *Cache) GetStateEncoderHashKey(oidcIssuer string) []byte {
|
2020-12-16 22:27:09 +00:00
|
|
|
return bytesOrNil(c.getFederationDomainCache(oidcIssuer).stateEncoderHashKey.Load())
|
2020-12-10 21:54:02 +00:00
|
|
|
}
|
|
|
|
|
2020-12-14 16:32:11 +00:00
|
|
|
func (c *Cache) SetStateEncoderHashKey(oidcIssuer string, key []byte) {
|
2020-12-16 22:27:09 +00:00
|
|
|
c.getFederationDomainCache(oidcIssuer).stateEncoderHashKey.Store(key)
|
2020-12-10 21:54:02 +00:00
|
|
|
}
|
|
|
|
|
2020-12-14 16:32:11 +00:00
|
|
|
func (c *Cache) GetStateEncoderBlockKey(oidcIssuer string) []byte {
|
2020-12-16 22:27:09 +00:00
|
|
|
return bytesOrNil(c.getFederationDomainCache(oidcIssuer).stateEncoderBlockKey.Load())
|
2020-12-10 21:54:02 +00:00
|
|
|
}
|
|
|
|
|
2020-12-14 16:32:11 +00:00
|
|
|
func (c *Cache) SetStateEncoderBlockKey(oidcIssuer string, key []byte) {
|
2020-12-16 22:27:09 +00:00
|
|
|
c.getFederationDomainCache(oidcIssuer).stateEncoderBlockKey.Store(key)
|
2020-12-10 21:54:02 +00:00
|
|
|
}
|
|
|
|
|
2020-12-16 22:27:09 +00:00
|
|
|
func (c *Cache) getFederationDomainCache(oidcIssuer string) *federationDomainCache {
|
|
|
|
value, ok := c.federationDomainCacheMap.Load(oidcIssuer)
|
2020-12-14 16:32:11 +00:00
|
|
|
if !ok {
|
2020-12-16 22:27:09 +00:00
|
|
|
value = &federationDomainCache{}
|
|
|
|
c.federationDomainCacheMap.Store(oidcIssuer, value)
|
2020-12-14 16:32:11 +00:00
|
|
|
}
|
2020-12-16 22:27:09 +00:00
|
|
|
return value.(*federationDomainCache)
|
2020-12-10 21:54:02 +00:00
|
|
|
}
|
|
|
|
|
2020-12-14 16:32:11 +00:00
|
|
|
func bytesOrNil(b interface{}) []byte {
|
|
|
|
if b == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return b.([]byte)
|
2020-12-10 21:54:02 +00:00
|
|
|
}
|