Skip to content

Commit 9fc3dd9

Browse files
author
dengbinbox
committed
feat: Add a general redirect method
--- 添加通用跳转方式
1 parent 93d2475 commit 9fc3dd9

File tree

5 files changed

+44
-13
lines changed

5 files changed

+44
-13
lines changed

cmd/main.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -143,10 +143,11 @@ var serveCmd = &cobra.Command{
143143
go func() {
144144
log.Info(nil, "Starting server...")
145145
server := handler.Server{
146-
ServerPort: globalConfig.Server.ServerPort,
147-
BaseURL: globalConfig.Server.BaseURL,
148-
HTTPClient: initHTTPClient(nil),
149-
IsPrivate: globalConfig.Server.IsPrivate,
146+
ServerPort: globalConfig.Server.ServerPort,
147+
BaseURL: globalConfig.Server.BaseURL,
148+
HTTPClient: initHTTPClient(nil),
149+
IsPrivate: globalConfig.Server.IsPrivate,
150+
RedirectURL: globalConfig.Redirect.Uris,
150151
}
151152
if err := server.StartServer(); err != nil {
152153
log.Error(nil, "Server error: %v", err)

config/config.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,3 +152,7 @@ log:
152152

153153
# Compress old log files to save disk space
154154
compress: true # Compress old logs
155+
156+
redirect:
157+
uris:
158+
"act_2026": "/credit/manager/annual-summary"

internal/config/config.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ type AppConfig struct {
2121
SMS SMSConfig `json:"sms" mapstructure:"sms" validate:"required"`
2222
Providers map[string]ProviderConfig `json:"providers" mapstructure:"providers"`
2323
QuotaManager QuotaConfig `json:"quotaManager" mapstructure:"quotaManager"`
24+
Redirect RedirectConfig `json:"redirect" mapstructure:"redirect"`
2425
}
2526

2627
type Server struct {
@@ -30,6 +31,10 @@ type Server struct {
3031
IsPrivate bool `json:"isPrivate" mapstructure:"isPrivate"`
3132
}
3233

34+
type RedirectConfig struct {
35+
Uris map[string]string `json:"uris" mapstructure:"uris"`
36+
}
37+
3338
type HTTPClientConfig struct {
3439
Timeout time.Duration `json:"timeout" mapstructure:"timeout" validate:"gte=0"`
3540
DialTimeout time.Duration `json:"dialTimeout" mapstructure:"dialTimeout" validate:"gte=0"`

internal/handler/router.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
11
package handler
22

33
import (
4-
"github.com/gin-gonic/gin"
54
"net/http"
65

6+
"github.com/gin-gonic/gin"
7+
78
"github.com/zgsm-ai/oidc-auth/internal/middleware"
89
"github.com/zgsm-ai/oidc-auth/pkg/log"
910
)
1011

1112
type Server struct {
12-
ServerPort string
13-
BaseURL string
14-
HTTPClient *http.Client
15-
IsPrivate bool
13+
ServerPort string
14+
BaseURL string
15+
HTTPClient *http.Client
16+
IsPrivate bool
17+
RedirectURL map[string]string
1618
}
1719

1820
type ParameterCarrier struct {
@@ -50,6 +52,7 @@ func (s *Server) SetupRouter(r *gin.Engine) {
5052
webOauthServer.GET("userinfo", s.userInfoHandler)
5153
webOauthServer.GET("login", s.webLoginHandler)
5254
webOauthServer.GET("login/callback", s.webLoginCallbackHandler)
55+
webOauthServer.GET("login/callback/:service", s.webLoginCallbackHandler)
5356
webOauthServer.GET("invite-code", s.getUserInviteCodeHandler)
5457
}
5558
r.POST("/oidc-auth/api/v1/send/sms", s.SMSHandler)

internal/handler/web_handler.go

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ type WebParameterCarrier struct {
2626
func (s *Server) webLoginHandler(c *gin.Context) {
2727
provider := c.DefaultQuery("provider", "casdoor")
2828
inviterCode := c.DefaultQuery("inviter_code", "")
29+
redirectService := c.DefaultQuery("redirect_service", "")
2930

3031
oauthManager := providers.GetManager()
3132
providerInstance, err := oauthManager.GetProvider(provider)
@@ -37,7 +38,9 @@ func (s *Server) webLoginHandler(c *gin.Context) {
3738
// Use inviterCode as state parameter
3839
state := inviterCode
3940
authURL := providerInstance.GetAuthURL(state, s.BaseURL+constants.WebLoginCallbackURI)
40-
41+
if redirectService != "" {
42+
authURL = fmt.Sprintf("%s/%s", authURL, redirectService)
43+
}
4144
response.JSONSuccess(c, "", map[string]interface{}{
4245
"state": state,
4346
"inviter_code": inviterCode,
@@ -49,7 +52,8 @@ func (s *Server) webLoginHandler(c *gin.Context) {
4952
func (s *Server) webLoginCallbackHandler(c *gin.Context) {
5053
code := c.DefaultQuery("code", "")
5154
state := c.DefaultQuery("state", "")
52-
inviterCode := state // inviter code is in the state parameter
55+
inviterCode := state // inviter code is in the state parameter
56+
service := c.Param("service") // service parameter for custom redirect
5357

5458
if code == "" {
5559
response.JSONError(c, http.StatusBadRequest, errs.ErrBadRequestParam,
@@ -130,8 +134,22 @@ func (s *Server) webLoginCallbackHandler(c *gin.Context) {
130134
tokenHash = user.Devices[0].AccessTokenHash
131135
}
132136

133-
// Redirect to bind account page with tokenHash as state parameter
134-
redirectURL := providerInstance.GetEndpoint(false) + constants.BindAccountBindURI + "?state=" + tokenHash
137+
// Determine redirect URL based on service parameter
138+
var redirectURL string
139+
if service != "" {
140+
// Check RedirectConfig for custom redirect URI
141+
if s.RedirectURL != nil {
142+
if uri, ok := s.RedirectURL[service]; ok && uri != "" {
143+
redirectURL = fmt.Sprintf("%s%s?state=%s", providerInstance.GetEndpoint(false), uri, tokenHash)
144+
}
145+
}
146+
}
147+
148+
// If no custom redirect configured, use default
149+
if redirectURL == "" {
150+
redirectURL = providerInstance.GetEndpoint(false) + constants.BindAccountBindURI + "?state=" + tokenHash
151+
}
152+
135153
c.Redirect(http.StatusFound, redirectURL)
136154
}
137155

0 commit comments

Comments
 (0)