Sha256: a3c9a89e05c8cdc83bfe7d2525d169ff403725580beb48c8aeb55f54a40d46f4

Contents?: true

Size: 1.6 KB

Versions: 2

Compression:

Stored size: 1.6 KB

Contents

$:.unshift(File.expand_path('../../vendor/ruby-ldapserver/lib', __FILE__))
require 'ldap/server'

module FakeLDAP
  class Server < LDAP::Server
    def initialize(options={})
      @users = {}
      super(default_options.merge(options))
    end

    def add_user(user, pass)
      @users[user] = pass
    end

    def valid_credentials?(user, pass)
      @users.has_key?(user) && @users[user] == pass
    end

    def find_users(basedn, filter)
      basedn_regex = /#{Regexp.escape(basedn)}$/
      filter_regex = /^#{filter[1]}=#{filter[3]}$/

      @users.keys.select { |dn|
        dn =~ basedn_regex && dn.split(",").grep(filter_regex).any?
      }
    end

    def default_options
      {
        :operation_class => ::FakeLDAP::Operation,
        :operation_args  => [self]
      }
    end
  end

  class Operation < LDAP::Server::Operation
    def initialize(connection, messageID, server)
      super(connection, messageID)
      @server = server
    end

    def simple_bind(version, dn, password)
      unless dn
        raise LDAP::ResultError::InappropriateAuthentication,
          "This server does not support anonymous bind"
      end

      unless @server.valid_credentials?(dn, password)
        raise LDAP::ResultError::InvalidCredentials,
          "Invalid credentials"
      end
    end

    def search(basedn, scope, deref, filter, attrs=nil)
      unless filter.first == :eq
        raise LDAP::ResultError::UnwillingToPerform,
          "Only equality matches are supported"
      end

      @server.find_users(basedn, filter).each do |dn|
        send_SearchResultEntry(dn, {})
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 2 rubygems

Version Path
fakeGenLdap-0.0.1 lib/fakeldap.rb
fakeldap-0.0.1 lib/fakeldap.rb