Files
rustdesk-api/http/controller/api/ouath.go
T

241 lines
6.6 KiB
Go
Raw Normal View History

2024-09-19 10:44:49 +08:00
package api
import (
"github.com/gin-gonic/gin"
2025-02-12 19:46:39 +08:00
"github.com/lejianwen/rustdesk-api/v2/global"
"github.com/lejianwen/rustdesk-api/v2/http/request/api"
"github.com/lejianwen/rustdesk-api/v2/http/response"
apiResp "github.com/lejianwen/rustdesk-api/v2/http/response/api"
"github.com/lejianwen/rustdesk-api/v2/model"
"github.com/lejianwen/rustdesk-api/v2/service"
2024-09-19 10:44:49 +08:00
"net/http"
)
type Oauth struct {
}
// OidcAuth
// @Tags Oauth
// @Summary OidcAuth
// @Description OidcAuth
// @Accept json
// @Produce json
// @Success 200 {object} apiResp.LoginRes
// @Failure 500 {object} response.ErrorResponse
// @Router /oidc/auth [post]
func (o *Oauth) OidcAuth(c *gin.Context) {
f := &api.OidcAuthRequest{}
err := c.ShouldBindJSON(&f)
if err != nil {
2024-09-25 22:41:57 +08:00
response.Error(c, response.TranslateMsg(c, "ParamsError")+err.Error())
2024-09-19 10:44:49 +08:00
return
}
2024-11-02 04:01:28 +08:00
oauthService := service.AllService.OauthService
var code string
var url string
err, code, url = oauthService.BeginAuth(f.Op)
2024-09-19 10:44:49 +08:00
if err != nil {
2024-09-25 22:41:57 +08:00
response.Error(c, response.TranslateMsg(c, err.Error()))
2024-09-19 10:44:49 +08:00
return
}
service.AllService.OauthService.SetOauthCache(code, &service.OauthCacheItem{
Action: service.OauthActionTypeLogin,
Id: f.Id,
Op: f.Op,
Uuid: f.Uuid,
DeviceName: f.DeviceInfo.Name,
DeviceOs: f.DeviceInfo.Os,
DeviceType: f.DeviceInfo.Type,
}, 5*60)
//fmt.Println("code url", code, url)
c.JSON(http.StatusOK, gin.H{
"code": code,
"url": url,
})
}
2024-10-31 09:21:30 +08:00
func (o *Oauth) OidcAuthQueryPre(c *gin.Context) (*model.User, *model.UserToken) {
var u *model.User
var ut *model.UserToken
q := &api.OidcAuthQuery{}
// 解析查询参数并处理错误
if err := c.ShouldBindQuery(q); err != nil {
response.Error(c, response.TranslateMsg(c, "ParamsError")+": "+err.Error())
return nil, nil
}
// 获取 OAuth 缓存
v := service.AllService.OauthService.GetOauthCache(q.Code)
if v == nil {
response.Error(c, response.TranslateMsg(c, "OauthExpired"))
return nil, nil
}
// 如果 UserId 为 0,说明还在授权中
if v.UserId == 0 {
2024-10-31 14:03:48 +08:00
c.JSON(http.StatusOK, gin.H{"message": "Authorization in progress, please login and bind"})
2024-10-31 09:21:30 +08:00
return nil, nil
}
// 获取用户信息
u = service.AllService.UserService.InfoById(v.UserId)
if u == nil {
response.Error(c, response.TranslateMsg(c, "UserNotFound"))
return nil, nil
}
// 删除 OAuth 缓存
service.AllService.OauthService.DeleteOauthCache(q.Code)
// 创建登录日志并生成用户令牌
ut = service.AllService.UserService.Login(u, &model.LoginLog{
UserId: u.Id,
Client: v.DeviceType,
2024-11-02 05:42:47 +08:00
DeviceId: v.Id,
2024-10-31 09:21:30 +08:00
Uuid: v.Uuid,
Ip: c.ClientIP(),
Type: model.LoginLogTypeOauth,
Platform: v.DeviceOs,
})
if ut == nil {
response.Error(c, response.TranslateMsg(c, "LoginFailed"))
return nil, nil
}
// 返回用户令牌
return u, ut
}
2024-09-19 10:44:49 +08:00
// OidcAuthQuery
// @Tags Oauth
// @Summary OidcAuthQuery
// @Description OidcAuthQuery
// @Accept json
// @Produce json
// @Success 200 {object} apiResp.LoginRes
// @Failure 500 {object} response.ErrorResponse
// @Router /oidc/auth-query [get]
func (o *Oauth) OidcAuthQuery(c *gin.Context) {
2024-10-31 09:21:30 +08:00
u, ut := o.OidcAuthQueryPre(c)
2024-10-31 14:03:48 +08:00
if u == nil || ut == nil {
return
}
2024-09-25 22:41:57 +08:00
c.JSON(http.StatusOK, apiResp.LoginRes{
AccessToken: ut.Token,
Type: "access_token",
User: *(&apiResp.UserPayload{}).FromUser(u),
})
2024-09-19 10:44:49 +08:00
}
// OauthCallback 回调
// @Tags Oauth
// @Summary OauthCallback
// @Description OauthCallback
// @Accept json
// @Produce json
// @Success 200 {object} apiResp.LoginRes
// @Failure 500 {object} response.ErrorResponse
// @Router /oauth/callback [get]
func (o *Oauth) OauthCallback(c *gin.Context) {
state := c.Query("state")
if state == "" {
2024-09-25 22:41:57 +08:00
c.String(http.StatusInternalServerError, response.TranslateParamMsg(c, "ParamIsEmpty", "state"))
2024-09-19 10:44:49 +08:00
return
}
cacheKey := state
2024-11-02 04:01:28 +08:00
oauthService := service.AllService.OauthService
2024-09-19 10:44:49 +08:00
//从缓存中获取
2024-11-02 04:01:28 +08:00
oauthCache := oauthService.GetOauthCache(cacheKey)
if oauthCache == nil {
2024-09-25 22:41:57 +08:00
c.String(http.StatusInternalServerError, response.TranslateMsg(c, "OauthExpired"))
2024-09-19 10:44:49 +08:00
return
}
2024-11-02 04:01:28 +08:00
op := oauthCache.Op
action := oauthCache.Action
var user *model.User
// 获取用户信息
code := c.Query("code")
err, oauthUser := oauthService.Callback(code, op)
if err != nil {
c.String(http.StatusInternalServerError, response.TranslateMsg(c, "OauthFailed")+response.TranslateMsg(c, err.Error()))
2024-10-31 14:03:48 +08:00
return
}
2024-11-02 04:01:28 +08:00
userId := oauthCache.UserId
openid := oauthUser.OpenId
if action == service.OauthActionTypeBind {
2024-10-31 14:03:48 +08:00
//fmt.Println("bind", ty, userData)
2024-11-02 04:01:28 +08:00
// 检查此openid是否已经绑定过
utr := oauthService.UserThirdInfo(op, openid)
2024-10-31 14:03:48 +08:00
if utr.UserId > 0 {
c.String(http.StatusInternalServerError, response.TranslateMsg(c, "OauthHasBindOtherUser"))
2024-10-29 11:51:01 +08:00
return
2024-10-31 14:03:48 +08:00
}
//绑定
2024-11-02 04:01:28 +08:00
user = service.AllService.UserService.InfoById(userId)
if user == nil {
2024-10-31 14:03:48 +08:00
c.String(http.StatusInternalServerError, response.TranslateMsg(c, "ItemNotFound"))
return
}
//绑定
2024-11-02 04:01:28 +08:00
err := oauthService.BindOauthUser(userId, oauthUser, op)
2024-10-31 14:03:48 +08:00
if err != nil {
c.String(http.StatusInternalServerError, response.TranslateMsg(c, "BindFail"))
return
}
c.String(http.StatusOK, response.TranslateMsg(c, "BindSuccess"))
return
2024-11-02 04:01:28 +08:00
} else if action == service.OauthActionTypeLogin {
2024-10-31 14:03:48 +08:00
//登录
2024-11-02 04:01:28 +08:00
if userId != 0 {
2024-10-31 14:03:48 +08:00
c.String(http.StatusInternalServerError, response.TranslateMsg(c, "OauthHasBeenSuccess"))
return
}
2024-11-02 04:01:28 +08:00
user = service.AllService.UserService.InfoByOauthId(op, openid)
if user == nil {
oauthConfig := oauthService.InfoByOp(op)
if !*oauthConfig.AutoRegister {
2024-10-31 14:03:48 +08:00
//c.String(http.StatusInternalServerError, "还未绑定用户,请先绑定")
2024-11-02 04:01:28 +08:00
oauthCache.UpdateFromOauthUser(oauthUser)
2024-10-31 14:03:48 +08:00
url := global.Config.Rustdesk.ApiServer + "/_admin/#/oauth/bind/" + cacheKey
c.Redirect(http.StatusFound, url)
2024-10-29 11:51:01 +08:00
return
}
2024-10-31 14:03:48 +08:00
//自动注册
2024-11-03 16:49:03 +08:00
err, user = service.AllService.UserService.RegisterByOauth(oauthUser, op)
if err != nil {
c.String(http.StatusInternalServerError, response.TranslateMsg(c, err.Error()))
2024-10-31 14:03:48 +08:00
return
2024-10-29 11:51:01 +08:00
}
}
2024-11-02 04:01:28 +08:00
oauthCache.UserId = user.Id
oauthService.SetOauthCache(cacheKey, oauthCache, 0)
2024-10-31 14:03:48 +08:00
// 如果是webadmin,登录成功后跳转到webadmin
2024-11-05 09:48:02 +08:00
if oauthCache.DeviceType == model.LoginLogClientWebAdmin {
2024-10-31 14:03:48 +08:00
/*service.AllService.UserService.Login(u, &model.LoginLog{
UserId: u.Id,
Client: "webadmin",
Uuid: "", //must be empty
Ip: c.ClientIP(),
Type: model.LoginLogTypeOauth,
2024-11-02 04:01:28 +08:00
Platform: oauthService.DeviceOs,
2024-10-31 14:03:48 +08:00
})*/
url := global.Config.Rustdesk.ApiServer + "/_admin/#/"
c.Redirect(http.StatusFound, url)
return
}
c.String(http.StatusOK, response.TranslateMsg(c, "OauthSuccess"))
return
} else {
c.String(http.StatusInternalServerError, response.TranslateMsg(c, "ParamsError"))
2024-10-31 09:21:30 +08:00
return
}
2024-09-20 12:13:15 +08:00
2024-09-19 10:44:49 +08:00
}