| @@ -1,6 +1,8 @@ | |||||
| package wechat | package wechat | ||||
| import "code.gitea.io/gitea/models" | |||||
| import ( | |||||
| "code.gitea.io/gitea/models" | |||||
| ) | |||||
| func BindWechat(userId int64, wechatOpenId string) error { | func BindWechat(userId int64, wechatOpenId string) error { | ||||
| return models.BindWechatOpenId(userId, wechatOpenId) | return models.BindWechatOpenId(userId, wechatOpenId) | ||||
| @@ -9,3 +11,13 @@ func BindWechat(userId int64, wechatOpenId string) error { | |||||
| func UnbindWechat(userId int64) error { | func UnbindWechat(userId int64) error { | ||||
| return models.UnbindWechatOpenId(userId) | return models.UnbindWechatOpenId(userId) | ||||
| } | } | ||||
| func IsUserFinishBind(sceneStr string) bool { | |||||
| //val, _ := redis_client.Get(redis_key.WechatBindingUserIdKey(sceneStr)) | |||||
| //if val == "" { | |||||
| // return false | |||||
| //} | |||||
| //userId, _ := strconv.ParseInt(val, 10, 64) | |||||
| return false | |||||
| } | |||||
| @@ -21,12 +21,13 @@ import ( | |||||
| // ToggleOptions contains required or check options | // ToggleOptions contains required or check options | ||||
| type ToggleOptions struct { | type ToggleOptions struct { | ||||
| SignInRequired bool | |||||
| SignOutRequired bool | |||||
| AdminRequired bool | |||||
| DisableCSRF bool | |||||
| BasicAuthRequired bool | |||||
| OperationRequired bool | |||||
| SignInRequired bool | |||||
| SignOutRequired bool | |||||
| AdminRequired bool | |||||
| DisableCSRF bool | |||||
| BasicAuthRequired bool | |||||
| OperationRequired bool | |||||
| WechatAuthRequired bool | |||||
| } | } | ||||
| // Toggle returns toggle options as middleware | // Toggle returns toggle options as middleware | ||||
| @@ -126,6 +127,18 @@ func Toggle(options *ToggleOptions) macaron.Handler { | |||||
| } | } | ||||
| } | } | ||||
| if options.WechatAuthRequired { | |||||
| if !ctx.IsSigned { | |||||
| ctx.SetCookie("redirect_to", setting.AppSubURL+ctx.Req.URL.RequestURI(), 0, setting.AppSubURL) | |||||
| ctx.Redirect(setting.AppSubURL + "/user/login") | |||||
| return | |||||
| } | |||||
| if ctx.User.WechatOpenId == "" { | |||||
| ctx.SetCookie("redirect_to", setting.AppSubURL+ctx.Req.URL.RequestURI(), 0, setting.AppSubURL) | |||||
| ctx.Redirect(setting.AppSubURL + "/explore/users") | |||||
| } | |||||
| } | |||||
| // Redirect to log in page if auto-signin info is provided and has not signed in. | // Redirect to log in page if auto-signin info is provided and has not signed in. | ||||
| if !options.SignOutRequired && !ctx.IsSigned && !auth.IsAPIPath(ctx.Req.URL.Path) && | if !options.SignOutRequired && !ctx.IsSigned && !auth.IsAPIPath(ctx.Req.URL.Path) && | ||||
| len(ctx.GetCookie(setting.CookieUserName)) > 0 { | len(ctx.GetCookie(setting.CookieUserName)) > 0 { | ||||
| @@ -997,7 +997,7 @@ func RegisterRoutes(m *macaron.Macaron) { | |||||
| m.Get("/search", repo.TopicSearch) | m.Get("/search", repo.TopicSearch) | ||||
| }) | }) | ||||
| m.Group("/from_wechat", func() { | m.Group("/from_wechat", func() { | ||||
| m.Get("/event", authentication.AcceptWechatEvent) | |||||
| m.Get("/event", authentication.ValidEventSource) | |||||
| m.Post("/event", authentication.AcceptWechatEvent) | m.Post("/event", authentication.AcceptWechatEvent) | ||||
| }) | }) | ||||
| }, securityHeaders(), context.APIContexter(), sudo()) | }, securityHeaders(), context.APIContexter(), sudo()) | ||||
| @@ -40,6 +40,42 @@ func GetQRCode4Bind(ctx *context.Context) { | |||||
| }) | }) | ||||
| } | } | ||||
| // GetQRCode4Bind get QR code for wechat binding | |||||
| func GetBindStatus(ctx *context.Context) { | |||||
| //var status int | |||||
| //sceneStr := ctx.Query("sceneStr") | |||||
| //val, _ := redis_client.Get(redis_key.WechatBindingUserIdKey(sceneStr)) | |||||
| //if val == "" { | |||||
| // ctx.JSON(200, map[string]interface{}{ | |||||
| // "code": "9999", | |||||
| // "msg": "Get QR code failed", | |||||
| // "data": | |||||
| // //todo 继续完善查询接口,注意性能 | |||||
| // //todo 二维码重定向页面需要给一下 | |||||
| // //todo 微信推送Ng转发验证 | |||||
| // //todo 整体联调 | |||||
| // }) | |||||
| // return | |||||
| //} | |||||
| // | |||||
| //userId, _ := strconv.ParseInt(val, 10, 64) | |||||
| // | |||||
| //r, err := createQRCode4Bind(userId) | |||||
| //if err != nil { | |||||
| // ctx.JSON(200, map[string]interface{}{ | |||||
| // "code": "9999", | |||||
| // "msg": "Get QR code failed", | |||||
| // }) | |||||
| // return | |||||
| //} | |||||
| ctx.JSON(200, map[string]interface{}{ | |||||
| "code": "00", | |||||
| "msg": "success", | |||||
| //"data": r, | |||||
| }) | |||||
| } | |||||
| func createQRCode4Bind(userId int64) (*QRCodeResponse, error) { | func createQRCode4Bind(userId int64) (*QRCodeResponse, error) { | ||||
| sceneStr := gouuid.NewV4().String() | sceneStr := gouuid.NewV4().String() | ||||
| r := wechat.GetWechatQRCode4Bind(sceneStr) | r := wechat.GetWechatQRCode4Bind(sceneStr) | ||||
| @@ -63,6 +63,12 @@ func AcceptWechatEvent(ctx *context.Context) { | |||||
| MsgType: "text", | MsgType: "text", | ||||
| Content: "启智账号认证微信成功", | Content: "启智账号认证微信成功", | ||||
| } | } | ||||
| redis_client.Del(key) | |||||
| ctx.XML(200, reply) | ctx.XML(200, reply) | ||||
| } | } | ||||
| // ValidEventSource | |||||
| func ValidEventSource(ctx *context.Context) { | |||||
| echostr := ctx.Query("echostr") | |||||
| ctx.Write([]byte(echostr)) | |||||
| return | |||||
| } | |||||
| @@ -275,6 +275,7 @@ func RegisterRoutes(m *macaron.Macaron) { | |||||
| ignSignInAndCsrf := context.Toggle(&context.ToggleOptions{DisableCSRF: true}) | ignSignInAndCsrf := context.Toggle(&context.ToggleOptions{DisableCSRF: true}) | ||||
| reqSignOut := context.Toggle(&context.ToggleOptions{SignOutRequired: true}) | reqSignOut := context.Toggle(&context.ToggleOptions{SignOutRequired: true}) | ||||
| reqBasicAuth := context.Toggle(&context.ToggleOptions{BasicAuthRequired: true, DisableCSRF: true}) | reqBasicAuth := context.Toggle(&context.ToggleOptions{BasicAuthRequired: true, DisableCSRF: true}) | ||||
| reqWechatBind := context.Toggle(&context.ToggleOptions{WechatAuthRequired: true}) | |||||
| bindIgnErr := binding.BindIgnErr | bindIgnErr := binding.BindIgnErr | ||||
| validation.AddBindingRules() | validation.AddBindingRules() | ||||
| @@ -394,7 +395,7 @@ func RegisterRoutes(m *macaron.Macaron) { | |||||
| m.Group("/authentication/wechat", func() { | m.Group("/authentication/wechat", func() { | ||||
| m.Get("/qrCode4Bind", authentication.GetQRCode4Bind) | m.Get("/qrCode4Bind", authentication.GetQRCode4Bind) | ||||
| m.Post("/grant", bindIgnErr(auth.GrantApplicationForm{}), user.GrantApplicationOAuth) | |||||
| m.Get("/bindStatus", authentication.GetBindStatus) | |||||
| // TODO manage redirection | // TODO manage redirection | ||||
| m.Post("/authorize", bindIgnErr(auth.AuthorizationForm{}), user.AuthorizeOAuth) | m.Post("/authorize", bindIgnErr(auth.AuthorizationForm{}), user.AuthorizeOAuth) | ||||
| }, reqSignIn) | }, reqSignIn) | ||||
| @@ -1033,7 +1034,7 @@ func RegisterRoutes(m *macaron.Macaron) { | |||||
| m.Group("/debugjob", func() { | m.Group("/debugjob", func() { | ||||
| m.Get("", reqRepoCloudBrainReader, repo.DebugJobIndex) | m.Get("", reqRepoCloudBrainReader, repo.DebugJobIndex) | ||||
| }, context.RepoRef()) | |||||
| }, context.RepoRef(), reqWechatBind) | |||||
| m.Group("/modelarts", func() { | m.Group("/modelarts", func() { | ||||
| m.Group("/notebook", func() { | m.Group("/notebook", func() { | ||||