persistent_cookie_authentication_generator 0.0.1
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/MIT-LICENCE +20 -0
- data/USAGE +22 -0
- data/persistent_cookie_authentication_generator.rb +52 -0
- data/templates/identities.yml +18 -0
- data/templates/identity.rb +4 -0
- data/templates/login_cookie.rb +254 -0
- data/templates/login_cookies.yml +32 -0
- data/templates/migration.rb +42 -0
- data/templates/smtp_tls.rb +67 -0
- data/templates/user.rb +147 -0
- data/templates/user_change_password.rhtml +22 -0
- data/templates/user_controller.rb +302 -0
- data/templates/user_edit.rhtml +15 -0
- data/templates/user_environment.rb +9 -0
- data/templates/user_forgot_password.rhtml +9 -0
- data/templates/user_integration_test.rb +303 -0
- data/templates/user_login.rhtml +23 -0
- data/templates/user_notify.rb +55 -0
- data/templates/user_notify_change_password.rhtml +10 -0
- data/templates/user_notify_forgot_password.rhtml +8 -0
- data/templates/user_notify_signup.rhtml +10 -0
- data/templates/user_show.rhtml +15 -0
- data/templates/user_signup.rhtml +29 -0
- data/templates/user_system.rb +158 -0
- data/templates/user_test.rb +72 -0
- data/templates/users.yml +46 -0
- metadata +78 -0
@@ -0,0 +1,15 @@
|
|
1
|
+
<p><h2>User Details</h2></p><br/>
|
2
|
+
|
3
|
+
<% if flash[:notice] %><div class="notice"><%= flash[:notice] %></div><% end %>
|
4
|
+
|
5
|
+
<% for column in User.columns %>
|
6
|
+
<p>
|
7
|
+
<b class="attribute"><%= column.human_name %>:</b> <%=h @user.send(column.name) %>
|
8
|
+
</p>
|
9
|
+
<% end %>
|
10
|
+
|
11
|
+
|
12
|
+
<p><%= button_to 'Edit My Details', :action => 'edit' %></p>
|
13
|
+
<p><%= button_to 'Change Password', :action => 'change_password' %></p>
|
14
|
+
<p><%= button_to 'Log out', :action => 'logout' %></p>
|
15
|
+
|
@@ -0,0 +1,29 @@
|
|
1
|
+
<h1>Sign up</h1>
|
2
|
+
|
3
|
+
<% if flash[:notice] %><div class="notice"><%= flash[:notice] %></div><% end %>
|
4
|
+
|
5
|
+
<% form_for(:user, :url => { :action => 'user_signup' }) do |f| %>
|
6
|
+
<table>
|
7
|
+
<tr>
|
8
|
+
<td>Login ID(3-40 characters)</td>
|
9
|
+
<td><%= f.text_field(:login) %></td>
|
10
|
+
</tr>
|
11
|
+
<tr>
|
12
|
+
<td>Email</td>
|
13
|
+
<td><%= f.text_field(:email) %></td>
|
14
|
+
</tr>
|
15
|
+
<tr>
|
16
|
+
<td>Password(5-40 characters)</td>
|
17
|
+
<td><%= f.password_field(:password) %></td>
|
18
|
+
</tr>
|
19
|
+
<tr>
|
20
|
+
<td>Password confirmation</td>
|
21
|
+
<td><%= f.password_field(:password_confirmation) %></td>
|
22
|
+
</tr>
|
23
|
+
</table>
|
24
|
+
|
25
|
+
<p><%= submit_tag 'Sign Up' %></p>
|
26
|
+
<% end %>
|
27
|
+
|
28
|
+
|
29
|
+
|
@@ -0,0 +1,158 @@
|
|
1
|
+
require 'cgi'
|
2
|
+
|
3
|
+
module UserSystem
|
4
|
+
|
5
|
+
protected
|
6
|
+
|
7
|
+
# check if the user is logged in. sends it to the log in page if it isnt.
|
8
|
+
def login_required
|
9
|
+
|
10
|
+
#check if the identity is not null(0), and the state is private
|
11
|
+
return true if session['identity'] != 0 and session['state'] == PRIVATE_STATE
|
12
|
+
|
13
|
+
# If not, is the user being authenticated by a token?
|
14
|
+
if params['user'] != nil
|
15
|
+
id = params['user']['id']
|
16
|
+
key = params['key']
|
17
|
+
|
18
|
+
if id and key
|
19
|
+
return true if User.authenticate_by_token(id, key)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
# store current location so that we can come back after the user logged in
|
24
|
+
store_location
|
25
|
+
|
26
|
+
# call overwriteable reaction to unauthorized access
|
27
|
+
redirect_to :controller => "/user", :action => "login"
|
28
|
+
|
29
|
+
return false
|
30
|
+
end
|
31
|
+
|
32
|
+
|
33
|
+
|
34
|
+
# checks if the browser has a cookie
|
35
|
+
def cookie_required
|
36
|
+
|
37
|
+
#if it is in the protected/private state
|
38
|
+
if session['state'] != nil and session['state'] >= PROTECTED_STATE and session['identity'] != nil and session['identity'] > 0
|
39
|
+
return true
|
40
|
+
else
|
41
|
+
|
42
|
+
#set the cookie and reload, cos it wont be loaded in the current request
|
43
|
+
if session['cookie-set'] == nil
|
44
|
+
store_location
|
45
|
+
session['cookie-set'] = "true"
|
46
|
+
cookies[COOKIE_NAME] = { :value => LoginCookie.requireCookie(cookies[COOKIE_NAME]), :expires => 10.years.from_now }
|
47
|
+
redirect_back_or_default :action => 'login', :controller => 'user'
|
48
|
+
else
|
49
|
+
session['cookie-set'] = nil
|
50
|
+
|
51
|
+
#making sure the cookie is saved in the browser
|
52
|
+
identityID = LoginCookie.getIdentity(cookies[:hushyhushy])
|
53
|
+
|
54
|
+
if identityID > 0
|
55
|
+
setCurrentIdentity(identityID, PROTECTED_STATE)
|
56
|
+
return true
|
57
|
+
else
|
58
|
+
#force user to log in if cookies is not enabled
|
59
|
+
setCurrentIdentity(0, PUBLIC_STATE)
|
60
|
+
|
61
|
+
# store current location so that we can come back after the user logged in
|
62
|
+
store_location
|
63
|
+
|
64
|
+
# call overwriteable reaction to unauthorized access
|
65
|
+
redirect_to :controller => "/user", :action => "login"
|
66
|
+
|
67
|
+
return false
|
68
|
+
end
|
69
|
+
|
70
|
+
end
|
71
|
+
|
72
|
+
end
|
73
|
+
|
74
|
+
end
|
75
|
+
|
76
|
+
|
77
|
+
|
78
|
+
|
79
|
+
# store current uri in the session.
|
80
|
+
def store_location
|
81
|
+
session['return-to'] = request.request_uri
|
82
|
+
|
83
|
+
if request.post?
|
84
|
+
requestHash = request.parameters
|
85
|
+
requestHash.delete("commit")
|
86
|
+
requestHash.merge(request.path_parameters)
|
87
|
+
|
88
|
+
session['request-params'] = requestHash
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
|
93
|
+
|
94
|
+
# move to the last store_location call or to the passed default one
|
95
|
+
def redirect_back_or_default(default)
|
96
|
+
if session['return-to'].nil?
|
97
|
+
redirect_to default
|
98
|
+
else
|
99
|
+
if session['request-params'].nil?
|
100
|
+
redirect_to session['return-to']
|
101
|
+
else
|
102
|
+
redirect_to session['request-params']
|
103
|
+
session['request-params'] = nil
|
104
|
+
end
|
105
|
+
|
106
|
+
session['return-to'] = nil
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
110
|
+
|
111
|
+
|
112
|
+
# resets the state and the identity
|
113
|
+
def resetSession
|
114
|
+
session['identity'] = 0
|
115
|
+
session['state'] = 0
|
116
|
+
end
|
117
|
+
|
118
|
+
|
119
|
+
|
120
|
+
# check that the status of the states
|
121
|
+
def checkPrivateState
|
122
|
+
if !(session['identity'] != nil && session['identity'] != 0 && session['state'] != nil && session['state'] == 2)
|
123
|
+
raise "not private state"
|
124
|
+
end
|
125
|
+
end
|
126
|
+
|
127
|
+
def checkProtectedState
|
128
|
+
if !(session['identity'] != nil && session['identity'] != 0 && session['state'] != nil && session['state'] >= 1)
|
129
|
+
raise "not protected state"
|
130
|
+
end
|
131
|
+
end
|
132
|
+
|
133
|
+
|
134
|
+
|
135
|
+
# returns the user from the current session
|
136
|
+
def getCurrentUser
|
137
|
+
checkPrivateState
|
138
|
+
@identity = Identity.find(session['identity'])
|
139
|
+
|
140
|
+
if @identity != nil && @identity.user != nil
|
141
|
+
return @identity.user
|
142
|
+
else
|
143
|
+
raise "user not found"
|
144
|
+
end
|
145
|
+
end
|
146
|
+
|
147
|
+
|
148
|
+
|
149
|
+
# sets the identity
|
150
|
+
def setCurrentIdentity(identity, state)
|
151
|
+
raise "bad state" if state != PUBLIC_STATE and state != PROTECTED_STATE and state != PRIVATE_STATE
|
152
|
+
|
153
|
+
session['identity'] = identity
|
154
|
+
session['state'] = state
|
155
|
+
end
|
156
|
+
|
157
|
+
|
158
|
+
end
|
@@ -0,0 +1,72 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../test_helper'
|
2
|
+
|
3
|
+
class UserTest < Test::Unit::TestCase
|
4
|
+
|
5
|
+
fixtures :users
|
6
|
+
|
7
|
+
def test_auth
|
8
|
+
bobIdentityID = User.authenticate("bob", "atest")
|
9
|
+
assert_equal 1, bobIdentityID
|
10
|
+
end
|
11
|
+
|
12
|
+
|
13
|
+
def test_bad_auth
|
14
|
+
funnyBobID = User.authenticate("funnybob", "atest")
|
15
|
+
assert_equal 0, funnyBobID
|
16
|
+
end
|
17
|
+
|
18
|
+
|
19
|
+
def test_password_change
|
20
|
+
@longbob = User.find(3)
|
21
|
+
@longbob.change_password("nonbobpasswd")
|
22
|
+
@longbob.save
|
23
|
+
assert_equal @longbob.identity_id, User.authenticate("longbob", "nonbobpasswd")
|
24
|
+
assert_equal 0, User.authenticate("longbob", "alongtest")
|
25
|
+
@longbob.change_password("alongtest")
|
26
|
+
@longbob.save
|
27
|
+
assert_equal @longbob.identity_id, User.authenticate("longbob", "alongtest")
|
28
|
+
assert_equal 0, User.authenticate("longbob", "nonbobpasswd")
|
29
|
+
end
|
30
|
+
|
31
|
+
|
32
|
+
|
33
|
+
def test_update_login
|
34
|
+
@user = User.find(1)
|
35
|
+
assert_equal "bob", @user.login
|
36
|
+
@user.login = "wow"
|
37
|
+
assert @user.save, @user.errors.full_messages.join("; ")
|
38
|
+
@user.reload
|
39
|
+
assert_equal "wow", @user.login
|
40
|
+
end
|
41
|
+
|
42
|
+
|
43
|
+
|
44
|
+
def test_update_verified
|
45
|
+
@user = User.find(1)
|
46
|
+
assert_equal 1, @user.verified
|
47
|
+
@user.verified = 0
|
48
|
+
assert @user.save, @user.errors.full_messages.join("; ")
|
49
|
+
@user.reload
|
50
|
+
assert_equal 0, @user.verified
|
51
|
+
end
|
52
|
+
|
53
|
+
|
54
|
+
|
55
|
+
def test_update_identity_id
|
56
|
+
@user = User.find(1)
|
57
|
+
assert_equal 1, @user.identity_id
|
58
|
+
@user.identity_id = 20
|
59
|
+
assert @user.save, @user.errors.full_messages.join("; ")
|
60
|
+
@user.reload
|
61
|
+
assert_equal 20, @user.identity_id
|
62
|
+
end
|
63
|
+
|
64
|
+
|
65
|
+
|
66
|
+
def test_destroy
|
67
|
+
@user = User.find(1)
|
68
|
+
@user.destroy
|
69
|
+
assert_raise(ActiveRecord::RecordNotFound) {User.find(1)}
|
70
|
+
end
|
71
|
+
|
72
|
+
end
|
data/templates/users.yml
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html
|
2
|
+
|
3
|
+
bob:
|
4
|
+
id: 1
|
5
|
+
login: bob
|
6
|
+
salted_password: ef94c16f6c124a4e84cc215c164767bfa25f6e92 # atest
|
7
|
+
salt: 7f8b036f9b647d46d22abdbfc8113f44a88f9889
|
8
|
+
email: bob@test.com
|
9
|
+
verified: 1
|
10
|
+
identity_id: 1
|
11
|
+
|
12
|
+
existingbob:
|
13
|
+
id: 2
|
14
|
+
login: existingbob
|
15
|
+
salted_password: 99d6b680d4bfa81cbd383ffa0390bb03323a0b9a # atest
|
16
|
+
salt: fc76daa7bc4e4b7833375cf9deca38beee4c5581
|
17
|
+
email: existingbob@test.com
|
18
|
+
verified: 1
|
19
|
+
identity_id: 2
|
20
|
+
|
21
|
+
longbob:
|
22
|
+
id: 3
|
23
|
+
login: longbob
|
24
|
+
salted_password: c841391e1d29100a4920de7a8fbb4b0fd180c6c0 # alongtest
|
25
|
+
salt: c068e3671780f16898c0a8295ae8d82cc59713e2
|
26
|
+
email: longbob@test.com
|
27
|
+
verified: 1
|
28
|
+
identity_id: 3
|
29
|
+
|
30
|
+
deletebob1:
|
31
|
+
id: 4
|
32
|
+
login: deletebob1
|
33
|
+
salted_password: c841391e1d29100a4920de7a8fbb4b0fd180c6c0 # alongtest
|
34
|
+
salt: c068e3671780f16898c0a8295ae8d82cc59713e2
|
35
|
+
email: deletebob1@test.com
|
36
|
+
verified: 1
|
37
|
+
identity_id: 4
|
38
|
+
|
39
|
+
deletebob2:
|
40
|
+
id: 5
|
41
|
+
login: deletebob2
|
42
|
+
salted_password: c841391e1d29100a4920de7a8fbb4b0fd180c6c0 # alongtest
|
43
|
+
salt: c068e3671780f16898c0a8295ae8d82cc59713e2
|
44
|
+
email: deletebob2@test.com
|
45
|
+
verified: 1
|
46
|
+
identity_id: 5
|
metadata
ADDED
@@ -0,0 +1,78 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: persistent_cookie_authentication_generator
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Wong Liang Zan
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2008-07-05 00:00:00 +08:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: Generates Rails code for an authentication system and persistent cookie management.
|
17
|
+
email: zan@liangzan.net
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files: []
|
23
|
+
|
24
|
+
files:
|
25
|
+
- MIT-LICENCE
|
26
|
+
- USAGE
|
27
|
+
- persistent_cookie_authentication_generator.rb
|
28
|
+
- templates/identities.yml
|
29
|
+
- templates/identity.rb
|
30
|
+
- templates/login_cookie.rb
|
31
|
+
- templates/login_cookies.yml
|
32
|
+
- templates/migration.rb
|
33
|
+
- templates/smtp_tls.rb
|
34
|
+
- templates/user_change_password.rhtml
|
35
|
+
- templates/user_controller.rb
|
36
|
+
- templates/user_edit.rhtml
|
37
|
+
- templates/user_environment.rb
|
38
|
+
- templates/user_forgot_password.rhtml
|
39
|
+
- templates/user_integration_test.rb
|
40
|
+
- templates/user_login.rhtml
|
41
|
+
- templates/user_notify_change_password.rhtml
|
42
|
+
- templates/user_notify_forgot_password.rhtml
|
43
|
+
- templates/user_notify.rb
|
44
|
+
- templates/user_notify_signup.rhtml
|
45
|
+
- templates/user.rb
|
46
|
+
- templates/user_show.rhtml
|
47
|
+
- templates/user_signup.rhtml
|
48
|
+
- templates/users.yml
|
49
|
+
- templates/user_system.rb
|
50
|
+
- templates/user_test.rb
|
51
|
+
has_rdoc: false
|
52
|
+
homepage: http://liangzan.net
|
53
|
+
post_install_message:
|
54
|
+
rdoc_options: []
|
55
|
+
|
56
|
+
require_paths:
|
57
|
+
- lib
|
58
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
59
|
+
requirements:
|
60
|
+
- - ">="
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: "0"
|
63
|
+
version:
|
64
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: "0"
|
69
|
+
version:
|
70
|
+
requirements: []
|
71
|
+
|
72
|
+
rubyforge_project: http://per-cookie-auth.rubyforge.org
|
73
|
+
rubygems_version: 1.2.0
|
74
|
+
signing_key:
|
75
|
+
specification_version: 1
|
76
|
+
summary: Authentication ssytem with persistent cookie management
|
77
|
+
test_files: []
|
78
|
+
|