Merge branch 'feature/auth' of https://gitee.com/fusenpack/fusenapi into feature/auth
This commit is contained in:
commit
abc315a39b
|
@ -25,14 +25,15 @@ func New{{.logic}}(ctx context.Context, svcCtx *svc.ServiceContext) *{{.logic}}
|
|||
// func (l *{{.logic}}) BeforeLogic(w http.ResponseWriter, r *http.Request) {
|
||||
// }
|
||||
|
||||
// 处理逻辑后 w,r 如:重定向, resp 必须重新处理
|
||||
// func (l *{{.logic}}) AfterLogic(w http.ResponseWriter, r *http.Request, resp *basic.Response) {
|
||||
// // httpx.OkJsonCtx(r.Context(), w, resp)
|
||||
// }
|
||||
|
||||
func (l *{{.logic}}) {{.function}}({{.request}}, userinfo *auth.UserInfo) (resp *basic.Response) {
|
||||
// 返回值必须调用Set重新返回, resp可以空指针调用 resp.SetStatus(basic.CodeOK, data)
|
||||
// userinfo 传入值时, 一定不为null
|
||||
|
||||
{{.returnString}} resp.SetStatus(basic.CodeOK)
|
||||
}
|
||||
|
||||
// 处理逻辑后 w,r 如:重定向, resp 必须重新处理
|
||||
// func (l *{{.logic}}) AfterLogic(w http.ResponseWriter, r *http.Request, resp *basic.Response) {
|
||||
// // httpx.OkJsonCtx(r.Context(), w, resp)
|
||||
// }
|
||||
|
||||
|
|
|
@ -27,6 +27,11 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
|
|||
Path: "/api/auth/oauth2/login/google",
|
||||
Handler: UserGoogleLoginHandler(serverCtx),
|
||||
},
|
||||
{
|
||||
Method: http.MethodGet,
|
||||
Path: "/api/auth/email/confirmation",
|
||||
Handler: UserEmailConfirmationHandler(serverCtx),
|
||||
},
|
||||
{
|
||||
Method: http.MethodGet,
|
||||
Path: "/api/auth/oauth2/register",
|
||||
|
|
35
server/auth/internal/handler/useremailconfirmationhandler.go
Normal file
35
server/auth/internal/handler/useremailconfirmationhandler.go
Normal file
|
@ -0,0 +1,35 @@
|
|||
package handler
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"reflect"
|
||||
|
||||
"fusenapi/utils/basic"
|
||||
|
||||
"fusenapi/server/auth/internal/logic"
|
||||
"fusenapi/server/auth/internal/svc"
|
||||
"fusenapi/server/auth/internal/types"
|
||||
)
|
||||
|
||||
func UserEmailConfirmationHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
var req types.RequestEmailConfirmation
|
||||
userinfo, err := basic.RequestParse(w, r, svcCtx.SharedState, &req)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
// 创建一个业务逻辑层实例
|
||||
l := logic.NewUserEmailConfirmationLogic(r.Context(), svcCtx)
|
||||
|
||||
rl := reflect.ValueOf(l)
|
||||
basic.BeforeLogic(w, r, rl)
|
||||
|
||||
resp := l.UserEmailConfirmation(&req, userinfo)
|
||||
|
||||
if !basic.AfterLogic(w, r, rl, resp) {
|
||||
basic.NormalAfterLogic(w, r, resp)
|
||||
}
|
||||
}
|
||||
}
|
43
server/auth/internal/logic/useremailconfirmationlogic.go
Normal file
43
server/auth/internal/logic/useremailconfirmationlogic.go
Normal file
|
@ -0,0 +1,43 @@
|
|||
package logic
|
||||
|
||||
import (
|
||||
"fusenapi/utils/auth"
|
||||
"fusenapi/utils/basic"
|
||||
|
||||
"context"
|
||||
|
||||
"fusenapi/server/auth/internal/svc"
|
||||
"fusenapi/server/auth/internal/types"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type UserEmailConfirmationLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewUserEmailConfirmationLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserEmailConfirmationLogic {
|
||||
return &UserEmailConfirmationLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
// 处理进入前逻辑w,r
|
||||
// func (l *UserEmailConfirmationLogic) BeforeLogic(w http.ResponseWriter, r *http.Request) {
|
||||
// }
|
||||
|
||||
func (l *UserEmailConfirmationLogic) UserEmailConfirmation(req *types.RequestEmailConfirmation, userinfo *auth.UserInfo) (resp *basic.Response) {
|
||||
// 返回值必须调用Set重新返回, resp可以空指针调用 resp.SetStatus(basic.CodeOK, data)
|
||||
// userinfo 传入值时, 一定不为null
|
||||
|
||||
return resp.SetStatus(basic.CodeOK)
|
||||
}
|
||||
|
||||
// 处理逻辑后 w,r 如:重定向, resp 必须重新处理
|
||||
// func (l *UserEmailConfirmationLogic) AfterLogic(w http.ResponseWriter, r *http.Request, resp *basic.Response) {
|
||||
// // httpx.OkJsonCtx(r.Context(), w, resp)
|
||||
// }
|
|
@ -46,6 +46,7 @@ func NewUserGoogleLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *U
|
|||
// func (l *UserGoogleLoginLogic) BeforeLogic(w http.ResponseWriter, r *http.Request) {
|
||||
// }
|
||||
|
||||
<<<<<<< HEAD
|
||||
// 处理逻辑后 w,r 如:重定向, resp 必须重新处理
|
||||
func (l *UserGoogleLoginLogic) AfterLogic(w http.ResponseWriter, r *http.Request, resp *basic.Response) {
|
||||
|
||||
|
@ -89,6 +90,8 @@ func (l *UserGoogleLoginLogic) AfterLogic(w http.ResponseWriter, r *http.Request
|
|||
|
||||
}
|
||||
|
||||
=======
|
||||
>>>>>>> 529a02ac7582babc634e6f9cddab126f5f962efb
|
||||
func (l *UserGoogleLoginLogic) UserGoogleLogin(req *types.RequestGoogleLogin, userinfo *auth.UserInfo) (resp *basic.Response) {
|
||||
// 返回值必须调用Set重新返回, resp可以空指针调用 resp.SetStatus(basic.CodeOK, data)
|
||||
// userinfo 传入值时, 一定不为null
|
||||
|
@ -138,19 +141,22 @@ func (l *UserGoogleLoginLogic) UserGoogleLogin(req *types.RequestGoogleLogin, us
|
|||
return resp.SetStatus(basic.CodeDbSqlErr)
|
||||
}
|
||||
|
||||
l.isRegistered = false
|
||||
l.registerToken = //
|
||||
|
||||
// 进入邮件注册流程
|
||||
if req.Email == "" {
|
||||
return resp.SetStatus(basic.CodeOK)
|
||||
}
|
||||
// if req.Email == "" {
|
||||
// return resp.SetStatus(basic.CodeOK)
|
||||
// }
|
||||
|
||||
// 这里是注册模块, 发邮件, 通过邮件注册确认邮箱存在
|
||||
// // 这里是注册模块, 发邮件, 通过邮件注册确认邮箱存在
|
||||
|
||||
// 邮箱验证格式错误
|
||||
if !auth.ValidateEmail(req.Email) {
|
||||
return resp.SetStatus(basic.CodeOAuthEmailErr)
|
||||
}
|
||||
// // 邮箱验证格式错误
|
||||
// if !auth.ValidateEmail(req.Email) {
|
||||
// return resp.SetStatus(basic.CodeOAuthEmailErr)
|
||||
// }
|
||||
|
||||
EmailManager.EmailTasks <- req.Email // email进入队
|
||||
// EmailManager.EmailTasks <- req.Email // email进入队
|
||||
|
||||
return resp.SetStatus(basic.CodeOK)
|
||||
}
|
||||
|
@ -170,3 +176,54 @@ func (l *UserGoogleLoginLogic) UserGoogleLogin(req *types.RequestGoogleLogin, us
|
|||
|
||||
return resp.SetStatus(basic.CodeOK)
|
||||
}
|
||||
|
||||
// 处理逻辑后 w,r 如:重定向, resp 必须重新处理
|
||||
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)
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -15,7 +15,11 @@ type RequestGoogleLogin struct {
|
|||
Scope string `form:"scope"`
|
||||
AuthUser string `form:"authuser"`
|
||||
Prompt string `form:"prompt"`
|
||||
Email string `form:"email,optional"`
|
||||
}
|
||||
|
||||
type RequestEmailConfirmation struct {
|
||||
Email string `json:"email"` // 要确认的email
|
||||
Token string `json:"token"` // 操作Token
|
||||
}
|
||||
|
||||
type RequestEmailRegister struct {
|
||||
|
|
|
@ -12,13 +12,16 @@ import "basic.api"
|
|||
service auth {
|
||||
@handler UserLoginHandler
|
||||
post /api/auth/login(RequestUserLogin) returns (response);
|
||||
|
||||
|
||||
@handler AcceptCookieHandler
|
||||
post /api/auth/accept-cookie(request) returns (response);
|
||||
|
||||
|
||||
@handler UserGoogleLoginHandler
|
||||
get /api/auth/oauth2/login/google(RequestGoogleLogin) returns (response);
|
||||
|
||||
|
||||
@handler UserEmailConfirmationHandler
|
||||
get /api/auth/email/confirmation(RequestEmailConfirmation) returns (response);
|
||||
|
||||
@handler UserEmailRegisterHandler
|
||||
get /api/auth/oauth2/register(RequestEmailRegister) returns (response);
|
||||
}
|
||||
|
@ -34,7 +37,11 @@ type RequestGoogleLogin {
|
|||
Scope string `form:"scope"`
|
||||
AuthUser string `form:"authuser"`
|
||||
Prompt string `form:"prompt"`
|
||||
Email string `form:"email,optional"`
|
||||
}
|
||||
|
||||
type RequestEmailConfirmation {
|
||||
Email string `json:"email"` // 要确认的email
|
||||
Token string `json:"token"` // 操作Token
|
||||
}
|
||||
|
||||
type RequestEmailRegister {
|
||||
|
|
Loading…
Reference in New Issue
Block a user