-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathsession.go
More file actions
135 lines (110 loc) · 3.59 KB
/
session.go
File metadata and controls
135 lines (110 loc) · 3.59 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package limen
import (
"encoding/json"
"time"
)
type Session struct {
ID any `json:"id,omitempty"`
Token string `json:"token"`
UserID any `json:"user_id"`
CreatedAt time.Time `json:"created_at"`
ExpiresAt time.Time `json:"expires_at"`
LastAccess time.Time `json:"last_access"`
Metadata map[string]any `json:"metadata,omitempty"`
raw map[string]any
}
// Raw returns the session raw data as returned from the database
func (s Session) Raw() map[string]any {
return s.raw
}
// IsExpired checks if the session has expired
func (s *Session) IsExpired(idleTimeout time.Duration) bool {
if idleTimeout == 0 {
return time.Now().After(s.ExpiresAt)
}
return time.Now().After(s.ExpiresAt) || time.Now().After(s.LastAccess.Add(idleTimeout))
}
// ShouldExtendExpiration checks if the session should be extended
func (s *Session) ShouldExtendExpiration(expiresIn, updateAge time.Duration) bool {
if updateAge == 0 {
return false
}
lastExtendedAt := s.ExpiresAt.Add(-expiresIn)
nextExtensionAt := lastExtendedAt.Add(updateAge)
now := time.Now()
return now.After(nextExtensionAt) || now.Equal(nextExtensionAt)
}
type SessionSchema struct {
BaseSchema
}
type SchemaConfigSessionOption func(*SchemaConfig, *SessionSchema)
func newDefaultSessionSchema(c *SchemaConfig, opts ...SchemaConfigSessionOption) *SessionSchema {
schema := &SessionSchema{
BaseSchema: BaseSchema{},
}
for _, opt := range opts {
opt(c, schema)
}
return schema
}
func (s *SessionSchema) GetSoftDeleteField() string {
// sessions should not have a soft delete field
return ""
}
func (s *SessionSchema) GetUserIDField() string {
return s.GetField(SessionSchemaUserIDField)
}
func (s *SessionSchema) GetTokenField() string {
return s.GetField(SessionSchemaTokenField)
}
func (s *SessionSchema) GetCreatedAtField() string {
return s.GetField(SessionSchemaCreatedAtField)
}
func (s *SessionSchema) GetExpiresAtField() string {
return s.GetField(SessionSchemaExpiresAtField)
}
func (s *SessionSchema) GetLastAccessField() string {
return s.GetField(SessionSchemaLastAccessField)
}
func (s *SessionSchema) GetMetadataField() string {
return s.GetField(SessionSchemaMetadataField)
}
func (s *SessionSchema) FromStorage(data map[string]any) Model {
session := &Session{
ID: data[s.GetIDField()],
Token: data[s.GetTokenField()].(string),
UserID: data[s.GetUserIDField()],
CreatedAt: data[s.GetCreatedAtField()].(time.Time),
ExpiresAt: data[s.GetExpiresAtField()].(time.Time),
LastAccess: data[s.GetLastAccessField()].(time.Time),
raw: data,
}
// Parse Metadata if it exists and is a string (JSON)
if metadataValue, exists := data[s.GetMetadataField()]; exists && metadataValue != nil {
if metadataStr, ok := metadataValue.(string); ok && metadataStr != "" {
var metadata map[string]any
if err := json.Unmarshal([]byte(metadataStr), &metadata); err == nil {
session.Metadata = metadata
}
} else if metadataMap, ok := metadataValue.(map[string]any); ok {
session.Metadata = metadataMap
}
}
return session
}
func (s *SessionSchema) ToStorage(data Model) map[string]any {
session := data.(*Session)
result := map[string]any{
s.GetTokenField(): session.Token,
s.GetUserIDField(): session.UserID,
s.GetCreatedAtField(): session.CreatedAt,
s.GetExpiresAtField(): session.ExpiresAt,
s.GetLastAccessField(): session.LastAccess,
}
if session.Metadata != nil {
if metadataJSON, err := json.Marshal(session.Metadata); err == nil {
result[s.GetMetadataField()] = string(metadataJSON)
}
}
return result
}