diff --git a/README.md b/README.md
index 240fb6d..f169166 100644
--- a/README.md
+++ b/README.md
@@ -1,7 +1,7 @@
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 @@
+
+
+
+
+