Finalize a SAML Request and get the response definition for success or failure. The response must be handled per the SAML specification to inform the application.
class SamlController < ApplicationController def sso # Receive SAML request saml_request_id = decode_saml_request(params[:SAMLRequest]) # Get request details saml_request = client.saml.get_saml_request( Zitadel::Client::SAMLServiceGetSAMLRequestRequest.new( saml_request_id: saml_request_id ) ) # Check if user is authenticated if current_user finalize_saml_authentication(saml_request_id, saml_request) else # Store request ID and redirect to login session[:saml_request_id] = saml_request_id redirect_to login_path end end def callback # Called after successful authentication saml_request_id = session[:saml_request_id] # Create SAML response saml_response = client.saml.create_response( Zitadel::Client::SAMLServiceCreateResponseRequest.new( saml_request_id: saml_request_id ) ) # Get original request for ACS URL saml_request = client.saml.get_saml_request( Zitadel::Client::SAMLServiceGetSAMLRequestRequest.new( saml_request_id: saml_request_id ) ) # POST back to Service Provider render_saml_post_form( acs_url: saml_request.acs_url, saml_response: saml_response.saml_response, relay_state: saml_response.relay_state ) endend
begin response = client.saml.create_response(request)rescue Zitadel::Client::ApiError => e case e.code when 400 # Invalid SAML request format render_saml_error("Invalid SAML request: #{e.message}") when 404 # SAML request not found or already processed render_saml_error("SAML request not found or expired") when 403 # Insufficient permissions render_saml_error("Insufficient permissions to process SAML request") else # Other errors Rails.logger.error("SAML error: #{e.message}") render_saml_error("An error occurred during SAML authentication") endend
If you attempt to call create_response multiple times for the same SAML request, you’ll receive a 404 error. SAML requests can only be finalized once.Solution: Track which requests have been processed and prevent duplicate submissions.
Expired SAML Request
SAML requests have a limited lifetime. Expired requests cannot be processed.Solution: Handle authentication promptly after receiving the SAML request. Consider implementing a timeout warning for users.
Invalid Request ID
Passing an invalid or malformed SAML request ID will result in a 404 error.Solution: Validate the request ID format before making API calls and handle decoding errors gracefully.