将 SAML 集成到 Rails 应用程序中

Ani*_*ari 10 ruby ruby-on-rails saml ruby-saml ruby-on-rails-6

我正在尝试在我的项目中添加ruby​​-saml。但我对如何在我的场景中实现它有点困惑。我在一个网站上,比如说 abc.com,有一个按钮。当我单击该按钮时,我需要重定向到网站 xyz.com,在该网站中我需要传递 SAML XML 并将其发送到 xyz.com/SAML。SAML 请求将由 xyz.com 处理,然后他们将向我发送响应。谁能给我一些想法如何实现它?

另外,我对这些领域感到困惑。有人可以给我一个快速总结吗?

settings.assertion_consumer_service_urlsettings.sp_entity_idsettings.idp_entity_idsettings.idp_sso_service_urlsettings.idp_slo_service_url




def init
  request = OneLogin::RubySaml::Authrequest.new
  saml_settings.name_identifier_value_requested = "testuser@example.com"
  saml_settings.name_identifier_format = "urn:oasis:names:tc:SAML:1.1:nameid-format:emailAddress"
  redirect_to(request.create(saml_settings))
end



 def saml_settings
  settings = OneLogin::RubySaml::Settings.new

  settings.assertion_consumer_service_url = "http://#{request.host}/saml/consume"
  settings.idp_sso_service_url            = "https://app.onelogin.com/trust/saml2/http-post/sso/#{OneLoginAppId}"
  settings.idp_sso_service_binding        = "urn:oasis:names:tc:SAML:2.0:bindings:HTTP-POST" # or :post, :redirect
  settings.idp_slo_service_binding        = "urn:oasis:names:tc:SAML:2.0:bindings:HTTP-Redirect" # or :post, :redirect
  settings.idp_cert_fingerprint           = OneLoginAppCertFingerPrint
  settings.idp_cert_fingerprint_algorithm = "http://www.w3.org/2000/09/xmldsig#sha1"
  settings.name_identifier_format         = "urn:oasis:names:tc:SAML:1.1:nameid-format:emailAddress"

  # Optional for most SAML IdPs
  settings.authn_context = "urn:oasis:names:tc:SAML:2.0:ac:classes:PasswordProtectedTransport"
  # or as an array
  settings.authn_context = [
    "urn:oasis:names:tc:SAML:2.0:ac:classes:PasswordProtectedTransport",
    "urn:oasis:names:tc:SAML:2.0:ac:classes:Password"
  ]

  # Optional bindings (defaults to Redirect for logout POST for ACS)
  settings.single_logout_service_binding      = "urn:oasis:names:tc:SAML:2.0:bindings:HTTP-Redirect" # or :post, :redirect
  settings.assertion_consumer_service_binding = "urn:oasis:names:tc:SAML:2.0:bindings:HTTP-POST" # or :post, :redirect

  settings
end
Run Code Online (Sandbox Code Playgroud)

stw*_*ert 13

你想要_____吗:

  • a.) 启用来自其他服务的 SAML 登录,例如 Microsoft Azure Directory、OneLogin 等?(那么你就是SP=服务提供商)
  • b.) 您的应用程序拥有用户并为其他应用程序提供登录服务(IDP = 身份提供商)

我想这是一个)?然后取决于:它是每个客户的提供商吗?它是动态的吗?每个客户都可以配置自己的 IDP,还是为整个应用程序固定一个?如果是后者,那么我强烈建议使用omniauth-saml它,这样配置起来要容易得多。

但如果您想使用“按客户企业登录”,那么我们就是这样做的。

  • 首先:我从来没有纠结过所有的具体设置和 url,Saml 可以通过“元数据 url”自动配置自身,我们让客户指定一个元数据 uri,该 uri 包含我们需要的所有信息,因此我们可以使用提供的解析它班级:
idp_metadata_parser = OneLogin::RubySaml::IdpMetadataParser.new
settings = idp_metadata_parser.parse_remote(organisation.idp_meta_data_url)
Run Code Online (Sandbox Code Playgroud)

然后我们添加自己的设置+路由信息:

   settings.assertion_consumer_service_url = "https://#{request.host}/saml/consume/#{organisation.id}"
        settings.issuer                         = "https://#{@request.host}/saml/metadata/#{organisation.id}"
        settings.name_identifier_format         = "urn:oasis:names:tc:SAML:1.1:nameid-format:emailAddress"
        # Optional for most SAML IdPs
        settings.authn_context = "urn:oasis:names:tc:SAML:2.0:ac:classes:PasswordProtectedTransport"
# You would need a normal certificate/private key to enable signature stuff
        settings.certificate = File.read('config/saml/certificate.crt')
        settings.private_key = File.read('config/saml/private_key.key')
# In our case customer can optional activate signature validation:
        if organisation.signature_enabled?
          settings.security[:authn_requests_signed]   = true     # Enable or not signature on AuthNRequest
          settings.security[:logout_requests_signed]  = true     # Enable or not signature on Logout Request
          settings.security[:logout_responses_signed] = true     # Enable or not signature on Logout Response
          settings.security[:want_assertions_signed]  = true     # Enable or not the requirement of signed assertion
          settings.security[:metadata_signed]         = true     # Enable or not signature on Metadata

          settings.security[:digest_method]    = XMLSecurity::Document::SHA1
          settings.security[:signature_method] = XMLSecurity::Document::RSA_SHA1
        end
Run Code Online (Sandbox Code Playgroud)

请求/响应

这些或多或少来自 ruby​​-saml 示例:

控制器:

  skip_before_action :verify_authenticity_token

  def init
    @saml_request = OneLogin::RubySaml::Authrequest.new
    redirect_url = @saml_request.create(saml_settings)
    if redirect_uri
      redirect_to(redirect_uri)
    else
      @error = t('saml_controller.error')
      render 'error'
    end
  end

  def consume
    @saml_response = OneLogin::RubySaml::Response.new(params[:SAMLResponse])
    @saml_response.settings = saml_settings
  
    if @saml_response.is_valid?
      # do application logic, create user/update user sign in user
      sign_in(....) 
      session[:session_valid_for] = 12.hours.from_now.to_i
      redirect_to '/'
      else
        redirect_to '/watcher/profile'
      end
    else
      @error = @saml_response.errors
      render 'error'
    end
  end
Run Code Online (Sandbox Code Playgroud)

元数据

大多数客户也需要元数据 uri,以将 SP 添加到他们的 IDP 中(并自动配置该部分),因此您还需要提供元数据,例如“/saml/#{org.id}/metadata”和返回元数据:

def metadata
  meta = OneLogin::RubySaml::Metadata.new
  render xml: meta.generate(saml_settings), content_type: "application/samlmetadata+xml"
end
Run Code Online (Sandbox Code Playgroud)

更新:使用omniauth-saml

我们还在应用程序中使用 Omniauth saml。它非常简单,但配置取决于您要集成的服务器。为了安全起见,您将需要一个 sso 目标 url(来自另一端的使用 url)和证书指纹或证书。您还可以指定“名称标识符”、电子邮件或用户名或类似的内容。

   settings.assertion_consumer_service_url = "https://#{request.host}/saml/consume/#{organisation.id}"
        settings.issuer                         = "https://#{@request.host}/saml/metadata/#{organisation.id}"
        settings.name_identifier_format         = "urn:oasis:names:tc:SAML:1.1:nameid-format:emailAddress"
        # Optional for most SAML IdPs
        settings.authn_context = "urn:oasis:names:tc:SAML:2.0:ac:classes:PasswordProtectedTransport"
# You would need a normal certificate/private key to enable signature stuff
        settings.certificate = File.read('config/saml/certificate.crt')
        settings.private_key = File.read('config/saml/private_key.key')
# In our case customer can optional activate signature validation:
        if organisation.signature_enabled?
          settings.security[:authn_requests_signed]   = true     # Enable or not signature on AuthNRequest
          settings.security[:logout_requests_signed]  = true     # Enable or not signature on Logout Request
          settings.security[:logout_responses_signed] = true     # Enable or not signature on Logout Response
          settings.security[:want_assertions_signed]  = true     # Enable or not the requirement of signed assertion
          settings.security[:metadata_signed]         = true     # Enable or not signature on Metadata

          settings.security[:digest_method]    = XMLSecurity::Document::SHA1
          settings.security[:signature_method] = XMLSecurity::Document::RSA_SHA1
        end
Run Code Online (Sandbox Code Playgroud)

请参阅omniauth-saml 的自述文件以获取所有选项的列表。您还可以使用以下命令从 OneLogin/IDP 请求更多属性request_attributes

:request_attributes - 用于构建元数据文件,以通知 IdP 将某些属性与 SAMLResponse 消息一起发送。默认请求 name、first_name、last_name 和 email 属性。请参阅 Ruby SAML gem 中的 OneLogin::RubySaml::AttributeService 类,了解每个属性的可用选项。

更新:将动态参数传递给omniauth saml的重定向到Idp:

Omniauth-saml 中似乎没有提供动态参数的选项,因此您可以尝试修补/覆盖该行为。Omniauth-SAML 是一个 Rack 中间件,因此您只能访问请求对象,而不能访问普通的 Rails 内容。如果您信任您的用户(您不应该信任),那么您可以将参数中的信息放入omniauth:,/auth/saml?something1=foo&bar=2或者您可以使用 ActiveSupport Message Encryptor 加密参数。

如果您知道如何从请求中提取动态参数,则可以动态应用此补丁,因为 Ruby!

# put this into
#
# config/initializers/omniauth_patch.rb
#
module OmniauthPatch
  def additional_params_for_authn_request
    # here you should have access to the current request
    # try around with binding.irb what you can do
    binding.irb
    # return parameters you want to pass to the saml redirect
    {
      email: email,
      # ...
    }
  end
end

OmniAuth::Strategies::SAML.include OmniauthPatch
Run Code Online (Sandbox Code Playgroud)