-
Notifications
You must be signed in to change notification settings - Fork 32
/
claims.go
62 lines (53 loc) · 2.46 KB
/
claims.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package claims
import (
"github.com/supertokens/supertokens-golang/supertokens"
)
func SessionClaim(key string, fetchValue FetchValueFunc) *TypeSessionClaim {
sessionClaim := &TypeSessionClaim{
Key: key,
FetchValue: fetchValue,
}
sessionClaim.Build = func(userId string, tenantId string, payloadToUpdate map[string]interface{}, userContext supertokens.UserContext) (map[string]interface{}, error) {
if payloadToUpdate == nil {
payloadToUpdate = map[string]interface{}{}
}
value, err := sessionClaim.FetchValue(userId, tenantId, userContext)
if err != nil {
return nil, err
}
if value == nil {
return payloadToUpdate, nil
}
update := sessionClaim.AddToPayload_internal(map[string]interface{}{}, value, userContext)
for k, v := range update {
payloadToUpdate[k] = v
}
return payloadToUpdate, nil
}
return sessionClaim
}
type FetchValueFunc func(userId string, tenantId string, userContext supertokens.UserContext) (interface{}, error)
type TypeSessionClaim struct {
Key string
FetchValue FetchValueFunc
AddToPayload_internal func(payload map[string]interface{}, value interface{}, userContext supertokens.UserContext) map[string]interface{}
RemoveFromPayloadByMerge_internal func(payload map[string]interface{}, userContext supertokens.UserContext) map[string]interface{}
RemoveFromPayload func(payload map[string]interface{}, userContext supertokens.UserContext) map[string]interface{}
GetValueFromPayload func(payload map[string]interface{}, userContext supertokens.UserContext) interface{}
GetLastRefetchTime func(payload map[string]interface{}, userContext supertokens.UserContext) *int64
Build func(userId string, tenantId string, payloadToUpdate map[string]interface{}, userContext supertokens.UserContext) (map[string]interface{}, error)
}
type SessionClaimValidator struct {
ID string
Claim *TypeSessionClaim
ShouldRefetch func(payload map[string]interface{}, userContext supertokens.UserContext) bool
Validate func(payload map[string]interface{}, userContext supertokens.UserContext) ClaimValidationResult
}
type ClaimValidationResult struct {
IsValid bool
Reason interface{} // This can be nil, add checks when used
}
type ClaimValidationError struct {
ID string `json:"id"`
Reason interface{} `json:"reason,omitempty"` // This can be nil, add checks when used
}