This library is for building a custom SAML 2.0 IdP with minimal headache. A simple example of a Rails controller that just passes on an already authenticated user to a single SP.
require 'saml2'
class SamlIdpController < ApplicationController
def create
authn_request = SAML2::AuthnRequest.decode(params[:SAMLRequest])
unless authn_request.valid_schema? &&
authn_request.valid_interoperable_profile? &&
authn_request.resolve(self.class.service_provider)
flash[:error] = "Invalid login request"
return redirect_to @current_user ? root_url : login_url
end
if @current_user
response = SAML2::Response.respond_to(authn_request,
NameID.new(self.class.entity_id),
self.class.idp_name_id(@current_user, sp))
response.sign(self.class.x509_certificate, self.class.private_key)
@saml_response = Base64.encode64(response.to_xml)
@saml_acs_url = authn_request.assertion_consumer_service.location
@relay_state = params[:RelayState]
render template: "saml2/http_post", layout: false
else
redirect_to login_url
end
end
protected
def self.idp_name_id(user)
SAML2::NameID.new(user.uuid, SAML2::NameID::Format::PERSISTENT)
end
def self.saml_config
@config ||= YAML.load(File.read('saml.yml'))
end
def self.service_provider
@sp ||= SAML2::Entity.parse(File.read(saml_config[:service_provider])).roles.first
end
def self.entity_id
saml_config[:entity_id]
end
def self.x509_certificate
@cert ||= File.read(saml_config[:encryption][:certificate])
end
def self.private_key
@key ||= File.read(saml_config[:encryption][:private_key])
end
def self.signature_algorithm
saml_config[:encryption][:algorithm]
end
endCopyright (c) 2015 Instructure, Inc. See LICENSE for details.

