/
admin.go
274 lines (235 loc) · 8.5 KB
/
admin.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
package impl
import (
"bufio"
"bytes"
"context"
"fmt"
"math/rand"
"os"
"path/filepath"
"time"
uuid2 "github.com/google/uuid"
"go.uber.org/zap"
"github.com/go-sonic/sonic/cache"
"github.com/go-sonic/sonic/config"
"github.com/go-sonic/sonic/consts"
"github.com/go-sonic/sonic/dal"
"github.com/go-sonic/sonic/event"
"github.com/go-sonic/sonic/log"
"github.com/go-sonic/sonic/model/dto"
"github.com/go-sonic/sonic/model/entity"
"github.com/go-sonic/sonic/model/param"
"github.com/go-sonic/sonic/model/property"
"github.com/go-sonic/sonic/service"
"github.com/go-sonic/sonic/util"
"github.com/go-sonic/sonic/util/xerr"
)
type adminServiceImpl struct {
UserService service.UserService
OptionService service.OptionService
Cache cache.Cache
Config *config.Config
Event event.Bus
TwoFactorTOTPMFA service.TwoFactorTOTPMFAService
EmailService service.EmailService
}
func NewAdminService(userService service.UserService, cache cache.Cache, config *config.Config, event event.Bus, twoFactorMFA service.TwoFactorTOTPMFAService, emailService service.EmailService) service.AdminService {
return &adminServiceImpl{
UserService: userService,
Cache: cache,
Config: config,
Event: event,
TwoFactorTOTPMFA: twoFactorMFA,
EmailService: emailService,
}
}
func (a *adminServiceImpl) Authenticate(ctx context.Context, loginParam param.LoginParam) (*entity.User, error) {
missMatchTip := "用户名或密码不正确"
var user *entity.User
err := util.Validate.Var(loginParam.Username, "email")
if err != nil {
user, err = a.UserService.GetByUsername(ctx, loginParam.Username)
} else {
user, err = a.UserService.GetByEmail(ctx, loginParam.Username)
}
if xerr.GetType(err) == xerr.NoRecord {
return nil, xerr.WithMsg(err, missMatchTip).WithStatus(xerr.StatusBadRequest)
}
if err != nil {
return nil, err
}
err = a.UserService.MustNotExpire(ctx, user.ExpireTime)
if err != nil {
return nil, err
}
if !a.UserService.PasswordMatch(ctx, user.Password, loginParam.Password) {
return nil, xerr.BadParam.New("").WithMsg(missMatchTip).WithStatus(xerr.StatusBadRequest)
}
return user, nil
}
func (a *adminServiceImpl) Auth(ctx context.Context, loginParam param.LoginParam) (*dto.AuthTokenDTO, error) {
user, err := a.Authenticate(ctx, loginParam)
if err != nil {
return nil, err
}
if a.TwoFactorTOTPMFA.UseMFA(user.MfaType) {
if len(loginParam.AuthCode) != 6 {
return nil, xerr.WithMsg(nil, "请输入6位两步验证码").WithStatus(xerr.StatusBadRequest)
}
mfaAuth := a.TwoFactorTOTPMFA.ValidateTFACode(user.MfaKey, loginParam.AuthCode)
if !mfaAuth {
return nil, xerr.WithStatus(nil, xerr.StatusBadRequest).WithMsg("两步验证码验证错误")
}
}
a.Event.Publish(ctx, &event.LogEvent{
LogKey: user.Username,
LogType: consts.LogTypeLoggedIn,
Content: user.Nickname,
IPAddress: util.GetClientIP(ctx),
})
return a.buildAuthToken(user), nil
}
func (a *adminServiceImpl) ClearToken(ctx context.Context) error {
user, ok := GetAuthorizedUser(ctx)
if !ok || user == nil {
return xerr.Forbidden.New("").WithStatus(xerr.StatusForbidden).WithMsg("未登录")
}
accessToken, _ := a.Cache.Get(cache.BuildAccessTokenKey(user.ID))
refreshToken, _ := a.Cache.Get(cache.BuildRefreshTokenKey(user.ID))
a.Cache.Delete(cache.BuildTokenAccessKey(accessToken.(string)))
a.Cache.Delete(cache.BuildTokenRefreshKey(refreshToken.(string)))
a.Cache.Delete(cache.BuildAccessTokenKey(user.ID))
a.Cache.Delete(cache.BuildRefreshTokenKey(user.ID))
a.Event.Publish(ctx, &event.LogEvent{
LogKey: user.Username,
LogType: consts.LogTypeLoggedOut,
Content: user.Nickname,
IPAddress: util.GetClientIP(ctx),
})
return nil
}
func (a *adminServiceImpl) SendResetPasswordCode(ctx context.Context, resetParam param.ResetPasswordParam) error {
user, ok := GetAuthorizedUser(ctx)
if !ok || user == nil {
return xerr.Forbidden.New("").WithStatus(xerr.StatusForbidden).WithMsg("未登录")
}
_, ok = a.Cache.Get(cache.BuildCodeCacheKey(user.ID))
if ok {
return xerr.NoType.New("").WithMsg("已经获取过验证码,不能重复获取").WithStatus(xerr.StatusInternalServerError)
}
rnd := rand.New(rand.NewSource(time.Now().UnixNano()))
code := fmt.Sprintf("%06v", rnd.Int31n(1000000))
log.CtxInfof(ctx, "重置密码验证码: %v", code)
a.Cache.Set(cache.BuildCodeCacheKey(user.ID), code, consts.CodeValidDuration)
emailEnabled, err := a.OptionService.GetOrByDefaultWithErr(ctx, property.EmailIsEnabled, false)
if err != nil {
return err
}
if !emailEnabled.(bool) {
return xerr.NoType.New("未启用 SMTP 服务").WithMsg("未启用 SMTP 服务,无法发送邮件,但是你可以通过系统日志找到验证码")
}
content := "您正在进行密码重置操作,如不是本人操作,请尽快做好相应措施。密码重置验证码如下(五分钟有效):\n" + code
return a.EmailService.SendTextEmail(ctx, resetParam.Email, "找回密码验证码", content)
}
func (a *adminServiceImpl) buildAuthToken(user *entity.User) *dto.AuthTokenDTO {
accessToken := uuid2.New().String()
refreshToken := uuid2.New().String()
authToken := &dto.AuthTokenDTO{}
authToken.AccessToken = accessToken
authToken.ExpiredIn = consts.AccessTokenExpiredSeconds
authToken.RefreshToken = refreshToken
a.Cache.Set(cache.BuildTokenAccessKey(accessToken), user.ID, time.Second*consts.AccessTokenExpiredSeconds)
a.Cache.Set(cache.BuildTokenRefreshKey(refreshToken), user.ID, consts.RefreshTokenExpiredDays*24*3600*time.Second)
a.Cache.Set(cache.BuildAccessTokenKey(user.ID), accessToken, time.Second*consts.AccessTokenExpiredSeconds)
a.Cache.Set(cache.BuildRefreshTokenKey(user.ID), refreshToken, consts.RefreshTokenExpiredDays*24*3600*time.Second)
return authToken
}
func (a *adminServiceImpl) RefreshToken(ctx context.Context, refreshToken string) (*dto.AuthTokenDTO, error) {
userID, ok := a.Cache.Get(cache.BuildTokenRefreshKey(refreshToken))
if !ok {
return nil, xerr.WithMsg(nil, "登录状态已失效,请重新登录").WithStatus(xerr.StatusBadRequest)
}
userDAL := dal.GetQueryByCtx(ctx).User
user, err := userDAL.WithContext(ctx).Where(userDAL.ID.Eq(userID.(int32))).First()
if err != nil {
return nil, err
}
return a.buildAuthToken(user), nil
}
func (a *adminServiceImpl) GetEnvironments(ctx context.Context) *dto.EnvironmentDTO {
environments := &dto.EnvironmentDTO{
Database: string(dal.DBType) + " " + consts.DatabaseVersion,
Version: consts.SonicVersion,
StartTime: consts.StartTime.UnixMilli(),
Mode: util.IfElse(a.Config.Sonic.Mode == "", "production", a.Config.Sonic.Mode).(string),
}
return environments
}
func (a *adminServiceImpl) GetLogFiles(ctx context.Context, lineNum int64) (string, error) {
errTips := "读取日志文件错误"
lineEndByte := []byte("\n")[0]
linesTotalByteNum := 0
var linesCount int64
fileName := filepath.Join(a.Config.Sonic.LogDir, a.Config.Log.FileName)
file, err := os.Open(fileName)
if err != nil {
return "", xerr.WithStatus(err, xerr.StatusInternalServerError).WithMsg(errTips)
}
defer func() {
err = file.Close()
if err != nil {
log.CtxError(ctx, "关闭日志文件错误", zap.Error(err))
}
}()
fileInfo, err := file.Stat()
if err != nil {
return "", xerr.WithStatus(err, xerr.StatusInternalServerError).WithMsg(errTips)
}
fileBytesLength := fileInfo.Size()
position := fileBytesLength - 1
bufReader := bufio.NewReader(file)
curLine := bytes.Buffer{}
globalIsPrefix := false
lines := make([][]byte, 0, lineNum)
for position > 0 {
if !globalIsPrefix {
position--
_, err = file.Seek(position, 0)
if err != nil {
return "", xerr.WithMsg(err, errTips).WithStatus(xerr.StatusInternalServerError)
}
bufReader.Reset(file)
curByte, err := bufReader.ReadByte()
if err != nil {
return "", xerr.WithMsg(err, errTips).WithStatus(xerr.StatusInternalServerError)
}
if curByte != lineEndByte {
continue
}
}
content, isPrefix, err := bufReader.ReadLine()
if err != nil {
return "", xerr.WithMsg(err, errTips).WithStatus(xerr.StatusInternalServerError)
}
globalIsPrefix = isPrefix
if !isPrefix {
curLine.Write(content)
lines = append(lines, curLine.Bytes())
linesTotalByteNum += len(content) + 1
linesCount++
curLine = bytes.Buffer{}
} else {
curLine.Write(content)
}
if linesCount == lineNum {
break
}
}
result := bytes.Buffer{}
result.Grow(linesTotalByteNum)
for i := len(lines) - 1; i >= 0; i-- {
result.Write(lines[i])
result.WriteByte(lineEndByte)
}
return result.String(), nil
}