Skip to content

Commit 0beee08

Browse files
masx200claude
andcommitted
重构授权验证功能,将验证逻辑分离到独立模块
- 将Validatepasswordortoken函数从AuthorizationHandler.go移至独立文件validatepasswordortoken.go - 在所有处理器中临时注释验证调用,为重构做准备 - 在authorizationmiddleware.go中添加Bearer token处理框架 - 优化代码组织结构,提高可维护性 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 201d4aa commit 0beee08

7 files changed

Lines changed: 109 additions & 90 deletions

go_ws_sh/AuthorizationHandler.go

Lines changed: 13 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@ func ListTokensHandler(credentialdb *gorm.DB, tokendb *gorm.DB) func(w context.C
3131
return
3232
}
3333

34-
validateFailure := Validatepasswordortoken(body.Authorization, credentialdb, tokendb, r)
35-
if validateFailure {
36-
return
37-
}
34+
// validateFailure := Validatepasswordortoken(body.Authorization, credentialdb, tokendb, r)
35+
// if validateFailure {
36+
// return
37+
// }
3838

3939
// 查询所有令牌
4040
var tokens []TokenStore
@@ -161,10 +161,10 @@ func CreateToken(r *app.RequestContext, credentialdb *gorm.DB, tokendb *gorm.DB)
161161
return
162162
}
163163

164-
validateFailure := Validatepasswordortoken(req.Authorization, credentialdb, tokendb, r)
165-
if validateFailure {
166-
return
167-
}
164+
// validateFailure := Validatepasswordortoken(req.Authorization, credentialdb, tokendb, r)
165+
// if validateFailure {
166+
// return
167+
// }
168168
numBytes := 120
169169
hexString, err := generateHexKey(numBytes)
170170
if err != nil {
@@ -228,42 +228,6 @@ func CreateToken(r *app.RequestContext, credentialdb *gorm.DB, tokendb *gorm.DB)
228228
})
229229
}
230230

231-
func Validatepasswordortoken(req CredentialsClient, credentialdb *gorm.DB, tokendb *gorm.DB, r *app.RequestContext) bool {
232-
if req.Type == "token" && req.Token != "" && req.Identifier != "" {
233-
log.Println("开始Token 认证")
234-
// Token 认证
235-
if ok, err := ValidateToken(req, tokendb); !ok {
236-
log.Println(err)
237-
r.AbortWithMsg("Error: Invalid credentials", consts.StatusUnauthorized)
238-
log.Println("Error: Invalid credentials")
239-
return true
240-
}
241-
log.Println("success: success credentials")
242-
return false
243-
}
244-
log.Println("开始password 认证")
245-
// 用户名密码认证
246-
var cred CredentialStore
247-
if err := credentialdb.Where("username = ?", req.Username).First(&cred).Error; err != nil {
248-
r.AbortWithMsg("Error: Invalid credentials", consts.StatusUnauthorized)
249-
return true
250-
}
251-
//用户名和密码都不为空
252-
if req.Username == "" || req.Password == "" {
253-
r.AbortWithMsg("Error: Username or password is empty", consts.StatusBadRequest)
254-
return true
255-
}
256-
// 验证密码
257-
// 这里需要实现具体的密码验证逻辑
258-
// 假设已经有一个函数 ValidatePassword 用于验证密码
259-
if ok, err := ValidatePassword(req.Password, cred.Hash, cred.Salt, cred.Algorithm); !ok {
260-
log.Println(err)
261-
r.AbortWithMsg("Error: Invalid credentials", consts.StatusUnauthorized)
262-
return true
263-
}
264-
return false
265-
}
266-
267231
func ValidatePassword(Password, Hash, Salt, Algorithm string) (bool, error) {
268232
var hashresult, err = password_hashed.HashPasswordWithSalt(Password, password_hashed.Options{Algorithm: Algorithm,
269233
SaltHex: Salt,
@@ -302,13 +266,13 @@ func ModifyPassword(r *app.RequestContext, credentialdb *gorm.DB, tokendb *gorm.
302266
r.AbortWithMsg("Error: Invalid credentials", consts.StatusUnauthorized)
303267
return
304268
}
305-
var reqcre CredentialsClient = req.Authorization
269+
// var reqcre CredentialsClient = req.Authorization
306270
// 验证旧密码
307271
// 假设已经有一个函数 ValidatePassword 用于验证密码
308-
validateFailure := Validatepasswordortoken(reqcre, credentialdb, tokendb, r)
309-
if validateFailure {
310-
return
311-
}
272+
// validateFailure := Validatepasswordortoken(reqcre, credentialdb, tokendb, r)
273+
// if validateFailure {
274+
// return
275+
// }
312276
// 更新密码
313277
newHashresult, err := password_hashed.HashPasswordWithSalt(req.Credential.Password, password_hashed.Options{Algorithm: "SHA-512"})
314278

go_ws_sh/ServerRouterHTTP.go

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ import (
88
"github.com/cloudwego/hertz/pkg/app"
99
"github.com/cloudwego/hertz/pkg/protocol/consts"
1010
"github.com/golang-module/carbon/v2"
11-
password_hashed "github.com/masx200/go_ws_sh/password-hashed"
1211
"gorm.io/gorm"
12+
13+
password_hashed "github.com/masx200/go_ws_sh/password-hashed"
1314
)
1415

1516
func FormatTimeWithCarbon(t carbon.Carbon) string {
@@ -41,10 +42,10 @@ func UpdateTokenHandler(credentialdb *gorm.DB, tokendb *gorm.DB, sessiondb *gorm
4142
}
4243

4344
// 验证身份
44-
validateFailure := Validatepasswordortoken(body.Authorization, credentialdb, tokendb, r)
45-
if validateFailure {
46-
return
47-
}
45+
// validateFailure := Validatepasswordortoken(body.Authorization, credentialdb, tokendb, r)
46+
// if validateFailure {
47+
// return
48+
// }
4849

4950
// 检查 Identifier 是否为空
5051
if body.Token.Identifier == "" {
@@ -130,11 +131,11 @@ func GetCredentialsHandler(credentialdb *gorm.DB, tokendb *gorm.DB, sessiondb *g
130131
}
131132

132133
// 验证身份
133-
validateFailure := Validatepasswordortoken(body.Authorization, credentialdb, tokendb, r)
134-
if validateFailure {
135-
log.Println("用户登录失败:")
136-
return
137-
}
134+
// validateFailure := Validatepasswordortoken(body.Authorization, credentialdb, tokendb, r)
135+
// if validateFailure {
136+
// log.Println("用户登录失败:")
137+
// return
138+
// }
138139

139140
// 查询所有用户的认证信息
140141
var credentials []CredentialStore
@@ -223,13 +224,13 @@ func CreateCredentialHandler(credentialdb *gorm.DB, tokendb *gorm.DB, sessiondb
223224
// r.AbortWithMsg("Error: Invalid credentials", consts.StatusUnauthorized)
224225
// return
225226
// }
226-
var reqcre CredentialsClient = req.Authorization
227+
// var reqcre CredentialsClient = req.Authorization
227228
// 验证旧密码
228229
// 假设已经有一个函数 ValidatePassword 用于验证密码
229-
validateFailure := Validatepasswordortoken(reqcre, credentialdb, tokendb, r)
230-
if validateFailure {
231-
return
232-
}
230+
// validateFailure := Validatepasswordortoken(reqcre, credentialdb, tokendb, r)
231+
// if validateFailure {
232+
// return
233+
// }
233234
// 更新密码
234235
newHashresult, err := password_hashed.HashPasswordWithSalt(req.Credential.Password, password_hashed.Options{Algorithm: "SHA-512"})
235236

@@ -293,10 +294,10 @@ func CreateSessionHandler(credentialdb *gorm.DB, tokendb *gorm.DB, sessiondb *go
293294
}
294295

295296
// 验证身份
296-
validateFailure := Validatepasswordortoken(body.Authorization, credentialdb, tokendb, r)
297-
if validateFailure {
298-
return
299-
}
297+
// validateFailure := Validatepasswordortoken(body.Authorization, credentialdb, tokendb, r)
298+
// if validateFailure {
299+
// return
300+
// }
300301

301302
// 检查 Name 是否为空
302303
if body.Session.Name == "" || body.Session.Cmd == "" || body.Session.Dir == "" {
@@ -398,10 +399,10 @@ func UpdateSessionHandler(credentialdb *gorm.DB, tokendb *gorm.DB, sessiondb *go
398399
}
399400

400401
// 验证身份
401-
validateFailure := Validatepasswordortoken(req.Authorization, credentialdb, tokendb, r)
402-
if validateFailure {
403-
return
404-
}
402+
// validateFailure := Validatepasswordortoken(req.Authorization, credentialdb, tokendb, r)
403+
// if validateFailure {
404+
// return
405+
// }
405406

406407
// 检查 Name 是否为空
407408
if req.Session.Name == "" {
@@ -486,12 +487,12 @@ func GetSessionsHandler(credentialdb *gorm.DB, tokendb *gorm.DB, sessiondb *gorm
486487
}
487488
log.Println(body)
488489

489-
validateFailure := Validatepasswordortoken(body.Authorization, credentialdb, tokendb, r)
490-
if validateFailure {
491-
log.Println("用户登录失败:")
492-
return
493-
}
494-
log.Println("用户登录成功:")
490+
// validateFailure := Validatepasswordortoken(body.Authorization, credentialdb, tokendb, r)
491+
// if validateFailure {
492+
// log.Println("用户登录失败:")
493+
// return
494+
// }
495+
// log.Println("用户登录成功:")
495496

496497
username := body.Authorization.Username
497498
if username == "" {

go_ws_sh/authorizationmiddleware.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,16 @@ import (
1111
// AuthorizationMiddleware 定义身份验证中间件
1212
func AuthorizationMiddleware(credentialdb *gorm.DB, tokendb *gorm.DB, sessiondb *gorm.DB) HertzMiddleWare {
1313
return func(c context.Context, r *app.RequestContext, next HertzNext) {
14+
15+
16+
17+
bearertoken:=r.Request.Header.Get("authorization")
18+
19+
20+
21+
if bearertoken!=""{
22+
23+
}
1424
var req struct {
1525
Authorization CredentialsClient `json:"authorization"`
1626
}

go_ws_sh/deletecredentialhandler.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,10 @@ func DeleteCredentialHandler(credentialdb *gorm.DB, tokendb *gorm.DB, sessiondb
3636
}
3737
}
3838
// 验证身份
39-
validateFailure := Validatepasswordortoken(req.Authorization, credentialdb, tokendb, r)
40-
if validateFailure {
41-
return
42-
}
39+
// validateFailure := Validatepasswordortoken(req.Authorization, credentialdb, tokendb, r)
40+
// if validateFailure {
41+
// return
42+
// }
4343
var err error
4444
username := req.Authorization.Username
4545
if username == "" {

go_ws_sh/deletesessionhandler.go

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

3939
// 检查 Name 是否为空
4040
if body.Session.Name == "" {

go_ws_sh/deletetokenhandler.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@ func DeleteTokenHandler(credentialdb *gorm.DB, tokendb *gorm.DB, sessiondb *gorm
2626
}
2727

2828
// 验证身份
29-
validateFailure := Validatepasswordortoken(req.Authorization, credentialdb, tokendb, r)
30-
if validateFailure {
31-
return
32-
}
29+
// validateFailure := Validatepasswordortoken(req.Authorization, credentialdb, tokendb, r)
30+
// if validateFailure {
31+
// return
32+
// }
3333
// log.Println(req)
3434
// 检查 Identifier 是否为空
3535
if req.Token.Identifier == "" {
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package go_ws_sh
2+
3+
import (
4+
"github.com/cloudwego/hertz/pkg/app"
5+
"github.com/cloudwego/hertz/pkg/protocol/consts"
6+
"gorm.io/gorm"
7+
"log"
8+
)
9+
10+
func Validatepasswordortoken(req CredentialsClient, credentialdb *gorm.DB, tokendb *gorm.DB, r *app.RequestContext) bool {
11+
if req.Type == "token" && req.Token != "" && req.Identifier != "" {
12+
log.Println("开始Token 认证")
13+
// Token 认证
14+
if ok, err := ValidateToken(req, tokendb); !ok {
15+
log.Println(err)
16+
r.AbortWithMsg("Error: Invalid credentials", consts.StatusUnauthorized)
17+
log.Println("Error: Invalid credentials")
18+
return true
19+
}
20+
log.Println("success: success credentials")
21+
return false
22+
}
23+
log.Println("开始password 认证")
24+
// 用户名密码认证
25+
var cred CredentialStore
26+
if err := credentialdb.Where("username = ?", req.Username).First(&cred).Error; err != nil {
27+
r.AbortWithMsg("Error: Invalid credentials", consts.StatusUnauthorized)
28+
return true
29+
}
30+
//用户名和密码都不为空
31+
if req.Username == "" || req.Password == "" {
32+
r.AbortWithMsg("Error: Username or password is empty", consts.StatusBadRequest)
33+
return true
34+
}
35+
// 验证密码
36+
// 这里需要实现具体的密码验证逻辑
37+
// 假设已经有一个函数 ValidatePassword 用于验证密码
38+
if ok, err := ValidatePassword(req.Password, cred.Hash, cred.Salt, cred.Algorithm); !ok {
39+
log.Println(err)
40+
r.AbortWithMsg("Error: Invalid credentials", consts.StatusUnauthorized)
41+
return true
42+
}
43+
return false
44+
}

0 commit comments

Comments
 (0)