central_authentication 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
5
+ *.sw*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in central_authentication.gemspec
4
+ gemspec
@@ -0,0 +1 @@
1
+ require 'bundler/gem_tasks'
@@ -0,0 +1,20 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "central_authentication/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "central_authentication"
7
+ s.version = CentralAuthentication::VERSION
8
+ s.authors = ["Hannes Benson"]
9
+ s.email = ["hannes@mpowered.co.za"]
10
+ s.homepage = ""
11
+ s.summary = %q{Central Authentication using Authlogic}
12
+ s.description = %q{This gem allows you to share a central database and using authlogic stores and retrieves passwords}
13
+
14
+ s.rubyforge_project = "central_authentication"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+ end
@@ -0,0 +1,13 @@
1
+ class CentralAuthenticationGenerator < Rails::Generator::Base
2
+ def manifest
3
+ record do |m|
4
+ m.migration_template 'add_auth_id_to_users.rb', 'db/migrate', :migration_file_name => 'add_auth_id_to_users.rb'
5
+ m.sleep(2)
6
+ m.migration_template 'add_users_table_to_cauth_db.rb', 'db/migrate', :migration_file_name => 'add_users_table_to_cauth_db.rb'
7
+ m.sleep(2)
8
+ m.migration_template 'create_central_authentication_users.rb', 'db/migrate', :migration_file_name => 'create_central_authentication_users.rb'
9
+ m.sleep(2)
10
+ m.migration_template 'remove_password_related_fields_from_user.rb', 'db/migrate', :migration_file_name => 'remove_password_related_fields_from_user.rb'
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,11 @@
1
+ class AddAuthIdToUsers < ActiveRecord::Migration
2
+ def self.up
3
+ change_table :users do |t|
4
+ t.references :central_auth_user
5
+ end
6
+ end
7
+
8
+ def self.down
9
+ remove_column :users, :central_auth_user_id
10
+ end
11
+ end
@@ -0,0 +1,21 @@
1
+ class AddUsersTableToCauthDb < CentralAuthentication::Migration
2
+ def self.up
3
+ ActiveRecord::Base.connection.initialize_schema_migrations_table
4
+ if !table_exists?(:users)
5
+ create_table :users do |t|
6
+ t.column :email, :string, :limit => 100
7
+ t.column :crypted_password, :string
8
+ t.column :password_salt, :string
9
+ t.column :created_at, :datetime
10
+ t.column :updated_at, :datetime
11
+ t.column :persistence_token, :string
12
+ t.column :perishable_token, :string
13
+ t.column :password_expires_on, :date
14
+ end
15
+ end
16
+ end
17
+
18
+ def self.down
19
+ drop_table :users
20
+ end
21
+ end
@@ -0,0 +1,17 @@
1
+ class CreateCentralAuthenticationUsers < ActiveRecord::Migration
2
+ def self.up
3
+ User.all.each do |user|
4
+ cu = CentralAuthentication::User.find_by_email user.email
5
+ if cu.nil?
6
+ cu = CentralAuthentication::User.create(:email => user.email, :password_salt => user.password_salt, :crypted_password => user.crypted_password, :perishable_token => user.perishable_token, :persistence_token => user.persistence_token)
7
+ puts "Create user #{user.name}"
8
+ else
9
+ puts "Updating user #{user.name}"
10
+ end
11
+ user.update_attribute(:central_auth_user_id, cu.id)
12
+ end
13
+ end
14
+
15
+ def self.down
16
+ end
17
+ end
@@ -0,0 +1,15 @@
1
+ class RemovePasswordRelatedFieldsFromUser < ActiveRecord::Migration
2
+ def self.up
3
+ remove_column :users, :password_salt
4
+ remove_column :users, :crypted_password
5
+ remove_column :users, :persistence_token
6
+ remove_column :users, :perishable_token
7
+ end
8
+
9
+ def self.down
10
+ add_column :users, :password_salt, :string
11
+ add_column :users, :crypted_password, :string
12
+ add_column :users, :persistence_token, :string
13
+ add_column :users, :perishable_token, :string
14
+ end
15
+ end
@@ -0,0 +1,7 @@
1
+ require "authlogic"
2
+
3
+ require "central_authentication/version"
4
+ require "central_authentication/connection"
5
+ require "central_authentication/migration"
6
+ require "central_authentication/user"
7
+ require "central_authentication/acts_as_central_authentication_user"
@@ -0,0 +1,84 @@
1
+ module ActiveRecord
2
+ module Acts
3
+ module ActsAsCentralAuthenticationUser
4
+ def self.included(base)
5
+ base.extend ClassMethods
6
+ end
7
+
8
+ module ClassMethods
9
+ def acts_as_central_authentication_user
10
+ include InstanceMethods
11
+ belongs_to :central_authentication_user, :foreign_key => 'central_auth_user_id', :class_name => 'CentralAuthentication::User'
12
+
13
+ attr_accessor :password_confirmation, :password
14
+
15
+ #Here we set the authlogic options:
16
+ # 1. Email will be used as the login field
17
+ # 2. It doesn't validate the password field against a database field
18
+ # 3. It doesn't validate the login field. Authlogic automatically validates the email field.
19
+ acts_as_authentic do |config|
20
+ config.login_field = :email
21
+ config.validate_password_field = false
22
+ config.validate_login_field = false
23
+ end
24
+
25
+ validates_presence_of :password
26
+ validates_presence_of :password_confirmation
27
+ validates_format_of :password, :with => /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?!.*\s).{9,15}$/,
28
+ :message => 'must contain at least one uppercase and one lowercase letter as well as at least one numerical character.'
29
+ validates_format_of :password_confirmation, :with => /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?!.*\s).{9,15}$/,
30
+ :message => 'must contain at least one uppercase and one lowercase letter as well as at least one numerical character.'
31
+ validates_confirmation_of :password
32
+
33
+ before_save :create_or_set_cauth_user
34
+ end
35
+ end
36
+ module InstanceMethods
37
+ def create_cauth_user
38
+ CentralAuthentication::User.create(:email => self.email, :password => self.password, :password_confirmation => self.password_confirmation, :password_expires_on => Date.today + 30.days, :persistence_token => self.persistence_token)
39
+ end
40
+
41
+ def update_cauth_user(cauth_user)
42
+ if self.password.blank?
43
+ cauth_user.update_attributes(:email => self.email)
44
+ else
45
+ cauth_user.update_attributes(:password => self.password, :password_confirmation => self.password_confirmation, :email => self.email)
46
+ end
47
+ end
48
+
49
+ def create_or_set_cauth_user
50
+ cauth_user = CentralAuthentication::User.find_by_email(self.email)
51
+ if cauth_user.nil?
52
+ cauth_user = create_cauth_user
53
+ self.central_auth_user_id = cauth_user.id
54
+ else
55
+ update_cauth_user(cauth_user)
56
+ end
57
+ end
58
+
59
+ def persistence_token
60
+ # This method returns the persistence_token from the central authentication user. If this user does not exist yet it returns a arbitrary string otherwise it fails validation.
61
+ return 'a' if central_authentication_user.nil?
62
+ central_authentication_user.persistence_token
63
+ end
64
+
65
+ def persistence_token=(value)
66
+ return value if central_authentication_user.nil?
67
+ central_authentication_user.update_attribute(persistence_token, value)
68
+ end
69
+
70
+ def persistence_token_changed?
71
+ return false if central_authentication_user.nil?
72
+ central_authentication_user.persistence_token_changed?
73
+ end
74
+
75
+ protected
76
+ def valid_central_authentication_credentials(password)
77
+ CentralAuthentication::User.valid?(self.email, password)
78
+ end
79
+ end
80
+ end
81
+ end
82
+ end
83
+
84
+ ActiveRecord::Base.send(:include, ActiveRecord::Acts::ActsAsCentralAuthenticationUser)
@@ -0,0 +1,8 @@
1
+ class CentralAuthentication::Connection < ActiveRecord::Base
2
+ establish_connection "cauth_#{Rails.env}"
3
+ self.abstract_class = true
4
+
5
+ def self.table_name_prefix
6
+ "cauth_#{Rails.env}."
7
+ end
8
+ end
@@ -0,0 +1,5 @@
1
+ class CentralAuthentication::Migration < ActiveRecord::Migration
2
+ def self.connection
3
+ CentralAuthentication::Connection.connection
4
+ end
5
+ end
@@ -0,0 +1,12 @@
1
+ class CentralAuthentication::User < CentralAuthentication::Connection
2
+ acts_as_authentic do |config|
3
+ config.merge_validates_length_of_password_field_options({:minimum => 9})
4
+ config.merge_validates_length_of_password_confirmation_field_options({:minimum => 9})
5
+ config.login_field = :email
6
+ end
7
+
8
+ def self.valid?(email, password)
9
+ return false if !(cu = CentralAuthentication::User.find_by_email(email))
10
+ cu.valid_password?(password)
11
+ end
12
+ end
@@ -0,0 +1,3 @@
1
+ module CentralAuthentication
2
+ VERSION = "0.0.2"
3
+ end
metadata ADDED
@@ -0,0 +1,63 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: central_authentication
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Hannes Benson
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-09-27 00:00:00.000000000 +02:00
13
+ default_executable:
14
+ dependencies: []
15
+ description: This gem allows you to share a central database and using authlogic stores
16
+ and retrieves passwords
17
+ email:
18
+ - hannes@mpowered.co.za
19
+ executables: []
20
+ extensions: []
21
+ extra_rdoc_files: []
22
+ files:
23
+ - .gitignore
24
+ - Gemfile
25
+ - Rakefile
26
+ - central_authentication.gemspec
27
+ - generators/central_authentication_generator.rb
28
+ - generators/templates/add_auth_id_to_users.rb
29
+ - generators/templates/add_users_table_to_cauth_db.rb
30
+ - generators/templates/create_central_authentication_users.rb
31
+ - generators/templates/remove_password_related_fields_from_user.rb
32
+ - lib/central_authentication.rb
33
+ - lib/central_authentication/acts_as_central_authentication_user.rb
34
+ - lib/central_authentication/connection.rb
35
+ - lib/central_authentication/migration.rb
36
+ - lib/central_authentication/user.rb
37
+ - lib/central_authentication/version.rb
38
+ has_rdoc: true
39
+ homepage: ''
40
+ licenses: []
41
+ post_install_message:
42
+ rdoc_options: []
43
+ require_paths:
44
+ - lib
45
+ required_ruby_version: !ruby/object:Gem::Requirement
46
+ none: false
47
+ requirements:
48
+ - - ! '>='
49
+ - !ruby/object:Gem::Version
50
+ version: '0'
51
+ required_rubygems_version: !ruby/object:Gem::Requirement
52
+ none: false
53
+ requirements:
54
+ - - ! '>='
55
+ - !ruby/object:Gem::Version
56
+ version: '0'
57
+ requirements: []
58
+ rubyforge_project: central_authentication
59
+ rubygems_version: 1.6.2
60
+ signing_key:
61
+ specification_version: 3
62
+ summary: Central Authentication using Authlogic
63
+ test_files: []