Skip to content

Commit fc532c9

Browse files
committed
优化授权处理和路由HTTP功能
1 parent 90310b6 commit fc532c9

9 files changed

Lines changed: 40 additions & 37 deletions

go_ws_sh/AuthorizationHandler.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@ import (
1010
"github.com/cloudwego/hertz/pkg/app"
1111
"github.com/cloudwego/hertz/pkg/protocol/consts"
1212
"github.com/golang-module/carbon/v2"
13-
password_hashed "github.com/masx200/go_ws_sh/password-hashed"
1413
"gorm.io/gorm"
14+
15+
password_hashed "github.com/masx200/go_ws_sh/password-hashed"
1516
)
1617

1718
// ListTokensHandler 列出所有令牌
@@ -30,8 +31,8 @@ func ListTokensHandler(credentialdb *gorm.DB, tokendb *gorm.DB) func(w context.C
3031
return
3132
}
3233

33-
shouldReturn := Validatepasswordortoken(body.Authorization, credentialdb, tokendb, r)
34-
if shouldReturn {
34+
validateFailure := Validatepasswordortoken(body.Authorization, credentialdb, tokendb, r)
35+
if validateFailure {
3536
return
3637
}
3738

@@ -160,8 +161,8 @@ func CreateToken(r *app.RequestContext, credentialdb *gorm.DB, tokendb *gorm.DB)
160161
return
161162
}
162163

163-
shouldReturn := Validatepasswordortoken(req.Authorization, credentialdb, tokendb, r)
164-
if shouldReturn {
164+
validateFailure := Validatepasswordortoken(req.Authorization, credentialdb, tokendb, r)
165+
if validateFailure {
165166
return
166167
}
167168
numBytes := 120
@@ -304,8 +305,8 @@ func ModifyPassword(r *app.RequestContext, credentialdb *gorm.DB, tokendb *gorm.
304305
var reqcre CredentialsClient = req.Authorization
305306
// 验证旧密码
306307
// 假设已经有一个函数 ValidatePassword 用于验证密码
307-
shouldReturn := Validatepasswordortoken(reqcre, credentialdb, tokendb, r)
308-
if shouldReturn {
308+
validateFailure := Validatepasswordortoken(reqcre, credentialdb, tokendb, r)
309+
if validateFailure {
309310
return
310311
}
311312
// 更新密码
@@ -361,8 +362,8 @@ func ModifyPassword(r *app.RequestContext, credentialdb *gorm.DB, tokendb *gorm.
361362
// return
362363
// }
363364

364-
// shouldReturn := Validatepasswordortoken(req, credentialdb, tokendb, r)
365-
// if shouldReturn {
365+
// validateFailure := Validatepasswordortoken(req, credentialdb, tokendb, r)
366+
// if validateFailure {
366367
// return
367368
// }
368369
// var data map[string]interface{}

go_ws_sh/ServerRouterHTTP.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ func UpdateTokenHandler(credentialdb *gorm.DB, tokendb *gorm.DB, sessiondb *gorm
4141
}
4242

4343
// 验证身份
44-
shouldReturn := Validatepasswordortoken(body.Authorization, credentialdb, tokendb, r)
45-
if shouldReturn {
44+
validateFailure := Validatepasswordortoken(body.Authorization, credentialdb, tokendb, r)
45+
if validateFailure {
4646
return
4747
}
4848

@@ -130,8 +130,8 @@ func GetCredentialsHandler(credentialdb *gorm.DB, tokendb *gorm.DB, sessiondb *g
130130
}
131131

132132
// 验证身份
133-
shouldReturn := Validatepasswordortoken(body.Authorization, credentialdb, tokendb, r)
134-
if shouldReturn {
133+
validateFailure := Validatepasswordortoken(body.Authorization, credentialdb, tokendb, r)
134+
if validateFailure {
135135
log.Println("用户登录失败:")
136136
return
137137
}
@@ -226,8 +226,8 @@ func CreateCredentialHandler(credentialdb *gorm.DB, tokendb *gorm.DB, sessiondb
226226
var reqcre CredentialsClient = req.Authorization
227227
// 验证旧密码
228228
// 假设已经有一个函数 ValidatePassword 用于验证密码
229-
shouldReturn := Validatepasswordortoken(reqcre, credentialdb, tokendb, r)
230-
if shouldReturn {
229+
validateFailure := Validatepasswordortoken(reqcre, credentialdb, tokendb, r)
230+
if validateFailure {
231231
return
232232
}
233233
// 更新密码
@@ -293,8 +293,8 @@ func CreateSessionHandler(credentialdb *gorm.DB, tokendb *gorm.DB, sessiondb *go
293293
}
294294

295295
// 验证身份
296-
shouldReturn := Validatepasswordortoken(body.Authorization, credentialdb, tokendb, r)
297-
if shouldReturn {
296+
validateFailure := Validatepasswordortoken(body.Authorization, credentialdb, tokendb, r)
297+
if validateFailure {
298298
return
299299
}
300300

@@ -398,8 +398,8 @@ func UpdateSessionHandler(credentialdb *gorm.DB, tokendb *gorm.DB, sessiondb *go
398398
}
399399

400400
// 验证身份
401-
shouldReturn := Validatepasswordortoken(req.Authorization, credentialdb, tokendb, r)
402-
if shouldReturn {
401+
validateFailure := Validatepasswordortoken(req.Authorization, credentialdb, tokendb, r)
402+
if validateFailure {
403403
return
404404
}
405405

@@ -486,8 +486,8 @@ func GetSessionsHandler(credentialdb *gorm.DB, tokendb *gorm.DB, sessiondb *gorm
486486
}
487487
log.Println(body)
488488

489-
shouldReturn := Validatepasswordortoken(body.Authorization, credentialdb, tokendb, r)
490-
if shouldReturn {
489+
validateFailure := Validatepasswordortoken(body.Authorization, credentialdb, tokendb, r)
490+
if validateFailure {
491491
log.Println("用户登录失败:")
492492
return
493493
}

go_ws_sh/authorizationmiddleware.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package go_ws_sh
22

33
import (
44
"context"
5+
56
"github.com/cloudwego/hertz/pkg/app"
67
"github.com/cloudwego/hertz/pkg/protocol/consts"
78
"gorm.io/gorm"
@@ -23,8 +24,8 @@ func AuthorizationMiddleware(credentialdb *gorm.DB, tokendb *gorm.DB, sessiondb
2324
}
2425

2526
// 调用 Validatepasswordortoken 函数进行身份验证
26-
shouldReturn := Validatepasswordortoken(req.Authorization, credentialdb, tokendb, r)
27-
if shouldReturn {
27+
validateFailure := Validatepasswordortoken(req.Authorization, credentialdb, tokendb, r)
28+
if validateFailure {
2829
return
2930
}
3031

go_ws_sh/createhandlerauthorization.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,12 @@ func createhandlerauthorization(credentialdb *gorm.DB, tokendb *gorm.DB, next fu
6363
req.Username = postData.Get("username")
6464
req.Identifier = postData.Get("identifier")
6565
req.Password = postData.Get("password")
66-
shouldReturn := Validatepasswordortoken(req, credentialdb, tokendb, r)
67-
if shouldReturn {
66+
validateFailure := Validatepasswordortoken(req, credentialdb, tokendb, r)
67+
if validateFailure {
6868
log.Println("用户登录失败:")
6969
return
7070
}
71-
var ok = !shouldReturn
71+
var ok = !validateFailure
7272
if ok {
7373
log.Println("用户登录成功:")
7474
next(w, r)

go_ws_sh/createhandlerloginlogout.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ type TokenInfo = CredentialsClient
3030
// return
3131
// }
3232
// log.Println(credential)
33-
// shouldReturn := Validatepasswordortoken(credential, credentialdb, tokendb, r)
34-
// if shouldReturn {
33+
// validateFailure := Validatepasswordortoken(credential, credentialdb, tokendb, r)
34+
// if validateFailure {
3535
// log.Println("用户登录失败:")
3636
// return
3737
// }

go_ws_sh/deletecredentialhandler.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ func DeleteCredentialHandler(credentialdb *gorm.DB, tokendb *gorm.DB, sessiondb
3636
}
3737
}
3838
// 验证身份
39-
shouldReturn := Validatepasswordortoken(req.Authorization, credentialdb, tokendb, r)
40-
if shouldReturn {
39+
validateFailure := Validatepasswordortoken(req.Authorization, credentialdb, tokendb, r)
40+
if validateFailure {
4141
return
4242
}
4343
var err error

go_ws_sh/deletesessionhandler.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ func DeleteSessionHandler(credentialdb *gorm.DB, tokendb *gorm.DB, sessiondb *go
3131
}
3232
}
3333
// 验证身份
34-
shouldReturn := Validatepasswordortoken(body.Authorization, credentialdb, tokendb, r)
35-
if shouldReturn {
34+
validateFailure := Validatepasswordortoken(body.Authorization, credentialdb, tokendb, r)
35+
if validateFailure {
3636
return
3737
}
3838

go_ws_sh/deletetokenhandler.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@ package go_ws_sh
22

33
import (
44
"context"
5+
"log"
6+
57
"github.com/cloudwego/hertz/pkg/app"
68
"github.com/cloudwego/hertz/pkg/protocol/consts"
79
"gorm.io/gorm"
8-
"log"
910
)
1011

1112
func DeleteTokenHandler(credentialdb *gorm.DB, tokendb *gorm.DB, sessiondb *gorm.DB, c context.Context, r *app.RequestContext) {
@@ -25,8 +26,8 @@ func DeleteTokenHandler(credentialdb *gorm.DB, tokendb *gorm.DB, sessiondb *gorm
2526
}
2627

2728
// 验证身份
28-
shouldReturn := Validatepasswordortoken(req.Authorization, credentialdb, tokendb, r)
29-
if shouldReturn {
29+
validateFailure := Validatepasswordortoken(req.Authorization, credentialdb, tokendb, r)
30+
if validateFailure {
3031
return
3132
}
3233
// log.Println(req)

go_ws_sh/pipe-std-ws-server.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,8 @@ func pipe_std_ws_server(config ConfigServer, credentialdb *gorm.DB, tokendb *gor
118118

119119
consts.StatusMethodNotAllowed)
120120
})
121-
// shouldReturn := MatchAndHandleRoute(w, routes, r)
122-
// if shouldReturn {
121+
// validateFailure := MatchAndHandleRoute(w, routes, r)
122+
// if validateFailure {
123123
// return
124124
// }
125125
// if string(r.Method()) == consts.MethodPost {

0 commit comments

Comments
 (0)