2026-04-09 10:01:23 +08:00
|
|
|
package api
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"cs-agent/internal/pkg/config"
|
|
|
|
|
"cs-agent/internal/pkg/dto/request"
|
2026-05-27 22:18:43 +08:00
|
|
|
"cs-agent/internal/pkg/dto/response"
|
2026-05-23 22:18:40 +08:00
|
|
|
"cs-agent/internal/pkg/httpx"
|
2026-05-23 22:10:20 +08:00
|
|
|
"cs-agent/internal/pkg/httpx/params"
|
2026-04-09 10:01:23 +08:00
|
|
|
"cs-agent/internal/services"
|
2026-05-23 22:10:20 +08:00
|
|
|
"net/http"
|
2026-04-09 10:01:23 +08:00
|
|
|
"net/url"
|
|
|
|
|
"strings"
|
|
|
|
|
|
2026-05-23 22:10:20 +08:00
|
|
|
"github.com/gin-gonic/gin"
|
2026-04-09 10:01:23 +08:00
|
|
|
)
|
|
|
|
|
|
2026-05-23 22:18:40 +08:00
|
|
|
func Login(ctx *gin.Context) {
|
2026-04-09 10:01:23 +08:00
|
|
|
cfg := config.Current()
|
|
|
|
|
req := request.LoginRequest{}
|
2026-05-23 22:18:40 +08:00
|
|
|
if err := params.ReadJSON(ctx, &req); err != nil {
|
|
|
|
|
httpx.WriteJSON(ctx, err)
|
|
|
|
|
return
|
2026-04-09 10:01:23 +08:00
|
|
|
}
|
|
|
|
|
|
2026-05-23 22:18:40 +08:00
|
|
|
ret, err := services.AuthService.Login(req, cfg.Auth, ctx.ClientIP(), ctx.GetHeader("User-Agent"))
|
2026-04-09 10:01:23 +08:00
|
|
|
if err != nil {
|
2026-05-23 22:18:40 +08:00
|
|
|
httpx.WriteJSON(ctx, err)
|
|
|
|
|
return
|
2026-04-09 10:01:23 +08:00
|
|
|
}
|
2026-05-23 22:18:40 +08:00
|
|
|
httpx.WriteJSON(ctx, ret)
|
2026-04-09 10:01:23 +08:00
|
|
|
}
|
|
|
|
|
|
2026-05-27 22:18:43 +08:00
|
|
|
func AuthOptions(ctx *gin.Context) {
|
|
|
|
|
cfg := config.Current()
|
|
|
|
|
httpx.WriteJSON(ctx, &response.AuthOptionsResponse{
|
|
|
|
|
WxWorkEnabled: cfg.WxWork.Enabled,
|
|
|
|
|
OIDCEnabled: cfg.OIDC.Enabled,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-23 22:18:40 +08:00
|
|
|
func WxWorkLogin(ctx *gin.Context) {
|
|
|
|
|
loginURL, err := services.WxWorkLoginService.BuildWxWorkLoginURL(ctx.Query("next"))
|
2026-04-09 10:01:23 +08:00
|
|
|
if err != nil {
|
2026-05-23 22:28:21 +08:00
|
|
|
ctx.Redirect(http.StatusFound, "/login?wxworkError="+url.QueryEscape(wxWorkErrorMessage(err.Error())))
|
2026-04-09 10:01:23 +08:00
|
|
|
return
|
|
|
|
|
}
|
2026-05-23 22:18:40 +08:00
|
|
|
ctx.Redirect(http.StatusFound, loginURL)
|
2026-04-09 10:01:23 +08:00
|
|
|
}
|
|
|
|
|
|
2026-05-23 22:18:40 +08:00
|
|
|
func WxWorkQRLogin(ctx *gin.Context) {
|
|
|
|
|
loginURL, err := services.WxWorkLoginService.BuildWxWorkQRCodeLoginURL(ctx.Query("next"))
|
2026-04-09 10:01:23 +08:00
|
|
|
if err != nil {
|
2026-05-23 22:28:21 +08:00
|
|
|
ctx.Redirect(http.StatusFound, "/login?wxworkError="+url.QueryEscape(wxWorkErrorMessage(err.Error())))
|
2026-04-09 10:01:23 +08:00
|
|
|
return
|
|
|
|
|
}
|
2026-05-23 22:18:40 +08:00
|
|
|
ctx.Redirect(http.StatusFound, loginURL)
|
2026-04-09 10:01:23 +08:00
|
|
|
}
|
|
|
|
|
|
2026-05-23 22:18:40 +08:00
|
|
|
func WxWorkCallback(ctx *gin.Context) {
|
2026-04-09 10:01:23 +08:00
|
|
|
cfg := config.Current()
|
|
|
|
|
ticket, next, err := services.WxWorkLoginService.LoginByWxWork(
|
2026-05-23 22:18:40 +08:00
|
|
|
ctx.Query("code"),
|
|
|
|
|
ctx.Query("state"),
|
2026-04-09 10:01:23 +08:00
|
|
|
cfg.Auth,
|
2026-05-23 22:18:40 +08:00
|
|
|
ctx.ClientIP(),
|
|
|
|
|
ctx.GetHeader("User-Agent"),
|
2026-04-09 10:01:23 +08:00
|
|
|
)
|
|
|
|
|
if err != nil {
|
2026-05-23 22:28:21 +08:00
|
|
|
ctx.Redirect(http.StatusFound, "/login?wxworkError="+url.QueryEscape(wxWorkErrorMessage(err.Error())))
|
2026-04-09 10:01:23 +08:00
|
|
|
return
|
|
|
|
|
}
|
2026-05-23 22:18:40 +08:00
|
|
|
ctx.Redirect(http.StatusFound, "/dashboard/login/wxwork/callback?ticket="+url.QueryEscape(ticket)+"&next="+url.QueryEscape(next))
|
2026-04-09 10:01:23 +08:00
|
|
|
}
|
|
|
|
|
|
2026-05-23 22:18:40 +08:00
|
|
|
func WxWorkExchange(ctx *gin.Context) {
|
2026-04-09 10:01:23 +08:00
|
|
|
req := request.WxWorkExchangeRequest{}
|
2026-05-23 22:18:40 +08:00
|
|
|
if err := params.ReadJSON(ctx, &req); err != nil {
|
|
|
|
|
httpx.WriteJSON(ctx, err)
|
|
|
|
|
return
|
2026-04-09 10:01:23 +08:00
|
|
|
}
|
|
|
|
|
ret, err := services.WxWorkLoginService.ExchangeWxWorkLoginTicket(req.Ticket)
|
|
|
|
|
if err != nil {
|
2026-05-23 22:18:40 +08:00
|
|
|
httpx.WriteJSON(ctx, err)
|
|
|
|
|
return
|
2026-04-09 10:01:23 +08:00
|
|
|
}
|
2026-05-23 22:18:40 +08:00
|
|
|
httpx.WriteJSON(ctx, ret)
|
2026-04-09 10:01:23 +08:00
|
|
|
}
|
|
|
|
|
|
2026-05-24 20:49:10 +08:00
|
|
|
func OIDCLogin(ctx *gin.Context) {
|
|
|
|
|
loginURL, err := services.OIDCLoginService.BuildOIDCLoginURL(ctx.Query("next"))
|
|
|
|
|
if err != nil {
|
|
|
|
|
ctx.Redirect(http.StatusFound, "/dashboard/login?oidcError="+url.QueryEscape(loginErrorMessage(err.Error())))
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
ctx.Redirect(http.StatusFound, loginURL)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func OIDCCallback(ctx *gin.Context) {
|
|
|
|
|
cfg := config.Current()
|
|
|
|
|
ticket, next, err := services.OIDCLoginService.LoginByOIDC(
|
|
|
|
|
ctx.Request.Context(),
|
|
|
|
|
ctx.Query("code"),
|
|
|
|
|
ctx.Query("state"),
|
|
|
|
|
cfg.Auth,
|
|
|
|
|
ctx.ClientIP(),
|
|
|
|
|
ctx.GetHeader("User-Agent"),
|
|
|
|
|
)
|
|
|
|
|
if err != nil {
|
|
|
|
|
ctx.Redirect(http.StatusFound, "/dashboard/login?oidcError="+url.QueryEscape(loginErrorMessage(err.Error())))
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
ctx.Redirect(http.StatusFound, "/dashboard/login/oidc/callback?ticket="+url.QueryEscape(ticket)+"&next="+url.QueryEscape(next))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func OIDCExchange(ctx *gin.Context) {
|
|
|
|
|
req := request.OIDCExchangeRequest{}
|
|
|
|
|
if err := params.ReadJSON(ctx, &req); err != nil {
|
|
|
|
|
httpx.WriteJSON(ctx, err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
ret, err := services.OIDCLoginService.ExchangeOIDCLoginTicket(req.Ticket)
|
|
|
|
|
if err != nil {
|
|
|
|
|
httpx.WriteJSON(ctx, err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
httpx.WriteJSON(ctx, ret)
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-23 22:18:40 +08:00
|
|
|
func Logout(ctx *gin.Context) {
|
|
|
|
|
if err := services.AuthService.Logout(ctx.GetHeader("Authorization")); err != nil {
|
|
|
|
|
httpx.WriteJSON(ctx, err)
|
|
|
|
|
return
|
2026-04-09 10:01:23 +08:00
|
|
|
}
|
2026-05-23 22:18:40 +08:00
|
|
|
httpx.WriteJSON(ctx, nil)
|
2026-04-09 10:01:23 +08:00
|
|
|
}
|
|
|
|
|
|
2026-05-23 22:18:40 +08:00
|
|
|
func Profile(ctx *gin.Context) {
|
|
|
|
|
ret, err := services.AuthService.CurrentProfile(ctx)
|
2026-04-09 10:01:23 +08:00
|
|
|
if err != nil {
|
2026-05-23 22:18:40 +08:00
|
|
|
httpx.WriteJSON(ctx, err)
|
|
|
|
|
return
|
2026-04-09 10:01:23 +08:00
|
|
|
}
|
2026-05-23 22:18:40 +08:00
|
|
|
httpx.WriteJSON(ctx, ret)
|
2026-04-09 10:01:23 +08:00
|
|
|
}
|
|
|
|
|
|
2026-05-23 22:28:21 +08:00
|
|
|
func wxWorkErrorMessage(message string) string {
|
2026-05-24 20:49:10 +08:00
|
|
|
return loginErrorMessage(message)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func loginErrorMessage(message string) string {
|
2026-04-09 10:01:23 +08:00
|
|
|
if idx := strings.Index(message, ": "); idx >= 0 {
|
|
|
|
|
message = message[idx+2:]
|
|
|
|
|
}
|
2026-05-23 22:28:21 +08:00
|
|
|
return message
|
2026-04-09 10:01:23 +08:00
|
|
|
}
|