public
Description: Merb Slice for application security/authentication
Clone URL: git://github.com/technomage/secure-magic.git
First test version
technomage (author)
Sun Jun 08 17:33:23 -0700 2008
commit  9f103f0ee195636e40ed06c41c0e07f5150eb083
tree    c61dcb5adf94220309123cae2d0689df083b072e
parent  30699ad193d92398ec983ee18b19bd70c1607896
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
...
1
2
3
 
 
 
 
 
 
 
 
 
 
 
4
5
6
0
@@ -1,17 +1,6 @@
0
 #require File.join(File.dirname(__FILE__), '..', '..', "lib", "authenticated_system", "authenticated_dependencies")
0
 class SecureMagic::Accounts < SecureMagic::Application
0
   provides :xml
0
-
0
- # Encrypts some data with the salt.
0
- def self.encrypt(password, salt)
0
- Digest::SHA1.hexdigest("--#{salt}--#{password}--")
0
- end
0
-
0
- # Authenticates a account by their login name and unencrypted password. Returns the account or nil.
0
- def self.authenticate(login, password)
0
- u = find_activated_authenticated_model_with_login(login) # need to get the salt
0
- u && u.authenticated?(password) ? u : nil
0
- end
0
   
0
   def new
0
     only_provides :html
...
43
44
45
 
 
 
 
 
 
 
 
 
 
 
46
47
48
...
80
81
82
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
83
 
 
 
84
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
85
86
87
...
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
...
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
0
@@ -43,6 +43,17 @@ class SecureMagic::Account
0
   def login=(value)
0
     attribute_set(:login, value.downcase) unless value.nil?
0
   end
0
+
0
+ # Encrypts some data with the salt.
0
+ def self.encrypt(password, salt)
0
+ Digest::SHA1.hexdigest("--#{salt}--#{password}--")
0
+ end
0
+
0
+ # Authenticates a account by their login name and unencrypted password. Returns the account or nil.
0
+ def self.authenticate(login, password)
0
+ u = find_activated_authenticated_model_with_login(login) # need to get the salt
0
+ u && u.authenticated?(password) ? u : nil
0
+ end
0
     
0
   
0
   EMAIL_FROM = "info@%s.com"
0
@@ -80,7 +91,100 @@ class SecureMagic::Account
0
     end
0
   end
0
   
0
+
0
+ def authenticated?(password)
0
+ crypted_password == encrypt(password)
0
+ end
0
+
0
+ # before filter
0
+ def encrypt_password
0
+ return if password.blank?
0
+ self.salt = Digest::SHA1.hexdigest("--#{Time.now.to_s}--#{login}--") if new_record?
0
+ self.crypted_password = encrypt(password)
0
+ end
0
+
0
+ # Encrypts the password with the account salt
0
+ def encrypt(password)
0
+ self.class.encrypt(password, salt)
0
+ end
0
+
0
+ def remember_token?
0
+ remember_token_expires_at && DateTime.now < DateTime.parse(remember_token_expires_at.to_s)
0
+ end
0
+
0
+ def remember_me_until(time)
0
+ self.remember_token_expires_at = time
0
+ self.remember_token = encrypt("#{email}--#{remember_token_expires_at}")
0
+ save
0
+ end
0
+
0
+ def remember_me_for(time)
0
+ remember_me_until (Time.now + time)
0
+ end
0
+
0
+ # These create and unset the fields required for remembering accounts between browser closes
0
+ # Default of 2 weeks
0
+ def remember_me
0
+ remember_me_for (Merb::Const::WEEK * 2)
0
+ end
0
+
0
+ def forget_me
0
+ self.remember_token_expires_at = nil
0
+ self.remember_token = nil
0
+ self.save
0
+ end
0
+ # Returns true if the account has just been activated.
0
+ def recently_activated?
0
+ @activated
0
+ end
0
+
0
+ def activated?
0
+ return false if self.new_record?
0
+ !! activation_code.nil?
0
+ end
0
+
0
+ def active?
0
+ # the existence of an activation code means they have not activated yet
0
+ activation_code.nil?
0
+ end
0
+
0
+ def self.find_authenticated_model_with_id(id)
0
+ SecureMagic::Account.first(:id => id)
0
+ end
0
+
0
+ def self.find_authenticated_model_with_remember_token(rt)
0
+ SecureMagic::Account.first(:remember_token => rt)
0
+ end
0
+
0
+ def self.find_activated_authenticated_model_with_login(login)
0
+ if SecureMagic::Account.instance_methods.include?("activated_at")
0
+ SecureMagic::Account.first(:login => login, :activated_at.not => nil)
0
+ else
0
+ SecureMagic::Account.first(:login => login)
0
+ end
0
+ end
0
 
0
+ def self.find_activated_authenticated_model(activation_code)
0
+ SecureMagic::Account.first(:activation_code => activation_code)
0
+ end
0
 
0
+ def self.find_with_conditions(conditions)
0
+ SecureMagic::Account.first(conditions)
0
+ end
0
+
0
+ # A method to assist with specs
0
+ def self.clear_database_table
0
+ Account.auto_upgrade!
0
+ end
0
+
0
+#protected
0
+
0
+ def make_activation_code
0
+ self.activation_code = Digest::SHA1.hexdigest( Time.now.to_s.split(//).sort_by {rand}.join )
0
+ end
0
+
0
+ def password_required?
0
+ crypted_password.blank? || !password.blank?
0
+ end
0
   
0
 end
0
\ No newline at end of file
...
20
21
22
23
24
25
26
27
28
...
20
21
22
 
 
 
23
24
25
0
@@ -20,8 +20,5 @@
0
   <tr>
0
   <td><%= submit_button "Sign up" %></td>
0
   </tr>
0
- <tr>
0
- <td><%= link_to "Lost Password", url(:secure_magic_lost_password) %></td>
0
- </tr>
0
 </table>
0
 <% end %>
0
\ No newline at end of file
...
1
 
2
3
4
...
15
16
17
 
 
 
18
19
...
 
1
2
3
4
...
15
16
17
18
19
20
21
22
0
@@ -1,4 +1,4 @@
0
-<% form_tag :action => url(:login) do -%>
0
+<% form_tag :action => url(:secure_magic_login) do -%>
0
 <table>
0
 <tr>
0
   <td><label for="login">Login</label></td>
0
@@ -15,5 +15,8 @@
0
 <tr>
0
   <td><%= submit_button 'Log in' %></td>
0
 </tr>
0
+ <tr>
0
+ <td><%= link_to "Lost Password", url(:secure_magic_lost_password) %></td>
0
+ </tr>
0
 </table>
0
 <% end -%>
...
152
153
154
 
155
156
157
...
152
153
154
155
156
157
158
0
@@ -152,5 +152,6 @@ module SecureMagic
0
 end
0
 
0
 Merb::BootLoader.after_app_loads do
0
+ SecureMagic::Application.send(:include, SecureMagic::Controller)
0
   Application.send(:include, SecureMagic::Controller)
0
 end
0
\ No newline at end of file

Comments

    No one has commented yet.