Class: Adauth::Connection

Inherits:
Object
  • Object
show all
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.



6
7
8
# File 'lib/adauth/connection.rb', line 6

def initialize(config)
    @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



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

def bind
    conn = Net::LDAP.new :host => @config[:server],
                         :port => @config[:port],
                         :base => @config[:base]
    if @config[:encryption]
       conn.encryption @config[:encryption]
    end

    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