Skip to content

Commit 319c4c2

Browse files
authored
Merge pull request #419 from supertokens/fix/emailverification-claim
feat!: remove default maxAgeInSeconds in emailverification claim
2 parents 38c30e0 + 421be56 commit 319c4c2

File tree

4 files changed

+143
-8
lines changed

4 files changed

+143
-8
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [unreleased]
99

10+
## [0.23.0] - 2024-07-10
11+
12+
### Breaking Changes
13+
14+
- 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.
15+
1016
## [0.22.1] - 2024-07-09
1117

1218
### Changes

recipe/emailverification/emailverificationClaim.go

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,7 @@ func NewEmailVerificationClaim() (*claims.TypeSessionClaim, evclaims.TypeEmailVe
3434
}
3535
}
3636

37-
var defaultMaxAge int64 = 300
38-
evClaim, booleanClaimValidators := claims.BooleanClaim("st-ev", fetchValue, &defaultMaxAge)
37+
evClaim, booleanClaimValidators := claims.BooleanClaim("st-ev", fetchValue, nil)
3938

4039
getLastRefetchTime := func(payload map[string]interface{}, userContext supertokens.UserContext) *int64 {
4140
if value, ok := payload[evClaim.Key].(map[string]interface{}); ok {
@@ -57,15 +56,31 @@ func NewEmailVerificationClaim() (*claims.TypeSessionClaim, evclaims.TypeEmailVe
5756
var defaultTimeout int64 = 10
5857
refetchTimeOnFalseInSeconds = &defaultTimeout
5958
}
60-
if maxAgeInSeconds == nil {
61-
var defaultTimeout int64 = 300
62-
maxAgeInSeconds = &defaultTimeout
63-
}
6459

6560
claimValidator := booleanClaimValidators.HasValue(true, maxAgeInSeconds, nil)
6661
claimValidator.ShouldRefetch = func(payload map[string]interface{}, userContext supertokens.UserContext) bool {
6762
value := evClaim.GetValueFromPayload(payload, userContext)
68-
return value == nil || (*getLastRefetchTime(payload, userContext) < time.Now().UnixNano()/1000000-*maxAgeInSeconds*1000) || (value == false && *getLastRefetchTime(payload, userContext) < time.Now().UnixNano()/1000000-*refetchTimeOnFalseInSeconds*1000)
63+
64+
if value == nil {
65+
return true
66+
}
67+
68+
currentTime := time.Now().UnixNano() / 1000000
69+
lastRefetchTime := getLastRefetchTime(payload, userContext)
70+
71+
if maxAgeInSeconds != nil {
72+
if lastRefetchTime != nil && *lastRefetchTime < currentTime-*maxAgeInSeconds*1000 {
73+
return true
74+
}
75+
}
76+
77+
if value == false {
78+
if lastRefetchTime != nil && *lastRefetchTime < currentTime-*refetchTimeOnFalseInSeconds*1000 {
79+
return true
80+
}
81+
}
82+
83+
return false
6984
}
7085
return claimValidator
7186
},
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
/*
2+
* Copyright (c) 2024, VRAI Labs and/or its affiliates. All rights reserved.
3+
*
4+
* This software is licensed under the Apache License, Version 2.0 (the
5+
* "License") as published by the Apache Software Foundation.
6+
*
7+
* You may not use this file except in compliance with the License. You may
8+
* obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations
14+
* under the License.
15+
*/
16+
17+
package emailverification
18+
19+
import (
20+
"testing"
21+
"time"
22+
23+
"github.com/stretchr/testify/assert"
24+
"github.com/supertokens/supertokens-golang/recipe/emailverification/evclaims"
25+
)
26+
27+
func TestEmailVerificationClaim(t *testing.T) {
28+
t.Run("value should be fetched if it is nil", func(t *testing.T) {
29+
validator := evclaims.EmailVerificationClaimValidators.IsVerified(nil, nil)
30+
31+
shouldRefreshNil := validator.ShouldRefetch(nil, nil)
32+
33+
assert.True(t, shouldRefreshNil)
34+
})
35+
36+
t.Run("value should be fetched as per maxAgeInSeconds if it is provided", func(t *testing.T) {
37+
refetchTimeOnFalseInSeconds := int64(10)
38+
maxAgeInSeconds := int64(200)
39+
validator := evclaims.EmailVerificationClaimValidators.IsVerified(&refetchTimeOnFalseInSeconds, &maxAgeInSeconds)
40+
41+
payload := map[string]interface{}{
42+
"st-ev": map[string]interface{}{
43+
"v": true,
44+
"t": time.Now().UnixMilli() - 199*1000,
45+
},
46+
}
47+
48+
shouldRefreshValid := validator.ShouldRefetch(payload, nil)
49+
50+
assert.False(t, shouldRefreshValid)
51+
52+
payload = map[string]interface{}{
53+
"st-ev": map[string]interface{}{
54+
"v": true,
55+
"t": time.Now().UnixMilli() - 201*1000,
56+
},
57+
}
58+
59+
shouldRefreshExpired := validator.ShouldRefetch(payload, nil)
60+
assert.True(t, shouldRefreshExpired)
61+
})
62+
63+
t.Run("value should be fetched as per refetchTimeOnFalseInSeconds if it is provided", func(t *testing.T) {
64+
refetchTimeOnFalseInSeconds := int64(8)
65+
validator := evclaims.EmailVerificationClaimValidators.IsVerified(&refetchTimeOnFalseInSeconds, nil)
66+
67+
payload := map[string]interface{}{
68+
"st-ev": map[string]interface{}{
69+
"v": false,
70+
"t": time.Now().UnixMilli() - 7*1000,
71+
},
72+
}
73+
74+
shouldRefreshValid := validator.ShouldRefetch(payload, nil)
75+
76+
assert.False(t, shouldRefreshValid)
77+
78+
payload = map[string]interface{}{
79+
"st-ev": map[string]interface{}{
80+
"v": false,
81+
"t": time.Now().UnixMilli() - 9*1000,
82+
},
83+
}
84+
85+
shouldRefreshExpired := validator.ShouldRefetch(payload, nil)
86+
assert.True(t, shouldRefreshExpired)
87+
})
88+
89+
t.Run("value should be fetched as per default the refetchTimeOnFalseInSeconds if it is not provided", func(t *testing.T) {
90+
validator := evclaims.EmailVerificationClaimValidators.IsVerified(nil, nil)
91+
92+
// NOTE: the default value of refetchTimeOnFalseInSeconds is 10 seconds
93+
payload := map[string]interface{}{
94+
"st-ev": map[string]interface{}{
95+
"v": false,
96+
"t": time.Now().UnixMilli() - 9*1000,
97+
},
98+
}
99+
100+
shouldRefreshValid := validator.ShouldRefetch(payload, nil)
101+
102+
assert.False(t, shouldRefreshValid)
103+
104+
payload = map[string]interface{}{
105+
"st-ev": map[string]interface{}{
106+
"v": false,
107+
"t": time.Now().UnixMilli() - 11*1000,
108+
},
109+
}
110+
111+
shouldRefreshExpired := validator.ShouldRefetch(payload, nil)
112+
assert.True(t, shouldRefreshExpired)
113+
})
114+
}

supertokens/constants.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ const (
2121
)
2222

2323
// VERSION current version of the lib
24-
const VERSION = "0.22.1"
24+
const VERSION = "0.23.0"
2525

2626
var (
2727
cdiSupported = []string{"3.0"}

0 commit comments

Comments
 (0)