Class: Rails::Auth::X509::Middleware

Inherits:
Object
  • Object
show all
Defined in:
lib/rails/auth/x509/middleware.rb

Overview

Extracts X.509 client certificates and adds credential objects to the rack environment as env[“x509”]

Instance Method Summary collapse

Constructor Details

#initialize(app, cert_filters: {}, logger: nil) ⇒ Rails::Auth::X509::Middleware

Create a new X.509 Middleware object

Parameters:

  • app (Object)

    next app in the Rack middleware chain

  • cert_filters (Hash) (defaults to: {})

    maps Rack environment names to cert extractors

  • logger (Logger) (defaults to: nil)

    place to log certificate extraction issues



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/rails/auth/x509/middleware.rb', line 16

def initialize(app, cert_filters: {}, logger: nil)
  @app          = app
  @cert_filters = cert_filters
  @logger       = logger

  @cert_filters.each do |key, filter|
    next unless filter.is_a?(Symbol)

    # Convert snake_case to CamelCase
    filter_name = filter.to_s.split("_").map(&:capitalize).join

    # Shortcut syntax for symbols
    @cert_filters[key] = Rails::Auth::X509::Filter.const_get(filter_name).new
  end
end

Instance Method Details

#call(env) ⇒ Object



32
33
34
35
36
37
# File 'lib/rails/auth/x509/middleware.rb', line 32

def call(env)
  credential = extract_credential(env)
  Rails::Auth.add_credential(env, "x509", credential.freeze) if credential

  @app.call(env)
end