mirror of
https://gitee.com/dromara/mayfly-go
synced 2025-11-04 00:10:25 +08:00
refactor: 引入dayjs、新增refreshToken无感刷新、团队新增有效期、数据库等问题修复
This commit is contained in:
@@ -2,7 +2,6 @@ package api
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"mayfly-go/internal/auth/api/form"
|
||||
"mayfly-go/internal/auth/config"
|
||||
@@ -71,11 +70,12 @@ func (a *AccountLogin) Login(rc *req.Ctx) {
|
||||
}
|
||||
|
||||
type OtpVerifyInfo struct {
|
||||
AccountId uint64
|
||||
Username string
|
||||
OptStatus int
|
||||
AccessToken string
|
||||
OtpSecret string
|
||||
AccountId uint64
|
||||
Username string
|
||||
OptStatus int
|
||||
AccessToken string
|
||||
RefreshToken string
|
||||
OtpSecret string
|
||||
}
|
||||
|
||||
// OTP双因素校验
|
||||
@@ -84,10 +84,9 @@ func (a *AccountLogin) OtpVerify(rc *req.Ctx) {
|
||||
req.BindJsonAndValid(rc, otpVerify)
|
||||
|
||||
tokenKey := fmt.Sprintf("otp:token:%s", otpVerify.OtpToken)
|
||||
otpInfoJson := cache.GetStr(tokenKey)
|
||||
biz.NotEmpty(otpInfoJson, "otpToken错误或失效, 请重新登陆获取")
|
||||
otpInfo := new(OtpVerifyInfo)
|
||||
json.Unmarshal([]byte(otpInfoJson), otpInfo)
|
||||
ok := cache.Get(tokenKey, otpInfo)
|
||||
biz.IsTrue(ok, "otpToken错误或失效, 请重新登陆获取")
|
||||
|
||||
failCountKey := fmt.Sprintf("account:otp:failcount:%d", otpInfo.AccountId)
|
||||
failCount := cache.GetInt(failCountKey)
|
||||
@@ -116,7 +115,20 @@ func (a *AccountLogin) OtpVerify(rc *req.Ctx) {
|
||||
go saveLogin(la, getIpAndRegion(rc))
|
||||
|
||||
cache.Del(tokenKey)
|
||||
rc.ResData = accessToken
|
||||
rc.ResData = collx.Kvs("token", accessToken, "refresh_token", otpInfo.RefreshToken)
|
||||
}
|
||||
|
||||
func (a *AccountLogin) RefreshToken(rc *req.Ctx) {
|
||||
refreshToken := rc.Query("refresh_token")
|
||||
biz.NotEmpty(refreshToken, "refresh_token不能为空")
|
||||
|
||||
accountId, username, err := req.ParseToken(refreshToken)
|
||||
if err != nil {
|
||||
panic(errorx.PermissionErr)
|
||||
}
|
||||
token, refreshToken, err := req.CreateToken(accountId, username)
|
||||
biz.ErrIsNil(err)
|
||||
rc.ResData = collx.Kvs("token", token, "refresh_token", refreshToken)
|
||||
}
|
||||
|
||||
func (a *AccountLogin) Logout(rc *req.Ctx) {
|
||||
|
||||
Reference in New Issue
Block a user