Skip to content

SigNoz is an open-source, OpenTelemetry-native observability platform for your team and their AI agents. Get logs, metrics, and traces in one tool with features like APM, distributed tracing, log management, infra monitoring, etc. Combined with SigNoz MCP and a native AI teammate (in SigNoz Cloud) it helps you build more resilient apps.

fix(tokenizer): accept the previous token pair only within the rotation duration (#12941)

Vikrant Guptae8324581b309
diff --git a/pkg/tokenizer/opaquetokenizer/provider.go b/pkg/tokenizer/opaquetokenizer/provider.go
index 1823836fa2..74c8840220 100644
--- a/pkg/tokenizer/opaquetokenizer/provider.go
+++ b/pkg/tokenizer/opaquetokenizer/provider.go
@@ -159,12 +159,14 @@ func (provider *provider) RotateToken(ctx context.Context, accessToken string, r
var rotatedToken *authtypes.Token
if err := provider.tokenStore.GetOrUpdateByAccessTokenOrPrevAccessToken(ctx, accessToken, func(ctx context.Context, token *authtypes.StorableToken) error {
+ currentAccessToken := token.AccessToken
+
if err := token.Rotate(accessToken, refreshToken, provider.config.Rotation.Duration, provider.config.Lifetime.Idle, provider.config.Lifetime.Max); err != nil {
return err
}
- // If the token passed the Rotate method and is the same as the input token, return the same token.
- if token.AccessToken == accessToken && token.RefreshToken == refreshToken {
+ // If the token passed the Rotate method and is the same as the stored token, return the same token.
+ if token.AccessToken == currentAccessToken {
rotatedToken = token
return nil
}
diff --git a/pkg/types/authtypes/token.go b/pkg/types/authtypes/token.go
index 3d0e2dfa35..626dbbd152 100644
--- a/pkg/types/authtypes/token.go
+++ b/pkg/types/authtypes/token.go
@@ -164,7 +164,7 @@ func (typ *Token) IsRotationRequired(rotationInterval time.Duration) error {
func (typ *Token) Rotate(accessTokenOrPrevAccessToken string, refreshTokenOrPrevRefreshToken string, rotationDuration time.Duration, idleDuration time.Duration, maxDuration time.Duration) error {
if typ.PrevAccessToken == accessTokenOrPrevAccessToken && typ.PrevRefreshToken == refreshTokenOrPrevRefreshToken {
// If the token has been rotated within the rotation duration, do nothing and return the same token.
- if !typ.RotatedAt.IsZero() && typ.RotatedAt.Before(time.Now().Add(-rotationDuration)) {
+ if !typ.RotatedAt.IsZero() && typ.RotatedAt.After(time.Now().Add(-rotationDuration)) {
return nil
}
diff --git a/pkg/types/authtypes/token_test.go b/pkg/types/authtypes/token_test.go
new file mode 100644
index 0000000000..990fe228bd
--- /dev/null
+++ b/pkg/types/authtypes/token_test.go
@@ -0,0 +1,62 @@
+package authtypes
+
+import (
+ "testing"
+ "time"
+
+ "github.com/SigNoz/signoz/pkg/errors"
+ "github.com/SigNoz/signoz/pkg/valuer"
+ "github.com/stretchr/testify/assert"
+ "github.com/stretchr/testify/require"
+)
+
+const (
+ testRotationDuration = 60 * time.Second
+ testIdleDuration = 7 * 24 * time.Hour
+ testMaxDuration = 30 * 24 * time.Hour
+)
+
+func newRotatedToken(t *testing.T, rotatedAt time.Time) (*Token, string, string) {
+ t.Helper()
+
+ token, err := NewToken(map[string]string{}, valuer.GenerateUUID())
+ require.NoError(t, err)
+
+ prevAccessToken, prevRefreshToken := token.AccessToken, token.RefreshToken
+ require.NoError(t, token.Rotate(prevAccessToken, prevRefreshToken, testRotationDuration, testIdleDuration, testMaxDuration))
+ token.RotatedAt = rotatedAt
+
+ return token, prevAccessToken, prevRefreshToken
+}
+
+func TestTokenRotatePrevPair(t *testing.T) {
+ testCases := []struct {
+ name string
+ rotatedAt time.Time
+ wantErr bool
+ }{
+ {name: "InsideRotationDuration", rotatedAt: time.Now().Add(-testRotationDuration / 2), wantErr: false},
+ {name: "OutsideRotationDuration", rotatedAt: time.Now().Add(-2 * testRotationDuration), wantErr: true},
+ }
+
+ for _, testCase := range testCases {
+ t.Run(testCase.name, func(t *testing.T) {
+ token, prevAccessToken, prevRefreshToken := newRotatedToken(t, testCase.rotatedAt)
+ accessToken, refreshToken := token.AccessToken, token.RefreshToken
+
+ err := token.Rotate(prevAccessToken, prevRefreshToken, testRotationDuration, testIdleDuration, testMaxDuration)
+ if testCase.wantErr {
+ require.Error(t, err)
+ assert.True(t, errors.Ast(err, errors.TypeUnauthenticated))
+ } else {
+ require.NoError(t, err)
+ }
+
+ assert.Equal(t, accessToken, token.AccessToken)
+ assert.Equal(t, refreshToken, token.RefreshToken)
+ assert.Equal(t, prevAccessToken, token.PrevAccessToken)
+ assert.Equal(t, prevRefreshToken, token.PrevRefreshToken)
+ assert.Equal(t, testCase.rotatedAt, token.RotatedAt)
+ })
+ }
+}