fix: normalize API key expiry timestamps to UTC (#2137)

Store authorization timestamps as timestamptz and normalize Unix expiry
inputs to UTC so tenant/platform API keys expire at the intended instant
even when the app process timezone differs from the PostgreSQL session.
This commit is contained in:
wizardchen
2026-07-21 18:24:16 +08:00
committed by lyingbug
parent 59fed12643
commit 0f2c6cee11
9 changed files with 150 additions and 12 deletions
@@ -260,3 +260,22 @@ func TestTenantAPIKeyServiceAuthenticateThrottlesLastUsedUpdates(t *testing.T) {
t.Fatalf("last_used update count = %d, want 1 (throttled async write)", repo.lastUsedUpdateCount)
}
}
func TestTenantAPIKeyServiceAuthenticateRejectsExpiredKey(t *testing.T) {
ctx := context.Background()
repo := newFakeTenantAPIKeyRepo()
svc := NewTenantAPIKeyService(repo)
expired := time.Now().UTC().Add(-time.Minute)
created, err := svc.CreateAPIKey(ctx, interfaces.TenantAPIKeyCreateRequest{
TenantID: 42,
Name: "short-lived",
ExpiresAt: &expired,
})
if err != nil {
t.Fatalf("CreateAPIKey returned error: %v", err)
}
if _, err := svc.AuthenticateAPIKey(ctx, created.Token); err == nil {
t.Fatal("expired key should not authenticate")
}
}