diff --git a/proxyserver/main.go b/proxyserver/main.go index 5534ee1c..1ac84415 100644 --- a/proxyserver/main.go +++ b/proxyserver/main.go @@ -75,10 +75,12 @@ func main() { fs := http.FileServer(http.Dir(vueBuild)) indexHtmlPath := vueBuild + "/index.html" mux.Handle("/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - if strings.HasPrefix(r.URL.Path, "/api") { + if strings.HasPrefix(r.URL.Path, "/api/") { // 对/api开头的请求进行反向代理 proxy := httputil.NewSingleHostReverseProxy(apiURL) proxy.ServeHTTP(w, r) + return + } else { // 根据请求路径判断是服务静态文件或者是返回index.html idx := strings.Index(r.URL.Path[1:], "/") @@ -144,7 +146,7 @@ func NewBackend(mux *http.ServeMux, httpAddress string, muxPaths ...string) *Bac // 创建处理请求的函数 handleRequest := func(w http.ResponseWriter, r *http.Request) { // 解析目标URL,包含了查询参数 - targetURL, err := url.Parse(httpAddress + r.URL.Path) + targetURL, err := url.Parse(httpAddress + r.URL.String()) if err != nil { http.Error(w, "Error parsing target URL", http.StatusInternalServerError) return diff --git a/server/home-user-auth/internal/config/config.go b/server/home-user-auth/internal/config/config.go index 9d09c058..c79a4367 100644 --- a/server/home-user-auth/internal/config/config.go +++ b/server/home-user-auth/internal/config/config.go @@ -7,9 +7,9 @@ import ( ) type OAuth struct { - Name string `json:"name"` - Appid string `json:"appid"` - Secret string `json:"secret"` + Name string `yaml:"name"` + Appid string `yaml:"appid"` + Secret string `yaml:"secret"` } type Config struct { diff --git a/server/home-user-auth/internal/handler/routes.go b/server/home-user-auth/internal/handler/routes.go index c0b3b4e7..46030762 100644 --- a/server/home-user-auth/internal/handler/routes.go +++ b/server/home-user-auth/internal/handler/routes.go @@ -68,15 +68,10 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) { Handler: UserOderDeleteHandler(serverCtx), }, { - Method: http.MethodPost, + Method: http.MethodGet, Path: "/api/user/oauth2/login/google", Handler: UserGoogleLoginHandler(serverCtx), }, - { - Method: http.MethodPost, - Path: "/api/user/oauth2/login", - Handler: UserOAuth2LoginHandler(serverCtx), - }, }, ) } diff --git a/server/home-user-auth/internal/handler/usergoogleloginhandler.go b/server/home-user-auth/internal/handler/usergoogleloginhandler.go index 934eebeb..a2daca84 100644 --- a/server/home-user-auth/internal/handler/usergoogleloginhandler.go +++ b/server/home-user-auth/internal/handler/usergoogleloginhandler.go @@ -2,6 +2,7 @@ package handler import ( "errors" + "log" "net/http" "github.com/zeromicro/go-zero/core/logx" @@ -53,6 +54,7 @@ func UserGoogleLoginHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { userinfo = &auth.UserInfo{UserId: 0, GuestId: 0} } + log.Println(r.URL.String(), r.URL.Query()) var req types.RequestGoogleLogin // 如果端点有请求结构体,则使用httpx.Parse方法从HTTP请求体中解析请求数据 if err := httpx.Parse(r, &req); err != nil { diff --git a/server/home-user-auth/internal/logic/usergoogleloginlogic.go b/server/home-user-auth/internal/logic/usergoogleloginlogic.go index ff655fe6..3bbb83c5 100644 --- a/server/home-user-auth/internal/logic/usergoogleloginlogic.go +++ b/server/home-user-auth/internal/logic/usergoogleloginlogic.go @@ -3,13 +3,17 @@ package logic import ( "fusenapi/utils/auth" "fusenapi/utils/basic" + "log" "context" "fusenapi/server/home-user-auth/internal/svc" "fusenapi/server/home-user-auth/internal/types" + "github.com/474420502/requests" "github.com/zeromicro/go-zero/core/logx" + "golang.org/x/oauth2" + "golang.org/x/oauth2/google" ) type UserGoogleLoginLogic struct { @@ -30,5 +34,24 @@ func (l *UserGoogleLoginLogic) UserGoogleLogin(req *types.RequestGoogleLogin, us // 返回值必须调用Set重新返回, resp可以空指针调用 resp.SetStatus(basic.CodeOK, data) // userinfo 传入值时, 一定不为null + var googleOauthConfig = &oauth2.Config{ + RedirectURL: "http://localhost:9900/api/user/oauth2/login/google", + ClientID: l.svcCtx.Config.OAuth[0].Appid, + ClientSecret: l.svcCtx.Config.OAuth[0].Secret, + Scopes: []string{"https://www.googleapis.com/auth/userinfo.email", "https://www.googleapis.com/auth/userinfo.profile"}, + Endpoint: google.Endpoint, + } + + token, err := googleOauthConfig.Exchange(context.Background(), req.Code) + if err != nil { + resp.SetStatus(basic.CodeApiErr) + } + + r, err := requests.Get("https://www.googleapis.com/oauth2/v2/userinfo" + token.AccessToken).Execute() + if err != nil { + panic(err) + } + log.Println(r.Json()) + return resp.SetStatus(basic.CodeOK) } diff --git a/server/home-user-auth/internal/logic/useroauth2loginlogic.go b/server/home-user-auth/internal/logic/useroauth2loginlogic.go index ba11c46c..ac4e189f 100644 --- a/server/home-user-auth/internal/logic/useroauth2loginlogic.go +++ b/server/home-user-auth/internal/logic/useroauth2loginlogic.go @@ -3,17 +3,13 @@ package logic import ( "fusenapi/utils/auth" "fusenapi/utils/basic" - "log" "context" "fusenapi/server/home-user-auth/internal/svc" "fusenapi/server/home-user-auth/internal/types" - "github.com/474420502/requests" "github.com/zeromicro/go-zero/core/logx" - "golang.org/x/oauth2" - "golang.org/x/oauth2/google" ) type UserOAuth2LoginLogic struct { @@ -34,31 +30,6 @@ func NewUserOAuth2LoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *U func (l *UserOAuth2LoginLogic) UserOAuth2Login(req *types.RequestOAuth, userinfo *auth.UserInfo) (resp *basic.Response) { // 返回值必须调用Set重新返回, resp可以空指针调用 resp.SetStatus(basic.CodeOK, data) // userinfo 传入值时, 一定不为null - switch req.Platform { - case "google": - var googleOauthConfig = &oauth2.Config{ - RedirectURL: "https://fusenh5.kayue.cn/api/user/oauth2/login/google", - ClientID: l.svcCtx.Config.OAuth[0].Appid, - ClientSecret: l.svcCtx.Config.OAuth[0].Secret, - Scopes: []string{"https://www.googleapis.com/auth/userinfo.email", "https://www.googleapis.com/auth/userinfo.profile"}, - Endpoint: google.Endpoint, - } - - token, err := googleOauthConfig.Exchange(context.Background(), req.Code) - if err != nil { - resp.SetStatus(basic.CodeApiErr) - } - - resp, err := requests.Get("https://www.googleapis.com/oauth2/v2/userinfo" + token.AccessToken).Execute() - if err != nil { - panic(err) - } - log.Println(resp.Json()) - - case "facebook": - default: - - } return resp.SetStatus(basic.CodeOK) } diff --git a/server/home-user-auth/internal/types/types.go b/server/home-user-auth/internal/types/types.go index 34a18890..5b0a384f 100644 --- a/server/home-user-auth/internal/types/types.go +++ b/server/home-user-auth/internal/types/types.go @@ -6,16 +6,10 @@ import ( ) type RequestGoogleLogin struct { - ID string `json:"id,,optional"` - Email string `json:"email,,optional"` - VerifiedEmail bool `json:"verified_email,,optional"` - Name string `json:"name,,optional"` - GivenName string `json:"given_name,,optional"` - FamilyName string `json:"family_name,,optional"` - Link string `json:"link,,optional"` - Picture string `json:"picture,,optional"` - Gender string `json:"gender,,optional"` - Locale string `json:"locale,,optional"` + Code string `form:"code"` + Scope string `form:"scope"` + AuthUser string `form:"authuser"` + Prompt string `form:"prompt"` } type RequestOAuth struct { diff --git a/server/upload/internal/logic/uploadfilebackendlogic.go b/server/upload/internal/logic/uploadfilebackendlogic.go index 34f74397..77de1f5f 100644 --- a/server/upload/internal/logic/uploadfilebackendlogic.go +++ b/server/upload/internal/logic/uploadfilebackendlogic.go @@ -32,33 +32,53 @@ func NewUploadFileBackendLogic(ctx context.Context, svcCtx *svc.ServiceContext) } } +// UploadFileBackend 这个函数接收一个文件上传请求和用户信息,处理文件上传,并返回响应 func (l *UploadFileBackendLogic) UploadFileBackend(req *types.RequestUploadFileBackend, userinfo *auth.UserInfo) (resp *basic.Response) { + // 返回值必须调用Set重新返回, resp可以空指针调用 resp.SetStatus(basic.CodeOK, data) // userinfo 传入值时, 一定不为null + + // 检查用户是否是旁观者,旁观者没有文件上传权限 if userinfo.IsOnlooker() { + // 如果是,返回未授权的错误码 return resp.SetStatus(basic.CodeUnAuth) } + // 定义用户ID和S3键名格式 var uid int64 var keytype format.TypeFormatS3KeyName + + // 检查用户是否是游客 if userinfo.IsGuest() { + // 如果是,使用游客ID和游客键名格式 uid = userinfo.GuestId keytype = format.TypeS3KeyGuest } else { + // 否则,使用用户ID和用户键名格式 uid = userinfo.UserId keytype = format.TypeS3KeyUser } + // 设置AWS会话的区域 l.svcCtx.AwsSession.Config.Region = aws.String("us-west-1") + + // 创建新的S3服务实例 svc := s3.New(l.svcCtx.AwsSession) + // 检查类别是否合法 if !check.CheckCategory(req.Category) { + // 如果不合法,返回类别错误的错误码 return resp.SetStatus(basic.CodeS3CategoryErr) } + // 定义S3请求和当前时间 var s3req *request.Request now := time.Now() + + // 格式化类别 category := format.TypeCategory(req.Category) + + // 格式化S3对象键名 ObjectKey := aws.String(format.FormatS3KeyName( keytype, uid, @@ -68,7 +88,10 @@ func (l *UploadFileBackendLogic) UploadFileBackend(req *types.RequestUploadFileB req.File.Filename, )) + // 定义存储桶名称 var bucketName *string + + // 根据类别选择存储桶 switch category { case format.TCategoryRenderMegre: bucketName = basic.TempfileBucketName @@ -76,21 +99,32 @@ func (l *UploadFileBackendLogic) UploadFileBackend(req *types.RequestUploadFileB bucketName = basic.StorageBucketName } + // 创建S3对象存储请求 s3req, _ = svc.PutObjectRequest( &s3.PutObjectInput{ Bucket: bucketName, Key: ObjectKey, }, ) + + // 设置请求体为文件数据 s3req.SetBufferBody(req.File.Data) + + // 发送请求 err := s3req.Send() + + // 检查是否有错误 if err != nil { + // 如果有,打印错误并返回错误码 logx.Error(err) return resp.SetStatus(basic.CodeS3PutObjectRequestErr) } + + // 打印请求URL logx.Info(s3req.HTTPRequest.URL.String()) + + // 返回成功的响应和上传URL return resp.SetStatus(basic.CodeOK, map[string]interface{}{ "upload_url": s3req.HTTPRequest.URL.String(), }) - } diff --git a/server_api/home-user-auth.api b/server_api/home-user-auth.api index a74d4b49..36a5e192 100644 --- a/server_api/home-user-auth.api +++ b/server_api/home-user-auth.api @@ -51,24 +51,15 @@ service home-user-auth { post /api/user/order-delete(RequestOrderId) returns (response); @handler UserGoogleLoginHandler - post /api/user/oauth2/login/google(RequestGoogleLogin) returns (response); - - @handler UserOAuth2LoginHandler - post /api/user/oauth2/login(RequestOAuth) returns (response); + get /api/user/oauth2/login/google(RequestGoogleLogin) returns (response); } type RequestGoogleLogin { - ID string `json:"id,,optional"` - Email string `json:"email,,optional"` - VerifiedEmail bool `json:"verified_email,,optional"` - Name string `json:"name,,optional"` - GivenName string `json:"given_name,,optional"` - FamilyName string `json:"family_name,,optional"` - Link string `json:"link,,optional"` - Picture string `json:"picture,,optional"` - Gender string `json:"gender,,optional"` - Locale string `json:"locale,,optional"` + Code string `form:"code"` + Scope string `form:"scope"` + AuthUser string `form:"authuser"` + Prompt string `form:"prompt"` } type RequestOAuth {