Created
March 22, 2012 05:57
-
-
Save rondale-sc/2156513 to your computer and use it in GitHub Desktop.
omniauth-and-other-drugs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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