simple_user_auth 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +4 -0
- data/README.rdoc +11 -0
- data/Rakefile +2 -0
- data/lib/simple_user_auth/version.rb +3 -0
- data/lib/simple_user_auth.rb +162 -0
- data/simple_user_auth.gemspec +21 -0
- metadata +61 -0
data/.gitignore
ADDED
data/README.rdoc
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
= SimpleUserAuth
|
2
|
+
|
3
|
+
I put the code I used for user authentication in my first Rails app in this gem for use in my new apps. Feel free to use it wherever you want but keep in mind this was some of the first code I wrote in Ruby, so there are probably better ways to do certain aspects.
|
4
|
+
|
5
|
+
In your model
|
6
|
+
include SimpleUserAuth::Model
|
7
|
+
authenticate_by :email # Authenticate the user by email attribute
|
8
|
+
|
9
|
+
In ApplicationController
|
10
|
+
include SimpleUserAuth::Controller
|
11
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,162 @@
|
|
1
|
+
module SimpleUserAuth
|
2
|
+
|
3
|
+
module Model
|
4
|
+
|
5
|
+
def self.included(klass)
|
6
|
+
klass.extend(ClassMethods)
|
7
|
+
klass.class_eval do
|
8
|
+
include ClassInstanceMethods
|
9
|
+
validate :change_password_validator
|
10
|
+
validates :password,
|
11
|
+
:presence => { :if => :new_record_or_change_password? },
|
12
|
+
:confirmation => { :if => :new_record_or_change_password? },
|
13
|
+
:length => { :within => 6..40, :if => :new_record_or_change_password? }
|
14
|
+
before_save :encrypt_password
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
module ClassMethods
|
19
|
+
|
20
|
+
def authenticate_by(authenticator)
|
21
|
+
write_inheritable_attribute(:authenticator, authenticator)
|
22
|
+
end
|
23
|
+
|
24
|
+
def authenticate(search, submitted_password)
|
25
|
+
authenticator = read_inheritable_attribute(:authenticator)
|
26
|
+
user = find(:first, :conditions => ["#{authenticator} = ?", search])
|
27
|
+
return nil if user.nil?
|
28
|
+
return user if user.has_password?(submitted_password)
|
29
|
+
end
|
30
|
+
|
31
|
+
def authenticate_with_salt(id, cookie_salt)
|
32
|
+
user = find_by_id(id)
|
33
|
+
(user && user.salt == cookie_salt) ? user : nil
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
37
|
+
|
38
|
+
module ClassInstanceMethods
|
39
|
+
def change_password_validator
|
40
|
+
if change_password?
|
41
|
+
errors.add(:old_password, "doesn't match.") unless has_password?(current_password)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
def change_password?
|
46
|
+
@change_password ||= false
|
47
|
+
end
|
48
|
+
|
49
|
+
def change_password=(bool)
|
50
|
+
@change_password = bool
|
51
|
+
end
|
52
|
+
|
53
|
+
def has_password?(submitted_password)
|
54
|
+
encrypted_password == encrypt(submitted_password)
|
55
|
+
end
|
56
|
+
|
57
|
+
private
|
58
|
+
|
59
|
+
def encrypt_password
|
60
|
+
self.salt = make_salt if new_record?
|
61
|
+
self.encrypted_password = encrypt(password) if new_record_or_change_password?
|
62
|
+
end
|
63
|
+
def encrypt(string)
|
64
|
+
secure_hash("#{salt}--#{string}")
|
65
|
+
end
|
66
|
+
|
67
|
+
def make_salt
|
68
|
+
secure_hash("#{Time.now.utc}--#{password}")
|
69
|
+
end
|
70
|
+
|
71
|
+
def secure_hash(string)
|
72
|
+
Digest::SHA2.hexdigest(string)
|
73
|
+
end
|
74
|
+
|
75
|
+
def new_record_or_change_password?
|
76
|
+
new_record? || change_password?
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
end
|
81
|
+
|
82
|
+
module Controller
|
83
|
+
|
84
|
+
def self.included(klass)
|
85
|
+
klass.class_eval do
|
86
|
+
include ClassMethods
|
87
|
+
end
|
88
|
+
ActionView::Base.send(:include, ClassMethods)
|
89
|
+
end
|
90
|
+
|
91
|
+
module ClassMethods
|
92
|
+
|
93
|
+
def sign_in(user, remember_me = false)
|
94
|
+
if remember_me
|
95
|
+
cookies.permanent.signed[:remember_token] = [user.id, user.salt]
|
96
|
+
else
|
97
|
+
cookies.signed[:remember_token] = [user.id, user.salt]
|
98
|
+
end
|
99
|
+
self.current_user = user
|
100
|
+
end
|
101
|
+
|
102
|
+
def current_user=(user)
|
103
|
+
@current_user = user
|
104
|
+
end
|
105
|
+
|
106
|
+
def current_user
|
107
|
+
@current_user ||= user_from_remember_token
|
108
|
+
end
|
109
|
+
|
110
|
+
def signed_in?
|
111
|
+
!current_user.nil?
|
112
|
+
end
|
113
|
+
|
114
|
+
def not_signed_in?
|
115
|
+
current_user.nil?
|
116
|
+
end
|
117
|
+
|
118
|
+
def sign_out
|
119
|
+
cookies.delete(:remember_token)
|
120
|
+
self.current_user = nil
|
121
|
+
end
|
122
|
+
|
123
|
+
def current_user?(user)
|
124
|
+
user == current_user
|
125
|
+
end
|
126
|
+
|
127
|
+
def deny_access
|
128
|
+
store_location
|
129
|
+
redirect_to signin_path, :notice => "Please sign in to access this page."
|
130
|
+
end
|
131
|
+
|
132
|
+
def redirect_back_or(default)
|
133
|
+
redirect_to(session[:return_to] || default)
|
134
|
+
clear_return_to
|
135
|
+
end
|
136
|
+
|
137
|
+
def authenticate
|
138
|
+
deny_access unless signed_in?
|
139
|
+
end
|
140
|
+
|
141
|
+
private
|
142
|
+
|
143
|
+
def user_from_remember_token
|
144
|
+
User.authenticate_with_salt(*remember_token)
|
145
|
+
end
|
146
|
+
|
147
|
+
def remember_token
|
148
|
+
cookies.signed[:remember_token] || [nil, nil]
|
149
|
+
end
|
150
|
+
|
151
|
+
def store_location
|
152
|
+
session[:return_to] = request.fullpath
|
153
|
+
end
|
154
|
+
|
155
|
+
def clear_return_to
|
156
|
+
session[:return_to] = nil
|
157
|
+
end
|
158
|
+
end
|
159
|
+
|
160
|
+
end
|
161
|
+
|
162
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "simple_user_auth/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "simple_user_auth"
|
7
|
+
s.version = SimpleUserAuth::VERSION
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.authors = ["Erich Menge"]
|
10
|
+
s.email = ["erich@zenstack.com"]
|
11
|
+
s.homepage = ""
|
12
|
+
s.summary = %q{A simple user authentication mixin for Rails}
|
13
|
+
s.description = %q{A simple no frills user authentication gem for my Rails projects.}
|
14
|
+
|
15
|
+
s.rubyforge_project = "simple_user_auth"
|
16
|
+
|
17
|
+
s.files = `git ls-files`.split("\n")
|
18
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
19
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
20
|
+
s.require_paths = ["lib"]
|
21
|
+
end
|
metadata
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: simple_user_auth
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease:
|
5
|
+
version: 0.0.2
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Erich Menge
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
|
13
|
+
date: 2011-05-01 00:00:00 Z
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: A simple no frills user authentication gem for my Rails projects.
|
17
|
+
email:
|
18
|
+
- erich@zenstack.com
|
19
|
+
executables: []
|
20
|
+
|
21
|
+
extensions: []
|
22
|
+
|
23
|
+
extra_rdoc_files: []
|
24
|
+
|
25
|
+
files:
|
26
|
+
- .gitignore
|
27
|
+
- README.rdoc
|
28
|
+
- Rakefile
|
29
|
+
- lib/simple_user_auth.rb
|
30
|
+
- lib/simple_user_auth/version.rb
|
31
|
+
- simple_user_auth.gemspec
|
32
|
+
homepage: ""
|
33
|
+
licenses: []
|
34
|
+
|
35
|
+
post_install_message:
|
36
|
+
rdoc_options: []
|
37
|
+
|
38
|
+
require_paths:
|
39
|
+
- lib
|
40
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ">="
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: "0"
|
46
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
47
|
+
none: false
|
48
|
+
requirements:
|
49
|
+
- - ">="
|
50
|
+
- !ruby/object:Gem::Version
|
51
|
+
version: "0"
|
52
|
+
requirements: []
|
53
|
+
|
54
|
+
rubyforge_project: simple_user_auth
|
55
|
+
rubygems_version: 1.7.2
|
56
|
+
signing_key:
|
57
|
+
specification_version: 3
|
58
|
+
summary: A simple user authentication mixin for Rails
|
59
|
+
test_files: []
|
60
|
+
|
61
|
+
has_rdoc:
|