You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
41 lines
1.1 KiB
41 lines
1.1 KiB
package auth
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"github.com/youruser/base/internal/logic/auth"
|
|
"github.com/youruser/base/internal/svc"
|
|
"github.com/zeromicro/go-zero/rest/httpx"
|
|
)
|
|
|
|
// GetSSOLoginUrlHandler 获取 SSO 登录链接
|
|
func GetSSOLoginUrlHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
l := auth.NewSSOLogic(r.Context(), svcCtx)
|
|
resp, err := l.GetLoginUrl()
|
|
if err != nil {
|
|
httpx.ErrorCtx(r.Context(), w, err)
|
|
return
|
|
}
|
|
httpx.OkJsonCtx(r.Context(), w, resp)
|
|
}
|
|
}
|
|
|
|
// SSOCallbackHandler 处理 SSO 回调
|
|
func SSOCallbackHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
code := r.URL.Query().Get("code")
|
|
state := r.URL.Query().Get("state")
|
|
|
|
l := auth.NewSSOLogic(r.Context(), svcCtx)
|
|
redirectUrl, err := l.HandleCallback(code, state)
|
|
if err != nil {
|
|
// 回调失败,重定向到前端登录页并附带错误信息
|
|
frontendUrl := svcCtx.Config.Casdoor.FrontendUrl
|
|
http.Redirect(w, r, frontendUrl+"/login?error=sso_failed", http.StatusFound)
|
|
return
|
|
}
|
|
|
|
http.Redirect(w, r, redirectUrl, http.StatusFound)
|
|
}
|
|
}
|
|
|