adauth 1.1.0 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -7,3 +7,7 @@ doc/*
7
7
  .yardoc/*
8
8
 
9
9
  .rvmrc
10
+
11
+ spec/test.sqlite3
12
+
13
+ spec/db/db.sqlite3
@@ -1,13 +1,26 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- adauth (1.0.0)
4
+ adauth (1.1.0)
5
5
  net-ldap
6
6
 
7
7
  GEM
8
8
  remote: http://rubygems.org/
9
9
  specs:
10
+ activemodel (3.0.7)
11
+ activesupport (= 3.0.7)
12
+ builder (~> 2.1.2)
13
+ i18n (~> 0.5.0)
14
+ activerecord (3.0.7)
15
+ activemodel (= 3.0.7)
16
+ activesupport (= 3.0.7)
17
+ arel (~> 2.0.2)
18
+ tzinfo (~> 0.3.23)
19
+ activesupport (3.0.7)
20
+ arel (2.0.10)
21
+ builder (2.1.2)
10
22
  diff-lcs (1.1.2)
23
+ i18n (0.5.0)
11
24
  net-ldap (0.2.2)
12
25
  rspec (2.6.0)
13
26
  rspec-core (~> 2.6.0)
@@ -17,10 +30,12 @@ GEM
17
30
  rspec-expectations (2.6.0)
18
31
  diff-lcs (~> 1.1.2)
19
32
  rspec-mocks (2.6.0)
33
+ tzinfo (0.3.29)
20
34
 
21
35
  PLATFORMS
22
36
  ruby
23
37
 
24
38
  DEPENDENCIES
39
+ activerecord
25
40
  adauth!
26
41
  rspec
data/Rakefile CHANGED
@@ -1,2 +1,3 @@
1
1
  require 'bundler'
2
+
2
3
  Bundler::GemHelper.install_tasks
@@ -3,37 +3,17 @@ require 'adauth/version'
3
3
  require 'adauth/user'
4
4
  require 'adauth/config'
5
5
  require 'adauth/helpers'
6
- require 'adauth/user_model' if defined? ActiveRecord
6
+ require 'adauth/connection'
7
+ require 'adauth/group'
8
+ require 'adauth/admin_connection'
9
+ require 'adauth/authenticate'
10
+ require 'adauth/user_model'
7
11
 
8
12
  # The top level module
9
13
  #
10
14
  # For Adauths documentation please see the github wiki.
11
15
  module Adauth
12
16
 
13
- # Takes a username and password as an input and returns an instance of `Adauth::User`
14
- #
15
- # Called as
16
- # Adauth.authenticate("Username", "Password")
17
- #
18
- # Will return `nil` if the username/password combo is wrong, if the username/password combo is correct it will return an instance of `Adauth::User` which can be used to populate your database.
19
- def self.authenticate(login, pass)
20
- if @config.allowed_groups != []
21
- user = Adauth::User.authenticate(login, pass)
22
- (user && @config.allowed_groups != (@config.allowed_groups - user.groups)) ? user : nil
23
- elsif @config.denied_groups != []
24
- user = Adauth::User.authenticate(login, pass)
25
- (user && @config.denied_groups == (@config.denied_groups - user.groups)) ? user : nil
26
- elsif @config.allowed_ous != []
27
- user = Adauth::User.authenticate(login, pass)
28
- (user && @config.allowed_ous != (@config.allowed_ous - user.ous)) ? user : nil
29
- elsif @config.denied_ous != []
30
- user = Adauth::User.authenticate(login, pass)
31
- (user && @config.denied_ous == (@config.denied_ous - user.ous)) ? user : nil
32
- else
33
- Adauth::User.authenticate(login, pass)
34
- end
35
- end
36
-
37
17
  # Used to configure Adauth
38
18
  #
39
19
  # Called as
@@ -0,0 +1,26 @@
1
+ module Adauth
2
+
3
+ # Uses the administrator login to create a Net::LDAP object that can query the whole domain
4
+ #
5
+ # Called as:
6
+ # Adauth::AdminConnection.bind(username,password)
7
+ class AdminConnection
8
+
9
+ # Uses the administrator login to create a Net::LDAP object that can query the whole domain
10
+ #
11
+ # Called as:
12
+ # Adauth::AdminConnection.bind(username,password)
13
+ def self.bind
14
+ if Adauth.config.admin_user and Adauth.config.admin_password
15
+ conn = Adauth::Connection.bind(Adauth.config.admin_user, Adauth.config.admin_password)
16
+ if conn
17
+ return conn
18
+ else
19
+ raise "admin_user and admin_password do not result in a succesful login"
20
+ end
21
+ else
22
+ raise "Can not create Adauth::AdminConnection without admin_user and admin_password set in config"
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,74 @@
1
+ module Adauth
2
+ # Takes a username and password as an input and returns an instance of `Adauth::User`
3
+ #
4
+ # Called as
5
+ # Adauth.authenticate("Username", "Password")
6
+ #
7
+ # Will return `nil` if the username/password combo is wrong, if the username/password combo is correct it will return an instance of `Adauth::User` which can be used to populate your database.
8
+ def self.authenticate(login, pass)
9
+ if user = Adauth::User.authenticate(login, pass)
10
+ return user if allowed_group_login(user) and allowed_ou_login(user)
11
+ else
12
+ return nil
13
+ end
14
+ end
15
+
16
+ # Takes a username as an input and returns and instance of `Adauth::User`
17
+ #
18
+ # Called as
19
+ # Adauth.authentication("Username")
20
+ #
21
+ # Will return `nil` if the username is worng, if the admin details are not set an error will be raised.
22
+ def self.passwordless_login(login)
23
+ @conn = Adauth::AdminConnection.bind
24
+ if user = @conn.search(:filter => Net::LDAP::Filter.eq('sAMAccountName', login)).first
25
+ return Adauth::User.new(user)
26
+ else
27
+ return nil
28
+ end
29
+ end
30
+
31
+ # Checks weather an users groups are allowed to login
32
+ #
33
+ # Called as:
34
+ # Adauth.allowed_group_login(Adauth::User)
35
+ #
36
+ # Returns true if the user can login and false if the user cant
37
+ def self.allowed_group_login(user)
38
+ if @config.allowed_groups != []
39
+ allowed = (user && @config.allowed_groups != (@config.allowed_groups - user.groups)) ? user : nil
40
+ else
41
+ allowed = user
42
+ end
43
+
44
+ if @config.denied_groups != []
45
+ denied = (user && @config.denied_groups == (@config.denied_groups - user.groups)) ? user : nil
46
+ else
47
+ denied = user
48
+ end
49
+
50
+ allowed == denied
51
+ end
52
+
53
+ # Checks weather an users ous are allowed to login
54
+ #
55
+ # Called as:
56
+ # Adauth.allowed_ou_login(Adauth::User)
57
+ #
58
+ # Returns true if the user can login and false if the user cant
59
+ def self.allowed_ou_login(user)
60
+ if @config.allowed_ous != []
61
+ allowed = (user && @config.allowed_ous != (@config.allowed_ous - user.ous)) ? user : nil
62
+ else
63
+ allowed = user
64
+ end
65
+
66
+ if @config.denied_ous != []
67
+ denied = (user && @config.denied_ous == (@config.denied_ous - user.ous)) ? user : nil
68
+ else
69
+ denied = user
70
+ end
71
+
72
+ allowed == denied
73
+ end
74
+ end
@@ -2,7 +2,8 @@ module Adauth
2
2
 
3
3
  # Holds all of adauth config in attr_accessor values
4
4
  class Config
5
- attr_accessor :domain, :port, :base, :server, :allowed_groups, :denied_groups, :ad_sv_attrs, :ad_mv_attrs, :allowed_ous, :denied_ous
5
+ attr_accessor :domain, :port, :base, :server, :allowed_groups, :denied_groups, :ad_sv_attrs, :ad_mv_attrs, :allowed_ous, :denied_ous,
6
+ :admin_user, :admin_password, :ad_sv_group_attrs, :ad_mv_group_attrs
6
7
 
7
8
  # Creates a new instance of Adauth::Config
8
9
  #
@@ -15,8 +16,16 @@ module Adauth
15
16
  @ad_mv_attrs = {}
16
17
  @allowed_ous = []
17
18
  @denied_ous = []
19
+ @ad_sv_group_attrs = {}
20
+ @ad_mv_group_attrs = {}
18
21
  end
19
22
 
23
+ # Sets domain valiable
24
+ #
25
+ # Called as:
26
+ # Adauth::Config.domain=(s)
27
+ #
28
+ # Calculates both base string and server
20
29
  def domain=(s)
21
30
  @domain = s
22
31
  work_out_base(s)
@@ -26,11 +35,7 @@ module Adauth
26
35
  private
27
36
 
28
37
  def work_out_base(s)
29
- dcs = []
30
- s.split(/\./).each do |split|
31
- dcs.push("dc=#{split}")
32
- end
33
- @base ||= dcs.join(', ')
38
+ @base ||= s.gsub(/\./,', dc=').gsub(/^/,"dc=")
34
39
  end
35
40
  end
36
41
  end
@@ -0,0 +1,31 @@
1
+ module Adauth
2
+
3
+ # Create a connection to LDAP using Net::LDAP
4
+ #
5
+ # Called as:
6
+ # Adauth::Connection.bind(username, password)
7
+ #
8
+ #
9
+ class Connection
10
+
11
+ # Create a connection to LDAP using Net::LDAP
12
+ #
13
+ # Called as:
14
+ # Adauth::Connection.bind(username, password)
15
+ #
16
+ #
17
+ def self.bind(login, pass)
18
+ conn = Net::LDAP.new :host => Adauth.config.server,
19
+ :port => Adauth.config.port,
20
+ :base => Adauth.config.base,
21
+ :auth => { :username => "#{login}@#{Adauth.config.domain}",
22
+ :password => pass,
23
+ :method => :simple }
24
+ if conn.bind
25
+ return conn
26
+ else
27
+ return nil
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,100 @@
1
+ module Adauth
2
+
3
+ # Active Directory Group object
4
+ #
5
+ # Called as:
6
+ # Adauth::Group.find(name)
7
+ #
8
+ # Returns an instance of Adauth::Group for the group specified in the find method
9
+ class Group
10
+
11
+ # Single vales where the method maps directly to one Active Directory attribute
12
+ ATTR_SV = {
13
+ :name => :name,
14
+ :dn => :distinguishedname
15
+ }
16
+
17
+ # Multi values were the method needs to return an array for values.
18
+ ATTR_MV = {
19
+ :ous => [ :distinguishedname,
20
+ Proc.new {|g| g.sub(/.*?OU=(.*?),.*/, '\1')} ]
21
+ }
22
+
23
+ # Finds the group specified
24
+ #
25
+ # Called as:
26
+ # Adauth::Group.find(name)
27
+ #
28
+ # Returns an instance of Adauth::Group for the group specified in the find method
29
+ def self.find(name)
30
+ @conn = Adauth::AdminConnection.bind
31
+ if group = @conn.search(:filter => Net::LDAP::Filter.eq('name', name)).first
32
+ return self.new(group)
33
+ else
34
+ return nil
35
+ end
36
+ end
37
+
38
+ # Returns the members of the group
39
+ #
40
+ # Called as:
41
+ # Adauth::Group.members
42
+ #
43
+ # Returns an array of Adauth::Users for the group
44
+ def members
45
+ filters = Net::LDAP::Filter.construct("(memberOf=#{dn})")
46
+ members_ldap = @conn.search(:filter => filters)
47
+ members = []
48
+ members_ldap.each do |member|
49
+ user = Adauth::User.create_from_login(member.samaccountname.first)
50
+ members.push(user)
51
+ end
52
+ return members
53
+ end
54
+
55
+ private
56
+
57
+ def initialize(entry)
58
+ @entry = entry
59
+ @conn = Adauth::AdminConnection.bind
60
+ self.class.class_eval do
61
+ generate_single_value_readers
62
+ generate_multi_value_readers
63
+ end
64
+ end
65
+
66
+ def self.generate_single_value_readers
67
+ ATTR_SV.merge(Adauth.config.ad_sv_group_attrs).each_pair do |k, v|
68
+ val, block = Array(v)
69
+ define_method(k) do
70
+ if @entry.attribute_names.include?(val)
71
+ if block.is_a?(Proc)
72
+ return block[@entry.send(val).to_s]
73
+ else
74
+ return @entry.send(val).to_s
75
+ end
76
+ else
77
+ return ''
78
+ end
79
+ end
80
+ end
81
+ end
82
+
83
+ def self.generate_multi_value_readers
84
+ ATTR_MV.merge(Adauth.config.ad_mv_group_attrs).each_pair do |k, v|
85
+ val, block = Array(v)
86
+ define_method(k) do
87
+ if @entry.attribute_names.include?(val)
88
+ if block.is_a?(Proc)
89
+ return @entry.send(val).collect(&block)
90
+ else
91
+ return @entry.send(val)
92
+ end
93
+ else
94
+ return []
95
+ end
96
+ end
97
+ end
98
+ end
99
+ end
100
+ end
@@ -14,12 +14,12 @@ module Adauth
14
14
  :name => :name
15
15
  }
16
16
 
17
- # Multi values were the method needs to return an array for values.
17
+ # Multi values where the method needs to return an array for values.
18
18
  ATTR_MV = {
19
19
  :groups => [ :memberof,
20
20
  Proc.new {|g| g.sub(/.*?CN=(.*?),.*/, '\1')} ],
21
21
  :ous => [ :memberof,
22
- Proc.new {|g| g.sub(/.*?OU=(.*?),.*/, '\1')} ]
22
+ Proc.new {|g| g.scan(/OU=.*?,/).map { |e| e.sub!(/OU=/,'').sub(/,/,'') } } ]
23
23
  }
24
24
 
25
25
  # Authenticates a user against Active Directory and returns an instance of self
@@ -30,13 +30,8 @@ module Adauth
30
30
  # Usage would by-pass Adauths group filtering.
31
31
  def self.authenticate(login, pass)
32
32
  return nil if login.empty? or pass.empty?
33
- conn = Net::LDAP.new :host => Adauth.config.server,
34
- :port => Adauth.config.port,
35
- :base => Adauth.config.base,
36
- :auth => { :username => "#{login}@#{Adauth.config.domain}",
37
- :password => pass,
38
- :method => :simple }
39
- if conn.bind and user = conn.search(:filter => Net::LDAP::Filter.eq('sAMAccountName', login)).first
33
+ conn = Adauth::Connection.bind(login, pass)
34
+ if conn and user = conn.search(:filter => Net::LDAP::Filter.eq('sAMAccountName', login)).first
40
35
  return self.new(user)
41
36
  else
42
37
  return nil
@@ -45,6 +40,19 @@ module Adauth
45
40
  return nil
46
41
  end
47
42
 
43
+ # Create a Adauth::User object from AD using just the username
44
+ #
45
+ # Called as:
46
+ # Adauth::User.create_from_login(login)
47
+ #
48
+ # Allows you to create objects for users without using thier password.
49
+ def self.create_from_login(login)
50
+ conn = Adauth::AdminConnection.bind
51
+ user = conn.search(:filter => Net::LDAP::Filter.eq('sAMAccountName', login)).first
52
+ obj = self.new(user)
53
+ return obj
54
+ end
55
+
48
56
  # Returns the full name of the user
49
57
  #
50
58
  # Combines the first_name and last_name attributes to create full_name
@@ -90,7 +98,9 @@ module Adauth
90
98
  define_method(k) do
91
99
  if @entry.attribute_names.include?(val)
92
100
  if block.is_a?(Proc)
93
- return @entry.send(val).collect(&block)
101
+ output = @entry.send(val).collect(&block)
102
+ output = output.first if output.first.is_a? Array
103
+ return output
94
104
  else
95
105
  return @entry.send(val)
96
106
  end
@@ -1,5 +1,5 @@
1
1
  module Adauth
2
2
 
3
3
  # The version of the gem
4
- Version = "1.1.0"
4
+ Version = "1.2.0"
5
5
  end
@@ -0,0 +1,51 @@
1
+ require 'lib/adauth'
2
+ require 'yaml'
3
+
4
+ describe Adauth::Group do
5
+ before :each do
6
+ @yaml = YAML::load(File.open('spec/test_data.yml'))
7
+ Adauth.configure do |c|
8
+ c.domain = @yaml["domain"]["domain"]
9
+ c.server = @yaml["domain"]["server"]
10
+ c.port = @yaml["domain"]["port"]
11
+ c.base = @yaml["domain"]["base"]
12
+ c.admin_user = @yaml["domain"]["admin_user"]
13
+ c.admin_password = @yaml["domain"]["admin_password"]
14
+ end
15
+ end
16
+
17
+ it "should return an instance of Adauth::Group if the group exists" do
18
+ group = Adauth::Group.find(@yaml["user"]["group"])
19
+ group.should be_a Adauth::Group
20
+ group.name.should eq(@yaml["user"]["group"])
21
+ end
22
+
23
+ it "should return nil for a group that doesn't exist" do
24
+ Adauth::Group.find(@yaml["user"]["group"][0..2]).should be_nil
25
+ end
26
+
27
+ it "should return an array from group.members" do
28
+ group = Adauth::Group.find(@yaml["user"]["group"])
29
+ group.members.should be_a Array
30
+ group.members.count.should_not eq(0)
31
+ end
32
+
33
+ it "should return an array of adauth::users from group.members" do
34
+ group = Adauth::Group.find(@yaml["user"]["group"])
35
+ group.members.each do |member|
36
+ member.should be_a Adauth::User
37
+ end
38
+ end
39
+
40
+ it "should only return users in this groups" do
41
+ group = Adauth::Group.find(@yaml["user"]["group"])
42
+ group.members.each do |member|
43
+ member.groups.include?(@yaml["user"]["group"]).should be_true
44
+ end
45
+ end
46
+
47
+ it "should return an array of ous" do
48
+ group = Adauth::Group.find(@yaml["user"]["group"])
49
+ group.ous.should be_a Array
50
+ end
51
+ end
@@ -33,122 +33,4 @@ describe Adauth, "#config" do
33
33
  it "should set port to 389 if not set" do
34
34
  Adauth.config.port.should == 389
35
35
  end
36
- end
37
-
38
- describe Adauth, "#authenticate" do
39
- before :each do
40
- @yaml = YAML::load(File.open('spec/test_data.yml'))
41
- Adauth.configure do |c|
42
- c.domain = @yaml["domain"]["domain"]
43
- c.server = @yaml["domain"]["server"]
44
- c.port = @yaml["domain"]["port"]
45
- c.base = @yaml["domain"]["base"]
46
- end
47
- end
48
-
49
- it "should succesfully authenticate with the example user" do
50
- Adauth.authenticate(@yaml["user"]["login"], @yaml["user"]["password"]).should be_a Adauth::User
51
- end
52
-
53
- it "should return nil for a failed bind" do
54
- Adauth.authenticate(@yaml["user"]["login"], @yaml["user"]["group"]).should == nil
55
- end
56
-
57
- it "should return nil for a failed bind whilst using allowed groups" do
58
- Adauth.config.allowed_groups = @yaml["domain"]["pass_allowed_groups"]
59
- Adauth.authenticate(@yaml["user"]["login"], @yaml["user"]["group"]).should be_nil
60
- end
61
-
62
- it "should allow users who are in an allowed group" do
63
- Adauth.config.allowed_groups = @yaml["domain"]["pass_allowed_groups"]
64
- Adauth.authenticate(@yaml["user"]["login"], @yaml["user"]["password"]).should be_a Adauth::User
65
- end
66
-
67
- it "should dis-allow users who are not in an allowed group" do
68
- Adauth.config.allowed_groups = @yaml["domain"]["fail_allowed_groups"]
69
- Adauth.authenticate(@yaml["user"]["login"], @yaml["user"]["password"]).should be_nil
70
- end
71
-
72
- it "should dis-allow users who are in a denied group" do
73
- Adauth.config.denied_groups = @yaml["domain"]["pass_allowed_groups"]
74
- Adauth.authenticate(@yaml["user"]["login"], @yaml["user"]["password"]).should be_nil
75
- end
76
-
77
- it "should allow users who are in a denied group" do
78
- Adauth.config.denied_groups = @yaml["domain"]["fail_allowed_groups"]
79
- Adauth.authenticate(@yaml["user"]["login"], @yaml["user"]["password"]).should be_a Adauth::User
80
- end
81
-
82
- it "should allow users who are in an allowed ou" do
83
- Adauth.config.allowed_ous = @yaml["domain"]["pass_allowed_ous"]
84
- Adauth.authenticate(@yaml["user"]["login"], @yaml["user"]["password"]).should be_a Adauth::User
85
- end
86
-
87
- it "should dis-allow users who are not in an allowed ou" do
88
- Adauth.config.allowed_ous = @yaml["domain"]["fail_allowed_ous"]
89
- Adauth.authenticate(@yaml["user"]["login"], @yaml["user"]["password"]).should be_nil
90
- end
91
-
92
- it "should dis-allow users who are in a denied ou" do
93
- Adauth.config.denied_ous = @yaml["domain"]["pass_allowed_ous"]
94
- Adauth.authenticate(@yaml["user"]["login"], @yaml["user"]["password"]).should be_nil
95
- end
96
-
97
- it "should allow users who are not in a denied ou" do
98
- Adauth.config.denied_ous = @yaml["domain"]["fail_allowed_ous"]
99
- Adauth.authenticate(@yaml["user"]["login"], @yaml["user"]["password"]).should be_a Adauth::User
100
- end
101
- end
102
-
103
- describe Adauth::User do
104
- before :each do
105
- @yaml = YAML::load(File.open('spec/test_data.yml'))
106
- Adauth.configure do |c|
107
- c.domain = @yaml["domain"]["domain"]
108
- c.server = @yaml["domain"]["server"]
109
- c.port = @yaml["domain"]["port"]
110
- c.base = @yaml["domain"]["base"]
111
- end
112
- @user = Adauth.authenticate(@yaml["user"]["login"], @yaml["user"]["password"])
113
- end
114
-
115
- it "should return groups for an authenticated user" do
116
- @user.groups.should be_a Array
117
- end
118
-
119
- it "should return true for a member_of test using the users group" do
120
- @user.member_of?(@yaml["user"]["group"]).should == true
121
- end
122
-
123
- it "should return false for a member_of test using the users password" do
124
- @user.member_of?(@yaml["user"]["password"]).should == false
125
- end
126
-
127
- it "should have the correct user" do
128
- @user.login.should == @yaml["user"]["login"]
129
- end
130
- end
131
-
132
- describe "Adauth::User custom returns" do
133
- before :each do
134
- @yaml = YAML::load(File.open('spec/test_data.yml'))
135
- Adauth.configure do |c|
136
- c.domain = @yaml["domain"]["domain"]
137
- c.server = @yaml["domain"]["server"]
138
- c.port = @yaml["domain"]["port"]
139
- c.base = @yaml["domain"]["base"]
140
- c.ad_sv_attrs = { :phone => :telephonenumber }
141
- c.ad_mv_attrs = { :ous => [ :memberof,
142
- Proc.new {|g| g.sub(/.*?OU=(.*?),.*/, '\1')} ] }
143
- end
144
- @user = Adauth.authenticate(@yaml["user"]["login"], @yaml["user"]["password"])
145
- end
146
-
147
- it "should pickup the custom single value from AD" do
148
- @user.phone.should be_a String
149
- end
150
-
151
- it "should pickup the custom multi value from AD" do
152
- @user.ous.should be_a Array
153
- end
154
36
  end
@@ -0,0 +1,80 @@
1
+ require 'lib/adauth'
2
+ require 'yaml'
3
+
4
+ ReturnDataForTest = []
5
+
6
+ class TestModel
7
+ include Adauth::UserModel
8
+
9
+ attr_accessor :login, :group_strings, :name, :ou_strings
10
+
11
+ def self.create!
12
+ @user = self.new
13
+ yield(@user)
14
+ return @user
15
+ end
16
+
17
+ def self.find_by_login(login)
18
+ ReturnDataForTest.last
19
+ end
20
+
21
+ def save
22
+ true
23
+ end
24
+ end
25
+
26
+ describe TestModel, "creations" do
27
+ before :each do
28
+ @yaml = YAML::load(File.open('spec/test_data.yml'))
29
+ Adauth.configure do |c|
30
+ c.domain = @yaml["domain"]["domain"]
31
+ c.server = @yaml["domain"]["server"]
32
+ c.port = @yaml["domain"]["port"]
33
+ c.base = @yaml["domain"]["base"]
34
+ end
35
+ @user = Adauth.authenticate(@yaml["user"]["login"], @yaml["user"]["password"])
36
+ end
37
+
38
+ it "should create a new user for method `create_user_with_adauth`" do
39
+ TestModel.create_user_with_adauth(@user).should be_a TestModel
40
+ end
41
+
42
+ it "should return a user for method `return_and_create_with_adauth`, if no user exists in the db" do
43
+ ReturnDataForTest.push nil
44
+ TestModel.return_and_create_with_adauth(@user).should be_a TestModel
45
+ end
46
+
47
+ it "should return a user for method `return_and_create_with_adauth`, if the user does exist" do
48
+ ReturnDataForTest.push TestModel.create_user_with_adauth(@user)
49
+ TestModel.return_and_create_with_adauth(@user).should be_a TestModel
50
+ end
51
+ end
52
+
53
+ describe TestModel, "methods" do
54
+ before :each do
55
+ @yaml = YAML::load(File.open('spec/test_data.yml'))
56
+ Adauth.configure do |c|
57
+ c.domain = @yaml["domain"]["domain"]
58
+ c.server = @yaml["domain"]["server"]
59
+ c.port = @yaml["domain"]["port"]
60
+ c.base = @yaml["domain"]["base"]
61
+ end
62
+ @user = Adauth.authenticate(@yaml["user"]["login"], @yaml["user"]["password"])
63
+ @model = TestModel.create_user_with_adauth(@user)
64
+ end
65
+
66
+ it "should return an array of groups for .groups" do
67
+ @model.groups.should be_a Array
68
+ end
69
+
70
+ it "should return an array of ous for .ous" do
71
+ @model.ous.should be_a Array
72
+ end
73
+
74
+ it "should update from adauth" do
75
+ @model.name = "Adauth Testing user that should be different"
76
+ @model.name.should_not eq(@user.name)
77
+ @model.update_from_adauth(@user)
78
+ @model.name.should eq(@user.name)
79
+ end
80
+ end
@@ -0,0 +1,199 @@
1
+ require 'lib/adauth'
2
+ require 'yaml'
3
+
4
+ describe Adauth, "#authenticate" do
5
+ before :each do
6
+ @yaml = YAML::load(File.open('spec/test_data.yml'))
7
+ Adauth.configure do |c|
8
+ c.domain = @yaml["domain"]["domain"]
9
+ c.server = @yaml["domain"]["server"]
10
+ c.port = @yaml["domain"]["port"]
11
+ c.base = @yaml["domain"]["base"]
12
+ end
13
+ end
14
+
15
+ it "should succesfully authenticate with the example user" do
16
+ Adauth.authenticate(@yaml["user"]["login"], @yaml["user"]["password"]).should be_a Adauth::User
17
+ end
18
+
19
+ it "should return nil for a failed bind" do
20
+ Adauth.authenticate(@yaml["user"]["login"], @yaml["user"]["group"]).should == nil
21
+ end
22
+
23
+ it "should return nil for a failed bind whilst using allowed groups" do
24
+ Adauth.config.allowed_groups = @yaml["domain"]["pass_allowed_groups"]
25
+ Adauth.authenticate(@yaml["user"]["login"], @yaml["user"]["group"]).should be_nil
26
+ end
27
+
28
+ it "should allow users who are in an allowed group" do
29
+ Adauth.config.allowed_groups = @yaml["domain"]["pass_allowed_groups"]
30
+ Adauth.authenticate(@yaml["user"]["login"], @yaml["user"]["password"]).should be_a Adauth::User
31
+ end
32
+
33
+ it "should dis-allow users who are not in an allowed group" do
34
+ Adauth.config.allowed_groups = @yaml["domain"]["fail_allowed_groups"]
35
+ Adauth.authenticate(@yaml["user"]["login"], @yaml["user"]["password"]).should be_nil
36
+ end
37
+
38
+ it "should dis-allow users who are in a denied group" do
39
+ Adauth.config.denied_groups = @yaml["domain"]["pass_allowed_groups"]
40
+ Adauth.authenticate(@yaml["user"]["login"], @yaml["user"]["password"]).should be_nil
41
+ end
42
+
43
+ it "should allow users who are in a denied group" do
44
+ Adauth.config.denied_groups = @yaml["domain"]["fail_allowed_groups"]
45
+ Adauth.authenticate(@yaml["user"]["login"], @yaml["user"]["password"]).should be_a Adauth::User
46
+ end
47
+
48
+ it "should allow users who are in an allowed ou" do
49
+ Adauth.config.allowed_ous = @yaml["domain"]["pass_allowed_ous"]
50
+ Adauth.authenticate(@yaml["user"]["login"], @yaml["user"]["password"]).should be_a Adauth::User
51
+ end
52
+
53
+ it "should dis-allow users who are not in an allowed ou" do
54
+ Adauth.config.allowed_ous = @yaml["domain"]["fail_allowed_ous"]
55
+ Adauth.authenticate(@yaml["user"]["login"], @yaml["user"]["password"]).should be_nil
56
+ end
57
+
58
+ it "should dis-allow users who are in a denied ou" do
59
+ Adauth.config.denied_ous = @yaml["domain"]["pass_allowed_ous"]
60
+ Adauth.authenticate(@yaml["user"]["login"], @yaml["user"]["password"]).should be_nil
61
+ end
62
+
63
+ it "should allow users who are not in a denied ou" do
64
+ Adauth.config.denied_ous = @yaml["domain"]["fail_allowed_ous"]
65
+ Adauth.authenticate(@yaml["user"]["login"], @yaml["user"]["password"]).should be_a Adauth::User
66
+ end
67
+
68
+ it "should dis-allow a user who is in an allowed ou but not an allowed group" do
69
+ Adauth.config.allowed_ous = @yaml["domain"]["pass_allowed_ous"]
70
+ Adauth.config.denied_groups = @yaml["domain"]["pass_allowed_groups"]
71
+ Adauth.authenticate(@yaml["user"]["login"], @yaml["user"]["password"]).should be_nil
72
+ end
73
+
74
+ it "should dis-allow a user who is in an allowed group but not an allowed ou" do
75
+ Adauth.config.denied_ous = @yaml["domain"]["pass_allowed_ous"]
76
+ Adauth.config.allowed_groups = @yaml["domain"]["pass_allowed_groups"]
77
+ Adauth.authenticate(@yaml["user"]["login"], @yaml["user"]["password"]).should be_nil
78
+ end
79
+
80
+ it "should allow a user who is in an allowed ou and an allowed group" do
81
+ Adauth.config.allowed_ous = @yaml["domain"]["pass_allowed_ous"]
82
+ Adauth.config.allowed_groups = @yaml["domain"]["pass_allowed_groups"]
83
+ Adauth.authenticate(@yaml["user"]["login"], @yaml["user"]["password"]).should be_a Adauth::User
84
+ end
85
+
86
+ it "should dis-allow a user who is in a dis-allowed ou and a dis-allowed group" do
87
+ Adauth.config.denied_ous = @yaml["domain"]["pass_allowed_ous"]
88
+ Adauth.config.denied_groups = @yaml["domain"]["pass_allowed_groups"]
89
+ Adauth.authenticate(@yaml["user"]["login"], @yaml["user"]["password"]).should be_nil
90
+ end
91
+ end
92
+
93
+ describe Adauth::User do
94
+ before :each do
95
+ @yaml = YAML::load(File.open('spec/test_data.yml'))
96
+ Adauth.configure do |c|
97
+ c.domain = @yaml["domain"]["domain"]
98
+ c.server = @yaml["domain"]["server"]
99
+ c.port = @yaml["domain"]["port"]
100
+ c.base = @yaml["domain"]["base"]
101
+ end
102
+ @user = Adauth.authenticate(@yaml["user"]["login"], @yaml["user"]["password"])
103
+ end
104
+
105
+ it "should return groups for an authenticated user" do
106
+ @user.groups.should be_a Array
107
+ end
108
+
109
+ it "should return ous for an authenticated user" do
110
+ @user.ous.should be_a Array
111
+ end
112
+
113
+ it "should have all the ous from the data file" do
114
+ @yaml["user"]["ous"].each do |ou|
115
+ @user.ous.include?(ou).should be_true
116
+ end
117
+ end
118
+
119
+ it "should return true for a member_of test using the users group" do
120
+ @user.member_of?(@yaml["user"]["group"]).should == true
121
+ end
122
+
123
+ it "should return false for a member_of test using the users password" do
124
+ @user.member_of?(@yaml["user"]["password"]).should == false
125
+ end
126
+
127
+ it "should have the correct user" do
128
+ @user.login.should == @yaml["user"]["login"]
129
+ end
130
+ end
131
+
132
+ describe "Adauth::User custom returns" do
133
+ before :each do
134
+ @yaml = YAML::load(File.open('spec/test_data.yml'))
135
+ Adauth.configure do |c|
136
+ c.domain = @yaml["domain"]["domain"]
137
+ c.server = @yaml["domain"]["server"]
138
+ c.port = @yaml["domain"]["port"]
139
+ c.base = @yaml["domain"]["base"]
140
+ c.ad_sv_attrs = { :phone => :telephonenumber }
141
+ c.ad_mv_attrs = { :ous => [ :memberof,
142
+ Proc.new {|g| g.sub(/.*?OU=(.*?),.*/, '\1')} ] }
143
+ end
144
+ @user = Adauth.authenticate(@yaml["user"]["login"], @yaml["user"]["password"])
145
+ end
146
+
147
+ it "should pickup the custom single value from AD" do
148
+ @user.phone.should be_a String
149
+ end
150
+
151
+ it "should pickup the custom multi value from AD" do
152
+ @user.ous.should be_a Array
153
+ end
154
+ end
155
+
156
+ describe Adauth::AdminConnection do
157
+ before :each do
158
+ @yaml = YAML::load(File.open('spec/test_data.yml'))
159
+ Adauth.configure do |c|
160
+ c.domain = @yaml["domain"]["domain"]
161
+ c.server = @yaml["domain"]["server"]
162
+ c.port = @yaml["domain"]["port"]
163
+ c.base = @yaml["domain"]["base"]
164
+ c.admin_user = @yaml["domain"]["admin_user"]
165
+ c.admin_password = @yaml["domain"]["admin_password"]
166
+ end
167
+ end
168
+
169
+ it "should create a connection" do
170
+ Adauth::AdminConnection.bind.should be_a Net::LDAP
171
+ end
172
+
173
+ it "should raise an exception if the password is wrong" do
174
+ Adauth.config.admin_password = @yaml["domain"]["admin_password"][1]
175
+ lambda { Adauth::AdminConnection.bind }.should raise_error
176
+ end
177
+ end
178
+
179
+ describe Adauth, "passwordless_login" do
180
+ before :each do
181
+ @yaml = YAML::load(File.open('spec/test_data.yml'))
182
+ Adauth.configure do |c|
183
+ c.domain = @yaml["domain"]["domain"]
184
+ c.server = @yaml["domain"]["server"]
185
+ c.port = @yaml["domain"]["port"]
186
+ c.base = @yaml["domain"]["base"]
187
+ c.admin_user = @yaml["domain"]["admin_user"]
188
+ c.admin_password = @yaml["domain"]["admin_password"]
189
+ end
190
+ end
191
+
192
+ it "should return an user when asked to" do
193
+ Adauth.passwordless_login(@yaml["user"]["login"]).should be_a Adauth::User
194
+ end
195
+
196
+ it "should be a viable user when passwordless login is used" do
197
+ Adauth.passwordless_login(@yaml["user"]["login"]).login.should eq(@yaml["user"]["login"])
198
+ end
199
+ end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: adauth
3
3
  version: !ruby/object:Gem::Version
4
- hash: 19
4
+ hash: 31
5
5
  prerelease:
6
6
  segments:
7
7
  - 1
8
- - 1
8
+ - 2
9
9
  - 0
10
- version: 1.1.0
10
+ version: 1.2.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Adam "Arcath" Laycock
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-08-08 00:00:00 +01:00
18
+ date: 2011-09-01 00:00:00 +01:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -63,7 +63,11 @@ files:
63
63
  - Readme.rdoc
64
64
  - adauth.gemspec
65
65
  - lib/adauth.rb
66
+ - lib/adauth/admin_connection.rb
67
+ - lib/adauth/authenticate.rb
66
68
  - lib/adauth/config.rb
69
+ - lib/adauth/connection.rb
70
+ - lib/adauth/group.rb
67
71
  - lib/adauth/helpers.rb
68
72
  - lib/adauth/user.rb
69
73
  - lib/adauth/user_model.rb
@@ -80,7 +84,10 @@ files:
80
84
  - lib/generators/adauth/user_model/USAGE
81
85
  - lib/generators/adauth/user_model/templates/model.rb.erb
82
86
  - lib/generators/adauth/user_model/user_model_generator.rb
87
+ - spec/adauth_group_spec.rb
83
88
  - spec/adauth_spec.rb
89
+ - spec/adauth_user_model_spec.rb
90
+ - spec/adauth_user_spec.rb
84
91
  has_rdoc: true
85
92
  homepage: http://adauth.arcath.net
86
93
  licenses: []
@@ -116,4 +123,7 @@ signing_key:
116
123
  specification_version: 3
117
124
  summary: Provides Active Directory authentication for Rails
118
125
  test_files:
126
+ - spec/adauth_group_spec.rb
119
127
  - spec/adauth_spec.rb
128
+ - spec/adauth_user_model_spec.rb
129
+ - spec/adauth_user_spec.rb