/
session.go
206 lines (171 loc) 路 4.54 KB
/
session.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
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package auth
import (
"context"
"crypto/rand"
"encoding/base64"
"time"
"github.com/ItsNotGoodName/ipcmanview/internal/bus"
"github.com/ItsNotGoodName/ipcmanview/internal/core"
"github.com/ItsNotGoodName/ipcmanview/internal/repo"
"github.com/ItsNotGoodName/ipcmanview/internal/types"
)
func generateSession() (string, error) {
b := make([]byte, 64)
_, err := rand.Read(b)
if err != nil {
return "", err
}
return base64.URLEncoding.EncodeToString(b), nil
}
type Session struct {
SessionID int64
UserID int64
Username string
Admin bool
Disabled bool
}
type sessionCtxKey struct{}
const defaultSessionDuration = 24 * time.Hour // 1 Day
const rememberMeSessionDuration = 365 * 24 * time.Hour // 1 Year
type CreateUserSessionParams struct {
UserAgent string
IP string
UserID int64
RememberMe bool
PreviousSession string
}
func CreateUserSession(ctx context.Context, arg CreateUserSessionParams) (string, error) {
session, err := generateSession()
if err != nil {
return "", err
}
sessionDuration := defaultSessionDuration
if arg.RememberMe {
sessionDuration = rememberMeSessionDuration
}
now := time.Now()
dbArg := repo.AuthCreateUserSessionParams{
UserID: arg.UserID,
Session: session,
UserAgent: arg.UserAgent,
Ip: arg.IP,
LastIp: arg.IP,
LastUsedAt: types.NewTime(now),
CreatedAt: types.NewTime(now),
ExpiredAt: types.NewTime(now.Add(sessionDuration)),
}
if arg.PreviousSession != "" {
err := createUserSessionAndDeletePrevious(ctx, dbArg, arg.PreviousSession)
if err != nil {
return "", err
}
} else {
err := app.DB.C().AuthCreateUserSession(ctx, dbArg)
if err != nil {
return "", err
}
}
return session, nil
}
func createUserSessionAndDeletePrevious(ctx context.Context, arg repo.AuthCreateUserSessionParams, previousSession string) error {
tx, err := app.DB.BeginTx(ctx, true)
if err != nil {
return err
}
defer tx.Rollback()
if err := tx.C().AuthCreateUserSession(ctx, arg); err != nil {
return err
}
if _, err := tx.C().AuthDeleteUserSessionBySession(ctx, previousSession); err != nil && !core.IsNotFound(err) {
return err
}
return tx.Commit()
}
func DeleteUserSessionBySession(ctx context.Context, session string) error {
userID, err := app.DB.C().AuthDeleteUserSessionBySession(ctx, session)
if err != nil {
if core.IsNotFound(err) {
return nil
}
return err
}
app.Hub.UserSecurityUpdated(bus.UserSecurityUpdated{
UserID: userID,
})
return nil
}
func DeleteUserSession(ctx context.Context, userID int64, sessionID int64) error {
if _, err := core.AssertAdminOrUser(ctx, userID); err != nil {
return err
}
err := app.DB.C().AuthDeleteUserSessionForUser(ctx, repo.AuthDeleteUserSessionForUserParams{
UserID: userID,
ID: sessionID,
})
if err != nil {
return err
}
app.Hub.UserSecurityUpdated(bus.UserSecurityUpdated{
UserID: userID,
})
return nil
}
func DeleteOtherUserSessions(ctx context.Context, userID int64, currentSessionID int64) error {
if _, err := core.AssertAdminOrUser(ctx, userID); err != nil {
return err
}
err := app.DB.C().AuthDeleteUserSessionForUserAndNotSession(ctx, repo.AuthDeleteUserSessionForUserAndNotSessionParams{
UserID: userID,
ID: currentSessionID,
})
if err != nil {
return err
}
app.Hub.UserSecurityUpdated(bus.UserSecurityUpdated{
UserID: userID,
})
return nil
}
func NewTouchSessionThrottle() TouchSessionThrottle {
return TouchSessionThrottle{
LockStore: core.NewLockStore[int64](),
Duration: time.Minute,
}
}
type TouchSessionThrottle struct {
*core.LockStore[int64]
Duration time.Duration
}
type TouchUserSessionParams struct {
CurrentSessionID int64
LastUsedAt time.Time
LastIP string
IP string
}
func TouchUserSession(ctx context.Context, arg TouchUserSessionParams) error {
now := time.Now()
if arg.LastIP == arg.IP && arg.LastUsedAt.After(now.Add(-app.TouchSessionThrottle.Duration)) {
return nil
}
unlock, err := app.TouchSessionThrottle.TryLock(arg.CurrentSessionID)
if err != nil {
return nil
}
defer unlock()
err = app.DB.C().AuthUpdateUserSession(ctx, repo.AuthUpdateUserSessionParams{
LastIp: arg.IP,
LastUsedAt: types.NewTime(now),
ID: arg.CurrentSessionID,
})
if err != nil {
return err
}
return nil
}
func WithSession(ctx context.Context, session Session) context.Context {
return context.WithValue(ctx, sessionCtxKey{}, session)
}
func UseSession(ctx context.Context) (Session, bool) {
user, ok := ctx.Value(sessionCtxKey{}).(Session)
return user, ok
}