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"` OpenidSigned string `form:"openid.signed"`
OpenidSig string `form:"openid.sig"` OpenidSig string `form:"openid.sig"`
} }
SteamLoginCallbackResp { // No response needed - endpoint will redirect to frontend
Success bool `json:"success"`
SteamId string `json:"steamId,omitempty"`
Message string `json:"message,omitempty"`
}
) )
@server ( @server (
@@ -60,9 +56,9 @@ service Authenticator {
@doc ( @doc (
summary: "Steam login callback" 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 @handler steamLoginCallbackHandler
get /steam/callback (SteamLoginCallbackReq) returns (SteamLoginCallbackResp) get /steam/callback (SteamLoginCallbackReq)
} }

View File

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

View File

@@ -13,4 +13,5 @@ type Config struct {
type SteamConfig struct { type SteamConfig struct {
CallbackURL string CallbackURL string
FrontendCallbackURL string
} }

View File

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

View File

@@ -23,11 +23,5 @@ type SteamLoginCallbackReq struct {
OpenidSig string `form:"openid.sig"` 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 { type SteamLoginInitReq struct {
} }