From 2fbdfb43a65f0c7abaf1b58092db3eb155e70bcb Mon Sep 17 00:00:00 2001 From: Ankit Tiwari Date: Wed, 10 Jul 2024 18:20:06 +0530 Subject: [PATCH] fix: remove default maxAgeInSeconds in emailverification claim --- CHANGELOG.md | 6 ++++ .../emailverificationClaim.go | 29 ++++++++++++++----- supertokens/constants.go | 2 +- 3 files changed, 29 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f318bc0b..03b212e1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [unreleased] +## [0.23.0] - 2024-07-10 + +### Breaking Changes + +- Removes the default `maxAgeInSeconds` value (previously 300 seconds) in EmailVerification Claim. If the claim value is true and `maxAgeInSeconds` is not provided, it will not be refetched. + ## [0.22.1] - 2024-07-09 ### Changes diff --git a/recipe/emailverification/emailverificationClaim.go b/recipe/emailverification/emailverificationClaim.go index 94e615b2..b3e147b8 100644 --- a/recipe/emailverification/emailverificationClaim.go +++ b/recipe/emailverification/emailverificationClaim.go @@ -34,8 +34,7 @@ func NewEmailVerificationClaim() (*claims.TypeSessionClaim, evclaims.TypeEmailVe } } - var defaultMaxAge int64 = 300 - evClaim, booleanClaimValidators := claims.BooleanClaim("st-ev", fetchValue, &defaultMaxAge) + evClaim, booleanClaimValidators := claims.BooleanClaim("st-ev", fetchValue, nil) getLastRefetchTime := func(payload map[string]interface{}, userContext supertokens.UserContext) *int64 { if value, ok := payload[evClaim.Key].(map[string]interface{}); ok { @@ -57,15 +56,31 @@ func NewEmailVerificationClaim() (*claims.TypeSessionClaim, evclaims.TypeEmailVe var defaultTimeout int64 = 10 refetchTimeOnFalseInSeconds = &defaultTimeout } - if maxAgeInSeconds == nil { - var defaultTimeout int64 = 300 - maxAgeInSeconds = &defaultTimeout - } claimValidator := booleanClaimValidators.HasValue(true, maxAgeInSeconds, nil) claimValidator.ShouldRefetch = func(payload map[string]interface{}, userContext supertokens.UserContext) bool { value := evClaim.GetValueFromPayload(payload, userContext) - return value == nil || (*getLastRefetchTime(payload, userContext) < time.Now().UnixNano()/1000000-*maxAgeInSeconds*1000) || (value == false && *getLastRefetchTime(payload, userContext) < time.Now().UnixNano()/1000000-*refetchTimeOnFalseInSeconds*1000) + + if value == nil { + return true + } + + currentTime := time.Now().UnixNano() / 1000000 + lastRefetchTime := getLastRefetchTime(payload, userContext) + + if maxAgeInSeconds != nil { + if lastRefetchTime != nil && *lastRefetchTime < currentTime-*maxAgeInSeconds*1000 { + return true + } + } + + if value == false { + if lastRefetchTime != nil && *lastRefetchTime < currentTime-*refetchTimeOnFalseInSeconds*1000 { + return true + } + } + + return false } return claimValidator }, diff --git a/supertokens/constants.go b/supertokens/constants.go index 0bac26fb..0b154170 100644 --- a/supertokens/constants.go +++ b/supertokens/constants.go @@ -21,7 +21,7 @@ const ( ) // VERSION current version of the lib -const VERSION = "0.22.1" +const VERSION = "0.23.0" var ( cdiSupported = []string{"3.0"}