2024-09-08 12:06:14 +02:00
|
|
|
package hmac
|
|
|
|
|
|
|
|
import (
|
2024-09-11 16:20:30 +02:00
|
|
|
"encoding/base64"
|
|
|
|
"fmt"
|
2024-09-08 12:06:14 +02:00
|
|
|
"sync"
|
|
|
|
|
2024-09-11 16:20:30 +02:00
|
|
|
v2 "github.com/netbirdio/netbird/relay/auth/hmac/v2"
|
2024-09-08 12:06:14 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// TokenStore is a simple in-memory store for token
|
|
|
|
// With this can update the token in thread safe way
|
|
|
|
type TokenStore struct {
|
|
|
|
mu sync.Mutex
|
|
|
|
token []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *TokenStore) UpdateToken(token *Token) error {
|
|
|
|
a.mu.Lock()
|
|
|
|
defer a.mu.Unlock()
|
|
|
|
if token == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-09-11 16:20:30 +02:00
|
|
|
sig, err := base64.StdEncoding.DecodeString(token.Signature)
|
2024-09-08 12:06:14 +02:00
|
|
|
if err != nil {
|
2024-09-11 16:20:30 +02:00
|
|
|
return fmt.Errorf("decode signature: %w", err)
|
2024-09-08 12:06:14 +02:00
|
|
|
}
|
2024-09-11 16:20:30 +02:00
|
|
|
|
|
|
|
tok := v2.Token{
|
|
|
|
AuthAlgo: v2.AuthAlgoHMACSHA256,
|
|
|
|
Signature: sig,
|
|
|
|
Payload: []byte(token.Payload),
|
|
|
|
}
|
|
|
|
|
|
|
|
a.token = tok.Marshal()
|
2024-09-08 12:06:14 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *TokenStore) TokenBinary() []byte {
|
|
|
|
a.mu.Lock()
|
|
|
|
defer a.mu.Unlock()
|
|
|
|
return a.token
|
|
|
|
}
|