Module: LdapAuthenticator

Defined in:
lib/bn-ldap-authentication.rb

Constant Summary collapse

LDAP_ATTRIBUTE_MAPPING =
{
  'uid' => [:dn],
  'name' => [:cn, :displayName],
  'first_name' => [:givenName],
  'last_name' => [:sn],
  'email' => [:mail, :email, :userPrincipalName],
  'nickname' => [:uid, :userid, :sAMAccountName],
  'image' => [:jpegPhoto]
}

Instance Method Summary collapse

Instance Method Details

#parse_auth(result, role_field, mapping) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/bn-ldap-authentication.rb', line 52

def parse_auth(result, role_field, mapping)
  use_attribute_mapping(mapping)

  auth = {}
  auth['info'] = {}
  auth['provider'] = :ldap

  LDAP_ATTRIBUTE_MAPPING.each do |key, value|
    value.each do |v|
      next unless result[v].first

      if key == "uid"
        auth[key] = result[v].first
        break
      else 
        auth['info'][key] = result[v].first
        break
      end
    end
  end

  auth['info']['roles'] = result[role_field].first

  auth
end

#send_ldap_request(user_params, provider_info) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/bn-ldap-authentication.rb', line 14

def send_ldap_request(user_params, provider_info)
  case provider_info[:auth_method]
  when 'anonymous'
    auth = {
        method: :anonymous
    }
  when 'user'
    auth = {
      method: :simple,
      username: provider_info[:uid] + '=' + user_params[:username] + ',' + provider_info[:base],
      password: user_params[:password]
    }
  else
    auth = {
      method: :simple,
      username: provider_info[:bind_dn],
      password: provider_info[:password]
    }
  end
  ldap = Net::LDAP.new(
    host: provider_info[:host],
    port: provider_info[:port],
    auth: auth,
    encryption: provider_info[:encryption]
  )

  ldap_filter = Net::LDAP::Filter.eq(provider_info[:uid], user_params[:username])
  if provider_info[:filter].present?
    ldap_filter = ldap_filter & Net::LDAP::Filter.construct(provider_info[:filter])
  end

  ldap.bind_as(
    base: provider_info[:base],
    filter: ldap_filter,
    password: user_params[:password]
  )
end