Refactor Steam login callback to remove response struct and handle redirects directly
All checks were successful
CI - Build and Push / Build and Push Docker Image (push) Successful in 58s

This commit is contained in:
2025-10-08 13:02:39 +08:00
parent 5afd712701
commit 4ca8e1db57
6 changed files with 36 additions and 38 deletions

View File

@@ -33,11 +33,7 @@ type (
OpenidSigned string `form:"openid.signed"`
OpenidSig string `form:"openid.sig"`
}
SteamLoginCallbackResp {
Success bool `json:"success"`
SteamId string `json:"steamId,omitempty"`
Message string `json:"message,omitempty"`
}
// No response needed - endpoint will redirect to frontend
)
@server (
@@ -60,9 +56,9 @@ service Authenticator {
@doc (
summary: "Steam login callback"
description: "Handles the callback from Steam after user authentication"
description: "Validates Steam authentication and redirects to frontend with status"
)
@handler steamLoginCallbackHandler
get /steam/callback (SteamLoginCallbackReq) returns (SteamLoginCallbackResp)
get /steam/callback (SteamLoginCallbackReq)
}

View File

@@ -4,6 +4,7 @@ Port: 8888
Steam:
CallbackURL: https://www.cialloo.com/api/authenticator/steam/callback
FrontendCallbackURL: https://www.cialloo.com/auth/callback
Redis:
Host: redis.production.svc.cluster.local:6379

View File

@@ -12,5 +12,6 @@ type Config struct {
}
type SteamConfig struct {
CallbackURL string
CallbackURL string
FrontendCallbackURL string
}

View File

@@ -3,10 +3,11 @@ package handler
import (
"net/http"
"github.com/zeromicro/go-zero/rest/httpx"
"src/internal/logic"
"src/internal/svc"
"src/internal/types"
"github.com/zeromicro/go-zero/rest/httpx"
)
// Steam login callback
@@ -19,11 +20,10 @@ func steamLoginCallbackHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
}
l := logic.NewSteamLoginCallbackLogic(r.Context(), svcCtx)
resp, err := l.SteamLoginCallback(&req)
err := l.SteamLoginCallback(&req, w, r)
if err != nil {
httpx.ErrorCtx(r.Context(), w, err)
} else {
httpx.OkJsonCtx(r.Context(), w, resp)
}
// No response needed - redirect is handled in logic
}
}

View File

@@ -3,6 +3,8 @@ package logic
import (
"context"
"fmt"
"net/http"
"net/url"
"src/internal/svc"
"src/internal/types"
@@ -26,9 +28,12 @@ func NewSteamLoginCallbackLogic(ctx context.Context, svcCtx *svc.ServiceContext)
}
}
func (l *SteamLoginCallbackLogic) SteamLoginCallback(req *types.SteamLoginCallbackReq) (resp *types.SteamLoginCallbackResp, err error) {
// Convert request to map for Steam validation
paramsMap := map[string]string{
func (l *SteamLoginCallbackLogic) SteamLoginCallback(req *types.SteamLoginCallbackReq, w http.ResponseWriter, r *http.Request) error {
// Get the frontend callback URL from config
frontendCallbackURL := l.svcCtx.Config.Steam.FrontendCallbackURL
// Convert the request to a map for validation
openidParams := map[string]string{
"openid.mode": req.OpenidMode,
"openid.ns": req.OpenidNs,
"openid.op_endpoint": req.OpenidOpEndpoint,
@@ -41,29 +46,30 @@ func (l *SteamLoginCallbackLogic) SteamLoginCallback(req *types.SteamLoginCallba
"openid.sig": req.OpenidSig,
}
// Validate the response with Steam
steamID, isValid, err := steamauth.ValidateResponse(paramsMap)
// Validate the Steam OpenID response
steamID, isValid, err := steamauth.ValidateResponse(openidParams)
if err != nil {
l.Logger.Errorf("Steam validation error: %v", err)
return &types.SteamLoginCallbackResp{
Success: false,
Message: fmt.Sprintf("Validation error: %v", err),
}, nil
l.Logger.Errorf("Failed to validate Steam OpenID response: %v", err)
// Redirect to frontend with error status
redirectURL := fmt.Sprintf("%s?status=error&message=%s", frontendCallbackURL, url.QueryEscape("Failed to validate Steam response"))
http.Redirect(w, r, redirectURL, http.StatusFound)
return nil
}
if !isValid {
l.Logger.Info("Steam validation failed: invalid credentials")
return &types.SteamLoginCallbackResp{
Success: false,
Message: "Invalid Steam credentials",
}, nil
l.Logger.Infof("Invalid Steam OpenID response")
// Redirect to frontend with failure status
redirectURL := fmt.Sprintf("%s?status=failed&message=%s", frontendCallbackURL, url.QueryEscape("Steam authentication failed"))
http.Redirect(w, r, redirectURL, http.StatusFound)
return nil
}
// Successful validation
l.Logger.Infof("Steam login successful for Steam ID: %s", steamID)
return &types.SteamLoginCallbackResp{
Success: true,
SteamId: steamID,
Message: "Login successful",
}, nil
// Redirect to frontend with success status and Steam ID
redirectURL := fmt.Sprintf("%s?status=success&steamId=%s", frontendCallbackURL, steamID)
http.Redirect(w, r, redirectURL, http.StatusFound)
return nil
}

View File

@@ -23,11 +23,5 @@ type SteamLoginCallbackReq struct {
OpenidSig string `form:"openid.sig"`
}
type SteamLoginCallbackResp struct {
Success bool `json:"success"`
SteamId string `json:"steamId,omitempty"`
Message string `json:"message,omitempty"`
}
type SteamLoginInitReq struct {
}