adauth 1.2.1 → 2.0.0pre

Sign up to get free protection for your applications and to get access to all the features.
Files changed (46) hide show
  1. data/.travis.yml +12 -0
  2. data/Gemfile.lock +13 -26
  3. data/Rakefile +1 -0
  4. data/Readme.md +48 -0
  5. data/adauth.gemspec +2 -1
  6. data/lib/adauth.rb +40 -28
  7. data/lib/adauth/ad_object.rb +104 -0
  8. data/lib/adauth/ad_objects/computer.rb +28 -0
  9. data/lib/adauth/ad_objects/group.rb +40 -0
  10. data/lib/adauth/ad_objects/ou.rb +41 -0
  11. data/lib/adauth/ad_objects/user.rb +45 -0
  12. data/lib/adauth/authenticate.rb +25 -46
  13. data/lib/adauth/config.rb +11 -28
  14. data/lib/adauth/connection.rb +19 -18
  15. data/lib/adauth/rails.rb +9 -0
  16. data/lib/adauth/rails/helpers.rb +29 -0
  17. data/lib/adauth/rails/model_bridge.rb +59 -0
  18. data/lib/adauth/version.rb +2 -3
  19. data/lib/generators/adauth/config/config_generator.rb +1 -1
  20. data/lib/generators/adauth/config/templates/config.rb.erb +18 -22
  21. data/lib/generators/adauth/sessions/sessions_generator.rb +2 -3
  22. data/lib/generators/adauth/sessions/templates/sessions_controller.rb.erb +1 -1
  23. data/spec/adauth_ad_object_computer_spec.rb +15 -0
  24. data/spec/adauth_ad_object_group_spec.rb +21 -0
  25. data/spec/adauth_ad_object_ou_spec.rb +18 -0
  26. data/spec/adauth_ad_object_user_spec.rb +27 -0
  27. data/spec/adauth_authenticate_spec.rb +39 -0
  28. data/spec/adauth_config_spec.rb +15 -0
  29. data/spec/adauth_rails_model_bridge_spec.rb +37 -0
  30. data/spec/adauth_spec.rb +2 -30
  31. data/spec/spec_helper.rb +34 -0
  32. metadata +52 -38
  33. data/Readme.rdoc +0 -66
  34. data/lib/adauth/admin_connection.rb +0 -26
  35. data/lib/adauth/group.rb +0 -100
  36. data/lib/adauth/helpers.rb +0 -28
  37. data/lib/adauth/user.rb +0 -114
  38. data/lib/adauth/user_model.rb +0 -76
  39. data/lib/generators/adauth/all/USAGE +0 -5
  40. data/lib/generators/adauth/all/all_generator.rb +0 -18
  41. data/lib/generators/adauth/user_model/USAGE +0 -14
  42. data/lib/generators/adauth/user_model/templates/model.rb.erb +0 -3
  43. data/lib/generators/adauth/user_model/user_model_generator.rb +0 -32
  44. data/spec/adauth_group_spec.rb +0 -51
  45. data/spec/adauth_user_model_spec.rb +0 -80
  46. data/spec/adauth_user_spec.rb +0 -213
@@ -1,26 +0,0 @@
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
@@ -1,100 +0,0 @@
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.eq("memberof","CN=#{name},#{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
@@ -1,28 +0,0 @@
1
- module Adauth
2
-
3
- # Helper methods for rails
4
- module Helpers
5
-
6
- # Creates a form_tag for the adauth form
7
- #
8
- # Sets the html id to "adauth_login" and the form destination to "/adauth"
9
- def adauth_form
10
- form_tag '/adauth', :id => "adauth_login" do
11
- yield.html_safe
12
- end
13
- end
14
-
15
- # Create the default form by calling `adauth_form` and passing a username and password input
16
- def default_adauth_form
17
- adauth_form do
18
- "<p>#{label_tag :username}:
19
- #{text_field_tag :username}</p>
20
- <p>#{label_tag :password}:
21
- #{password_field_tag :password}</p>
22
- <p>#{submit_tag "Login!"}</p>"
23
- end
24
- end
25
- end
26
- end
27
-
28
- ActionView::Base.send :include, Adauth::Helpers if defined? ActionView
@@ -1,114 +0,0 @@
1
- module Adauth
2
-
3
- # The class which links to Active Directory, based on http://metautonomo.us/2008/04/04/simplified-active-directory-authentication/
4
- #
5
- # Do no call Adauth::User.new, use Adauth::User.authenticate instead. For all of Adauth additional filtering use Adauth.authenticate.
6
- class User
7
-
8
- # Single vales where the method maps directly to one Active Directory attribute
9
- ATTR_SV = {
10
- :login => :samaccountname,
11
- :first_name => :givenname,
12
- :last_name => :sn,
13
- :email => :mail,
14
- :name => :name
15
- }
16
-
17
- # Multi values where the method needs to return an array for values.
18
- ATTR_MV = {
19
- :groups => [ :memberof,
20
- Proc.new {|g| g.sub(/.*?CN=(.*?),.*/, '\1')} ],
21
- :ous => [ :memberof,
22
- Proc.new {|g| g.scan(/OU=.*?,/).map { |e| e.sub!(/OU=/,'').sub(/,/,'') } } ]
23
- }
24
-
25
- # Authenticates a user against Active Directory and returns an instance of self
26
- #
27
- # Called as:
28
- # Adauth::User.authenticate("username", "password")
29
- #
30
- # Usage would by-pass Adauths group filtering.
31
- def self.authenticate(login, pass)
32
- return nil if login.empty? or pass.empty?
33
- conn = Adauth::Connection.bind(login, pass)
34
- if conn and user = conn.search(:filter => Net::LDAP::Filter.eq('sAMAccountName', login)).first
35
- return self.new(user)
36
- else
37
- return nil
38
- end
39
- rescue Net::LDAP::LdapError => e
40
- return nil
41
- end
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
-
56
- # Returns the full name of the user
57
- #
58
- # Combines the first_name and last_name attributes to create full_name
59
- def full_name
60
- self.first_name + ' ' + self.last_name
61
- end
62
-
63
- # Returns true if the user is a member of the passed group.
64
- def member_of?(group)
65
- self.groups.include?(group)
66
- end
67
-
68
- private
69
-
70
- def initialize(entry)
71
- @entry = entry
72
- self.class.class_eval do
73
- generate_single_value_readers
74
- generate_multi_value_readers
75
- end
76
- end
77
-
78
- def self.generate_single_value_readers
79
- ATTR_SV.merge(Adauth.config.ad_sv_attrs).each_pair do |k, v|
80
- val, block = Array(v)
81
- define_method(k) do
82
- if @entry.attribute_names.include?(val)
83
- if block.is_a?(Proc)
84
- return block[@entry.send(val)]
85
- else
86
- return @entry.send(val).to_s
87
- end
88
- else
89
- return ''
90
- end
91
- end
92
- end
93
- end
94
-
95
- def self.generate_multi_value_readers
96
- ATTR_MV.merge(Adauth.config.ad_mv_attrs).each_pair do |k, v|
97
- val, block = Array(v)
98
- define_method(k) do
99
- if @entry.attribute_names.include?(val)
100
- if block.is_a?(Proc)
101
- output = @entry.send(val).collect(&block)
102
- output = output.first if output.first.is_a? Array
103
- return output
104
- else
105
- return @entry.send(val)
106
- end
107
- else
108
- return []
109
- end
110
- end
111
- end
112
- end
113
- end
114
- end
@@ -1,76 +0,0 @@
1
- module Adauth
2
-
3
- # Module desgined to be included in a ActiveRecord user model
4
- module UserModel
5
-
6
- # Adds class methods to the ActiveRecord model when included
7
- def self.included(base)
8
- base.extend ClassMethods
9
- end
10
-
11
- # Returns an array of groups for the user
12
- #
13
- # Called as:
14
- # UserInstance.groups
15
- #
16
- # The array is generated from the group_strings attribute which is set by the adauth update and create methods. This array will match the windows security groups the user is a member of.
17
- def groups
18
- group_strings.split(", ")
19
- end
20
-
21
- # Returns an array of groups for the user
22
- #
23
- # Called as:
24
- # UserInstance.ous
25
- #
26
- # The array is generated from the group_strings attribute which is set by the adauth update and create methods. This array will match the orginizational units the user is a member of.
27
- def ous
28
- ou_strings.split(", ")
29
- end
30
-
31
- # Update the user record using an instance of Adauth::User
32
- #
33
- # Called as:
34
- # UserInstance.update_from_adauth(AdauthUserInstance)
35
- #
36
- # This method is called on login and shouldn't need to be called at any other time
37
- def update_from_adauth(adauth_user)
38
- self.group_strings = adauth_user.groups.join(", ")
39
- self.name = adauth_user.name.gsub(/\"|\[|\]/, "")
40
- self.save
41
- end
42
-
43
- # Class methods for the UserModel
44
- module ClassMethods
45
-
46
- # Used during the login process to return the users database record.
47
- #
48
- # Takes an instance of Adauth::User as an input
49
- #
50
- # Called as
51
- # YourUserModel.return_and_create_with_adauth(AdauthUserInstance)
52
- #
53
- # If the user has no user record in the database one will be created. All the details on the record (new and old) will be updated to the lastest details from the AD server
54
- def return_and_create_with_adauth(adauth_user)
55
- user = (find_by_login(adauth_user.login.gsub(/\"|\[|\]/, "")) || create_user_with_adauth(adauth_user))
56
- user.update_from_adauth(adauth_user)
57
- return user
58
- end
59
-
60
- # Creates a user record from an instance of Adauth::User
61
- #
62
- # Called as:
63
- # YourUserModel.create_user_with_adauth(AdauthUserInstance)
64
- #
65
- # Takes the Adauth::User input and creates a user record with matching details
66
- def create_user_with_adauth(adauth_user)
67
- create! do |user|
68
- user.login = adauth_user.login.gsub(/\"|\[|\]/, "")
69
- user.group_strings = adauth_user.groups.join(", ")
70
- user.ou_strings = adauth_user.ous.join(", ")
71
- user.name = adauth_user.name.gsub(/\"|\[|\]/, "")
72
- end
73
- end
74
- end
75
- end
76
- end
@@ -1,5 +0,0 @@
1
- Description:
2
- Runs all the adauth generators with defaults.
3
-
4
- Example:
5
- rails g adauth:all
@@ -1,18 +0,0 @@
1
- module Adauth
2
- module Generators
3
-
4
- # Runs all of Adauths Generators
5
- class AllGenerator < Rails::Generators::Base
6
-
7
- # Calls all of Adauth Generators
8
- #
9
- # Called by running
10
- # rails g adauth:all
11
- def all_generators
12
- generate "adauth:config"
13
- generate "adauth:user_model"
14
- generate "adauth:sessions"
15
- end
16
- end
17
- end
18
- end
@@ -1,14 +0,0 @@
1
- Description:
2
- Default for MODEL_NAME is user
3
- Default for MIGRATION_NAME is "create_(plural of MODEL_NAME)"
4
- Creates a model for storing Adauth users which inherits from Adauth::UserModel
5
-
6
- Example:
7
- adauth:user_model
8
- Will result in app/model/user.rb and a migration called create_users
9
-
10
- adauth:user_model employee
11
- Will result in app/model/employee.rb and a migration called create_employees
12
-
13
- adauth:user_model employee add_adauth_to_employees
14
- Will result in app/model/employee.rb and a migration called add_adauth_to_employees
@@ -1,3 +0,0 @@
1
- class <%= model_name.camelize %> < ActiveRecord::Base
2
- include Adauth::UserModel
3
- end
@@ -1,32 +0,0 @@
1
- module Adauth
2
- module Generators
3
-
4
- # Creates a user model with migration
5
- class UserModelGenerator < Rails::Generators::Base
6
- source_root File.expand_path('../templates', __FILE__)
7
- argument :model_name, :type => :string, :default => "user"
8
- argument :migration_name, :type => :string, :default => false
9
-
10
- # Creates a user model with migration
11
- #
12
- # Called as:
13
- # rails g adauth:user_model
14
- #
15
- # Has 2 optional parameters, model_name which defaults to "user" and migration_name which defaults to "create_users"
16
- def generate_user_model
17
- template "model.rb.erb", "app/models/#{file_name}.rb"
18
- generate "migration", "#{migration_name_for_array}", "login:string", "group_strings:string", "name:string", "ou_strings:string"
19
- end
20
-
21
- private
22
-
23
- def file_name
24
- model_name.underscore
25
- end
26
-
27
- def migration_name_for_array
28
- migration_name || "create_#{model_name.pluralize}"
29
- end
30
- end
31
- end
32
- end
@@ -1,51 +0,0 @@
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