This commit is contained in:
eson
2023-07-17 19:43:43 +08:00
parent 73ce1e6db3
commit 4d9f14eb30
13 changed files with 398 additions and 39 deletions

View File

@@ -8,5 +8,13 @@ Auth:
AccessExpire: 2592000
RefreshAfter: 1592000
OAuth:
- name: google
appid: 1064842923358-e94msq2glj6qr4lrva9ts3q8h.apps.googleusercontent.com
secret: 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
- name: facebook
appid: 1095953604597065
secret: b146872550a190d5275b1420c212002e
Stripe:
SK: "sk_test_51IisojHygnIJZeghPVSBhkwySfcyDV4SoAduIxu3J7bvSJ9cZMD96LY1LO6SpdbYquLJX5oKvgEBB67KT9pecfCy00iEC4pp9y"
SK: sk_test_51IisojHygnIJZeghPVSBhkwySfcyDV4SoAduIxu3J7bvSJ9cZMD96LY1LO6SpdbYquLJX5oKvgEBB67KT9pecfCy00iEC4pp9y

View File

@@ -6,11 +6,19 @@ import (
"github.com/zeromicro/go-zero/rest"
)
type OAuth struct {
Name string `json:"name"`
Appid string `json:"appid"`
Secret string `json:"secret"`
}
type Config struct {
rest.RestConf
SourceMysql string
Auth types.Auth
OAuth []OAuth
Stripe struct {
SK string
}

View File

@@ -67,6 +67,16 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
Path: "/api/user/order-delete",
Handler: UserOderDeleteHandler(serverCtx),
},
{
Method: http.MethodPost,
Path: "/api/user/oauth2/login/google",
Handler: UserGoogleLoginHandler(serverCtx),
},
{
Method: http.MethodPost,
Path: "/api/user/oauth2/login",
Handler: UserOAuth2LoginHandler(serverCtx),
},
},
)
}

View File

@@ -0,0 +1,78 @@
package handler
import (
"errors"
"net/http"
"github.com/zeromicro/go-zero/core/logx"
"github.com/zeromicro/go-zero/rest/httpx"
"fusenapi/utils/auth"
"fusenapi/utils/basic"
"fusenapi/server/home-user-auth/internal/logic"
"fusenapi/server/home-user-auth/internal/svc"
"fusenapi/server/home-user-auth/internal/types"
)
func UserGoogleLoginHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var (
// 定义错误变量
err error
// 定义用户信息变量
userinfo *auth.UserInfo
)
// 解析JWT token,并对空用户进行判断
claims, err := svcCtx.ParseJwtToken(r)
// 如果解析JWT token出错,则返回未授权的JSON响应并记录错误消息
if err != nil {
httpx.OkJsonCtx(r.Context(), w, &basic.Response{
Code: 401, // 返回401状态码,表示未授权
Message: "unauthorized", // 返回未授权信息
})
logx.Info("unauthorized:", err.Error()) // 记录错误日志
return
}
if claims != nil {
// 从token中获取对应的用户信息
userinfo, err = auth.GetUserInfoFormMapClaims(claims)
// 如果获取用户信息出错,则返回未授权的JSON响应并记录错误消息
if err != nil {
httpx.OkJsonCtx(r.Context(), w, &basic.Response{
Code: 401,
Message: "unauthorized",
})
logx.Info("unauthorized:", err.Error())
return
}
} else {
// 如果claims为nil,则认为用户身份为白板用户
userinfo = &auth.UserInfo{UserId: 0, GuestId: 0}
}
var req types.RequestGoogleLogin
// 如果端点有请求结构体则使用httpx.Parse方法从HTTP请求体中解析请求数据
if err := httpx.Parse(r, &req); err != nil {
httpx.OkJsonCtx(r.Context(), w, &basic.Response{
Code: 510,
Message: "parameter error",
})
logx.Info(err)
return
}
// 创建一个业务逻辑层实例
l := logic.NewUserGoogleLoginLogic(r.Context(), svcCtx)
resp := l.UserGoogleLogin(&req, userinfo)
// 如果响应不为nil则使用httpx.OkJsonCtx方法返回JSON响应;
if resp != nil {
httpx.OkJsonCtx(r.Context(), w, resp)
} else {
err := errors.New("server logic is error, resp must not be nil")
httpx.ErrorCtx(r.Context(), w, err)
logx.Error(err)
}
}
}

View File

@@ -0,0 +1,78 @@
package handler
import (
"errors"
"net/http"
"github.com/zeromicro/go-zero/core/logx"
"github.com/zeromicro/go-zero/rest/httpx"
"fusenapi/utils/auth"
"fusenapi/utils/basic"
"fusenapi/server/home-user-auth/internal/logic"
"fusenapi/server/home-user-auth/internal/svc"
"fusenapi/server/home-user-auth/internal/types"
)
func UserOAuth2LoginHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var (
// 定义错误变量
err error
// 定义用户信息变量
userinfo *auth.UserInfo
)
// 解析JWT token,并对空用户进行判断
claims, err := svcCtx.ParseJwtToken(r)
// 如果解析JWT token出错,则返回未授权的JSON响应并记录错误消息
if err != nil {
httpx.OkJsonCtx(r.Context(), w, &basic.Response{
Code: 401, // 返回401状态码,表示未授权
Message: "unauthorized", // 返回未授权信息
})
logx.Info("unauthorized:", err.Error()) // 记录错误日志
return
}
if claims != nil {
// 从token中获取对应的用户信息
userinfo, err = auth.GetUserInfoFormMapClaims(claims)
// 如果获取用户信息出错,则返回未授权的JSON响应并记录错误消息
if err != nil {
httpx.OkJsonCtx(r.Context(), w, &basic.Response{
Code: 401,
Message: "unauthorized",
})
logx.Info("unauthorized:", err.Error())
return
}
} else {
// 如果claims为nil,则认为用户身份为白板用户
userinfo = &auth.UserInfo{UserId: 0, GuestId: 0}
}
var req types.RequestOAuth
// 如果端点有请求结构体则使用httpx.Parse方法从HTTP请求体中解析请求数据
if err := httpx.Parse(r, &req); err != nil {
httpx.OkJsonCtx(r.Context(), w, &basic.Response{
Code: 510,
Message: "parameter error",
})
logx.Info(err)
return
}
// 创建一个业务逻辑层实例
l := logic.NewUserOAuth2LoginLogic(r.Context(), svcCtx)
resp := l.UserOAuth2Login(&req, userinfo)
// 如果响应不为nil则使用httpx.OkJsonCtx方法返回JSON响应;
if resp != nil {
httpx.OkJsonCtx(r.Context(), w, resp)
} else {
err := errors.New("server logic is error, resp must not be nil")
httpx.ErrorCtx(r.Context(), w, err)
logx.Error(err)
}
}
}

View File

@@ -0,0 +1,34 @@
package logic
import (
"fusenapi/utils/auth"
"fusenapi/utils/basic"
"context"
"fusenapi/server/home-user-auth/internal/svc"
"fusenapi/server/home-user-auth/internal/types"
"github.com/zeromicro/go-zero/core/logx"
)
type UserGoogleLoginLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewUserGoogleLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserGoogleLoginLogic {
return &UserGoogleLoginLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *UserGoogleLoginLogic) UserGoogleLogin(req *types.RequestGoogleLogin, userinfo *auth.UserInfo) (resp *basic.Response) {
// 返回值必须调用Set重新返回, resp可以空指针调用 resp.SetStatus(basic.CodeOK, data)
// userinfo 传入值时, 一定不为null
return resp.SetStatus(basic.CodeOK)
}

View File

@@ -0,0 +1,64 @@
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 {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewUserOAuth2LoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserOAuth2LoginLogic {
return &UserOAuth2LoginLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
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)
}

View File

@@ -5,6 +5,24 @@ import (
"fusenapi/utils/basic"
)
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"`
}
type RequestOAuth struct {
Platform string `json:"platform"`
Code string `json:"code"`
}
type RequestContactService struct {
Type string `json:"type"` // 类型
RelationID int64 `json:"relation_id"` // 关系id