Skip to content

Commit 8a16b80

Browse files
committed
fix: fallback redirection and allow non redirect_url query params
1 parent 55af9d0 commit 8a16b80

File tree

10 files changed

+43
-14
lines changed

10 files changed

+43
-14
lines changed

.env.example

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ APP_CORS_ALLOW_ORIGINS
1414
APP_CORS_ALLOW_METHODS
1515
APP_CORS_ALLOW_CREDENTIALS
1616

17+
APP_FALLBACK_REDIRECT
18+
1719
DB_HOST
1820
DB_PORT
1921
DB_USERNAME

cmd/http.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,11 @@ func serveHTTP(cmd *cobra.Command, args []string) error {
4444
http_middleware.WithAllowCredentials(cfg.App.CORSAllowCredentials),
4545
)
4646

47-
e.Use(CORS, middleware.RequestID())
47+
e.Use(
48+
CORS,
49+
http_middleware.FallbackRedirect(cfg.App.FallbackRedirect),
50+
middleware.RequestID(),
51+
)
4852

4953
discordRepository := discord_repository.NewDiscordRepository(cfg.Discord)
5054
discordUsecase := module.NewDiscordUsecase(discordRepository)

config/application.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ type AppConfig struct {
2727
CORSAllowOrigins []string `env:"APP_CORS_ALLOW_ORIGINS" envSeparator:"," envDefault:"*.mocha-bot.xyz,mocha-bot.xyz"`
2828
CORSAllowMethods []string `env:"APP_CORS_ALLOW_METHODS" envSeparator:"," envDefault:"GET,POST,PUT,DELETE,OPTIONS"`
2929
CORSAllowCredentials bool `env:"APP_CORS_ALLOW_CREDENTIALS" envDefault:"true"`
30+
31+
// Fallback Redirect
32+
FallbackRedirect string `env:"APP_FALLBACK_REDIRECT" envDefault:"https://mocha-bot.xyz"`
3033
}
3134

3235
func (a AppConfig) GetAddress() string {

core/entity/discord.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"time"
77

88
"github.com/go-playground/validator/v10"
9-
cookiey "github.com/mocha-bot/mochus/pkg/cookie"
9+
cookiey "github.com/mocha-bot/mochus/pkg/cookiey"
1010
)
1111

1212
const (

core/repository/discord/discord.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
)
88

99
type DiscordRepository interface {
10-
GetToken(ctx context.Context, code, redirectURL string) (*entity.AccessToken, error)
10+
GetToken(ctx context.Context, code, requestURL string) (*entity.AccessToken, error)
1111
GetTokenByRefresh(ctx context.Context, refreshToken string) (*entity.AccessToken, error)
1212
RevokeToken(ctx context.Context, req *entity.RevokeTokenRequest) error
1313

handler/http/discord.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"github.com/labstack/echo/v4"
77
"github.com/mocha-bot/mochus/config"
88
"github.com/mocha-bot/mochus/core/module"
9-
cookiey "github.com/mocha-bot/mochus/pkg/cookie"
9+
cookiey "github.com/mocha-bot/mochus/pkg/cookiey"
1010
)
1111

1212
type discordHandler struct {

handler/http/middleware/redirect.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package http_middleware
2+
3+
import (
4+
"github.com/labstack/echo/v4"
5+
)
6+
7+
func FallbackRedirect(host string) echo.MiddlewareFunc {
8+
return func(next echo.HandlerFunc) echo.HandlerFunc {
9+
return func(c echo.Context) error {
10+
c.Request().Header.Set("X-Fallback-Host", host)
11+
return next(c)
12+
}
13+
}
14+
}

handler/http/parser.discord.go

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88

99
"github.com/labstack/echo/v4"
1010
"github.com/mocha-bot/mochus/core/entity"
11-
cookiey "github.com/mocha-bot/mochus/pkg/cookie"
11+
cookiey "github.com/mocha-bot/mochus/pkg/cookiey"
1212
"github.com/mocha-bot/mochus/pkg/echoy"
1313
zLog "github.com/rs/zerolog/log"
1414
)
@@ -26,15 +26,21 @@ func parseOauthCallbackRequest(c echo.Context) (*entity.OauthCallbackRequest, er
2626
}
2727

2828
redirectURL := parsedURL.Query().Get(RedirectURLKey)
29-
if redirectURL == "" {
30-
return nil, fmt.Errorf("%w: %s", entity.ErrorBind, "redirect_url is required")
31-
}
3229

30+
// Construct the final URL for the request URL
31+
// Discord known this as a redirect_uri to verify the request
3332
finalURL := url.URL{
34-
Scheme: echoy.GetScheme(c),
35-
Host: c.Request().Host,
36-
Path: c.Request().URL.Path,
37-
RawQuery: url.Values{RedirectURLKey: {redirectURL}}.Encode(),
33+
Scheme: echoy.GetScheme(c),
34+
Host: c.Request().Host,
35+
Path: c.Request().URL.Path,
36+
}
37+
38+
// The redirect URL is optional for the client redirection
39+
// If it's not provided, the fallback host will be used
40+
if redirectURL == "" {
41+
req.RedirectURL = c.Request().Header.Get("X-Fallback-Host")
42+
} else {
43+
finalURL.RawQuery = url.Values{RedirectURLKey: {req.RedirectURL}}.Encode()
3844
}
3945

4046
req.RequestURL, err = url.QueryUnescape(finalURL.String())
File renamed without changes.

repository/discord/discord.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ func NewDiscordRepository(cfg config.DiscordConfig) repository.DiscordRepository
2424
}
2525
}
2626

27-
func (d *discordRepository) GetToken(ctx context.Context, code, redirectURL string) (*entity.AccessToken, error) {
27+
func (d *discordRepository) GetToken(ctx context.Context, code, requestURL string) (*entity.AccessToken, error) {
2828
var response AccessTokenResponse
2929

3030
headers := map[string]string{
@@ -34,7 +34,7 @@ func (d *discordRepository) GetToken(ctx context.Context, code, redirectURL stri
3434
payload := map[string]string{
3535
"grant_type": GrantTypeAuthorizationCode,
3636
"code": code,
37-
"redirect_uri": redirectURL,
37+
"redirect_uri": requestURL,
3838
}
3939

4040
req := d.client.R().

0 commit comments

Comments
 (0)