toretore-simple_password_authentication 0.1.2 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- data/app/views/logins/_password.html.erb +13 -0
- data/app/views/logins/password.html.erb +3 -0
- data/config/locales/simple_password_authentication.yml +11 -0
- data/generators/simple_password_authentication/USAGE +5 -0
- data/generators/simple_password_authentication/simple_password_authentication_generator.rb +38 -0
- data/lib/simple_password_authentication.rb +10 -0
- data/lib/simple_password_authentication/controller_methods.rb +20 -0
- data/lib/simple_password_authentication/model_methods.rb +82 -0
- data/lib/simple_password_authentication/password_authenticator.rb +20 -0
- data/rails/init.rb +1 -0
- data/rails/install.rb +1 -0
- data/rails/uninstall.rb +1 -0
- data/tasks/simple_password_authentication_tasks.rake +4 -0
- metadata +23 -5
@@ -0,0 +1,13 @@
|
|
1
|
+
<% form_tag login_url(:authenticator => "password") do -%>
|
2
|
+
<p class="input text">
|
3
|
+
<label for="username"><%= User.human_attribute_name('username') %></label>
|
4
|
+
<%= text_field_tag "username" %>
|
5
|
+
</p>
|
6
|
+
<p class="input password">
|
7
|
+
<label for="password"><%= User.human_attribute_name('password') %></label>
|
8
|
+
<%= password_field_tag "password" %>
|
9
|
+
</p>
|
10
|
+
<p class="submit button">
|
11
|
+
<%= submit_tag t('simple_password_authentication.new.button') %>
|
12
|
+
</p>
|
13
|
+
<% end -%>
|
@@ -0,0 +1,38 @@
|
|
1
|
+
class SimplePasswordAuthenticationGenerator < Rails::Generator::NamedBase
|
2
|
+
def manifest
|
3
|
+
record do |m|
|
4
|
+
m.migration_template "migration:migration.rb", "db/migrate", :assigns => password_auth_assigns,
|
5
|
+
:migration_file_name => "add_password_authentication_fields_to_#{custom_file_name}" if options[:migration]
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
private
|
10
|
+
|
11
|
+
def custom_file_name
|
12
|
+
custom_name = class_name.underscore.downcase
|
13
|
+
custom_name = custom_name.pluralize if ActiveRecord::Base.pluralize_table_names
|
14
|
+
custom_name
|
15
|
+
end
|
16
|
+
|
17
|
+
def password_auth_assigns
|
18
|
+
returning({}) do |assigns|
|
19
|
+
assigns[:migration_action] = "add"
|
20
|
+
assigns[:class_name] = "add_password_authentication_fields_to_#{custom_file_name}"
|
21
|
+
assigns[:table_name] = "users"
|
22
|
+
assigns[:attributes] = password_auth_attributes
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def password_auth_attributes
|
27
|
+
returning attributes.dup do |attributes|
|
28
|
+
{"username" => "string", "hashed_password" => "string", "salt" => "string"}.each do |name, type|
|
29
|
+
attributes << Rails::Generator::GeneratedAttribute.new(name, type) unless attributes.any?{|a| a.name == name }
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def add_options!(opt)
|
35
|
+
opt.on('--migration', 'Create migration'){|v| options[:migration] = true }
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
@@ -0,0 +1,10 @@
|
|
1
|
+
require "simple_authentication"
|
2
|
+
require "simple_password_authentication/password_authenticator"
|
3
|
+
require "simple_password_authentication/model_methods"
|
4
|
+
require "simple_password_authentication/controller_methods"
|
5
|
+
|
6
|
+
SimpleAuthentication::ModelMethods::User.send(:include, SimplePasswordAuthentication::ModelMethods::User)
|
7
|
+
SimpleAuthentication::ModelMethods::User::ClassMethods.send(:include, SimplePasswordAuthentication::ModelMethods::User::ClassMethods)
|
8
|
+
#User.send(:include, SimplePasswordAuthentication::ModelMethods::User::Behavior)
|
9
|
+
SimpleAuthentication::ControllerMethods::Logins.send(:include, SimplePasswordAuthentication::ControllerMethods::Logins)
|
10
|
+
I18n.load_path.unshift(File.join(File.dirname(__FILE__), '..', 'config', 'locales', 'simple_password_authentication.yml'))
|
@@ -0,0 +1,82 @@
|
|
1
|
+
require "digest/sha1"
|
2
|
+
|
3
|
+
module SimplePasswordAuthentication
|
4
|
+
|
5
|
+
|
6
|
+
module ModelMethods
|
7
|
+
|
8
|
+
|
9
|
+
module User
|
10
|
+
|
11
|
+
attr_accessor :password
|
12
|
+
|
13
|
+
#Returns the salt value. If there is no value, one is generated.
|
14
|
+
def salt
|
15
|
+
read_attribute(:salt) or returning self.class.generate_salt do |s|
|
16
|
+
write_attribute(:salt, s)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
#Force re-generation of salt value. Returns new value.
|
21
|
+
def salt!
|
22
|
+
returning self.class.generate_salt do |s|
|
23
|
+
write_attribute(:salt, s)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def hashed_password=(pw)
|
28
|
+
write_attribute(:hashed_password, self.class.hash_password(pw, salt))
|
29
|
+
end
|
30
|
+
|
31
|
+
def password=(pw)
|
32
|
+
self.hashed_password = @password = pw
|
33
|
+
end
|
34
|
+
|
35
|
+
def correct_password?(pw)
|
36
|
+
self.class.hash_password(pw, salt) == hashed_password
|
37
|
+
end
|
38
|
+
|
39
|
+
private
|
40
|
+
|
41
|
+
|
42
|
+
module ClassMethods
|
43
|
+
|
44
|
+
#Hashes a password using a salt value
|
45
|
+
def hash_password(pw, salt)
|
46
|
+
Digest::SHA1.hexdigest(salt+pw)
|
47
|
+
end
|
48
|
+
|
49
|
+
#Generate a random salt value
|
50
|
+
def generate_salt
|
51
|
+
Digest::SHA1.hexdigest(Time.now.to_f.to_s)
|
52
|
+
end
|
53
|
+
|
54
|
+
#Find a user using a username and password
|
55
|
+
#Returns the user if successful, false otherwise
|
56
|
+
def authenticate(username, password)
|
57
|
+
if user = find_by_username(username)
|
58
|
+
if user.correct_password?(password)
|
59
|
+
user
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
end
|
65
|
+
|
66
|
+
|
67
|
+
module Behavior
|
68
|
+
|
69
|
+
def self.included(model_class)
|
70
|
+
model_class.validates_presence_of :username, :hashed_password
|
71
|
+
model_class.validates_confirmation_of :password
|
72
|
+
model_class.validates_uniqueness_of :username
|
73
|
+
end
|
74
|
+
|
75
|
+
end
|
76
|
+
|
77
|
+
|
78
|
+
end
|
79
|
+
|
80
|
+
end
|
81
|
+
|
82
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module SimpleAuthentication
|
2
|
+
|
3
|
+
|
4
|
+
class PasswordAuthenticator < Authenticator
|
5
|
+
|
6
|
+
|
7
|
+
def authentication_possible?
|
8
|
+
!params.values_at(:username, :password).any?{|v| v.blank? }
|
9
|
+
end
|
10
|
+
|
11
|
+
|
12
|
+
def authenticate
|
13
|
+
User.authenticate(params[:username], params[:password])
|
14
|
+
end
|
15
|
+
|
16
|
+
|
17
|
+
end
|
18
|
+
|
19
|
+
|
20
|
+
end
|
data/rails/init.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "simple_password_authentication"
|
data/rails/install.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
# Install hook code here
|
data/rails/uninstall.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
# Uninstall hook code here
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: toretore-simple_password_authentication
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Tore Darell
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-03-
|
12
|
+
date: 2009-03-16 00:00:00 -07:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
@@ -20,7 +20,7 @@ dependencies:
|
|
20
20
|
requirements:
|
21
21
|
- - "="
|
22
22
|
- !ruby/object:Gem::Version
|
23
|
-
version: 0.1.
|
23
|
+
version: 0.1.3
|
24
24
|
version:
|
25
25
|
description: Password authentication for SimpleAuthentication
|
26
26
|
email: toredarell@gmail.com
|
@@ -30,8 +30,26 @@ extensions: []
|
|
30
30
|
|
31
31
|
extra_rdoc_files: []
|
32
32
|
|
33
|
-
files:
|
34
|
-
|
33
|
+
files:
|
34
|
+
- lib/simple_password_authentication.rb
|
35
|
+
- lib/simple_password_authentication
|
36
|
+
- lib/simple_password_authentication/password_authenticator.rb
|
37
|
+
- lib/simple_password_authentication/controller_methods.rb
|
38
|
+
- lib/simple_password_authentication/model_methods.rb
|
39
|
+
- rails/uninstall.rb
|
40
|
+
- rails/install.rb
|
41
|
+
- rails/init.rb
|
42
|
+
- config/locales
|
43
|
+
- config/locales/simple_password_authentication.yml
|
44
|
+
- app/views
|
45
|
+
- app/views/logins
|
46
|
+
- app/views/logins/password.html.erb
|
47
|
+
- app/views/logins/_password.html.erb
|
48
|
+
- generators/simple_password_authentication
|
49
|
+
- generators/simple_password_authentication/USAGE
|
50
|
+
- generators/simple_password_authentication/templates
|
51
|
+
- generators/simple_password_authentication/simple_password_authentication_generator.rb
|
52
|
+
- tasks/simple_password_authentication_tasks.rake
|
35
53
|
has_rdoc: false
|
36
54
|
homepage: http://github.com/toretore/simple_password_authentication
|
37
55
|
post_install_message:
|