netbird/relay/auth/hmac/store.go

35 lines
562 B
Go
Raw Normal View History

2024-07-05 16:12:30 +02:00
package hmac
import (
"sync"
2024-07-10 13:21:50 +02:00
log "github.com/sirupsen/logrus"
2024-07-05 16:12:30 +02:00
)
2024-07-08 17:01:11 +02:00
// TokenStore is a simple in-memory store for token
2024-07-05 16:12:30 +02:00
// With this can update the token in thread safe way
2024-07-08 17:01:11 +02:00
type TokenStore struct {
2024-07-05 16:12:30 +02:00
mu sync.Mutex
2024-07-10 13:21:50 +02:00
token []byte
2024-07-05 16:12:30 +02:00
}
2024-07-10 13:21:50 +02:00
func (a *TokenStore) UpdateToken(token *Token) {
2024-07-05 16:12:30 +02:00
a.mu.Lock()
defer a.mu.Unlock()
2024-07-10 13:21:50 +02:00
if token == nil {
return
}
t, err := marshalToken(*token)
if err != nil {
log.Errorf("failed to marshal token: %s", err)
}
a.token = t
2024-07-05 16:12:30 +02:00
}
2024-07-10 13:21:50 +02:00
func (a *TokenStore) TokenBinary() []byte {
2024-07-05 16:12:30 +02:00
a.mu.Lock()
defer a.mu.Unlock()
2024-07-10 13:21:50 +02:00
return a.token
2024-07-05 16:12:30 +02:00
}