Skip to content

Commit e570564

Browse files
authored
refactor: allow secret configuration to return errors (#726)
BREAKING CHANGE: `GetGlobalSecret` and `GetRotatedGlobalSecrets` signatures changed and it is now possible to add an error.
1 parent f52879d commit e570564

File tree

3 files changed

+27
-12
lines changed

3 files changed

+27
-12
lines changed

config.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -177,13 +177,13 @@ type TokenEntropyProvider interface {
177177
// GlobalSecretProvider returns the provider for configuring the global secret.
178178
type GlobalSecretProvider interface {
179179
// GetGlobalSecret returns the global secret.
180-
GetGlobalSecret(ctx context.Context) []byte
180+
GetGlobalSecret(ctx context.Context) ([]byte, error)
181181
}
182182

183183
// RotatedGlobalSecretsProvider returns the provider for configuring the rotated global secrets.
184184
type RotatedGlobalSecretsProvider interface {
185185
// GetRotatedGlobalSecrets returns the rotated global secrets.
186-
GetRotatedGlobalSecrets(ctx context.Context) [][]byte
186+
GetRotatedGlobalSecrets(ctx context.Context) ([][]byte, error)
187187
}
188188

189189
// HMACHashingProvider returns the provider for configuring the hash function.

config_default.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -214,16 +214,16 @@ type Config struct {
214214
IsPushedAuthorizeEnforced bool
215215
}
216216

217-
func (c *Config) GetGlobalSecret(ctx context.Context) []byte {
218-
return c.GlobalSecret
217+
func (c *Config) GetGlobalSecret(ctx context.Context) ([]byte, error) {
218+
return c.GlobalSecret, nil
219219
}
220220

221221
func (c *Config) GetUseLegacyErrorFormat(ctx context.Context) bool {
222222
return c.UseLegacyErrorFormat
223223
}
224224

225-
func (c *Config) GetRotatedGlobalSecrets(ctx context.Context) [][]byte {
226-
return c.RotatedGlobalSecrets
225+
func (c *Config) GetRotatedGlobalSecrets(ctx context.Context) ([][]byte, error) {
226+
return c.RotatedGlobalSecrets, nil
227227
}
228228

229229
func (c *Config) GetHMACHasher(ctx context.Context) func() hash.Hash {

token/hmac/hmacsha.go

+21-6
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,17 @@ func (c *HMACStrategy) Generate(ctx context.Context) (string, string, error) {
5151
c.Lock()
5252
defer c.Unlock()
5353

54-
if len(c.Config.GetGlobalSecret(ctx)) < minimumSecretLength {
55-
return "", "", errors.Errorf("secret for signing HMAC-SHA512/256 is expected to be 32 byte long, got %d byte", len(c.Config.GetGlobalSecret(ctx)))
54+
secrets, err := c.Config.GetGlobalSecret(ctx)
55+
if err != nil {
56+
return "", "", err
57+
}
58+
59+
if len(secrets) < minimumSecretLength {
60+
return "", "", errors.Errorf("secret for signing HMAC-SHA512/256 is expected to be 32 byte long, got %d byte", len(secrets))
5661
}
5762

5863
var signingKey [32]byte
59-
copy(signingKey[:], c.Config.GetGlobalSecret(ctx))
64+
copy(signingKey[:], secrets)
6065

6166
entropy := c.Config.GetTokenEntropy(ctx)
6267
if entropy < minimumEntropy {
@@ -86,11 +91,21 @@ func (c *HMACStrategy) Generate(ctx context.Context) (string, string, error) {
8691
func (c *HMACStrategy) Validate(ctx context.Context, token string) (err error) {
8792
var keys [][]byte
8893

89-
if len(c.Config.GetGlobalSecret(ctx)) > 0 {
90-
keys = append(keys, c.Config.GetGlobalSecret(ctx))
94+
secrets, err := c.Config.GetGlobalSecret(ctx)
95+
if err != nil {
96+
return err
97+
}
98+
99+
rotatedSecrets, err := c.Config.GetRotatedGlobalSecrets(ctx)
100+
if err != nil {
101+
return err
102+
}
103+
104+
if len(secrets) > 0 {
105+
keys = append(keys, secrets)
91106
}
92107

93-
keys = append(keys, c.Config.GetRotatedGlobalSecrets(ctx)...)
108+
keys = append(keys, rotatedSecrets...)
94109
for _, key := range keys {
95110
if err = c.validate(ctx, key, token); err == nil {
96111
return nil

0 commit comments

Comments
 (0)