fix
This commit is contained in:
		
							parent
							
								
									a10e100364
								
							
						
					
					
						commit
						b43b5dbd59
					
				| @ -19,17 +19,17 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) { | ||||
| 			}, | ||||
| 			{ | ||||
| 				Method:  http.MethodPost, | ||||
| 				Path:    "/api/user/accept-cookie", | ||||
| 				Path:    "/api/auth/accept-cookie", | ||||
| 				Handler: AcceptCookieHandler(serverCtx), | ||||
| 			}, | ||||
| 			{ | ||||
| 				Method:  http.MethodGet, | ||||
| 				Path:    "/api/user/oauth2/login/google", | ||||
| 				Path:    "/api/auth/oauth2/login/google", | ||||
| 				Handler: UserGoogleLoginHandler(serverCtx), | ||||
| 			}, | ||||
| 			{ | ||||
| 				Method:  http.MethodGet, | ||||
| 				Path:    "/api/user/oauth2/login/register", | ||||
| 				Path:    "/api/auth/oauth2/login/register", | ||||
| 				Handler: UserEmailRegisterHandler(serverCtx), | ||||
| 			}, | ||||
| 		}, | ||||
|  | ||||
| @ -38,6 +38,17 @@ func NewAcceptCookieLogic(ctx context.Context, svcCtx *svc.ServiceContext) *Acce | ||||
| func (l *AcceptCookieLogic) AcceptCookie(req *types.Request, userinfo *auth.UserInfo) (resp *basic.Response) { | ||||
| 	// 返回值必须调用Set重新返回, resp可以空指针调用 resp.SetStatus(basic.CodeOK, data) | ||||
| 	// userinfo 传入值时, 一定不为null | ||||
| 	idtyp := userinfo.GetIdType() | ||||
| 	if idtyp == auth.IDTYPE_Guest { | ||||
| 		return resp.SetStatus(basic.CodeGuestDupErr) | ||||
| 	} | ||||
| 
 | ||||
| 	return resp.SetStatus(basic.CodeOK) | ||||
| 	m := l.svcCtx.AllModels.FsGuest | ||||
| 	token, err := m.GenerateGuestID(l.ctx, &l.svcCtx.Config.Auth.AccessSecret) | ||||
| 	if err != nil { | ||||
| 		return resp.SetStatus(basic.CodeGuestGenErr) | ||||
| 	} | ||||
| 	return resp.SetStatus(basic.CodeOK, types.DataGuest{ | ||||
| 		Token: token, | ||||
| 	}) | ||||
| } | ||||
|  | ||||
| @ -49,7 +49,6 @@ func (m *EmailSender) ProcessEmailTasks() { | ||||
| 		} | ||||
| 		m.lock.Unlock() | ||||
| 
 | ||||
| 		// TODO: Replace with actual email content | ||||
| 		content := []byte("Hello, this is a test email") | ||||
| 		err := smtp.SendMail(emailTarget, m.Auth, m.FromEmail, []string{emailTarget}, content) | ||||
| 		if err != nil { | ||||
| @ -1,4 +1,4 @@ | ||||
| package homeuserauthtest | ||||
| package authtest | ||||
| 
 | ||||
| import ( | ||||
| 	"fmt" | ||||
| @ -19,7 +19,7 @@ func TestAcceptCookieLogic(t *testing.T) { | ||||
| 	ses := fstests.GetSesssion() | ||||
| 
 | ||||
| 	// 向服务器发送 GET 请求,获取 cookie 信息 | ||||
| 	resp, err = ses.Post(fmt.Sprintf("http://%s:%d/user/accept-cookie", cnf.Host, cnf.Port)).TestExecute(gserver) | ||||
| 	resp, err = ses.Post(fmt.Sprintf("http://%s:%d/api/auth/accept-cookie", cnf.Host, cnf.Port)).TestExecute(gserver) | ||||
| 	if err != nil { | ||||
| 		t.Error(err) | ||||
| 	} | ||||
							
								
								
									
										40
									
								
								server/auth/test/basic.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										40
									
								
								server/auth/test/basic.go
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,40 @@ | ||||
| package authtest | ||||
| 
 | ||||
| import ( | ||||
| 	"fmt" | ||||
| 	"fusenapi/server/auth/internal/config" | ||||
| 	"fusenapi/server/auth/internal/handler" | ||||
| 	"fusenapi/server/auth/internal/svc" | ||||
| 	"fusenapi/utils/fstests" | ||||
| 	"log" | ||||
| 	"runtime" | ||||
| 
 | ||||
| 	"github.com/zeromicro/go-zero/core/conf" | ||||
| 	"github.com/zeromicro/go-zero/rest" | ||||
| ) | ||||
| 
 | ||||
| var cnf config.Config | ||||
| var gserver *rest.Server | ||||
| 
 | ||||
| func init() { | ||||
| 	log.SetFlags(log.Llongfile) | ||||
| 	gserver = GetTestServer() | ||||
| } | ||||
| 
 | ||||
| func GetTestServer() *rest.Server { | ||||
| 
 | ||||
| 	conf.MustLoad(fstests.GetEtcYamlPathAuto(), &cnf) | ||||
| 
 | ||||
| 	server := rest.MustNewServer(cnf.RestConf) | ||||
| 	runtime.SetFinalizer(server, func(server *rest.Server) { | ||||
| 		if server != nil { | ||||
| 			server.Stop() | ||||
| 		} | ||||
| 	}) | ||||
| 
 | ||||
| 	ctx := svc.NewServiceContext(cnf) | ||||
| 	handler.RegisterHandlers(server, ctx) | ||||
| 
 | ||||
| 	fmt.Printf("Starting server at %s:%d...\n", cnf.Host, cnf.Port) | ||||
| 	return server | ||||
| } | ||||
| @ -2,10 +2,11 @@ package backendtest | ||||
| 
 | ||||
| import ( | ||||
| 	"fmt" | ||||
| 
 | ||||
| 	authtest "fusenapi/server/auth/test" | ||||
| 	"fusenapi/server/backend/internal/config" | ||||
| 	"fusenapi/server/backend/internal/handler" | ||||
| 	"fusenapi/server/backend/internal/svc" | ||||
| 	homeuserauthtest "fusenapi/server/home-user-auth/test" | ||||
| 	"fusenapi/utils/fstests" | ||||
| 	"log" | ||||
| 	"runtime" | ||||
| @ -19,7 +20,7 @@ var userver, gserver *rest.Server | ||||
| 
 | ||||
| func init() { | ||||
| 	log.SetFlags(log.Llongfile) | ||||
| 	userver = homeuserauthtest.GetTestServer() | ||||
| 	userver = authtest.GetTestServer() | ||||
| 	gserver = GetTestServer() | ||||
| } | ||||
| 
 | ||||
|  | ||||
| @ -16,7 +16,7 @@ func TestCaseQuotationDetail(t *testing.T) { | ||||
| 	// } | ||||
| 
 | ||||
| 	// 向服务器发送 POST 请求,新增用户地址 | ||||
| 	tp := ses.Get(fmt.Sprintf("http://%s:%d//quotation/detail", cnf.Host, cnf.Port)) | ||||
| 	tp := ses.Get(fmt.Sprintf("http://%s:%d/api//quotation/detail", cnf.Host, cnf.Port)) | ||||
| 	tp.QueryParam("id").Set(1003) | ||||
| 	resp, err := tp.TestExecute(gserver) | ||||
| 	log.Println(resp.Json()) | ||||
|  | ||||
| @ -1,6 +1,7 @@ | ||||
| Name: home-user-auth | ||||
| Host: 0.0.0.0 | ||||
| Port: 9904 | ||||
| MainAddress: "http://localhost:9900" | ||||
| SourceMysql: fusentest:XErSYmLELKMnf3Dh@tcp(110.41.19.98:3306)/fusentest | ||||
| 
 | ||||
| Auth: | ||||
|  | ||||
| @ -7,6 +7,5 @@ import ( | ||||
| // var configFile = flag.String("f", "etc/home-user-auth.yaml", "the config file") | ||||
| 
 | ||||
| func TestMain(t *testing.T) { | ||||
| 	// log.Println(model.RawFieldNames[FsCanteenType]()) | ||||
| 	main() | ||||
| } | ||||
|  | ||||
| @ -13,18 +13,6 @@ type Config struct { | ||||
| 
 | ||||
| 	MainAddress string | ||||
| 
 | ||||
| 	OAuth struct { | ||||
| 		Google struct { | ||||
| 			Appid  string | ||||
| 			Secret string | ||||
| 		} | ||||
| 
 | ||||
| 		Facebook struct { | ||||
| 			Appid  string | ||||
| 			Secret string | ||||
| 		} | ||||
| 	} | ||||
| 
 | ||||
| 	Stripe struct { | ||||
| 		SK string | ||||
| 	} | ||||
|  | ||||
| @ -1,35 +0,0 @@ | ||||
| package handler | ||||
| 
 | ||||
| import ( | ||||
| 	"net/http" | ||||
| 	"reflect" | ||||
| 
 | ||||
| 	"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 AcceptCookieHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { | ||||
| 	return func(w http.ResponseWriter, r *http.Request) { | ||||
| 
 | ||||
| 		var req types.Request | ||||
| 		userinfo, err := basic.RequestParse(w, r, svcCtx, &req) | ||||
| 		if err != nil { | ||||
| 			return | ||||
| 		} | ||||
| 
 | ||||
| 		// 创建一个业务逻辑层实例 | ||||
| 		l := logic.NewAcceptCookieLogic(r.Context(), svcCtx) | ||||
| 
 | ||||
| 		rl := reflect.ValueOf(l) | ||||
| 		basic.BeforeLogic(w, r, rl) | ||||
| 
 | ||||
| 		resp := l.AcceptCookie(&req, userinfo) | ||||
| 
 | ||||
| 		if !basic.AfterLogic(w, r, rl, resp) { | ||||
| 			basic.NormalAfterLogic(w, r, resp) | ||||
| 		} | ||||
| 	} | ||||
| } | ||||
| @ -1,45 +0,0 @@ | ||||
| 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 AcceptCookieLogic struct { | ||||
| 	logx.Logger | ||||
| 	ctx    context.Context | ||||
| 	svcCtx *svc.ServiceContext | ||||
| } | ||||
| 
 | ||||
| func NewAcceptCookieLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AcceptCookieLogic { | ||||
| 	return &AcceptCookieLogic{ | ||||
| 		Logger: logx.WithContext(ctx), | ||||
| 		ctx:    ctx, | ||||
| 		svcCtx: svcCtx, | ||||
| 	} | ||||
| } | ||||
| 
 | ||||
| func (l *AcceptCookieLogic) AcceptCookie(req *types.Request, userinfo *auth.UserInfo) (resp *basic.Response) { | ||||
| 	// 返回值必须调用Set重新返回, resp可以空指针调用 resp.SetStatus(basic.CodeOK, data) | ||||
| 	// userinfo 传入值时, 一定不为null | ||||
| 	idtyp := userinfo.GetIdType() | ||||
| 	if idtyp == auth.IDTYPE_Guest { | ||||
| 		return resp.SetStatus(basic.CodeGuestDupErr) | ||||
| 	} | ||||
| 
 | ||||
| 	m := l.svcCtx.AllModels.FsGuest | ||||
| 	token, err := m.GenerateGuestID(l.ctx, &l.svcCtx.Config.Auth.AccessSecret) | ||||
| 	if err != nil { | ||||
| 		return resp.SetStatus(basic.CodeGuestGenErr) | ||||
| 	} | ||||
| 	return resp.SetStatus(basic.CodeOK, types.DataGuest{ | ||||
| 		Token: token, | ||||
| 	}) | ||||
| } | ||||
| @ -1,176 +0,0 @@ | ||||
| package logic | ||||
| 
 | ||||
| import ( | ||||
| 	"fmt" | ||||
| 	"fusenapi/utils/auth" | ||||
| 	"fusenapi/utils/basic" | ||||
| 	"log" | ||||
| 	"net/http" | ||||
| 	"time" | ||||
| 
 | ||||
| 	"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" | ||||
| 	"github.com/zeromicro/go-zero/rest/httpx" | ||||
| 	"golang.org/x/net/proxy" | ||||
| 	"golang.org/x/oauth2" | ||||
| 	"golang.org/x/oauth2/google" | ||||
| 	"gorm.io/gorm" | ||||
| ) | ||||
| 
 | ||||
| type UserGoogleLoginLogic struct { | ||||
| 	logx.Logger | ||||
| 	ctx    context.Context | ||||
| 	svcCtx *svc.ServiceContext | ||||
| 
 | ||||
| 	token string // 登录 token | ||||
| 
 | ||||
| 	isRegistered  bool   // 是否注册 | ||||
| 	registerToken string // 注册邮箱的token | ||||
| 	oauthinfo     *auth.OAuthInfo | ||||
| } | ||||
| 
 | ||||
| func NewUserGoogleLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserGoogleLoginLogic { | ||||
| 	return &UserGoogleLoginLogic{ | ||||
| 		Logger: logx.WithContext(ctx), | ||||
| 		ctx:    ctx, | ||||
| 		svcCtx: svcCtx, | ||||
| 	} | ||||
| } | ||||
| 
 | ||||
| // func (l *UserGoogleLoginLogic) BeforeLogic(w http.ResponseWriter, r *http.Request) { | ||||
| // 	log.Println(r, w) | ||||
| // } | ||||
| 
 | ||||
| func (l *UserGoogleLoginLogic) AfterLogic(w http.ResponseWriter, r *http.Request, resp *basic.Response) { | ||||
| 
 | ||||
| 	if resp.Code == 200 { | ||||
| 
 | ||||
| 		if !l.isRegistered { | ||||
| 			now := time.Now() | ||||
| 			rtoken, err := auth.GenerateRegisterToken( | ||||
| 				&l.svcCtx.Config.Auth.AccessSecret, | ||||
| 				l.svcCtx.Config.Auth.AccessExpire, | ||||
| 				now.Unix(), | ||||
| 				l.oauthinfo.Id, | ||||
| 				l.oauthinfo.Platform, | ||||
| 			) | ||||
| 
 | ||||
| 			if err != nil { | ||||
| 				resp.SetStatus(basic.CodeOAuthRegisterTokenErr) | ||||
| 			} | ||||
| 
 | ||||
| 			l.registerToken = rtoken | ||||
| 		} | ||||
| 
 | ||||
| 		rurl := fmt.Sprintf( | ||||
| 			l.svcCtx.Config.MainAddress+"/oauth?token=%s&is_registered=%t®ister_token=%s", | ||||
| 			l.token, | ||||
| 			l.isRegistered, | ||||
| 			l.registerToken, | ||||
| 		) | ||||
| 
 | ||||
| 		html := fmt.Sprintf(` | ||||
| 		<!DOCTYPE html> | ||||
| 		<html> | ||||
| 		<head> | ||||
| 			<title>Redirect</title> | ||||
| 			<script type="text/javascript"> | ||||
| 				window.onload = function() { | ||||
| 					window.location = "%s"; | ||||
| 				} | ||||
| 			</script> | ||||
| 		</head> | ||||
| 		<body> | ||||
| 		</body> | ||||
| 		</html> | ||||
| 		`, rurl) | ||||
| 		fmt.Fprintln(w, html) | ||||
| 	} else { | ||||
| 		httpx.OkJson(w, resp) | ||||
| 	} | ||||
| 
 | ||||
| } | ||||
| 
 | ||||
| func (l *UserGoogleLoginLogic) UserGoogleLogin(req *types.RequestGoogleLogin, userinfo *auth.UserInfo) (resp *basic.Response) { | ||||
| 	// 返回值必须调用Set重新返回, resp可以空指针调用 resp.SetStatus(basic.CodeOK, data) | ||||
| 	// userinfo 传入值时, 一定不为null | ||||
| 
 | ||||
| 	dialer, err := proxy.SOCKS5("tcp", "127.0.0.1:1080", nil, proxy.Direct) | ||||
| 	if err != nil { | ||||
| 		log.Fatal(err) | ||||
| 	} | ||||
| 
 | ||||
| 	customClient := &http.Client{ | ||||
| 		Transport: &http.Transport{ | ||||
| 			Dial: dialer.Dial, | ||||
| 		}, | ||||
| 	} | ||||
| 
 | ||||
| 	ctx := context.WithValue(context.Background(), oauth2.HTTPClient, customClient) | ||||
| 
 | ||||
| 	var googleOauthConfig = &oauth2.Config{ | ||||
| 		RedirectURL:  "http://localhost:9900/api/user/oauth2/login/google", | ||||
| 		ClientID:     l.svcCtx.Config.OAuth.Google.Appid, | ||||
| 		ClientSecret: l.svcCtx.Config.OAuth.Google.Secret, | ||||
| 		Scopes:       []string{"https://www.googleapis.com/auth/userinfo.email", "https://www.googleapis.com/auth/userinfo.profile"}, | ||||
| 		Endpoint:     google.Endpoint, | ||||
| 	} | ||||
| 
 | ||||
| 	token, err := googleOauthConfig.Exchange(ctx, req.Code) | ||||
| 	if err != nil { | ||||
| 		logx.Error(err) | ||||
| 		resp.SetStatus(basic.CodeApiErr) | ||||
| 	} | ||||
| 	ses := requests.NewSession() | ||||
| 	ses.Config().SetProxy("socks5://127.0.0.1:1080") // 代理 为了测试功能 | ||||
| 
 | ||||
| 	r, err := ses.Get("https://www.googleapis.com/oauth2/v2/userinfo?access_token=" + token.AccessToken).Execute() | ||||
| 	if err != nil { | ||||
| 		logx.Error(err) | ||||
| 		return resp.SetStatus(basic.CodeOAuthGoogleApiErr) | ||||
| 	} | ||||
| 
 | ||||
| 	log.Println(r.Json()) | ||||
| 
 | ||||
| 	googleId := r.Json().Get("id").Int() | ||||
| 	user, err := l.svcCtx.AllModels.FsUser.FindUserByGoogleId(context.TODO(), googleId) | ||||
| 	if err != nil { | ||||
| 		if err != gorm.ErrRecordNotFound { | ||||
| 			logx.Error(err) | ||||
| 			return resp.SetStatus(basic.CodeDbSqlErr) | ||||
| 		} | ||||
| 
 | ||||
| 		// 进入邮件注册流程 | ||||
| 		if req.Email == "" { | ||||
| 			return resp.SetStatus(basic.CodeOK) | ||||
| 		} | ||||
| 
 | ||||
| 		// 这里是注册模块, 发邮件, 通过邮件注册确认邮箱存在 | ||||
| 
 | ||||
| 		// 邮箱验证格式错误 | ||||
| 		if !auth.ValidateEmail(req.Email) { | ||||
| 			return resp.SetStatus(basic.CodeOAuthEmailErr) | ||||
| 		} | ||||
| 
 | ||||
| 		return resp.SetStatus(basic.CodeOK) | ||||
| 	} | ||||
| 
 | ||||
| 	// 如果密码匹配,则生成 JWT Token。 | ||||
| 	nowSec := time.Now().Unix() | ||||
| 	jwtToken, err := auth.GenerateJwtToken(&l.svcCtx.Config.Auth.AccessSecret, l.svcCtx.Config.Auth.AccessExpire, nowSec, user.Id, 0) | ||||
| 
 | ||||
| 	// 如果生成 JWT Token 失败,则抛出错误并返回未认证的状态码。 | ||||
| 	if err != nil { | ||||
| 		logx.Error(err) | ||||
| 		return resp.SetStatus(basic.CodeServiceErr) | ||||
| 	} | ||||
| 
 | ||||
| 	l.token = jwtToken | ||||
| 
 | ||||
| 	return resp.SetStatus(basic.CodeOK) | ||||
| } | ||||
| @ -1,84 +0,0 @@ | ||||
| package logic | ||||
| 
 | ||||
| import ( | ||||
| 	"context" | ||||
| 	"errors" | ||||
| 	"fmt" | ||||
| 	"net/http" | ||||
| 	"time" | ||||
| 
 | ||||
| 	"fusenapi/server/home-user-auth/internal/svc" | ||||
| 	"fusenapi/server/home-user-auth/internal/types" | ||||
| 	"fusenapi/utils/auth" | ||||
| 	"fusenapi/utils/basic" | ||||
| 
 | ||||
| 	"github.com/zeromicro/go-zero/core/logx" | ||||
| 	"github.com/zeromicro/go-zero/rest/httpx" | ||||
| 	"gorm.io/gorm" | ||||
| ) | ||||
| 
 | ||||
| type UserLoginLogic struct { | ||||
| 	logx.Logger | ||||
| 	ctx    context.Context | ||||
| 	svcCtx *svc.ServiceContext | ||||
| 
 | ||||
| 	token string | ||||
| } | ||||
| 
 | ||||
| func NewUserLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserLoginLogic { | ||||
| 	return &UserLoginLogic{ | ||||
| 		Logger: logx.WithContext(ctx), | ||||
| 		ctx:    ctx, | ||||
| 		svcCtx: svcCtx, | ||||
| 	} | ||||
| } | ||||
| 
 | ||||
| func (l *UserLoginLogic) AfterLogic(w http.ResponseWriter, r *http.Request, resp *basic.Response) { | ||||
| 	if l.token != "" { | ||||
| 		w.Header().Add("Authorization", fmt.Sprintf("Bearer %s", l.token)) | ||||
| 	} | ||||
| 
 | ||||
| 	httpx.OkJsonCtx(r.Context(), w, resp) | ||||
| } | ||||
| 
 | ||||
| func (l *UserLoginLogic) UserLogin(req *types.RequestUserLogin, userinfo *auth.UserInfo) (resp *basic.Response) { | ||||
| 	// 创建一个 FsUserModel 对象 m 并实例化之,该对象用于操作 MySQL 数据库中的用户数据表。 | ||||
| 	m := l.svcCtx.AllModels.FsUser | ||||
| 
 | ||||
| 	// 在用户数据表中根据登录名(email)查找用户记录,并返回 UserModel 类型的结构体对象 userModel。 | ||||
| 	user, err := m.FindUserByEmail(l.ctx, req.Email) | ||||
| 	if errors.Is(err, gorm.ErrRecordNotFound) { | ||||
| 		return resp.SetStatus(basic.CodeEmailNotFoundErr) | ||||
| 	} | ||||
| 
 | ||||
| 	// 如果在用户数据表中找到了登录名匹配的用户记录,则判断密码是否匹配。 | ||||
| 	if *user.PasswordHash != req.Password { | ||||
| 		logx.Info("密码错误") | ||||
| 		return resp.SetStatus(basic.CodePasswordErr) | ||||
| 	} | ||||
| 
 | ||||
| 	// 如果密码匹配,则生成 JWT Token。 | ||||
| 	nowSec := time.Now().Unix() | ||||
| 	jwtToken, err := auth.GenerateJwtToken(&l.svcCtx.Config.Auth.AccessSecret, l.svcCtx.Config.Auth.AccessExpire, nowSec, user.Id, 0) | ||||
| 
 | ||||
| 	// 如果生成 JWT Token 失败,则抛出错误并返回未认证的状态码。 | ||||
| 	if err != nil { | ||||
| 		logx.Error(err) | ||||
| 		return resp.SetStatus(basic.CodeUnAuth) | ||||
| 	} | ||||
| 
 | ||||
| 	// 如果更新 VerificationToken 字段失败,则返回未认证的状态码。 | ||||
| 	if err != nil { | ||||
| 		return resp.SetStatus(basic.CodeUnAuth) | ||||
| 	} | ||||
| 
 | ||||
| 	// 构造 DataUserLogin 类型的数据对象 data 并设置其属性值为生成的 JWT Token。 | ||||
| 	data := &types.DataUserLogin{ | ||||
| 		Token: jwtToken, | ||||
| 	} | ||||
| 
 | ||||
| 	l.token = jwtToken | ||||
| 
 | ||||
| 	// 返回认证成功的状态码以及数据对象 data 和 JWT Token。 | ||||
| 	return resp.SetStatus(basic.CodeOK, data) | ||||
| } | ||||
| @ -115,14 +115,6 @@ type RequestOrderId struct { | ||||
| 	RefundReason   string `json:"refund_reason"`    //取消原因 | ||||
| } | ||||
| 
 | ||||
| type DataUserLogin struct { | ||||
| 	Token string `json:"token"` // 登录jwt token | ||||
| } | ||||
| 
 | ||||
| type DataGuest struct { | ||||
| 	Token string `json:"token"` // 登录jwt token | ||||
| } | ||||
| 
 | ||||
| type DataUserBasicInfo struct { | ||||
| 	Type                 int64 `json:"type"`                   // 1普通餐厅 2连锁餐厅 | ||||
| 	IsOrderStatusEmail   int64 `json:"is_order_status_email"`  // 订单状态改变时是否接收邮件 | ||||
|  | ||||
| @ -2,6 +2,7 @@ package homeuserauthtest | ||||
| 
 | ||||
| import ( | ||||
| 	"fmt" | ||||
| 	authtest "fusenapi/server/auth/test" | ||||
| 	"fusenapi/server/home-user-auth/internal/config" | ||||
| 	"fusenapi/server/home-user-auth/internal/handler" | ||||
| 	"fusenapi/server/home-user-auth/internal/svc" | ||||
| @ -15,10 +16,12 @@ import ( | ||||
| ) | ||||
| 
 | ||||
| var cnf config.Config | ||||
| var userserver *rest.Server | ||||
| var gserver *rest.Server | ||||
| 
 | ||||
| func init() { | ||||
| 	log.SetFlags(log.Llongfile) | ||||
| 	userserver = authtest.GetTestServer() | ||||
| 	gserver = GetTestServer() | ||||
| } | ||||
| 
 | ||||
|  | ||||
| @ -16,7 +16,7 @@ func TestCaseUserAddAddress(t *testing.T) { | ||||
| 	var result gjson.Result | ||||
| 
 | ||||
| 	// 获取 session,并携带 JWT token | ||||
| 	ses := fstests.GetSessionWithUserToken(t, gserver, cnf.Host, cnf.Port) | ||||
| 	ses := fstests.GetSessionWithUserToken(t, userserver, cnf.Host, cnf.Port) | ||||
| 
 | ||||
| 	// 构建新增地址请求体 | ||||
| 	addrReq := types.RequestAddAddress{ | ||||
| @ -32,7 +32,7 @@ func TestCaseUserAddAddress(t *testing.T) { | ||||
| 	} | ||||
| 
 | ||||
| 	// 向服务器发送 POST 请求,新增用户地址 | ||||
| 	tp := ses.Post(fmt.Sprintf("http://%s:%d/user/add-address", cnf.Host, cnf.Port)) | ||||
| 	tp := ses.Post(fmt.Sprintf("http://%s:%d/api/user/add-address", cnf.Host, cnf.Port)) | ||||
| 	tp.SetBodyJson(addrReq) | ||||
| 	resp, err = tp.TestExecute(gserver) | ||||
| 	if err != nil { | ||||
| @ -85,7 +85,7 @@ func TestCaseUserAddAddress(t *testing.T) { | ||||
| 	} | ||||
| 
 | ||||
| 	// 向服务器发送 POST 请求,新增用户地址 | ||||
| 	tp = ses.Post(fmt.Sprintf("http://%s:%d/user/add-address", cnf.Host, cnf.Port)) | ||||
| 	tp = ses.Post(fmt.Sprintf("http://%s:%d/api/user/add-address", cnf.Host, cnf.Port)) | ||||
| 	tp.SetBodyJson(addrReq) | ||||
| 	resp, err = tp.TestExecute(gserver) | ||||
| 	if err != nil { | ||||
|  | ||||
| @ -15,10 +15,10 @@ func TestCaseAddressList(t *testing.T) { | ||||
| 	var result gjson.Result | ||||
| 
 | ||||
| 	// 获取 session,并携带 JWT token | ||||
| 	ses := fstests.GetSessionWithUserToken(t, gserver, cnf.Host, cnf.Port) | ||||
| 	ses := fstests.GetSessionWithUserToken(t, userserver, cnf.Host, cnf.Port) | ||||
| 
 | ||||
| 	// 向服务器发送 GET 请求,获取用户地址列表 | ||||
| 	resp, err = ses.Get(fmt.Sprintf("http://%s:%d/user/address-list", cnf.Host, cnf.Port)).TestExecute(gserver) | ||||
| 	resp, err = ses.Get(fmt.Sprintf("http://%s:%d/api/user/address-list", cnf.Host, cnf.Port)).TestExecute(gserver) | ||||
| 	if err != nil { | ||||
| 		t.Error(err) | ||||
| 	} | ||||
|  | ||||
| @ -15,10 +15,10 @@ func TestCaseBasicInfoLogic(t *testing.T) { | ||||
| 	var result gjson.Result | ||||
| 
 | ||||
| 	// 获取 session,并携带 JWT token | ||||
| 	ses := fstests.GetSessionWithUserToken(t, gserver, cnf.Host, cnf.Port) | ||||
| 	ses := fstests.GetSessionWithUserToken(t, userserver, cnf.Host, cnf.Port) | ||||
| 
 | ||||
| 	// 向服务器发送 GET 请求,获取用户基本信息 | ||||
| 	resp, err = ses.Get(fmt.Sprintf("http://%s:%d/user/basic-info", cnf.Host, cnf.Port)).TestExecute(gserver) | ||||
| 	resp, err = ses.Get(fmt.Sprintf("http://%s:%d/api/user/basic-info", cnf.Host, cnf.Port)).TestExecute(gserver) | ||||
| 	if err != nil { | ||||
| 		t.Error(err) | ||||
| 	} | ||||
|  | ||||
| @ -16,8 +16,8 @@ func TestUserContactService(t *testing.T) { | ||||
| 	var result gjson.Result | ||||
| 
 | ||||
| 	// 获取 session,并携带 JWT token | ||||
| 	ses := fstests.GetSessionWithUserToken(t, gserver, cnf.Host, cnf.Port) | ||||
| 	tp := ses.Post(fmt.Sprintf("http://%s:%d/user/contact-service", cnf.Host, cnf.Port)) | ||||
| 	ses := fstests.GetSessionWithUserToken(t, userserver, cnf.Host, cnf.Port) | ||||
| 	tp := ses.Post(fmt.Sprintf("http://%s:%d/api/user/contact-service", cnf.Host, cnf.Port)) | ||||
| 	req := types.RequestContactService{Type: "order", RelationID: 481, Email: "9107058@qq.com", Name: "zhang"} | ||||
| 	tp.SetBodyJson(req) | ||||
| 	// 向服务器发送 GET 请求,获取用户基本信息 | ||||
|  | ||||
| @ -16,10 +16,10 @@ func TestCaseUserFontsLogic(t *testing.T) { | ||||
| 	var result gjson.Result | ||||
| 
 | ||||
| 	// 获取 session,并携带 JWT token | ||||
| 	ses := fstests.GetSessionWithUserToken(t, gserver, cnf.Host, cnf.Port) | ||||
| 	ses := fstests.GetSessionWithUserToken(t, userserver, cnf.Host, cnf.Port) | ||||
| 
 | ||||
| 	// 向服务器发送 GET 请求,获取字体列表 | ||||
| 	resp, err = ses.Get(fmt.Sprintf("http://%s:%d/user/fonts", cnf.Host, cnf.Port)).TestExecute(gserver) | ||||
| 	resp, err = ses.Get(fmt.Sprintf("http://%s:%d/api/user/fonts", cnf.Host, cnf.Port)).TestExecute(gserver) | ||||
| 	if err != nil { | ||||
| 		t.Error(err) | ||||
| 	} | ||||
|  | ||||
| @ -15,10 +15,10 @@ func TestCaseGetTypeLogic(t *testing.T) { | ||||
| 	var result gjson.Result | ||||
| 
 | ||||
| 	// 获取 session,并携带 JWT token | ||||
| 	ses := fstests.GetSessionWithUserToken(t, gserver, cnf.Host, cnf.Port) | ||||
| 	ses := fstests.GetSessionWithUserToken(t, userserver, cnf.Host, cnf.Port) | ||||
| 
 | ||||
| 	// 向服务器发送 GET 请求,获取用户类型信息 | ||||
| 	resp, err = ses.Get(fmt.Sprintf("http://%s:%d/user/get-type", cnf.Host, cnf.Port)).TestExecute(gserver) | ||||
| 	resp, err = ses.Get(fmt.Sprintf("http://%s:%d/api/user/get-type", cnf.Host, cnf.Port)).TestExecute(gserver) | ||||
| 	if err != nil { | ||||
| 		t.Error(err) | ||||
| 	} | ||||
|  | ||||
| @ -16,10 +16,10 @@ func TestCaseLogic(t *testing.T) { | ||||
| 	var result gjson.Result | ||||
| 
 | ||||
| 	// 获取 session,并携带 JWT token | ||||
| 	ses := fstests.GetSessionWithUserToken(t, gserver, cnf.Host, cnf.Port) | ||||
| 	ses := fstests.GetSessionWithUserToken(t, userserver, cnf.Host, cnf.Port) | ||||
| 
 | ||||
| 	// 向服务器发送 GET 请求,获取用户类型信息 | ||||
| 	tp := ses.Post(fmt.Sprintf("http://%s:%d/user/order-delete", cnf.Host, cnf.Port)) | ||||
| 	tp := ses.Post(fmt.Sprintf("http://%s:%d/api/user/order-delete", cnf.Host, cnf.Port)) | ||||
| 	reqO := types.RequestOrderId{ | ||||
| 		OrderId: 12, | ||||
| 	} | ||||
|  | ||||
| @ -17,10 +17,10 @@ func TestCaseUserSaveBasicinfoLogic(t *testing.T) { | ||||
| 	var result gjson.Result | ||||
| 
 | ||||
| 	// 获取 session,并携带 JWT token | ||||
| 	ses := fstests.GetSessionWithUserToken(t, gserver, cnf.Host, cnf.Port) | ||||
| 	ses := fstests.GetSessionWithUserToken(t, userserver, cnf.Host, cnf.Port) | ||||
| 	var tp *requests.Temporary | ||||
| 
 | ||||
| 	tp = ses.Get(fmt.Sprintf("http://%s:%d/user/basic-info", cnf.Host, cnf.Port)) | ||||
| 	tp = ses.Get(fmt.Sprintf("http://%s:%d/api/user/basic-info", cnf.Host, cnf.Port)) | ||||
| 	resp, err = tp.TestExecute(gserver) | ||||
| 	if err != nil { | ||||
| 		t.Error(err) | ||||
| @ -52,7 +52,7 @@ func TestCaseUserSaveBasicinfoLogic(t *testing.T) { | ||||
| 	req.IsRemoveBg = ^req.IsRemoveBg + 2 | ||||
| 
 | ||||
| 	// 向服务器发送 GET 请求,获取用户类型信息 | ||||
| 	tp = ses.Post(fmt.Sprintf("http://%s:%d/user/basic-info", cnf.Host, cnf.Port)).SetBodyJson(req) | ||||
| 	tp = ses.Post(fmt.Sprintf("http://%s:%d/api/user/basic-info", cnf.Host, cnf.Port)).SetBodyJson(req) | ||||
| 	resp, err = tp.TestExecute(gserver) | ||||
| 	if err != nil { | ||||
| 		t.Error(err) | ||||
| @ -69,7 +69,7 @@ func TestCaseUserSaveBasicinfoLogic(t *testing.T) { | ||||
| 		return | ||||
| 	} | ||||
| 
 | ||||
| 	tp = ses.Get(fmt.Sprintf("http://%s:%d/user/basic-info", cnf.Host, cnf.Port)) | ||||
| 	tp = ses.Get(fmt.Sprintf("http://%s:%d/api/user/basic-info", cnf.Host, cnf.Port)) | ||||
| 	resp, err = tp.TestExecute(gserver) | ||||
| 	if err != nil { | ||||
| 		t.Error(err) | ||||
|  | ||||
| @ -15,10 +15,10 @@ func TestCaseUserStatusConfigLogic(t *testing.T) { | ||||
| 	var result gjson.Result | ||||
| 
 | ||||
| 	// 获取 session,并携带 JWT token | ||||
| 	ses := fstests.GetSessionWithUserToken(t, gserver, cnf.Host, cnf.Port) | ||||
| 	ses := fstests.GetSessionWithUserToken(t, userserver, cnf.Host, cnf.Port) | ||||
| 
 | ||||
| 	// 向服务器发送 GET 请求,获取用户类型信息 | ||||
| 	resp, err = ses.Post(fmt.Sprintf("http://%s:%d/user/status-config", cnf.Host, cnf.Port)).TestExecute(gserver) | ||||
| 	resp, err = ses.Post(fmt.Sprintf("http://%s:%d/api/user/status-config", cnf.Host, cnf.Port)).TestExecute(gserver) | ||||
| 	if err != nil { | ||||
| 		t.Error(err) | ||||
| 	} | ||||
|  | ||||
| @ -2,7 +2,8 @@ package test | ||||
| 
 | ||||
| import ( | ||||
| 	"fmt" | ||||
| 	homeuserauthtest "fusenapi/server/home-user-auth/test" | ||||
| 
 | ||||
| 	authtest "fusenapi/server/auth/test" | ||||
| 	"fusenapi/server/upload/internal/config" | ||||
| 	"fusenapi/server/upload/internal/handler" | ||||
| 	"fusenapi/server/upload/internal/svc" | ||||
| @ -20,7 +21,7 @@ var userver, gserver *rest.Server | ||||
| 
 | ||||
| func init() { | ||||
| 	log.SetFlags(log.Llongfile) | ||||
| 	userver = homeuserauthtest.GetTestServer() | ||||
| 	userver = authtest.GetTestServer() | ||||
| 	gserver = GetTestServer() | ||||
| } | ||||
| 
 | ||||
|  | ||||
| @ -19,7 +19,7 @@ func TestCaseRenderMegre(t *testing.T) { | ||||
| 
 | ||||
| 	// 获取 session,并携带 JWT token | ||||
| 	ses := fstests.GetSessionWithUserToken(t, userver, cnf.Host, cnf.Port) | ||||
| 	tp := ses.Post(fmt.Sprintf("http://%s:%d/upload/upload-file-backend", cnf.Host, cnf.Port)) | ||||
| 	tp := ses.Post(fmt.Sprintf("http://%s:%d/api/upload/upload-file-backend", cnf.Host, cnf.Port)) | ||||
| 
 | ||||
| 	mp := tp.CreateBodyMultipart() | ||||
| 	f, err := os.Open("./fusen.webp") | ||||
|  | ||||
| @ -20,7 +20,7 @@ func TestCasePersonalization(t *testing.T) { | ||||
| 
 | ||||
| 	// 获取 session,并携带 JWT token | ||||
| 	ses := fstests.GetSessionWithUserToken(t, userver, cnf.Host, cnf.Port) | ||||
| 	tp := ses.Post(fmt.Sprintf("http://%s:%d/upload/upload-file-frontend", cnf.Host, cnf.Port)) | ||||
| 	tp := ses.Post(fmt.Sprintf("http://%s:%d/api/upload/upload-file-frontend", cnf.Host, cnf.Port)) | ||||
| 
 | ||||
| 	data, err := ioutil.ReadFile("./fusen.webp") | ||||
| 	if err != nil { | ||||
| @ -75,7 +75,7 @@ func TestCaseRenderMegreF(t *testing.T) { | ||||
| 
 | ||||
| 	// 获取 session,并携带 JWT token | ||||
| 	ses := fstests.GetSessionWithUserToken(t, userver, cnf.Host, cnf.Port) | ||||
| 	tp := ses.Post(fmt.Sprintf("http://%s:%d/upload/upload-file-frontend", cnf.Host, cnf.Port)) | ||||
| 	tp := ses.Post(fmt.Sprintf("http://%s:%d/api/upload/upload-file-frontend", cnf.Host, cnf.Port)) | ||||
| 
 | ||||
| 	data, err := ioutil.ReadFile("./fusen.webp") | ||||
| 	if err != nil { | ||||
|  | ||||
| @ -2,7 +2,8 @@ package webset_test | ||||
| 
 | ||||
| import ( | ||||
| 	"fmt" | ||||
| 	homeuserauthtest "fusenapi/server/home-user-auth/test" | ||||
| 
 | ||||
| 	authtest "fusenapi/server/auth/test" | ||||
| 	"fusenapi/server/webset/internal/config" | ||||
| 	"fusenapi/server/webset/internal/handler" | ||||
| 	"fusenapi/server/webset/internal/svc" | ||||
| @ -19,7 +20,7 @@ var userver, gserver *rest.Server | ||||
| 
 | ||||
| func init() { | ||||
| 	log.SetFlags(log.Llongfile) | ||||
| 	userver = homeuserauthtest.GetTestServer() | ||||
| 	userver = authtest.GetTestServer() | ||||
| 	gserver = GetTestServer() | ||||
| } | ||||
| 
 | ||||
|  | ||||
| @ -20,7 +20,7 @@ func TestWebSetLogic(t *testing.T) { | ||||
| 
 | ||||
| 	// 获取 session,并携带 JWT token | ||||
| 	ses := fstests.GetSesssion() | ||||
| 	tp := ses.Get(fmt.Sprintf("http://%s:%d/web-set/setting", cnf.Host, cnf.Port)) | ||||
| 	tp := ses.Get(fmt.Sprintf("http://%s:%d/api/web-set/setting", cnf.Host, cnf.Port)) | ||||
| 
 | ||||
| 	for _, tType := range testTypes { | ||||
| 		tp.QueryParam("type").Set(tType) | ||||
|  | ||||
| @ -14,13 +14,13 @@ service auth { | ||||
| 	post /api/auth/login(RequestUserLogin) returns (response); | ||||
| 	 | ||||
| 	@handler AcceptCookieHandler | ||||
| 	post /api/user/accept-cookie(request) returns (response); | ||||
| 	post /api/auth/accept-cookie(request) returns (response); | ||||
| 	 | ||||
| 	@handler UserGoogleLoginHandler | ||||
| 	get /api/user/oauth2/login/google(RequestGoogleLogin) returns (response); | ||||
| 	get /api/auth/oauth2/login/google(RequestGoogleLogin) returns (response); | ||||
| 	 | ||||
| 	@handler UserEmailRegisterHandler | ||||
| 	get /api/user/oauth2/login/register(RequestEmailRegister) returns (response); | ||||
| 	get /api/auth/oauth2/login/register(RequestEmailRegister) returns (response); | ||||
| } | ||||
| 
 | ||||
| // UserAddAddressHandler 用户登录请求结构 | ||||
|  | ||||
| @ -173,8 +173,6 @@ type RequestOrderId { | ||||
| 	RefundReason   string `json:"refund_reason"`    //取消原因 | ||||
| } | ||||
| 
 | ||||
| 
 | ||||
| 
 | ||||
| // UserBasicInfoHandler 返回data结构 | ||||
| type DataUserBasicInfo { | ||||
| 	Type                 int64 `json:"type"`                   // 1普通餐厅 2连锁餐厅 | ||||
|  | ||||
| @ -85,10 +85,10 @@ func GetSesssion() *requests.Session { | ||||
| 
 | ||||
| func GetSessionWithUserToken(t *testing.T, server requests.ITestServer, Host string, Port int) *requests.Session { | ||||
| 	ses := requests.NewSession() | ||||
| 	tp := ses.Post(fmt.Sprintf("http://%s:%d/user/login", Host, Port)) | ||||
| 	tp := ses.Post(fmt.Sprintf("http://%s:%d/api/auth/login", Host, Port)) | ||||
| 	tp.SetBodyJson(map[string]interface{}{ | ||||
| 		"name": "9107058@qq.com", | ||||
| 		"pwd":  "$2y$13$2y4O4OIz/zcK5C0vlSc9LuSpjWySjInLBSe49yDkE.iURb.R1hDsy", | ||||
| 		"email":    "9107058@qq.com", | ||||
| 		"password": "t1I0hOs0/AmhZfNk9ZiNnd3YZJpI+LL6COnHAmYEJk4=", | ||||
| 	}) | ||||
| 	resp, err := tp.TestExecute(server) | ||||
| 	if err != nil { | ||||
| @ -104,16 +104,16 @@ func GetSessionWithUserToken(t *testing.T, server requests.ITestServer, Host str | ||||
| 	if !token.Exists() { | ||||
| 		t.Error("data.token is not exists") | ||||
| 	} | ||||
| 	ses.Header.Add("Authorization", token.String()) | ||||
| 	ses.Header.Add("Authorization", fmt.Sprintf("Bearer %s", token.String())) | ||||
| 
 | ||||
| 	return ses | ||||
| } | ||||
| 
 | ||||
| func GetBackendSessionWithUserToken(t *testing.T, server requests.ITestServer, Host string, Port int) *requests.Session { | ||||
| 	ses := requests.NewSession() | ||||
| 	tp := ses.Post(fmt.Sprintf("http://%s:%d/backend-user/login", Host, Port)) | ||||
| 	tp := ses.Post(fmt.Sprintf("http://%s:%d/api/backend-user/login", Host, Port)) | ||||
| 	tp.SetBodyJson(map[string]interface{}{ | ||||
| 		"name": "admin@admin.com", | ||||
| 		"email": "admin@admin.com", | ||||
| 		"pwd":   "ZnVzZW5fYmFja2VuZF8yMDIz47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU=", | ||||
| 	}) | ||||
| 	resp, err := tp.TestExecute(server) | ||||
| @ -130,14 +130,14 @@ func GetBackendSessionWithUserToken(t *testing.T, server requests.ITestServer, H | ||||
| 	if !token.Exists() { | ||||
| 		t.Error("data.token is not exists") | ||||
| 	} | ||||
| 	ses.Header.Add("Authorization", token.String()) | ||||
| 	ses.Header.Add("Authorization", fmt.Sprintf("Bearer %s", token.String())) | ||||
| 
 | ||||
| 	return ses | ||||
| } | ||||
| 
 | ||||
| func GetSesssionWithGuestToken(t *testing.T, server requests.ITestServer, Host string, Port int) *requests.Session { | ||||
| 	ses := requests.NewSession() | ||||
| 	tp := ses.Post(fmt.Sprintf("http://%s:%d/accept/cookie", Host, Port)) | ||||
| 	tp := ses.Post(fmt.Sprintf("http://%s:%d/api/auth/accept/cookie", Host, Port)) | ||||
| 
 | ||||
| 	resp, err := tp.TestExecute(server) | ||||
| 	if err != nil { | ||||
| @ -148,7 +148,7 @@ func GetSesssionWithGuestToken(t *testing.T, server requests.ITestServer, Host s | ||||
| 	if !token.Exists() { | ||||
| 		t.Error("data.token is not exists") | ||||
| 	} | ||||
| 	ses.Header.Add("Authorization", token.String()) | ||||
| 	ses.Header.Add("Authorization", fmt.Sprintf("Bearer %s", token.String())) | ||||
| 
 | ||||
| 	return ses | ||||
| } | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user