Skip to content

Commit 7df19db

Browse files
committed
Add configurable clock skew
1 parent 6ce86d9 commit 7df19db

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

oidc/verify.go

+12-5
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,9 @@ type Config struct {
100100

101101
// Time function to check Token expiry. Defaults to time.Now
102102
Now func() time.Time
103+
104+
// Duration for clock skew. Defaults to 5 minutes.
105+
ClockSkew time.Duration
103106
}
104107

105108
// Verifier returns an IDTokenVerifier that uses the provider's key set to verify JWTs.
@@ -267,18 +270,22 @@ func (v *IDTokenVerifier) Verify(ctx context.Context, rawIDToken string) (*IDTok
267270
}
268271
nowTime := now()
269272

270-
if t.Expiry.Before(nowTime) {
273+
// Set to 5 minutes by default since this is what other OpenID Connect providers do to deal with clock skew.
274+
// https://github.com/AzureAD/azure-activedirectory-identitymodel-extensions-for-dotnet/blob/6.12.2/src/Microsoft.IdentityModel.Tokens/TokenValidationParameters.cs#L149-L153
275+
clockSkew := 5 * time.Minute
276+
if v.config.ClockSkew > 0 {
277+
clockSkew = v.config.ClockSkew
278+
}
279+
280+
if t.Expiry.Before(nowTime.Add(-clockSkew)) {
271281
return nil, fmt.Errorf("oidc: token is expired (Token Expiry: %v)", t.Expiry)
272282
}
273283

274284
// If nbf claim is provided in token, ensure that it is indeed in the past.
275285
if token.NotBefore != nil {
276286
nbfTime := time.Time(*token.NotBefore)
277-
// Set to 5 minutes since this is what other OpenID Connect providers do to deal with clock skew.
278-
// https://github.com/AzureAD/azure-activedirectory-identitymodel-extensions-for-dotnet/blob/6.12.2/src/Microsoft.IdentityModel.Tokens/TokenValidationParameters.cs#L149-L153
279-
leeway := 5 * time.Minute
280287

281-
if nowTime.Add(leeway).Before(nbfTime) {
288+
if nowTime.Add(clockSkew).Before(nbfTime) {
282289
return nil, fmt.Errorf("oidc: current time %v before the nbf (not before) time: %v", nowTime, nbfTime)
283290
}
284291
}

0 commit comments

Comments
 (0)