Class: Adauth::Connection

Inherits:
Object
  • Object
show all
Includes:
Expects
Defined in:
lib/adauth/connection.rb

Overview

Active Directory Connection wrapper

Handles errors and configures the connection.

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Connection

Returns a new instance of Connection


8
9
10
11
# File 'lib/adauth/connection.rb', line 8

def initialize(config)
    expects config, Hash
    @config = config
end

Instance Method Details

#bindObject

Attempts to bind to Active Directory

If it works it returns the connection

If it fails it raises and exception


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
# File 'lib/adauth/connection.rb', line 18

def bind
    conn = Net::LDAP.new :host => @config[:server],
                         :port => @config[:port],
                         :base => @config[:base]
    if @config[:encryption]
       conn.encryption @config[:encryption]
    end
    
    raise "Anonymous Bind is disabled" if @config[:password] == "" && !(@config[:anonymous_bind])
    
    conn.auth "#{@config[:username]}@#{@config[:domain]}", @config[:password]
    
    begin
        Timeout::timeout(10){
            if conn.bind
                return conn
            else
                raise 'Query User Rejected'
            end
        }
    rescue Timeout::Error
        raise 'Unable to connect to LDAP Server'
    rescue Errno::ECONNRESET
      if @config[:allow_fallback]
        @config[:port] = @config[:allow_fallback]
        @config[:encryption] = false
        return Adauth::Connection.new(@config).bind
      end
    end
end