Class: OmniAuth::Strategies::LDAP

Inherits:
Object
  • Object
show all
Includes:
OmniAuth::Strategy
Defined in:
lib/omniauth/strategies/ldap.rb

Defined Under Namespace

Classes: MissingCredentialsError

Constant Summary collapse

@@config =
{
  'name' => 'cn',
  'first_name' => 'givenName',
  'last_name' => 'sn',
  'email' => ['mail', "email", 'userPrincipalName'],
  'phone' => ['telephoneNumber', 'homePhone', 'facsimileTelephoneNumber'],
  'mobile' => ['mobile', 'mobileTelephoneNumber'],
  'nickname' => ['uid', 'userid', 'sAMAccountName'],
  'title' => 'title',
  'location' => {"%0, %1, %2, %3 %4" => [['address', 'postalAddress', 'homePostalAddress', 'street', 'streetAddress'], ['l'], ['st'],['co'],['postOfficeBox']]},
  'uid' => 'dn',
  'url' => ['wwwhomepage'],
  'image' => 'jpegPhoto',
  'description' => 'description'
}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, *args, &block) ⇒ LDAP

Returns a new instance of LDAP.



28
29
30
31
# File 'lib/omniauth/strategies/ldap.rb', line 28

def initialize(app, *args, &block)
  super
  @adaptor = OmniAuth::LDAP::Adaptor.new @options
end

Class Method Details

.map_user(mapper, object) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/omniauth/strategies/ldap.rb', line 63

def self.map_user(mapper, object)
  user = {}
  mapper.each do |key, value|
    case value
      when String
        user[key] = object[value.downcase.to_sym].first if object[value.downcase.to_sym]
      when Array
        value.each {|v| (user[key] = object[v.downcase.to_sym].first; break;) if object[v.downcase.to_sym]}
      when Hash
        value.map do |key1, value1|
          pattern = key1.dup
          value1.each_with_index do |v,i|
            part = ''; v.collect(&:downcase).collect(&:to_sym).each {|v1| (part = object[v1].first; break;) if object[v1]}
            pattern.gsub!("%#{i}",part||'')
          end
          user[key] = pattern
        end
      end
  end
  user
end

Instance Method Details

#callback_phaseObject



40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/omniauth/strategies/ldap.rb', line 40

def callback_phase
  raise MissingCredentialsError.new("Missing login credentials") if request['username'].nil? || request['password'].nil?
  begin
  @ldap_user_info = @adaptor.bind_as(:filter => Net::LDAP::Filter.eq(@adaptor.uid, @options[:name_proc].call(request['username'])),:size => 1, :password => request['password'])
  return fail!(:invalid_credentials) if !@ldap_user_info

  @user_info = self.class.map_user(@@config, @ldap_user_info)
  super
  rescue Exception => e
    return fail!(:ldap_error, e)
  end
end

#request_phaseObject



32
33
34
35
36
37
38
# File 'lib/omniauth/strategies/ldap.rb', line 32

def request_phase
  f = OmniAuth::Form.new(:title => (options[:title] || "LDAP Authentication"), :url => callback_path)
  f.text_field 'Login', 'username'
  f.password_field 'Password', 'password'
  f.button "Sign In"
  f.to_response
end