package public import ( "encoding/json" "errors" "net/http" "git.sendico.io/tech/sxchnge/product/auth/http/v1/request" "git.sendico.io/tech/sxchnge/shared/http/response" "git.sendico.io/tech/sxchnge/shared/service" "go.uber.org/zap" ) func (pr *PublicRouter) rotateRefreshToken(r *http.Request) http.HandlerFunc { pr.logger.Debug("Processing token rotation request...") var req srequest.TokenRefreshRotate if err := json.NewDecoder(r.Body).Decode(&req); err != nil { pr.logger.Info("Failed to decode token rotation request", zap.Error(err)) return response.BadPayload(pr.logger, mservice.RefreshTokens, err) } account, _, _, err := pr.validateRefreshToken(r.Context(), r, &req) if err != nil { if errors.Is(err, errClientIPNotAllowed) { return response.Forbidden(pr.logger, pr.service, "ip_not_allowed", "request ip is not allowed for this client") } if isExpectedRefreshValidationError(err) { pr.logger.Debug("Refresh token rotation rejected", zap.Error(err)) return pr.rejectRefresh(r, err) } pr.logger.Warn("Failed to validate refresh token", zap.Error(err)) return response.Auto(pr.logger, pr.service, err) } return pr.refreshAndRespondLogin(r.Context(), r, &req.SessionIdentifier, account) }