From fc0aee7c49dec4ed177fbbe6b5b14362d412f57f Mon Sep 17 00:00:00 2001 From: naiba Date: Sun, 2 May 2021 17:21:16 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20=E4=BF=AE=E5=A4=8D=E4=BF=AE?= =?UTF-8?q?=E6=94=B9=E9=85=8D=E7=BD=AE=E5=90=8E=E9=87=8D=E7=BD=AE=E4=B8=BA?= =?UTF-8?q?GitHub=E7=99=BB=E5=BD=95=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- cmd/dashboard/controller/guest_page.go | 19 ------------ cmd/dashboard/controller/oauth2.go | 37 ++++++++++++++++++------ resource/template/dashboard/setting.html | 8 +++++ service/dao/dao.go | 2 +- 5 files changed, 38 insertions(+), 30 deletions(-) diff --git a/README.md b/README.md index 240fb6d..f169166 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@


-    +   

:trollface: 哪吒监控 一站式轻监控轻运维系统。支持系统状态、HTTP(SSL 证书变更、即将到期、到期)、TCP、Ping 监控报警,命令批量执行和计划任务。

diff --git a/cmd/dashboard/controller/guest_page.go b/cmd/dashboard/controller/guest_page.go index eb0e1be..c5db724 100644 --- a/cmd/dashboard/controller/guest_page.go +++ b/cmd/dashboard/controller/guest_page.go @@ -7,8 +7,6 @@ import ( "github.com/naiba/nezha/model" "github.com/naiba/nezha/pkg/mygin" "github.com/naiba/nezha/service/dao" - "golang.org/x/oauth2" - "golang.org/x/oauth2/github" ) type guestPage struct { @@ -27,24 +25,7 @@ func (gp *guestPage) serve() { gr.GET("/login", gp.login) - var endPoint oauth2.Endpoint - - if dao.Conf.Oauth2.Type == model.ConfigTypeGitee { - endPoint = oauth2.Endpoint{ - AuthURL: "https://gitee.com/oauth/authorize", - TokenURL: "https://gitee.com/oauth/token", - } - } else { - endPoint = github.Endpoint - } - oauth := &oauth2controller{ - oauth2Config: &oauth2.Config{ - ClientID: dao.Conf.Oauth2.ClientID, - ClientSecret: dao.Conf.Oauth2.ClientSecret, - Scopes: []string{}, - Endpoint: endPoint, - }, r: gr, } oauth.serve() diff --git a/cmd/dashboard/controller/oauth2.go b/cmd/dashboard/controller/oauth2.go index ccac017..eefa0bb 100644 --- a/cmd/dashboard/controller/oauth2.go +++ b/cmd/dashboard/controller/oauth2.go @@ -11,6 +11,7 @@ import ( "github.com/google/go-github/github" GitHubAPI "github.com/google/go-github/github" "golang.org/x/oauth2" + GitHubOauth2 "golang.org/x/oauth2/github" "github.com/naiba/nezha/model" "github.com/naiba/nezha/pkg/mygin" @@ -19,8 +20,7 @@ import ( ) type oauth2controller struct { - oauth2Config *oauth2.Config - r gin.IRoutes + r gin.IRoutes } func (oa *oauth2controller) serve() { @@ -28,38 +28,57 @@ func (oa *oauth2controller) serve() { oa.r.GET("/oauth2/callback", oa.callback) } -func (oa *oauth2controller) fillRedirectURL(c *gin.Context) { +func (oa *oauth2controller) getCommonOauth2Config(c *gin.Context) *oauth2.Config { + var endPoint oauth2.Endpoint + if dao.Conf.Oauth2.Type == model.ConfigTypeGitee { + endPoint = oauth2.Endpoint{ + AuthURL: "https://gitee.com/oauth/authorize", + TokenURL: "https://gitee.com/oauth/token", + } + } else { + endPoint = GitHubOauth2.Endpoint + } + + return &oauth2.Config{ + ClientID: dao.Conf.Oauth2.ClientID, + ClientSecret: dao.Conf.Oauth2.ClientSecret, + Scopes: []string{}, + Endpoint: endPoint, + RedirectURL: oa.getRedirectURL(c), + } +} + +func (oa *oauth2controller) getRedirectURL(c *gin.Context) string { schame := "http://" if strings.HasPrefix(c.Request.Referer(), "https://") { schame = "https://" } - oa.oauth2Config.RedirectURL = schame + c.Request.Host + "/oauth2/callback" + return schame + c.Request.Host + "/oauth2/callback" } func (oa *oauth2controller) login(c *gin.Context) { - oa.fillRedirectURL(c) state := utils.RandStringBytesMaskImprSrcUnsafe(6) dao.Cache.Set(fmt.Sprintf("%s%s", model.CacheKeyOauth2State, c.ClientIP()), state, 0) - url := oa.oauth2Config.AuthCodeURL(state, oauth2.AccessTypeOnline) + url := oa.getCommonOauth2Config(c).AuthCodeURL(state, oauth2.AccessTypeOnline) c.Redirect(http.StatusFound, url) } func (oa *oauth2controller) callback(c *gin.Context) { - oa.fillRedirectURL(c) var err error // 验证登录跳转时的 State state, ok := dao.Cache.Get(fmt.Sprintf("%s%s", model.CacheKeyOauth2State, c.ClientIP())) if !ok || state.(string) != c.Query("state") { err = errors.New("非法的登录方式") } + oauth2Config := oa.getCommonOauth2Config(c) ctx := context.Background() var otk *oauth2.Token if err == nil { - otk, err = oa.oauth2Config.Exchange(ctx, c.Query("code")) + otk, err = oauth2Config.Exchange(ctx, c.Query("code")) } var client *GitHubAPI.Client if err == nil { - oc := oa.oauth2Config.Client(ctx, otk) + oc := oauth2Config.Client(ctx, otk) if dao.Conf.Oauth2.Type == "gitee" { client, err = GitHubAPI.NewEnterpriseClient("https://gitee.com/api/v5/", "https://gitee.com/api/v5/", oc) } else { diff --git a/resource/template/dashboard/setting.html b/resource/template/dashboard/setting.html index 39b769b..2791c11 100644 --- a/resource/template/dashboard/setting.html +++ b/resource/template/dashboard/setting.html @@ -8,11 +8,19 @@ +
+ + +
+