-
Notifications
You must be signed in to change notification settings - Fork 62
/
mysql_repo.go
144 lines (119 loc) · 3.99 KB
/
mysql_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
// 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"
"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/model"
"github.com/bangumi/server/internal/pkg/errgo"
"github.com/bangumi/server/internal/pkg/timex"
)
func NewMysqlRepo(q *query.Query, logger *zap.Logger) Repo {
return mysqlRepo{q: q, log: logger.Named("session.mysqlRepo")}
}
type mysqlRepo struct {
q *query.Query
log *zap.Logger
}
func (r mysqlRepo) Create(
ctx context.Context, userID model.UserID, regTime time.Time, keyGen func() string,
) (string, Session, error) {
createdAt := time.Now().Unix()
expiredAt := createdAt + timex.OneWeekSec
s := Session{RegTime: regTime, UserID: userID, ExpiredAt: expiredAt}
encodedJSON, err := json.MarshalWithOption(s, json.DisableHTMLEscape(), json.DisableNormalizeUTF8())
if err != nil {
return "", Session{}, errgo.Wrap(err, "json.MarshalWithOption")
}
tx := r.q.Begin()
for i := 0; i < defaultRetry; i++ {
key := keyGen()
c, err := tx.WithContext(ctx).WebSession.Where(tx.WebSession.Key.Eq(key)).Count()
if err != nil {
return "", Session{}, errgo.Wrap(err, "tx.WebSession.Count")
}
if c != 0 {
// session id conflict, re-generate key
continue
}
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 key, s, nil
}
return "", Session{}, errTooManyRetry
}
func (r mysqlRepo) Get(ctx context.Context, key string) (Session, error) {
record, 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 Session{}, domain.ErrNotFound
}
return Session{}, errgo.Wrap(err, "orm.Tx.Where.First")
}
var s Session
if err = json.Unmarshal(record.Value, &s); err != nil {
return Session{}, errgo.Wrap(err, "json.Unmarshal")
}
s.UserID = record.UserID
s.CreatedAt = record.CreatedAt
s.ExpiredAt = record.ExpiredAt
return s, 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
}