Rails/Devise/SAML 元数据不正确(不适用于 PingFederate)

Rails/Devise/SAML Metadata Incorrect (not working with PingFederate)

让我先说一下我是 SAML 的新手,几乎不了解它是如何工作的。

设置

我正在使用 devise_saml_authenticatable gem 和 Rails 4 应用程序来实现 SSO。 Rails 应用充当服务提供者 (SP)。为了测试我的设置,我创建了一个 OneLogin 开发者帐户并使用以下属性设置了一个 SAML Test Connector (IdP w/attr w/ sign response)

配置选项卡

SSO 选项卡

在我的 devise.rb 中,我有以下配置:

config.saml_create_user = false
config.saml_update_user = true
config.saml_default_user_key = :email
config.saml_session_index_key = :session_index
config.saml_use_subject = true
config.idp_settings_adapter = IdPSettingsAdapter
config.idp_entity_id_reader = DeviseSamlAuthenticatable::DefaultIdpEntityIdReader

这是我的 IdPSettingsAdapter:

class IdPSettingsAdapter
    def self.settings(idp_entity_id)
        company = Company.find_by(idp_entity_id: idp_entity_id)

        if company.present?
            {
                assertion_consumer_service_url: company.assertion_consumer_service_url,
                assertion_consumer_service_binding: company.assertion_consumer_service_binding,
                name_identifier_format: company.name_identifier_format,
                issuer: company.issuer,
                idp_entity_id: company.idp_entity_id,
                authn_context: company.authn_context,
                idp_slo_target_url: company.idp_slo_target_url,
                idp_sso_target_url: company.idp_sso_target_url,
                idp_cert_fingerprint: company.idp_cert_fingerprint
            }
        else
            {}
        end
    end
end

请注意,我的用户模型 Contact belongs_to Company,并且 SSO 设置存储在 Company 模型中。

这是我的 saml 路由:

devise_for :contacts, skip: :saml_authenticatable, controllers: {
    registrations: "registrations",
    sessions: "sessions",
    passwords: "passwords",
    confirmations: "confirmations"
}
devise_scope :contact do
    get '/sign_in' => 'sessions#new'
    get '/sign_out' => 'sessions#destroy'

    # SSO Routes
    get 'saml/sign_in' => 'saml_sessions#new', as: :new_user_sso_session
    post 'saml/auth' => 'saml_sessions#create', as: :user_sso_session
    get 'saml/sign_out' => 'saml_sessions#destroy', as: :destroy_user_sso_session
    get 'saml/metadata' => 'saml_sessions#metadata', as: :metadata_user_sso_session
    match 'saml/idp_sign_out' => 'saml_sessions#idp_sign_out', via: [:get, :post]
end

最后是我的 SamlSessionsController:

require "ruby-saml"

class SamlSessionsController < SessionsController
    include DeviseSamlAuthenticatable::SamlConfig
    skip_before_filter :verify_authenticity_token, raise: false
    before_action :authorize_viewer, except: [:metadata]
    protect_from_forgery with: :null_session, except: :create

    def new
        idp_entity_id = Company.friendly.find(@_request.env['HTTP_HOST'].split('.')[0]).idp_entity_id
        request = OneLogin::RubySaml::Authrequest.new
        action = request.create(saml_config(idp_entity_id))
        redirect_to action
    end

    def metadata
        idp_entity_id = Company.friendly.find(@_request.env['HTTP_HOST'].split('.')[0]).idp_entity_id
        meta = OneLogin::RubySaml::Metadata.new
        render :xml => meta.generate(saml_config(idp_entity_id)), content_type: 'application/samlmetadata+xml'
    end

    def create
        @idp_entity_id = Company.friendly.find(@_request.env['HTTP_HOST'].split('.')[0]).idp_entity_id
        response = OneLogin::RubySaml::Response.new(params[:SAMLResponse], settings: saml_config(@idp_entity_id))

        if !response.is_valid?
            puts "SAML FAILED WITH ERROR: "
            puts response.errors
        end
        super
    end

    def idp_sign_out
        company = Company.friendly.find(request.subdomain.downcase)
        idp_entity_id = Company.friendly.find(@_request.env['HTTP_HOST'].split('.')[0]).idp_entity_id

        if params[:SAMLRequest] && Devise.saml_session_index_key
            saml_config = saml_config(idp_entity_id)
            logout_request = OneLogin::RubySaml::SloLogoutrequest.new(params[:SAMLRequest], settings: saml_config(idp_entity_id))
            resource_class.reset_session_key_for(logout_request.name_id)

            # binding.pry
            sign_out current_contact if contact_signed_in?
            redirect_to company.after_slo_url.present? ? company.after_slo_url : 'https://' + company.issuer
            # redirect_to generate_idp_logout_response(saml_config(idp_entity_id), logout_request.id)
        elsif params[:SAMLResponse]
            #Currently Devise handles the session invalidation when the request is made.
            #To support a true SP initiated logout response, the request ID would have to be tracked and session invalidated
            #based on that.
            if Devise.saml_sign_out_success_url
                redirect_to Devise.saml_sign_out_success_url
            else
                redirect_to action: :new
            end
        else
            head :invalid_request
        end
    end

    protected

    # Override devise to send user to IdP logout for SLO
    def after_sign_out_path_for(_)
        request = OneLogin::RubySaml::Logoutrequest.new
        request.create(saml_config)
    end

    def generate_idp_logout_response(saml_config, logout_request_id)
        OneLogin::RubySaml::SloLogoutresponse.new.create(saml_config, logout_request_id, nil)
    end
end

问题

当我手动将我的 OneLogin 适配器的设置保存到我的 Company 模型时(见屏幕截图),我能够使用 OneLogin 作为身份提供者 (IdP) 作为我的应用程序的用户进行身份验证.但是现在我需要向客户端提供代表应用程序设置的 XML 元数据。当我转到 /saml/metadata.xml 时,我得到以下配置,根据我的客户,这是不正确的。客户没有提供有关问题所在的任何进一步详细信息。如果重要的话,他们正在使用 PingFederate。

<?xml version='1.0' encoding='UTF-8'?>
<md:EntityDescriptor ID='_a3581975-b73d-4784-a106-bafd61e15f87' xmlns:md='urn:oasis:names:tc:SAML:2.0:metadata'>
    <md:SPSSODescriptor AuthnRequestsSigned='false' WantAssertionsSigned='false' protocolSupportEnumeration='urn:oasis:names:tc:SAML:2.0:protocol'>
        <md:NameIDFormat>urn:oasis:names:tc:SAML:2.0:nameid-format:transient</md:NameIDFormat>
        <md:AssertionConsumerService Binding='urn:oasis:names:tc:SAML:2.0:bindings:HTTP-POST' Location='https://mysubdomain.myapp.local:3000/saml/auth' index='0' isDefault='true'/>
    </md:SPSSODescriptor>
</md:EntityDescriptor>

我的问题是,我在这里做错了什么,我该如何纠正?正如我所说,我几乎不了解 SAML 的工作原理。

该元数据上没有定义 EntityID XML。 如果您尝试在 validation tool 上验证 XML,您将得到

Line: 2 | Column: 0 --> Element '{urn:oasis:names:tc:SAML:2.0:metadata}EntityDescriptor': The attribute 'entityID' is required but missing.

如果您查看 ruby-saml 代码,如果定义了 settings.issuer,EntityID 将添加到元数据 XML。您能否验证是否提供了该数据?也许我在 IdPSettingsAdapter class 看到的 company.issuer 有一个空值。