Skip to content

Commit 50608c2

Browse files
hperlory-bot
authored andcommitted
fix: increase refresh token grace period
GitOrigin-RevId: 36a5d2a3038209b91452ef7b600c2b28ad8f8e45
1 parent aefee95 commit 50608c2

File tree

5 files changed

+25
-17
lines changed

5 files changed

+25
-17
lines changed

.reports/dep-licenses.csv

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,4 @@
33
"github.com/ory/x","Apache-2.0"
44
"github.com/stretchr/testify","MIT"
55
"go.opentelemetry.io/otel/sdk","Apache-2.0"
6-
"go.opentelemetry.io/otel/sdk","BSD-3-Clause"
76

driver/config/provider.go

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -770,20 +770,17 @@ type GracefulRefreshTokenRotation struct {
770770
}
771771

772772
func (p *DefaultProvider) GracefulRefreshTokenRotation(ctx context.Context) (cfg GracefulRefreshTokenRotation) {
773-
switch reuseCount := p.getProvider(ctx).IntF(KeyRefreshTokenRotationGraceReuseCount, 0); {
774-
case reuseCount > math.MaxInt32:
775-
cfg.Count = math.MaxInt32
776-
case reuseCount < 0:
777-
cfg.Count = 0
778-
default:
779-
cfg.Count = int32(reuseCount)
780-
}
781-
cfg.Period = p.getProvider(ctx).DurationF(KeyRefreshTokenRotationGracePeriod, 0)
782-
if cfg.Count == 0 && cfg.Period > 5*time.Minute {
783-
cfg.Period = 5 * time.Minute
784-
} else if cfg.Count > 0 && cfg.Period > 30*24*time.Hour {
785-
cfg.Period = 30 * 24 * time.Hour
773+
//nolint:gosec
774+
cfg.Count = int32(x.Clamp(p.getProvider(ctx).IntF(KeyRefreshTokenRotationGraceReuseCount, 0), 0, math.MaxInt32))
775+
776+
// The maximum value is 5 minutes, unless also a reuse count is configured, in
777+
// which case the maximum is 180 days
778+
maxPeriod := 5 * time.Minute
779+
if cfg.Count > 0 {
780+
maxPeriod = 180 * 24 * time.Hour
786781
}
782+
cfg.Period = x.Clamp(p.getProvider(ctx).DurationF(KeyRefreshTokenRotationGracePeriod, 0), 0, maxPeriod)
783+
787784
return
788785
}
789786

driver/config/provider_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -288,8 +288,8 @@ func TestProviderValidates(t *testing.T) {
288288
assert.Equal(t, 5*time.Minute, c.GracefulRefreshTokenRotation(ctx).Period)
289289
require.NoError(t, c.Set(ctx, KeyRefreshTokenRotationGraceReuseCount, "2"))
290290
assert.Equal(t, GracefulRefreshTokenRotation{Count: 2, Period: 2 * time.Hour}, c.GracefulRefreshTokenRotation(ctx))
291-
require.NoError(t, c.Set(ctx, KeyRefreshTokenRotationGracePeriod, (time.Hour*24*90).String()))
292-
assert.Equal(t, GracefulRefreshTokenRotation{Count: 2, Period: time.Hour * 24 * 30}, c.GracefulRefreshTokenRotation(ctx))
291+
require.NoError(t, c.Set(ctx, KeyRefreshTokenRotationGracePeriod, (time.Hour*24*200).String()))
292+
assert.Equal(t, GracefulRefreshTokenRotation{Count: 2, Period: time.Hour * 24 * 180}, c.GracefulRefreshTokenRotation(ctx))
293293

294294
// urls
295295
assert.Equal(t, urlx.ParseOrPanic("https://issuer"), c.IssuerURL(ctx))

spec/config.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -852,7 +852,7 @@
852852
"properties": {
853853
"rotation_grace_period": {
854854
"title": "Refresh Token Rotation Grace Period",
855-
"description": "Configures how long a Refresh Token remains valid after it has been used. The maximum value is 5 minutes, unless also a reuse count is configured, in which case the maximum is 30 days.",
855+
"description": "Configures how long a Refresh Token remains valid after it has been used. The maximum value is 5 minutes, unless also a reuse count is configured, in which case the maximum is 180 days.",
856856
"default": "0s",
857857
"type": "string",
858858
"allOf": [

x/clamp.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package x
2+
3+
// Clamp clamps val to be within the range [min, max] for any integer type.
4+
func Clamp[T ~int | ~int8 | ~int16 | ~int32 | ~int64 | ~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64](val, min, max T) T {
5+
if val < min {
6+
return min
7+
}
8+
if val > max {
9+
return max
10+
}
11+
return val
12+
}

0 commit comments

Comments
 (0)