nezha/cmd/dashboard/controller/oauth2.go

122 lines
3.3 KiB
Go
Raw Normal View History

2019-12-08 03:59:58 -05:00
package controller
import (
"context"
"errors"
2019-12-08 03:59:58 -05:00
"fmt"
"net/http"
2020-11-29 09:17:40 -05:00
"strings"
2019-12-08 03:59:58 -05:00
"github.com/gin-gonic/gin"
"github.com/google/go-github/github"
2020-12-12 11:43:25 -05:00
GitHubAPI "github.com/google/go-github/github"
2019-12-08 03:59:58 -05:00
"golang.org/x/oauth2"
GitHubOauth2 "golang.org/x/oauth2/github"
2019-12-08 03:59:58 -05:00
2020-11-10 21:07:45 -05:00
"github.com/naiba/nezha/model"
"github.com/naiba/nezha/pkg/mygin"
"github.com/naiba/nezha/pkg/utils"
2020-11-10 21:07:45 -05:00
"github.com/naiba/nezha/service/dao"
2019-12-08 03:59:58 -05:00
)
type oauth2controller struct {
r gin.IRoutes
2019-12-08 03:59:58 -05:00
}
func (oa *oauth2controller) serve() {
oa.r.GET("/oauth2/login", oa.login)
oa.r.GET("/oauth2/callback", oa.callback)
}
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://"
}
return schame + c.Request.Host + "/oauth2/callback"
}
2019-12-08 03:59:58 -05:00
func (oa *oauth2controller) login(c *gin.Context) {
state := utils.RandStringBytesMaskImprSrcUnsafe(6)
dao.Cache.Set(fmt.Sprintf("%s%s", model.CacheKeyOauth2State, c.ClientIP()), state, 0)
url := oa.getCommonOauth2Config(c).AuthCodeURL(state, oauth2.AccessTypeOnline)
2019-12-08 03:59:58 -05:00
c.Redirect(http.StatusFound, url)
}
func (oa *oauth2controller) callback(c *gin.Context) {
var err error
2019-12-08 03:59:58 -05:00
// 验证登录跳转时的 State
state, ok := dao.Cache.Get(fmt.Sprintf("%s%s", model.CacheKeyOauth2State, c.ClientIP()))
2019-12-08 03:59:58 -05:00
if !ok || state.(string) != c.Query("state") {
err = errors.New("非法的登录方式")
2019-12-08 03:59:58 -05:00
}
oauth2Config := oa.getCommonOauth2Config(c)
2019-12-08 03:59:58 -05:00
ctx := context.Background()
var otk *oauth2.Token
if err == nil {
otk, err = oauth2Config.Exchange(ctx, c.Query("code"))
}
var client *GitHubAPI.Client
if err == nil {
oc := oauth2Config.Client(ctx, otk)
2021-05-03 22:22:38 -04:00
if dao.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 *github.User
if err == nil {
gu, _, err = client.Users.Get(ctx, "")
2019-12-08 03:59:58 -05:00
}
if err != nil {
mygin.ShowErrorPage(c, mygin.ErrInfo{
Code: http.StatusBadRequest,
Title: "登录失败",
Msg: fmt.Sprintf("错误信息:%s", err),
}, true)
return
}
2020-03-22 10:28:25 -04:00
var isAdmin bool
for _, admin := range strings.Split(dao.Conf.Oauth2.Admin, ",") {
if admin != "" && gu.GetLogin() == admin {
isAdmin = true
break
2020-03-22 10:28:25 -04:00
}
}
if !isAdmin {
2019-12-08 03:59:58 -05:00
mygin.ShowErrorPage(c, mygin.ErrInfo{
Code: http.StatusBadRequest,
Title: "登录失败",
Msg: fmt.Sprintf("错误信息:%s", "该用户不是本站点管理员,无法登录"),
}, true)
return
}
user := model.NewUserFromGitHub(gu)
2019-12-20 10:58:09 -05:00
user.IssueNewToken()
dao.DB.Save(&user)
2020-03-22 10:28:25 -04:00
c.SetCookie(dao.Conf.Site.CookieName, user.Token, 60*60*24, "", "", false, false)
2019-12-08 03:59:58 -05:00
c.Status(http.StatusOK)
c.Writer.WriteString("<script>window.location.href='/'</script>")
}