-
Notifications
You must be signed in to change notification settings - Fork 62
/
repo.go
158 lines (130 loc) · 4.36 KB
/
repo.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
// SPDX-License-Identifier: AGPL-3.0-only
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, version 3.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// See the GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>
package session
import (
"context"
"errors"
"time"
"github.com/goccy/go-json"
"github.com/gookit/goutil/timex"
"go.uber.org/zap"
"gorm.io/gorm"
"github.com/bangumi/server/internal/dal/dao"
"github.com/bangumi/server/internal/dal/query"
"github.com/bangumi/server/internal/domain"
"github.com/bangumi/server/internal/errgo"
"github.com/bangumi/server/internal/model"
)
const Key = "sessionID"
type persistSession struct {
CreatedAt time.Time
ExpiredAt time.Time
Key string
Value Session
UserID model.UserID
}
type Repo interface {
Create(ctx context.Context, key string, userID model.UserID, regTime time.Time) (Session, error)
Get(ctx context.Context, key string) (persistSession, error)
RevokeUser(ctx context.Context, userID model.UserID) (keys []string, err error)
Revoke(ctx context.Context, key string) error
}
func NewMysqlRepo(q *query.Query, logger *zap.Logger) Repo {
return mysqlRepo{q: q, log: logger.Named("session.mysqlRepo")}
}
var ErrKeyConflict = errors.New("conflict key in database")
type mysqlRepo struct {
q *query.Query
log *zap.Logger
}
func (r mysqlRepo) Create(ctx context.Context, key string, userID model.UserID, regTime time.Time) (Session, error) {
tx := r.q.Begin()
_, err := tx.WithContext(ctx).WebSession.Where(tx.WebSession.Key.Eq(key)).First()
if err == nil {
return Session{}, ErrKeyConflict
}
if !errors.Is(err, gorm.ErrRecordNotFound) {
return Session{}, errgo.Wrap(err, "orm.Tx.Where.First")
}
createdAt := time.Now().Unix()
expiredAt := createdAt + timex.OneWeekSec
s := Session{RegTime: regTime, UserID: userID, ExpiredAt: expiredAt}
encodedJSON, err := json.Marshal(s)
if err != nil {
return Session{}, errgo.Wrap(err, "json.Marshal")
}
webSession := dao.WebSession{
Key: key,
UserID: userID,
Value: encodedJSON,
CreatedAt: createdAt,
ExpiredAt: expiredAt,
}
err = tx.WebSession.WithContext(ctx).Create(&webSession)
if err != nil {
return Session{}, errgo.Wrap(err, "orm.Tx.Create")
}
err = tx.Commit()
if err != nil {
return Session{}, errgo.Wrap(err, "tx.Commit")
}
return s, nil
}
func (r mysqlRepo) Get(ctx context.Context, key string) (persistSession, error) {
s, err := r.q.WithContext(ctx).WebSession.
Where(r.q.WebSession.Key.Eq(key), r.q.WebSession.ExpiredAt.Gte(time.Now().Unix())).First()
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return persistSession{}, domain.ErrNotFound
}
return persistSession{}, errgo.Wrap(err, "orm.Tx.Where.First")
}
var v Session
if err = json.Unmarshal(s.Value, &v); err != nil {
return persistSession{}, errgo.Wrap(err, "json.Unmarshal")
}
return persistSession{
Key: s.Key,
Value: v,
CreatedAt: time.Unix(s.CreatedAt, 0),
ExpiredAt: time.Unix(s.ExpiredAt, 0),
UserID: s.UserID,
}, nil
}
func (r mysqlRepo) Revoke(ctx context.Context, key string) error {
_, err := r.q.WithContext(ctx).WebSession.Where(r.q.WebSession.Key.Eq(key)).
UpdateSimple(r.q.WebSession.ExpiredAt.Value(time.Now().Unix()))
if err != nil {
r.log.Error("unexpected error", zap.Error(err))
return errgo.Wrap(err, "gorm.UpdateSimple")
}
return nil
}
func (r mysqlRepo) RevokeUser(ctx context.Context, userID model.UserID) ([]string, error) {
s, err := r.q.WithContext(ctx).WebSession.Where(r.q.WebSession.UserID.Eq(userID)).Find()
if err != nil {
r.log.Error("unexpected error", zap.Error(err))
return nil, errgo.Wrap(err, "gorm.Find")
}
_, err = r.q.WithContext(ctx).WebSession.Where(r.q.WebSession.UserID.Eq(userID)).
UpdateSimple(r.q.WebSession.ExpiredAt.Value(time.Now().Unix()))
if err != nil {
return nil, errgo.Wrap(err, "dal.UpdateSimple")
}
var keys = make([]string, len(s))
for i, session := range s {
keys[i] = session.Key
}
return keys, nil
}