avdt_ldap_legacy 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +4 -0
- data/Gemfile +4 -0
- data/LICENSE +19 -0
- data/README.rdoc +118 -0
- data/Rakefile +1 -0
- data/avdt_ldap_legacy.gemspec +22 -0
- data/ldap.example.yml +34 -0
- data/lib/avdt_ldap_legacy.rb +4 -0
- data/lib/avdt_ldap_legacy/avdt_ldap_legacy.rb +155 -0
- data/lib/avdt_ldap_legacy/configuration.rb +7 -0
- data/lib/avdt_ldap_legacy/hash.rb +20 -0
- data/lib/avdt_ldap_legacy/version.rb +3 -0
- metadata +96 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
Copyright (c) 2011 Alessandro Verlato, Davide Targa
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
4
|
+
of this software and associated documentation files (the "Software"), to deal
|
5
|
+
in the Software without restriction, including without limitation the rights
|
6
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
7
|
+
copies of the Software, and to permit persons to whom the Software is
|
8
|
+
furnished to do so, subject to the following conditions:
|
9
|
+
|
10
|
+
The above copyright notice and this permission notice shall be included in
|
11
|
+
all copies or substantial portions of the Software.
|
12
|
+
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
18
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
19
|
+
SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,118 @@
|
|
1
|
+
= AvdtLdapLegacy
|
2
|
+
|
3
|
+
avdt_ldap version for Ruby 1.8.6
|
4
|
+
|
5
|
+
This gem supports LDAP authentication both on sigle and multiple LDAP servers with a minimal configuration.
|
6
|
+
It requires 'ruby-net-ldap' gem (automatically installed)
|
7
|
+
|
8
|
+
== Installation
|
9
|
+
|
10
|
+
=== Rails 3
|
11
|
+
|
12
|
+
This gem doesn't work with Rails 3 because it requires Ruby >= 1.8.7
|
13
|
+
If you use Rails 3 you have to install avdt_ldap. Documentation[https://github.com/davide-targa/avdt_ldap] RubyGem[https://rubygems.org/gems/avdt_ldap]
|
14
|
+
|
15
|
+
=== Rails 2
|
16
|
+
|
17
|
+
Add this to your environment.rb file:
|
18
|
+
|
19
|
+
config.gem "avdt_ldap_legacy"
|
20
|
+
|
21
|
+
== Usage
|
22
|
+
|
23
|
+
Just add a config file named ldap.yml in config/ directory.
|
24
|
+
|
25
|
+
You can change default file name by setting +ldap_config_file+ configuration parameter.
|
26
|
+
For example, inside the avdt_ldap initializer:
|
27
|
+
|
28
|
+
AvdtLdapLegacy.configure do |c|
|
29
|
+
c.ldap_config_file = "#{Rails.root}/config/foobar.yml"
|
30
|
+
end
|
31
|
+
|
32
|
+
== ldap.yml
|
33
|
+
|
34
|
+
Inside this file you have to specify connection parameters for all the directories on which to verify users credentials
|
35
|
+
|
36
|
+
Example file:
|
37
|
+
|
38
|
+
# All the directory attributes (except "base") are optional. Defaults are specified in the example below.
|
39
|
+
|
40
|
+
development:
|
41
|
+
dir1:
|
42
|
+
host: ldap.foobar.com # defaults to "127.0.0.1"
|
43
|
+
base: ou=People,dc=foobar,dc=com # REQUIRED
|
44
|
+
port: 123 # defaults to 389
|
45
|
+
ssl: true # defaults to false
|
46
|
+
attribute: cn # defaults to "uid"
|
47
|
+
|
48
|
+
|
49
|
+
dir2:
|
50
|
+
host: ldap.goofy.foobar.com
|
51
|
+
base: ou=People,dc=goofy,dc=foobar,dc=com
|
52
|
+
|
53
|
+
test:
|
54
|
+
dir1:
|
55
|
+
host: ldap.test.foobar.com
|
56
|
+
base: ou=People,dc=foobar,dc=com
|
57
|
+
|
58
|
+
dir2:
|
59
|
+
host: ldap.goofy.foobar.com
|
60
|
+
base: ou=People,dc=goofy,dc=foobar,dc=com
|
61
|
+
|
62
|
+
production:
|
63
|
+
dir2:
|
64
|
+
host: ldap.live.foobar.com
|
65
|
+
base: ou=People,dc=foobar,dc=com
|
66
|
+
attribute: cn
|
67
|
+
|
68
|
+
new_dir:
|
69
|
+
host: donald.duck.com
|
70
|
+
attribute: foo
|
71
|
+
base: ou=Ducks,dc=foobar,dc=com
|
72
|
+
|
73
|
+
|
74
|
+
Not specified parameters (except for "base" which is required) will be set to the default values:
|
75
|
+
|
76
|
+
host: "127.0.0.1"
|
77
|
+
port: 389
|
78
|
+
attribute: uid
|
79
|
+
base: %s
|
80
|
+
ssl: false
|
81
|
+
|
82
|
+
== Authentication
|
83
|
+
|
84
|
+
To verify user's credentials on ALL the specified directories (default) simply do this:
|
85
|
+
|
86
|
+
AvdtLdap.new.valid?(login, password)
|
87
|
+
|
88
|
+
As mentioned this will try to authenticate the user on all the directories specified on ldap.yml and will return true or false.
|
89
|
+
If authentication fails an error message, containing directory response (error message and code), will be displayed on server's logs.
|
90
|
+
|
91
|
+
=== Authentication only on specified directories
|
92
|
+
|
93
|
+
If you have to check user's credentials only on some specific directories, you can pass an hash to AvdtLdap.new(), specifying on which to do the check.
|
94
|
+
|
95
|
+
a = AvdtLdap.new(:directories => [:dir1,dir3])
|
96
|
+
a.valid?(login,password)
|
97
|
+
=> true (false)
|
98
|
+
|
99
|
+
NOTE: The authentication process stops as soon as one positive match is found, so it's possible that not all the directories are queried.
|
100
|
+
|
101
|
+
=== User's attributes access
|
102
|
+
|
103
|
+
If the authentication process is successfull, you can access user's attributes simply calling a method on your AvdtLdap object, with the same name of the desired attribute. For example let's suppose we want the user's name and surname (+givenName+ and +sn+ attributes on the directory), then you can do this:
|
104
|
+
|
105
|
+
username = a.givenname
|
106
|
+
surname = a.cn
|
107
|
+
|
108
|
+
Note: theese methods must be called on lowercase
|
109
|
+
|
110
|
+
You can also access the whole attributes hash by calling:
|
111
|
+
|
112
|
+
a.user_attributes
|
113
|
+
|
114
|
+
==== On which directory is located the user ?
|
115
|
+
|
116
|
+
You can know it by calling the +user_location+ method on your AvdtLdap object:
|
117
|
+
|
118
|
+
location = a.user_location
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'bundler/gem_tasks'
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "avdt_ldap_legacy/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "avdt_ldap_legacy"
|
7
|
+
s.version = AvdtLdapLegacy::VERSION
|
8
|
+
s.authors = ["Alessandro Verlato","Davide Targa"]
|
9
|
+
s.email = ["averlato@gmail.com","davide.targa@gmail.com"]
|
10
|
+
s.homepage = "https://rubygems.org/gems/avdt_ldap_legacy"
|
11
|
+
s.summary = %q{avdt_ldap for Ruby 1.8.6}
|
12
|
+
s.description = %q{avdt_ldap is a simple LDAP authentication library for user authentication on multiple LDAP directories}
|
13
|
+
s.required_ruby_version = "~> 1.8.6"
|
14
|
+
|
15
|
+
s.rubyforge_project = "avdt_ldap_legacy"
|
16
|
+
|
17
|
+
s.files = `git ls-files`.split("\n")
|
18
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
19
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
20
|
+
s.require_paths = ["lib"]
|
21
|
+
s.add_dependency "ruby-net-ldap"
|
22
|
+
end
|
data/ldap.example.yml
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
# All the directory attributes (except "base") are optional. Defaults are specified in the example below.
|
2
|
+
|
3
|
+
development:
|
4
|
+
dir1:
|
5
|
+
host: ldap.foobar.com # defaults to "127.0.0.1"
|
6
|
+
base: ou=People,dc=foobar,dc=com # REQUIRED
|
7
|
+
port: 123 # defaults to 389
|
8
|
+
ssl: true # defaults to false
|
9
|
+
attribute: cn # defaults to "uid"
|
10
|
+
|
11
|
+
|
12
|
+
dir2:
|
13
|
+
host: ldap.goofy.foobar.com
|
14
|
+
base: ou=People,dc=goofy,dc=foobar,dc=com
|
15
|
+
|
16
|
+
test:
|
17
|
+
dir1:
|
18
|
+
host: ldap.test.foobar.com
|
19
|
+
base: ou=People,dc=foobar,dc=com
|
20
|
+
|
21
|
+
dir2:
|
22
|
+
host: ldap.goofy.foobar.com
|
23
|
+
base: ou=People,dc=goofy,dc=foobar,dc=com
|
24
|
+
|
25
|
+
production:
|
26
|
+
dir2:
|
27
|
+
host: ldap.live.foobar.com
|
28
|
+
base: ou=People,dc=foobar,dc=com
|
29
|
+
attribute: cn
|
30
|
+
|
31
|
+
new_dir:
|
32
|
+
host: donald.duck.com
|
33
|
+
attribute: foo
|
34
|
+
base: ou=Ducks,dc=foobar,dc=com
|
@@ -0,0 +1,155 @@
|
|
1
|
+
# AvdtLdapLegacy
|
2
|
+
|
3
|
+
# This gem supports LDAP authentication both on sigle and multiple LDAP servers
|
4
|
+
# with a minimal configuration.
|
5
|
+
# It requires 'ruby-net-ldap' gem.
|
6
|
+
#
|
7
|
+
# USAGE
|
8
|
+
# Authentication
|
9
|
+
|
10
|
+
# To verify user's credentials on ALL the specified directories (default) simply do this:
|
11
|
+
#
|
12
|
+
# AvdtLdapLegacy.new.valid?(login, password)
|
13
|
+
#
|
14
|
+
# As mentioned this will try to authenticate the user on all the directories specified on ldap.yml and will return true or false.
|
15
|
+
# If authentication fails an error message, containing directory response (error message and code), will be displayed on server's logs.
|
16
|
+
#
|
17
|
+
# Authentication only on specified directories
|
18
|
+
#
|
19
|
+
# If you have to check user's credentials only on some specific directories, you can pass an hash to AvdtLdapLegacy.new(), specifying on which to do the check.
|
20
|
+
#
|
21
|
+
# a = AvdtLdapLegacy.new(:directories => [:dir1,dir3])
|
22
|
+
# a.valid?(login,password)
|
23
|
+
# => true (false)
|
24
|
+
#
|
25
|
+
# NOTE: The authentication process stops as soon as one positive match is found, so it's possible that not all the directories are queried.
|
26
|
+
#
|
27
|
+
# User's attributes access:
|
28
|
+
# If you have to access (read) user's attributes from the directory you can
|
29
|
+
# use the handy methods provided by the gem. Let's suppose we need two attributes,
|
30
|
+
# the user's name and surname ("givenName" and "sn" attributes on the directory).
|
31
|
+
# Simply access attributes as in the example below:
|
32
|
+
#
|
33
|
+
# a = AvdtLdapLegacy.new.valid?(login, password)
|
34
|
+
# name = a.givenname
|
35
|
+
# surname = a.cn
|
36
|
+
#
|
37
|
+
# As you can see methods names reflects attribute's name (but always in downcase).
|
38
|
+
# You can also access the whole attributes hash by calling:
|
39
|
+
|
40
|
+
# a.user_attributes
|
41
|
+
#
|
42
|
+
# On which directory is located the user ?
|
43
|
+
# You can know it by calling the +user_location+ method on your AvdtLdapLegacy object:
|
44
|
+
#
|
45
|
+
# location = a.user_location
|
46
|
+
|
47
|
+
require 'net/ldap'
|
48
|
+
|
49
|
+
class AvdtLdapLegacy
|
50
|
+
|
51
|
+
# Used to simplify configuration from rails initializers.
|
52
|
+
# Works with the methods configuration and configure defined below.
|
53
|
+
class << self
|
54
|
+
attr_accessor :configuration
|
55
|
+
end
|
56
|
+
|
57
|
+
attr_accessor :directories, :include_default, :user_attributes, :user_location
|
58
|
+
|
59
|
+
# Loads ldap configuration file and sets up the object's parameters
|
60
|
+
def initialize(args = {})
|
61
|
+
if File.exist?(AvdtLdapLegacy.configuration.ldap_config_file)
|
62
|
+
@LDAP = YAML.load_file(AvdtLdapLegacy.configuration.ldap_config_file).symbolize_keys!
|
63
|
+
else
|
64
|
+
raise "AvdtLdapLegacy: File #{AvdtLdapLegacy.configuration.ldap_config_file} not found, maybe you forgot to define it ?"
|
65
|
+
end
|
66
|
+
@directories = args[:directories] || @LDAP[env].keys
|
67
|
+
end
|
68
|
+
|
69
|
+
# Checks for user's existance on specified directories. Just pass "login" and
|
70
|
+
# "password" parameters to chech if a user resides on one of the directories.
|
71
|
+
# After this method calling, if the user is authenticated, his (directory)
|
72
|
+
# attributes are availaible.
|
73
|
+
def valid? login, password
|
74
|
+
@directories.each do |ldap|
|
75
|
+
ldap = ldap.to_sym
|
76
|
+
unless @LDAP[env][ldap].nil?
|
77
|
+
conn = connection(ldap)
|
78
|
+
conn.authenticate("#{attribute(ldap)}=#{login.to_s},#{base(ldap)}", password.to_s)
|
79
|
+
begin
|
80
|
+
# if bind => OK
|
81
|
+
if conn.bind
|
82
|
+
logger.info("Authenticated #{login.to_s} by #{host(ldap)}") if logger
|
83
|
+
@user_attributes = conn.search(:base => base(ldap),:filter => Net::LDAP::Filter.eq(attribute(ldap),login.to_s)).first.each do |k,v|
|
84
|
+
class_eval "attr_reader :#{k}"
|
85
|
+
self.instance_variable_set "@#{k}".to_sym, v
|
86
|
+
end
|
87
|
+
@user_location = ldap
|
88
|
+
return true
|
89
|
+
else
|
90
|
+
logger.info("Error attempting to authenticate #{login.to_s} by #{host(ldap)}: #{conn.get_operation_result.code} #{conn.get_operation_result.message}") if logger
|
91
|
+
end
|
92
|
+
rescue Net::LDAP::LdapError => error
|
93
|
+
logger.info("Error attempting to authenticate #{login.to_s} by #{host(ldap)}: #{error.message}") if logger
|
94
|
+
return false
|
95
|
+
end
|
96
|
+
else
|
97
|
+
logger.info "ERROR ! \"#{ldap}\" directory data are missing in ldap.yml" if logger
|
98
|
+
raise Net::LDAP::LdapError, "\"#{ldap}\" directory data are missing in ldap.yml"
|
99
|
+
end
|
100
|
+
end
|
101
|
+
false
|
102
|
+
end
|
103
|
+
|
104
|
+
# Adds configuration ability to the gem
|
105
|
+
def self.configuration
|
106
|
+
@configuration ||= Configuration.new
|
107
|
+
end
|
108
|
+
|
109
|
+
def self.configure
|
110
|
+
yield(configuration)
|
111
|
+
end
|
112
|
+
|
113
|
+
private
|
114
|
+
|
115
|
+
# Given a directory name returns a connection to that server using parameters
|
116
|
+
# specified in ldap.yml
|
117
|
+
def connection(which_ldap)
|
118
|
+
Net::LDAP.new(:host => host(which_ldap), :port => port(which_ldap), :encryption => (:simple_tls if ssl?(which_ldap)))
|
119
|
+
end
|
120
|
+
|
121
|
+
# Given a directory return it's host name
|
122
|
+
def host(which_ldap)
|
123
|
+
@LDAP[env][which_ldap][:host] || "127.0.0.1"
|
124
|
+
end
|
125
|
+
|
126
|
+
# Given a directory returns it's host port
|
127
|
+
def port(which_ldap)
|
128
|
+
ssl?(which_ldap) ? (@LDAP[env][which_ldap][:port] || 636) : (@LDAP[env][which_ldap][:port] || 389)
|
129
|
+
end
|
130
|
+
|
131
|
+
# Given a directory returns it's attribute (example: uid)
|
132
|
+
def attribute(which_ldap)
|
133
|
+
@LDAP[env][which_ldap][:attribute] || "uid"
|
134
|
+
end
|
135
|
+
|
136
|
+
# Given a directory returns it's base path (example ou=People,dc=foo,dc=bar)
|
137
|
+
def base(which_ldap)
|
138
|
+
@LDAP[env][which_ldap][:base] || "%s"
|
139
|
+
end
|
140
|
+
|
141
|
+
# Given a directory returns if connection should use ssl
|
142
|
+
def ssl?(which_ldap)
|
143
|
+
@LDAP[env][which_ldap][:ssl] ? true : false
|
144
|
+
end
|
145
|
+
|
146
|
+
# Returns Rails Default logger
|
147
|
+
def logger
|
148
|
+
Rails.logger
|
149
|
+
end
|
150
|
+
|
151
|
+
def env
|
152
|
+
Rails.env.to_sym
|
153
|
+
end
|
154
|
+
|
155
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
class Hash
|
2
|
+
|
3
|
+
# Transforms all the hash keys from strings to symbols.
|
4
|
+
# Example:
|
5
|
+
# {"one" => "two", "three" => "four"}.symbolize_keys
|
6
|
+
# => {:one=>"two", :three=>"four"}
|
7
|
+
#
|
8
|
+
def symbolize_keys!
|
9
|
+
t = self.dup
|
10
|
+
self.clear
|
11
|
+
t.each_pair do |k,v|
|
12
|
+
self[k.to_sym] = v
|
13
|
+
if v.kind_of?(Hash)
|
14
|
+
v.symbolize_keys!
|
15
|
+
end
|
16
|
+
self
|
17
|
+
end
|
18
|
+
self
|
19
|
+
end
|
20
|
+
end
|
metadata
ADDED
@@ -0,0 +1,96 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: avdt_ldap_legacy
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 19
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 1
|
8
|
+
- 1
|
9
|
+
- 0
|
10
|
+
version: 1.1.0
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Alessandro Verlato
|
14
|
+
- Davide Targa
|
15
|
+
autorequire:
|
16
|
+
bindir: bin
|
17
|
+
cert_chain: []
|
18
|
+
|
19
|
+
date: 2011-06-22 00:00:00 +02:00
|
20
|
+
default_executable:
|
21
|
+
dependencies:
|
22
|
+
- !ruby/object:Gem::Dependency
|
23
|
+
name: ruby-net-ldap
|
24
|
+
prerelease: false
|
25
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
26
|
+
none: false
|
27
|
+
requirements:
|
28
|
+
- - ">="
|
29
|
+
- !ruby/object:Gem::Version
|
30
|
+
hash: 3
|
31
|
+
segments:
|
32
|
+
- 0
|
33
|
+
version: "0"
|
34
|
+
type: :runtime
|
35
|
+
version_requirements: *id001
|
36
|
+
description: avdt_ldap is a simple LDAP authentication library for user authentication on multiple LDAP directories
|
37
|
+
email:
|
38
|
+
- averlato@gmail.com
|
39
|
+
- davide.targa@gmail.com
|
40
|
+
executables: []
|
41
|
+
|
42
|
+
extensions: []
|
43
|
+
|
44
|
+
extra_rdoc_files: []
|
45
|
+
|
46
|
+
files:
|
47
|
+
- .gitignore
|
48
|
+
- Gemfile
|
49
|
+
- LICENSE
|
50
|
+
- README.rdoc
|
51
|
+
- Rakefile
|
52
|
+
- avdt_ldap_legacy.gemspec
|
53
|
+
- ldap.example.yml
|
54
|
+
- lib/avdt_ldap_legacy.rb
|
55
|
+
- lib/avdt_ldap_legacy/avdt_ldap_legacy.rb
|
56
|
+
- lib/avdt_ldap_legacy/configuration.rb
|
57
|
+
- lib/avdt_ldap_legacy/hash.rb
|
58
|
+
- lib/avdt_ldap_legacy/version.rb
|
59
|
+
has_rdoc: true
|
60
|
+
homepage: https://rubygems.org/gems/avdt_ldap_legacy
|
61
|
+
licenses: []
|
62
|
+
|
63
|
+
post_install_message:
|
64
|
+
rdoc_options: []
|
65
|
+
|
66
|
+
require_paths:
|
67
|
+
- lib
|
68
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
69
|
+
none: false
|
70
|
+
requirements:
|
71
|
+
- - ~>
|
72
|
+
- !ruby/object:Gem::Version
|
73
|
+
hash: 59
|
74
|
+
segments:
|
75
|
+
- 1
|
76
|
+
- 8
|
77
|
+
- 6
|
78
|
+
version: 1.8.6
|
79
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
80
|
+
none: false
|
81
|
+
requirements:
|
82
|
+
- - ">="
|
83
|
+
- !ruby/object:Gem::Version
|
84
|
+
hash: 3
|
85
|
+
segments:
|
86
|
+
- 0
|
87
|
+
version: "0"
|
88
|
+
requirements: []
|
89
|
+
|
90
|
+
rubyforge_project: avdt_ldap_legacy
|
91
|
+
rubygems_version: 1.3.7
|
92
|
+
signing_key:
|
93
|
+
specification_version: 3
|
94
|
+
summary: avdt_ldap for Ruby 1.8.6
|
95
|
+
test_files: []
|
96
|
+
|