devise_pg_authenticatable 0.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 ADDED
@@ -0,0 +1,12 @@
1
+ ## VIM
2
+ *.swp
3
+
4
+ ## GIT
5
+ .gitconfig
6
+
7
+ ## PROJECT::GENERAL
8
+ coverage
9
+ rdoc
10
+ pkg
11
+
12
+ ## PROJECT::SPECIFIC
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 [name of plugin creator]
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,90 @@
1
+ Devise PG Authenticatable
2
+ =================
3
+
4
+ Devise PG Authenticatable is a PostgreSQL based authentication strategy for the [Devise](http://github.com/plataformatec/devise) authentication framework. PG Authenticatable is replacement for module :database\_authenticatable from Devise. This module authenticate users against PostgreSQL system database.
5
+
6
+ This module is based on Devise-Imapable and Devise-LDAP-ldap\_authenticatable.
7
+
8
+ Requirements
9
+ ------------
10
+
11
+ - Rails 2.3.5
12
+ - Devise 1.0.7
13
+
14
+ Installation
15
+ ------------
16
+
17
+ script/plugin install git://github.com/boblin/devise_pg_authenticatable.git
18
+
19
+
20
+ Setup
21
+ -----
22
+
23
+ Once devise\_pg\_authenticatable is installed, all you need to do is setup the user model which includes a small addition to the model itself and to the schema.
24
+
25
+ First the schema :
26
+
27
+ create_table :users do |t|
28
+ t.pg_authenticatable, :null => false
29
+ end
30
+
31
+ and indexes (optional) :
32
+
33
+ add_index :login, :unique => true
34
+
35
+ and don’t forget to migrate :
36
+
37
+ rake db:migrate.
38
+
39
+ then the model :
40
+
41
+ class User < ActiveRecord::Base
42
+ devise :pg_authenticatable, :rememberable, :trackable, :timeoutable
43
+
44
+ # Setup accessible (or protected) attributes for your model
45
+ attr_accessible :login, :password, :remember_me
46
+ ...
47
+ end
48
+
49
+ and finally change the authentication key in the devise initializer :
50
+
51
+ Devise.setup do |config|
52
+ ...
53
+ config.authentication_keys = [ :login ]
54
+ ...
55
+ end
56
+
57
+ I recommend using :rememberable, :trackable, :timeoutable as it gives a full feature set for logins.
58
+
59
+ Usage
60
+ -----
61
+
62
+ Devise PG Authenticatable works in replacement of Authenticatable,
63
+ but because we have to change the authentication\_keys, you'll need to run:
64
+
65
+ script/generate devise_views
66
+
67
+ and customize your login pages to use :login, instead of :email.
68
+
69
+ ------------------------------------------------------------
70
+
71
+ **_Please Note_**
72
+
73
+ This devise plugin has not been tested with Authenticatable enabled at the same time. This is meant as a drop in replacement for Authenticatable allowing for a semi single sign on approach.
74
+
75
+
76
+ References
77
+ ----------
78
+
79
+ * [Devise](http://github.com/plataformatec/devise)
80
+ * [Warden](http://github.com/hassox/warden)
81
+
82
+
83
+ TODO
84
+ ----
85
+
86
+ - Tests
87
+
88
+ Released under the MIT license
89
+
90
+ Copyright (c) 2010 Bohuslav Blin
data/Rakefile ADDED
@@ -0,0 +1,41 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'rake/testtask'
4
+ require 'rake/rdoctask'
5
+
6
+ desc 'Default: run unit tests.'
7
+ task :default => :test
8
+
9
+ desc 'Test the devise_pg_authenticatable plugin.'
10
+ Rake::TestTask.new(:test) do |t|
11
+ t.libs << 'lib'
12
+ t.libs << 'test'
13
+ t.pattern = 'test/**/*_test.rb'
14
+ t.verbose = true
15
+ end
16
+
17
+ desc 'Generate documentation for the devise_pg_authenticatable plugin.'
18
+ Rake::RDocTask.new(:rdoc) do |rdoc|
19
+ rdoc.rdoc_dir = 'rdoc'
20
+ rdoc.title = 'DevisePgAuthenticatable'
21
+ rdoc.options << '--line-numbers' << '--inline-source'
22
+ rdoc.rdoc_files.include('README')
23
+ rdoc.rdoc_files.include('lib/**/*.rb')
24
+ end
25
+
26
+ begin
27
+ require 'jeweler'
28
+ Jeweler::Tasks.new do |gemspec|
29
+ gemspec.name = "devise_pg_authenticatable"
30
+ gemspec.summary = "Devise PostgreSQL authentication module"
31
+ gemspec.description = "Devise PostgreSQL authentication module"
32
+ gemspec.email = "bohuslav@blin.cz"
33
+ gemspec.homepage = "http://github.com/boblin/devise_pg_authenticatable"
34
+ gemspec.authors = ["Bohuslav Blin"]
35
+ gemspec.add_runtime_dependency "devise", "> 1.0.5"
36
+ end
37
+ Jeweler::GemcutterTasks.new
38
+ rescue LoadError
39
+ puts "Jeweler not available. Install it with: gem install jeweler"
40
+ end
41
+
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,58 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{devise_pg_authenticatable}
8
+ s.version = "0.1.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Bohuslav Blin"]
12
+ s.date = %q{2010-06-11}
13
+ s.description = %q{Devise PostgreSQL authentication module}
14
+ s.email = %q{bohuslav@blin.cz}
15
+ s.extra_rdoc_files = [
16
+ "README.md"
17
+ ]
18
+ s.files = [
19
+ ".gitignore",
20
+ "MIT-LICENSE",
21
+ "README.md",
22
+ "Rakefile",
23
+ "VERSION",
24
+ "devise_pg_authenticatable.gemspec",
25
+ "lib/devise_pg_authenticatable.rb",
26
+ "lib/devise_pg_authenticatable/model.rb",
27
+ "lib/devise_pg_authenticatable/pg_adapter.rb",
28
+ "lib/devise_pg_authenticatable/routes.rb",
29
+ "lib/devise_pg_authenticatable/schema.rb",
30
+ "lib/devise_pg_authenticatable/strategy.rb",
31
+ "rails/init.rb",
32
+ "test/devise_pg_authenticatable_test.rb",
33
+ "test/test_helper.rb"
34
+ ]
35
+ s.homepage = %q{http://github.com/boblin/devise_pg_authenticatable}
36
+ s.rdoc_options = ["--charset=UTF-8"]
37
+ s.require_paths = ["lib"]
38
+ s.rubygems_version = %q{1.3.6}
39
+ s.summary = %q{Devise PostgreSQL authentication module}
40
+ s.test_files = [
41
+ "test/test_helper.rb",
42
+ "test/devise_pg_authenticatable_test.rb"
43
+ ]
44
+
45
+ if s.respond_to? :specification_version then
46
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
47
+ s.specification_version = 3
48
+
49
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
50
+ s.add_runtime_dependency(%q<devise>, ["> 1.0.5"])
51
+ else
52
+ s.add_dependency(%q<devise>, ["> 1.0.5"])
53
+ end
54
+ else
55
+ s.add_dependency(%q<devise>, ["> 1.0.5"])
56
+ end
57
+ end
58
+
@@ -0,0 +1,70 @@
1
+ require 'devise_pg_authenticatable/strategy'
2
+
3
+ module Devise
4
+ module Models
5
+ # PG Module, responsible for validating the user credentials via postgresql users.
6
+ #
7
+ # Examples:
8
+ #
9
+ # Person.authenticate_with_pg(:login => 'user1',:password => 'pass')
10
+ #
11
+ module PgAuthenticatable
12
+ def self.included(base)
13
+ base.class_eval do
14
+ extend ClassMethods
15
+
16
+ attr_accessor :password
17
+ end
18
+ end
19
+
20
+ # Set password to nil
21
+ def clean_up_passwords
22
+ self.password = nil
23
+ end
24
+
25
+ # Checks if a resource is valid upon authentication.
26
+ def valid_pg_authentication?(password)
27
+ Devise::PgAdapter.valid_credentials?(self.login, password)
28
+ end
29
+
30
+ module ClassMethods
31
+ # Authenticate a user based on configured attribute keys. Returns the
32
+ # authenticated user if it's valid or nil.
33
+ def authenticate_with_pg(attributes={})
34
+ return unless attributes[:login].present?
35
+ conditions = attributes.slice(:login)
36
+
37
+ unless conditions[:login]
38
+ conditions[:login] = "#{conditions[:login]}"
39
+ end
40
+
41
+ resource = find_for_pg_authentication(conditions)
42
+ # resource = new(conditions) if resource.nil?
43
+
44
+ # resource = find_for_pg_authentication(conditions) || new(conditions)
45
+
46
+
47
+ if resource.try(:valid_pg_authentication?, attributes[:password])
48
+ resource.new_record? ? create(conditions) : resource
49
+ end
50
+ end
51
+
52
+ protected
53
+
54
+ # Find first record based on conditions given (ie by the sign in form).
55
+ # Overwrite to add customized conditions, create a join, or maybe use a
56
+ # namedscope to filter records while authenticating.
57
+ # Example:
58
+ #
59
+ # def self.find_for_imap_authentication(conditions={})
60
+ # conditions[:active] = true
61
+ # find(:first, :conditions => conditions)
62
+ # end
63
+ #
64
+ def find_for_pg_authentication(conditions)
65
+ find(:first, :conditions => conditions)
66
+ end
67
+ end
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,16 @@
1
+ module Devise
2
+ # simple adapter for postgresql credential checking
3
+ module PgAdapter
4
+ def self.valid_credentials?(login, password)
5
+ abcs = ActiveRecord::Base.configurations[RAILS_ENV]
6
+ host = abcs[:host]
7
+ port = abcs[:port] || 5432
8
+ conn = PGconn.connect(host, port, '', '', "template1", login, password)
9
+ conn.close()
10
+ true
11
+ rescue PGError, NoMethodError
12
+ false
13
+ end
14
+ end
15
+
16
+ end
@@ -0,0 +1,6 @@
1
+ ActionController::Routing::RouteSet::Mapper.class_eval do
2
+
3
+ protected
4
+ # reuse the session routes and controller
5
+ alias :pg_authenticatable :database_authenticatable
6
+ end
@@ -0,0 +1,12 @@
1
+ Devise::Schema.class_eval do
2
+ # Creates login
3
+ #
4
+ # == Options
5
+ # * :null - When true, allow columns to be null.
6
+ def pg_authenticatable(options={})
7
+ null = options[:null] || false
8
+
9
+ apply_schema :login, String, :null => null
10
+ end
11
+
12
+ end
@@ -0,0 +1,36 @@
1
+ require 'devise/strategies/base'
2
+
3
+ module Devise
4
+ module Strategies
5
+ # Strategy for signing in a user based on his login and password using postgresql users
6
+ # Redirects to sign_in page if it's not authenticated
7
+ class PgAuthenticatable < Base
8
+ def valid?
9
+ valid_controller? && valid_params? && mapping.to.respond_to?(:authenticate_with_pg)
10
+ end
11
+
12
+ # Authenticate a user based on login and password params, returning to warden
13
+ # success and the authenticated user if everything is okay. Otherwise redirect
14
+ # to sign in page.
15
+ def authenticate!
16
+ if resource = mapping.to.authenticate_with_pg(params[scope])
17
+ success!(resource)
18
+ else
19
+ fail(:invalid)
20
+ end
21
+ end
22
+
23
+ protected
24
+
25
+ def valid_controller?
26
+ params[:controller] == 'sessions'
27
+ end
28
+
29
+ def valid_params?
30
+ params[scope] && params[scope][:password].present?
31
+ end
32
+ end
33
+ end
34
+ end
35
+
36
+ Warden::Strategies.add(:pg_authenticatable, Devise::Strategies::PgAuthenticatable)
@@ -0,0 +1,19 @@
1
+ # encoding: utf-8
2
+ require 'devise'
3
+
4
+ require 'devise_pg_authenticatable/schema'
5
+ require 'devise_pg_authenticatable/pg_adapter'
6
+ require 'devise_pg_authenticatable/routes'
7
+
8
+ module Devise
9
+ # database
10
+ # mattr_accessor :pg_database
11
+ # @@pg_database = nil
12
+ end
13
+
14
+ # Add pg_authenticatable strategy to defaults.
15
+ #
16
+ Devise.add_module(:pg_authenticatable,
17
+ :strategy => true,
18
+ :controller => :sessions,
19
+ :model => 'devise_pg_authenticatable/model')
data/rails/init.rb ADDED
@@ -0,0 +1,2 @@
1
+ # Include hook code here
2
+ require 'devise_pg_authenticatable'
@@ -0,0 +1,8 @@
1
+ require 'test_helper'
2
+
3
+ class DevisePgAuthenticatableTest < ActiveSupport::TestCase
4
+ # Replace this with your real tests.
5
+ test "the truth" do
6
+ assert true
7
+ end
8
+ end
@@ -0,0 +1,3 @@
1
+ require 'rubygems'
2
+ require 'active_support'
3
+ require 'active_support/test_case'
metadata ADDED
@@ -0,0 +1,90 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: devise_pg_authenticatable
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 0
9
+ version: 0.1.0
10
+ platform: ruby
11
+ authors:
12
+ - Bohuslav Blin
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-06-11 00:00:00 +02:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: devise
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">"
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 1
29
+ - 0
30
+ - 5
31
+ version: 1.0.5
32
+ type: :runtime
33
+ version_requirements: *id001
34
+ description: Devise PostgreSQL authentication module
35
+ email: bohuslav@blin.cz
36
+ executables: []
37
+
38
+ extensions: []
39
+
40
+ extra_rdoc_files:
41
+ - README.md
42
+ files:
43
+ - .gitignore
44
+ - MIT-LICENSE
45
+ - README.md
46
+ - Rakefile
47
+ - VERSION
48
+ - devise_pg_authenticatable.gemspec
49
+ - lib/devise_pg_authenticatable.rb
50
+ - lib/devise_pg_authenticatable/model.rb
51
+ - lib/devise_pg_authenticatable/pg_adapter.rb
52
+ - lib/devise_pg_authenticatable/routes.rb
53
+ - lib/devise_pg_authenticatable/schema.rb
54
+ - lib/devise_pg_authenticatable/strategy.rb
55
+ - rails/init.rb
56
+ - test/devise_pg_authenticatable_test.rb
57
+ - test/test_helper.rb
58
+ has_rdoc: true
59
+ homepage: http://github.com/boblin/devise_pg_authenticatable
60
+ licenses: []
61
+
62
+ post_install_message:
63
+ rdoc_options:
64
+ - --charset=UTF-8
65
+ require_paths:
66
+ - lib
67
+ required_ruby_version: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ segments:
72
+ - 0
73
+ version: "0"
74
+ required_rubygems_version: !ruby/object:Gem::Requirement
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ segments:
79
+ - 0
80
+ version: "0"
81
+ requirements: []
82
+
83
+ rubyforge_project:
84
+ rubygems_version: 1.3.6
85
+ signing_key:
86
+ specification_version: 3
87
+ summary: Devise PostgreSQL authentication module
88
+ test_files:
89
+ - test/test_helper.rb
90
+ - test/devise_pg_authenticatable_test.rb