🐛 修复修改配置后重置为GitHub登录的问题

This commit is contained in:
naiba
2021-05-02 17:21:16 +08:00
parent 2adcc9cbc1
commit fc0aee7c49
5 changed files with 38 additions and 30 deletions

View File

@@ -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()

View File

@@ -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 {