认证授权测试代码与业务代码质量修复

This commit is contained in:
2025-03-22 16:37:24 +08:00
parent 6ddf1118a5
commit c3abb42bce
10 changed files with 960 additions and 33 deletions

View File

@@ -8,8 +8,6 @@ import (
var Auth = &authService{}
type authService struct{}
type AuthServiceError string
func (e AuthServiceError) Error() string {
@@ -31,6 +29,8 @@ var (
ErrOauthUnsupportedGrantType = AuthServiceOauthError("unsupported_grant_type")
)
type authService struct{}
// OauthAuthorizationCode 验证授权码
func (s *authService) OauthAuthorizationCode(ctx context.Context, client *models.Client, code, redirectURI, codeVerifier string) (*TokenDetails, error) {
// TODO: 从数据库验证授权码
@@ -38,7 +38,7 @@ func (s *authService) OauthAuthorizationCode(ctx context.Context, client *models
}
// OauthClientCredentials 验证客户端凭证
func (s *authService) OauthClientCredentials(ctx context.Context, client *models.Client, scope ...[]string) (*TokenDetails, error) {
func (s *authService) OauthClientCredentials(ctx context.Context, client *models.Client, scope ...string) (*TokenDetails, error) {
var clientType PayloadType
switch client.Spec {
@@ -47,14 +47,17 @@ func (s *authService) OauthClientCredentials(ctx context.Context, client *models
case 1:
clientType = PayloadClientPublic
case 2:
clientType = PayloadClientConfidential
clientType = PayloadClientPublic
}
var permissions = make(map[string]struct{}, len(scope))
for _, item := range scope {
permissions[item] = struct{}{}
}
// 保存会话并返回令牌
auth := AuthContext{
Permissions: map[string]struct{}{
"client": {},
},
Permissions: permissions,
Payload: Payload{
Type: clientType,
Id: client.ID,