mirror of
https://github.com/Buriburizaem0n/nezha_domains.git
synced 2026-02-04 04:30:05 +00:00
✨ dashboard v0.13.20 Login with Gitlab、Jihulab
This commit is contained in:
@@ -39,6 +39,12 @@ func (gp *guestPage) login(c *gin.Context) {
|
||||
if singleton.Conf.Oauth2.Type == model.ConfigTypeGitee {
|
||||
LoginType = "Gitee"
|
||||
RegistrationLink = "https://gitee.com/signup"
|
||||
} else if singleton.Conf.Oauth2.Type == model.ConfigTypeGitlab {
|
||||
LoginType = "Gitlab"
|
||||
RegistrationLink = "https://gitlab.com/users/sign_up"
|
||||
} else if singleton.Conf.Oauth2.Type == model.ConfigTypeJihulab {
|
||||
LoginType = "Jihulab"
|
||||
RegistrationLink = "https://jihulab.com/users/sign_up"
|
||||
}
|
||||
c.HTML(http.StatusOK, "dashboard/login", mygin.CommonEnvironment(c, gin.H{
|
||||
"Title": singleton.Localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "Login"}),
|
||||
|
||||
@@ -10,8 +10,10 @@ import (
|
||||
"github.com/gin-gonic/gin"
|
||||
GitHubAPI "github.com/google/go-github/github"
|
||||
"github.com/patrickmn/go-cache"
|
||||
"github.com/xanzy/go-gitlab"
|
||||
"golang.org/x/oauth2"
|
||||
GitHubOauth2 "golang.org/x/oauth2/github"
|
||||
GitlabOauth2 "golang.org/x/oauth2/github"
|
||||
|
||||
"github.com/naiba/nezha/model"
|
||||
"github.com/naiba/nezha/pkg/mygin"
|
||||
@@ -40,6 +42,25 @@ func (oa *oauth2controller) getCommonOauth2Config(c *gin.Context) *oauth2.Config
|
||||
},
|
||||
RedirectURL: oa.getRedirectURL(c),
|
||||
}
|
||||
} else if singleton.Conf.Oauth2.Type == model.ConfigTypeGitlab {
|
||||
return &oauth2.Config{
|
||||
ClientID: singleton.Conf.Oauth2.ClientID,
|
||||
ClientSecret: singleton.Conf.Oauth2.ClientSecret,
|
||||
Scopes: []string{"read_user", "read_api"},
|
||||
Endpoint: GitlabOauth2.Endpoint,
|
||||
RedirectURL: oa.getRedirectURL(c),
|
||||
}
|
||||
} else if singleton.Conf.Oauth2.Type == model.ConfigTypeJihulab {
|
||||
return &oauth2.Config{
|
||||
ClientID: singleton.Conf.Oauth2.ClientID,
|
||||
ClientSecret: singleton.Conf.Oauth2.ClientSecret,
|
||||
Scopes: []string{"read_user", "read_api"},
|
||||
Endpoint: oauth2.Endpoint{
|
||||
AuthURL: "https://jihulab.com/oauth/authorize",
|
||||
TokenURL: "https://jihulab.com/oauth/token",
|
||||
},
|
||||
RedirectURL: oa.getRedirectURL(c),
|
||||
}
|
||||
} else {
|
||||
return &oauth2.Config{
|
||||
ClientID: singleton.Conf.Oauth2.ClientID,
|
||||
@@ -85,20 +106,47 @@ func (oa *oauth2controller) callback(c *gin.Context) {
|
||||
if err == nil {
|
||||
otk, err = oauth2Config.Exchange(ctx, c.Query("code"))
|
||||
}
|
||||
var client *GitHubAPI.Client
|
||||
|
||||
var user model.User
|
||||
|
||||
if err == nil {
|
||||
oc := oauth2Config.Client(ctx, otk)
|
||||
if singleton.Conf.Oauth2.Type == model.ConfigTypeGitee {
|
||||
client, err = GitHubAPI.NewEnterpriseClient("https://gitee.com/api/v5/", "https://gitee.com/api/v5/", oc)
|
||||
if singleton.Conf.Oauth2.Type == model.ConfigTypeGitlab || singleton.Conf.Oauth2.Type == model.ConfigTypeJihulab {
|
||||
var gitlabApiClient *gitlab.Client
|
||||
if singleton.Conf.Oauth2.Type == model.ConfigTypeGitlab {
|
||||
gitlabApiClient, err = gitlab.NewOAuthClient(otk.AccessToken)
|
||||
} else {
|
||||
gitlabApiClient, err = gitlab.NewOAuthClient(otk.AccessToken, gitlab.WithBaseURL("https://jihulab.com/api/v4/"))
|
||||
}
|
||||
var u *gitlab.User
|
||||
if err == nil {
|
||||
u, _, err = gitlabApiClient.Users.CurrentUser()
|
||||
}
|
||||
if err == nil {
|
||||
user = model.NewUserFromGitlab(u)
|
||||
}
|
||||
} else {
|
||||
client = GitHubAPI.NewClient(oc)
|
||||
var client *GitHubAPI.Client
|
||||
oc := oauth2Config.Client(ctx, otk)
|
||||
if singleton.Conf.Oauth2.Type == model.ConfigTypeGitee {
|
||||
client, err = GitHubAPI.NewEnterpriseClient("https://gitee.com/api/v5/", "https://gitee.com/api/v5/", oc)
|
||||
} else {
|
||||
client = GitHubAPI.NewClient(oc)
|
||||
}
|
||||
var gu *GitHubAPI.User
|
||||
if err == nil {
|
||||
gu, _, err = client.Users.Get(ctx, "")
|
||||
}
|
||||
if err == nil {
|
||||
user = model.NewUserFromGitHub(gu)
|
||||
}
|
||||
}
|
||||
}
|
||||
var gu *GitHubAPI.User
|
||||
if err == nil {
|
||||
gu, _, err = client.Users.Get(ctx, "")
|
||||
|
||||
if err == nil && user.Login == "" {
|
||||
err = errors.New("获取用户信息失败")
|
||||
}
|
||||
if err != nil {
|
||||
|
||||
if err != nil || user.Login == "" {
|
||||
mygin.ShowErrorPage(c, mygin.ErrInfo{
|
||||
Code: http.StatusBadRequest,
|
||||
Title: "登录失败",
|
||||
@@ -108,7 +156,7 @@ func (oa *oauth2controller) callback(c *gin.Context) {
|
||||
}
|
||||
var isAdmin bool
|
||||
for _, admin := range strings.Split(singleton.Conf.Oauth2.Admin, ",") {
|
||||
if admin != "" && strings.ToLower(gu.GetLogin()) == strings.ToLower(admin) {
|
||||
if admin != "" && strings.ToLower(user.Login) == strings.ToLower(admin) {
|
||||
isAdmin = true
|
||||
break
|
||||
}
|
||||
@@ -121,7 +169,6 @@ func (oa *oauth2controller) callback(c *gin.Context) {
|
||||
}, true)
|
||||
return
|
||||
}
|
||||
user := model.NewUserFromGitHub(gu)
|
||||
user.IssueNewToken()
|
||||
singleton.DB.Save(&user)
|
||||
c.SetCookie(singleton.Conf.Site.CookieName, user.Token, 60*60*24, "", "", false, false)
|
||||
|
||||
Reference in New Issue
Block a user