Skip to content

Commit f23bbf9

Browse files
author
rn-h
committed
update auth
1 parent 4e163e3 commit f23bbf9

File tree

4 files changed

+14
-82
lines changed

4 files changed

+14
-82
lines changed

middleware/middleware.go

+5-8
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ import (
55
"log"
66
"net/http"
77
"reflect"
8-
"strconv"
9-
"strings"
108

119
"com.lc.go.codepush/server/model"
1210
"com.lc.go.codepush/server/model/constants"
@@ -24,17 +22,16 @@ func CheckToken(ctx *gin.Context) {
2422
if token == "" {
2523
log.Panic("Token不能为空")
2624
}
27-
str := utils.GetDecToken(token)
28-
info := strings.Split(str, ":")
29-
expireTime, _ := strconv.ParseInt(info[1], 10, 64)
30-
if *utils.GetTimeNow() > expireTime {
25+
26+
tokenNow := model.GetOne[model.Token]("token=?", token)
27+
28+
if *utils.GetTimeNow() > *tokenNow.ExpireTime || *tokenNow.Del {
3129
ctx.JSON(http.StatusInternalServerError, gin.H{
3230
"code": 1100,
3331
"msg": "Token expire",
3432
})
3533
ctx.Abort()
3634
} else {
37-
tokenNow := model.GetOne[model.Token]("token=?", token)
3835
if (tokenNow != nil && tokenNow.Del != nil && *tokenNow.Del) || tokenNow == nil {
3936
ctx.JSON(http.StatusInternalServerError, gin.H{
4037
"code": 1100,
@@ -44,7 +41,7 @@ func CheckToken(ctx *gin.Context) {
4441
}
4542
}
4643

47-
ctx.Set(constants.GIN_USER_ID, info[0])
44+
ctx.Set(constants.GIN_USER_ID, *tokenNow.Uid)
4845
}
4946

5047
// 異常處理

request/app.go

+6-7
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"log"
66
"net/http"
77
"os"
8-
"strconv"
98

109
"com.lc.go.codepush/server/config"
1110
"com.lc.go.codepush/server/db/redis"
@@ -32,7 +31,7 @@ type createAppReq struct {
3231
func (App) CreateApp(ctx *gin.Context) {
3332
createAppInfo := createAppReq{}
3433
if err := ctx.ShouldBindBodyWith(&createAppInfo, binding.JSON); err == nil {
35-
uid, _ := strconv.Atoi(ctx.MustGet(constants.GIN_USER_ID).(string))
34+
uid := ctx.MustGet(constants.GIN_USER_ID).(int)
3635
oldApp := model.App{}.GetAppByUidAndAppName(uid, *createAppInfo.AppName)
3736
if oldApp != nil {
3837
log.Panic("AppName " + *createAppInfo.AppName + " exist")
@@ -67,7 +66,7 @@ type createBundleReq struct {
6766
func (App) CreateBundle(ctx *gin.Context) {
6867
createBundleReq := createBundleReq{}
6968
if err := ctx.ShouldBindBodyWith(&createBundleReq, binding.JSON); err == nil {
70-
uid, _ := strconv.Atoi(ctx.MustGet(constants.GIN_USER_ID).(string))
69+
uid := ctx.MustGet(constants.GIN_USER_ID).(int)
7170

7271
app := model.App{}.GetAppByUidAndAppName(uid, *createBundleReq.AppName)
7372
if app == nil {
@@ -136,7 +135,7 @@ type createDeploymentInfo struct {
136135
func (App) CreateDeployment(ctx *gin.Context) {
137136
createDeploymentInfo := createDeploymentInfo{}
138137
if err := ctx.ShouldBindBodyWith(&createDeploymentInfo, binding.JSON); err == nil {
139-
uid, _ := strconv.Atoi(ctx.MustGet(constants.GIN_USER_ID).(string))
138+
uid := ctx.MustGet(constants.GIN_USER_ID).(int)
140139
app := model.App{}.GetAppByUidAndAppName(uid, *createDeploymentInfo.AppName)
141140
if app == nil {
142141
log.Panic("App not found")
@@ -260,7 +259,7 @@ type deploymentInfo struct {
260259
func (App) LsDeployment(ctx *gin.Context) {
261260
lsAppReq := lsDeploymentReq{}
262261
if err := ctx.ShouldBindBodyWith(&lsAppReq, binding.JSON); err == nil {
263-
uid, _ := strconv.Atoi(ctx.MustGet(constants.GIN_USER_ID).(string))
262+
uid := ctx.MustGet(constants.GIN_USER_ID).(int)
264263
app := model.App{}.GetAppByUidAndAppName(uid, *lsAppReq.AppName)
265264
if app == nil {
266265
log.Panic("App not found")
@@ -301,7 +300,7 @@ func (App) LsDeployment(ctx *gin.Context) {
301300
}
302301

303302
func (App) LsApp(ctx *gin.Context) {
304-
uid, _ := strconv.Atoi(ctx.MustGet(constants.GIN_USER_ID).(string))
303+
uid := ctx.MustGet(constants.GIN_USER_ID).(int)
305304
apps := model.GetList[model.App]("uid=?", uid)
306305
if len(*apps) <= 0 {
307306
log.Panic("No app")
@@ -322,7 +321,7 @@ type checkBundleReq struct {
322321
func (App) CheckBundle(ctx *gin.Context) {
323322
checkBundleReq := checkBundleReq{}
324323
if err := ctx.ShouldBindBodyWith(&checkBundleReq, binding.JSON); err == nil {
325-
uid, _ := strconv.Atoi(ctx.MustGet(constants.GIN_USER_ID).(string))
324+
uid := ctx.MustGet(constants.GIN_USER_ID).(int)
326325

327326
app := model.App{}.GetAppByUidAndAppName(uid, *checkBundleReq.AppName)
328327
if app == nil {

request/user.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@ package request
33
import (
44
"log"
55
"net/http"
6-
"strconv"
76

87
"com.lc.go.codepush/server/config"
98
"com.lc.go.codepush/server/model"
109
"com.lc.go.codepush/server/utils"
1110
"github.com/gin-gonic/gin"
1211
"github.com/gin-gonic/gin/binding"
12+
"github.com/google/uuid"
1313
)
1414

1515
type User struct{}
@@ -26,10 +26,10 @@ func (User) Login(ctx *gin.Context) {
2626
if user == nil || *user.Password != *loginUser.Password {
2727
panic("UserName or Psssword error")
2828
}
29-
// uuid, _ := uuid.NewUUID()
29+
uuid, _ := uuid.NewUUID()
3030
timeNow := utils.GetTimeNow()
3131
expireTime := *timeNow + (config.GetConfig().TokenExpireTime * 24 * 60 * 60 * 1000)
32-
token := utils.CreateToken(strconv.Itoa(*user.Id) + ":" + strconv.FormatInt(expireTime, 10))
32+
token := uuid.String()
3333
del := false
3434
tokenInfo := model.Token{
3535
Uid: user.Id,

utils/utils.go

-64
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
package utils
22

33
import (
4-
"bytes"
5-
"crypto/cipher"
6-
"crypto/des"
7-
"encoding/base64"
84
"log"
95
"os"
106
"sort"
@@ -13,66 +9,6 @@ import (
139
"time"
1410
)
1511

16-
var key = []byte("8x&*i}.r")
17-
18-
func CreateToken(str string) string {
19-
b, err := desEncrypt([]byte(str), key)
20-
if err != nil {
21-
log.Panic(err.Error())
22-
}
23-
24-
return base64.StdEncoding.EncodeToString(b)
25-
}
26-
func GetDecToken(str string) string {
27-
b, err := base64.StdEncoding.DecodeString(str)
28-
if err != nil {
29-
log.Panic(err.Error())
30-
}
31-
b, err = desDecrypt(b, key)
32-
if err != nil {
33-
log.Panic(err.Error())
34-
}
35-
return string(b)
36-
}
37-
func desEncrypt(origData, key []byte) ([]byte, error) {
38-
block, err := des.NewCipher(key)
39-
if err != nil {
40-
return nil, err
41-
}
42-
origData = pKCS5Padding(origData, block.BlockSize())
43-
blockMode := cipher.NewCBCEncrypter(block, key)
44-
crypted := make([]byte, len(origData))
45-
blockMode.CryptBlocks(crypted, origData)
46-
return crypted, nil
47-
}
48-
49-
func pKCS5Padding(cipherText []byte, blockSize int) []byte {
50-
padding := blockSize - len(cipherText)%blockSize
51-
padText := bytes.Repeat([]byte{byte(padding)}, padding)
52-
return append(cipherText, padText...)
53-
}
54-
55-
func desDecrypt(crypted, key []byte) ([]byte, error) {
56-
block, err := des.NewCipher(key)
57-
if err != nil {
58-
return nil, err
59-
}
60-
blockMode := cipher.NewCBCDecrypter(block, key)
61-
origData := make([]byte, len(crypted))
62-
// origData := crypted
63-
blockMode.CryptBlocks(origData, crypted)
64-
origData = pKCS5UnPadding(origData)
65-
// origData = ZeroUnPadding(origData)
66-
return origData, nil
67-
}
68-
69-
func pKCS5UnPadding(origData []byte) []byte {
70-
length := len(origData)
71-
// 去掉最后一个字节 unpadding 次
72-
unpadding := int(origData[length-1])
73-
return origData[:(length - unpadding)]
74-
}
75-
7612
func GetTimeNow() *int64 {
7713
t := time.Now().UnixMilli()
7814
return &t

0 commit comments

Comments
 (0)