diff --git a/handler/tool.go b/handler/tool.go index fac4476..57ea660 100644 --- a/handler/tool.go +++ b/handler/tool.go @@ -57,6 +57,8 @@ func SetUpToolGroup(router *gin.Engine) { toolGroup.POST("/monitor", SetDeviceStatusV2) toolGroup.POST("/qq_callback", handleQQCallback) toolGroup.POST("/qq_auth", GetAuthCode) + toolGroup.POST("/github_auth", ToGithubAuthPage) + toolGroup.POST("/github_callback", handleGithubCallback) //发送邮件 toolGroup.POST("/send_mail", SendMailTool) toolGroup.POST("/dashboard", DashBoardStatistics) @@ -631,3 +633,23 @@ func GetSparkCreatePPTStatus(c *gin.Context) { return } } + +func handleGithubCallback(c *gin.Context) { + +} + +func ToGithubAuthPage(c *gin.Context) { + uuid := c.Query("uuid") + if uuid == "" { + c.JSON(http.StatusOK, gin.H{"code": proto.ParameterError, "message": "uuid is empty"}) + return + } + params := url.Values{} + params.Add("client_id", proto.Config.GITHUB_CLIENT_ID) + params.Add("login", uuid) + params.Add("state", "saw_"+uuid) + baseUri := "https://github.com/login/oauth/authorize" + redirectUrl := fmt.Sprintf("%s?%s", baseUri, params.Encode()) + redirectUrl = fmt.Sprintf("%s&redirect_uri=%s", redirectUrl, "https://sv.ljsea.top") + c.Redirect(http.StatusFound, redirectUrl) +} diff --git a/handler/user.go b/handler/user.go index 86a5846..1ded230 100644 --- a/handler/user.go +++ b/handler/user.go @@ -29,6 +29,8 @@ func SetUpUserGroup(router *gin.Engine) { userGroup.POST("/sync", GetSyncUserInfo) userGroup.POST("/delete", DeleteUser) userGroup.POST("/reset", ResetPassword) + userGroup.GET("/oAuth", LoginOAuth) //第三方登录 + userGroup.GET("/oAuth_uuid", GetOAuthUUID) userGroup.POST("/statistic", GetUserStatistic) } @@ -531,3 +533,56 @@ func GetSyncUserInfo(c *gin.Context) { return } } + +func LoginOAuth(c *gin.Context) { + uuid := c.Query("uuid") + var resp proto.GenerateResp + if uuid == "" { + resp.Code = proto.ParameterError + resp.Message = "uuid is empty" + c.JSON(200, resp) + return + } + //获取用户信息 + loginStatus := worker.GetRedis(uuid) + if loginStatus == "" { + resp.Code = proto.ThirdPartyLoginUUIDInvalid + resp.Message = "已失效" + c.JSON(200, resp) + return + } + var status proto.ThirdPartyLoginStatus + if err := json.Unmarshal([]byte(loginStatus), &status); err != nil { + resp.Code = proto.OperationFailed + resp.Message = "error" + c.JSON(200, resp) + return + } + resp.Code = proto.SuccessCode + resp.Message = "success" + resp.Data = status + c.JSON(200, resp) +} + +func GetOAuthUUID(c *gin.Context) { + var resp proto.GenerateResp + loginType := c.Query("type") + if loginType == "" { + resp.Code = proto.ParameterError + resp.Message = "type is empty" + c.JSON(200, resp) + return + } + uuid := uuid.NewString() + //设置状态 + var status proto.ThirdPartyLoginStatus + status.Status = 1 + status.Type = loginType + //设置过期时间 + statusStr, _ := json.Marshal(status) + worker.SetRedisWithExpire(uuid, string(statusStr), time.Minute*10) //10min过期 + resp.Code = proto.SuccessCode + resp.Message = "success" + resp.Data = uuid + c.JSON(200, resp) +} diff --git a/proto/conf.go b/proto/conf.go index dcb593e..2688bc7 100644 --- a/proto/conf.go +++ b/proto/conf.go @@ -85,6 +85,7 @@ type ConfigStruct struct { SERVER_NAME string `json:"server_name"` // 服务器名称,用于区分不同服务器 SPARK_PPT_USAGE bool `json:"spark_ppt_usage"` // 是否使用spark ppt功能 KBASE_SERVER []KBaseServer `json:"kbase_server"` // 知识库服务器列表 + GITHUB_CLIENT_ID string `json:"github_client_id"` // github client id } type KBaseServer struct { diff --git a/proto/status.go b/proto/status.go index 8cc8a78..0a41017 100644 --- a/proto/status.go +++ b/proto/status.go @@ -94,6 +94,10 @@ const ( //下面是ws消息错误码 WSKBaseServerError = 150 // ws知识库服务器错误 WSKBaseSessionError = 151 // ws知识库会话错误 + + //第三方登录 + ThirdPartyLoginUUIDInvalid = 161 //第三方登录uuid失效 + ) const ( diff --git a/proto/tool.go b/proto/tool.go index a8f00d1..65a496b 100644 --- a/proto/tool.go +++ b/proto/tool.go @@ -54,3 +54,17 @@ type MessageConvertFileReq struct { FileType string `json:"file_type" form:"file_type"` // 需要转成的文件类型 FileName string `json:"file_name" form:"file_name"` // 文件名称 } + +// 第三方登录 +type ThirdPartyLoginStatus struct { + Status int `json:"status"` // 登录状态,0:登录成功,1:登录失败 + Type string `json:"type"` // 登录类型,qq,github + UserInfo UserLoginInfo `json:"user_info"` // 用户信息 +} + +type UserLoginInfo struct { + UserID int `json:"id"` // 用户ID + Username string `json:"username"` // 用户名 + Email string `json:"email"` // 用户邮箱 + Token string `json:"token"` // 用户token +}