Class: Another::Ldap::Proxy::Backend::Ldap

Inherits:
Object
  • Object
show all
Defined in:
lib/another/ldap/proxy/backend/ldap.rb

Constant Summary collapse

TYPE =
'ldap'
CONNECT_TIMEOUT =
15

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url:, base:, auth_method:, username:, password:, type:, ca_file: nil, logger: nil, **unused) ⇒ Ldap

Returns a new instance of Ldap.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/another/ldap/proxy/backend/ldap.rb', line 17

def initialize(url:, base:, auth_method:, username:, password:, type:, ca_file: nil, logger: nil,
               **unused)
  @url = url
  @base = base
  @auth_method = auth_method
  @username = username
  @password = password
  @type = type
  @ca_file = ca_file
  @logger = logger
  @unused = unused

  @parsed_url = URI.parse(url)

  raise "backend type is expected to be #{TYPE} not '#{type}'" unless type == TYPE
end

Instance Attribute Details

#auth_methodObject (readonly)

Returns the value of attribute auth_method.



14
15
16
# File 'lib/another/ldap/proxy/backend/ldap.rb', line 14

def auth_method
  @auth_method
end

#baseObject (readonly)

Returns the value of attribute base.



14
15
16
# File 'lib/another/ldap/proxy/backend/ldap.rb', line 14

def base
  @base
end

#ca_fileObject (readonly)

Returns the value of attribute ca_file.



14
15
16
# File 'lib/another/ldap/proxy/backend/ldap.rb', line 14

def ca_file
  @ca_file
end

#loggerObject (readonly)

Returns the value of attribute logger.



14
15
16
# File 'lib/another/ldap/proxy/backend/ldap.rb', line 14

def logger
  @logger
end

#parsed_urlObject (readonly)

Returns the value of attribute parsed_url.



14
15
16
# File 'lib/another/ldap/proxy/backend/ldap.rb', line 14

def parsed_url
  @parsed_url
end

#passwordObject (readonly)

Returns the value of attribute password.



14
15
16
# File 'lib/another/ldap/proxy/backend/ldap.rb', line 14

def password
  @password
end

#typeObject (readonly)

Returns the value of attribute type.



14
15
16
# File 'lib/another/ldap/proxy/backend/ldap.rb', line 14

def type
  @type
end

#urlObject (readonly)

Returns the value of attribute url.



14
15
16
# File 'lib/another/ldap/proxy/backend/ldap.rb', line 14

def url
  @url
end

#usernameObject (readonly)

Returns the value of attribute username.



14
15
16
# File 'lib/another/ldap/proxy/backend/ldap.rb', line 14

def username
  @username
end

Instance Method Details

#auth(username: self.username, password: self.password) ⇒ Object



58
59
60
61
62
63
64
# File 'lib/another/ldap/proxy/backend/ldap.rb', line 58

def auth(username: self.username, password: self.password)
  return unless auth_method && username && password

  { method: auth_method.to_sym,
    username: username,
    password: password }
end

#bind(_version, dn, password) ⇒ Object



73
74
75
76
77
78
79
80
81
# File 'lib/another/ldap/proxy/backend/ldap.rb', line 73

def bind(_version, dn, password)
  logger&.info "Ldap#bind : dn:#{dn}"
  logger&.debug "Ldap#bind : dn:#{dn} password:#{password}"
  bind_ldap = new_client(auth: auth(username: dn, password: password))
  status = bind_ldap.bind ? true : false
  logger&.info "Ldap#bind : #{dn} => #{status}"

  status
end

#clientObject



46
47
48
# File 'lib/another/ldap/proxy/backend/ldap.rb', line 46

def client
  @client ||= new_client
end

#encryptionObject



66
67
68
69
70
71
# File 'lib/another/ldap/proxy/backend/ldap.rb', line 66

def encryption
  return unless parsed_url.scheme == 'ldaps'

  { method: :simple_tls,
    tls_options: { ca_file: ca_file } }
end

#hostObject



50
51
52
# File 'lib/another/ldap/proxy/backend/ldap.rb', line 50

def host
  parsed_url.host
end

#new_client(host: self.host, port: self.port, base: self.base, auth: self.auth, encryption: self.encryption, connect_timeout: CONNECT_TIMEOUT) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/another/ldap/proxy/backend/ldap.rb', line 34

def new_client(host: self.host, port: self.port, base: self.base, auth: self.auth,
               encryption: self.encryption, connect_timeout: CONNECT_TIMEOUT)
  Net::LDAP.new(
    host: host,
    port: port,
    base: base,
    auth: auth,
    encryption: encryption,
    connect_timeout: connect_timeout
  )
end

#portObject



54
55
56
# File 'lib/another/ldap/proxy/backend/ldap.rb', line 54

def port
  parsed_url.port
end

#search(basedn, scope, _deref, filter) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/another/ldap/proxy/backend/ldap.rb', line 83

def search(basedn, scope, _deref, filter)
  logger&.debug "Ldap#search : filter:#{filter}"
  results = []
  client.search(base: search_base(basedn), scope: scope, filter: filter, return_results: true) do |entry|
    results << entry
  end
  logger&.info "Ldap#search : results => #{results.size}"
  results
rescue Net::LDAP::Error => e
  logger&.warn "Ldap#search : Error querying LDAP server: #{e.message}"
  []
end

#search_base(basedn) ⇒ Object



96
97
98
99
100
101
# File 'lib/another/ldap/proxy/backend/ldap.rb', line 96

def search_base(basedn)
  return base unless basedn
  return basedn if basedn.include?(base)

  base
end