Skip to content

Instantly share code, notes, and snippets.

@rondale-sc
Created March 22, 2012 05:57
Show Gist options
  • Select an option

  • Save rondale-sc/2156513 to your computer and use it in GitHub Desktop.

Select an option

Save rondale-sc/2156513 to your computer and use it in GitHub Desktop.
omniauth-and-other-drugs
#config/initializers/omniauth.rb
provider :LDAP,
:host => '0.0.0.0',
:port => 389,
:method => :plain,
:base => 'dc=example,dc=com',
:uid => 'samaccountname',
:bind_dn => 'Authorized User',
:password => 'Password',
:name_proc => Proc.new {|name| name.gsub(/@.*$/,'') }
provider :identity
# First we abstract the parameters of the LDAP
# strategy for use in our callback.
provider :LDAP,
ldap_parameters = { :host => '0.0.0.0',
:port => 389,
:method => :plain,
:base => 'dc=example,dc=com',
:uid => 'samaccountname',
:bind_dn => 'Authorized User',
:password => 'Password',
:name_proc => Proc.new {|name| name.gsub(/@.*$/,'') }
}
provider :identity
#----- Focus on this code! ------------------------------------
on_failure do |env|
# Set up variables in case LDAP fails.
# this is the ugliest part of all of this.
# especially the strategy gsub (Yuck)
message_key = env['omniauth.error.type']
strategy = env['omniauth.error.strategy'].class.name.gsub('OmniAuth::Strategies::','')
new_path = "#{OmniAuth.config.path_prefix}/failure?provider=#{strategy}&message=#{message_key}"
# If they failed to authenticate with default strategy. Use LDAP strategy.
if env['omniauth.error.strategy'].class == OmniAuth::Strategies::Identity
ldap_provider = OmniAuth::Strategies::LDAP.new( @app, ldap_parameters)
env['rack.request.form_hash']['username'] = env['rack.request.form_hash']['auth_key']
ldap_provider.instance_variable_set(:@env, env)
ldap_provider.callback_call # run LDAP strategy
else
[302, {'Location' => new_path, 'Content-Type'=> 'text/html'}, []]
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment