summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorsimon <simon@zagal.(none)>2009-02-08 23:15:11 +0100
committerhukl <hukl@eight.local>2009-02-15 20:22:01 +0100
commit9f94a70c3e3d9bf766cb9663b0a904d30a190d85 (patch)
tree4b4bbf567ec60a939d024b083b478d72476700a5 /lib
parent48ffd4eb446bcaeba7651758ec3002f342702249 (diff)
* initial commit of the stripped restful-authentication
* http basic auth and login from cookie have been removed * no it does not work yet, it's so f*cking secure, it won't even let legitimate users login
Diffstat (limited to 'lib')
-rw-r--r--lib/authenticated_system.rb127
-rw-r--r--lib/authenticated_test_helper.rb6
-rw-r--r--lib/authentication.rb103
3 files changed, 236 insertions, 0 deletions
diff --git a/lib/authenticated_system.rb b/lib/authenticated_system.rb
new file mode 100644
index 0000000..7b813f4
--- /dev/null
+++ b/lib/authenticated_system.rb
@@ -0,0 +1,127 @@
1module AuthenticatedSystem
2 protected
3 # Returns true or false if the user is logged in.
4 # Preloads @current_user with the user model if they're logged in.
5 def logged_in?
6 !!current_user
7 end
8
9 # Accesses the current user from the session.
10 # Future calls avoid the database because nil is not equal to false.
11 def current_user
12 @current_user ||= login_from_session unless @current_user == false
13 end
14
15 # Store the given user id in the session.
16 def current_user=(new_user)
17 session[:user_id] = new_user ? new_user.id : nil
18 @current_user = new_user || false
19 end
20
21 # Check if the user is authorized
22 #
23 # Override this method in your controllers if you want to restrict access
24 # to only a few actions or if you want to check if the user
25 # has the correct rights.
26 #
27 # Example:
28 #
29 # # only allow nonbobs
30 # def authorized?
31 # current_user.login != "bob"
32 # end
33 #
34 def authorized?(action = action_name, resource = nil)
35 logged_in?
36 end
37
38 # Filter method to enforce a login requirement.
39 #
40 # To require logins for all actions, use this in your controllers:
41 #
42 # before_filter :login_required
43 #
44 # To require logins for specific actions, use this in your controllers:
45 #
46 # before_filter :login_required, :only => [ :edit, :update ]
47 #
48 # To skip this in a subclassed controller:
49 #
50 # skip_before_filter :login_required
51 #
52 def login_required
53 authorized? || access_denied
54 end
55
56 # Redirect as appropriate when an access request fails.
57 #
58 # The default action is to redirect to the login screen.
59 #
60 # Override this method in your controllers if you want to have special
61 # behavior in case the user is not authorized
62 # to access the requested action. For example, a popup window might
63 # simply close itself.
64 def access_denied
65 respond_to do |format|
66 format.html do
67 store_location
68 redirect_to new_session_path
69 end
70 end
71 end
72
73 # Store the URI of the current request in the session.
74 #
75 # We can return to this location by calling #redirect_back_or_default.
76 def store_location
77 session[:return_to] = request.request_uri
78 end
79
80 # Redirect to the URI stored by the most recent store_location call or
81 # to the passed default. Set an appropriately modified
82 # after_filter :store_location, :only => [:index, :new, :show, :edit]
83 # for any controller you want to be bounce-backable.
84 def redirect_back_or_default(default)
85 redirect_to(session[:return_to] || default)
86 session[:return_to] = nil
87 end
88
89 # Inclusion hook to make #current_user and #logged_in?
90 # available as ActionView helper methods.
91 def self.included(base)
92 base.send :helper_method, :current_user, :logged_in?, :authorized? if base.respond_to? :helper_method
93 end
94
95 #
96 # Login
97 #
98
99 # Called from #current_user. First attempt to login by the user id stored in the session.
100 def login_from_session
101 self.current_user = User.find_by_id(session[:user_id]) if session[:user_id]
102 end
103
104 #
105 # Logout
106 #
107
108 # This is ususally what you want; resetting the session willy-nilly wreaks
109 # havoc with forgery protection, and is only strictly necessary on login.
110 # However, **all session state variables should be unset here**.
111 def logout_keeping_session!
112 # Kill server-side auth cookie
113 @current_user.forget_me if @current_user.is_a? User
114 @current_user = false # not logged in, and don't do it for me
115 kill_remember_cookie! # Kill client-side auth cookie
116 session[:user_id] = nil # keeps the session but kill our variable
117 # explicitly kill any other session variables you set
118 end
119
120 # The session should only be reset at the tail end of a form POST --
121 # otherwise the request forgery protection fails. It's only really necessary
122 # when you cross quarantine (logged-out to logged-in).
123 def logout_killing_session!
124 logout_keeping_session!
125 reset_session
126 end
127end
diff --git a/lib/authenticated_test_helper.rb b/lib/authenticated_test_helper.rb
new file mode 100644
index 0000000..c0ec5f4
--- /dev/null
+++ b/lib/authenticated_test_helper.rb
@@ -0,0 +1,6 @@
1module AuthenticatedTestHelper
2 # Sets the current user in the session from the user fixtures.
3 def login_as(user)
4 @request.session[:user_id] = user ? users(user).id : nil
5 end
6end
diff --git a/lib/authentication.rb b/lib/authentication.rb
new file mode 100644
index 0000000..a936589
--- /dev/null
+++ b/lib/authentication.rb
@@ -0,0 +1,103 @@
1module Authentication
2 mattr_accessor :login_regex, :bad_login_message,
3 :name_regex, :bad_name_message,
4 :email_name_regex, :domain_head_regex, :domain_tld_regex, :email_regex, :bad_email_message
5
6 self.login_regex = /\A\w[\w\.\-_@]+\z/ # ASCII, strict
7 # self.login_regex = /\A[[:alnum:]][[:alnum:]\.\-_@]+\z/ # Unicode, strict
8 # self.login_regex = /\A[^[:cntrl:]\\<>\/&]*\z/ # Unicode, permissive
9
10 self.bad_login_message = "use only letters, numbers, and .-_@ please.".freeze
11
12 self.name_regex = /\A[^[:cntrl:]\\<>\/&]*\z/ # Unicode, permissive
13 self.bad_name_message = "avoid non-printing characters and \\&gt;&lt;&amp;/ please.".freeze
14
15 self.email_name_regex = '[\w\.%\+\-]+'.freeze
16 self.domain_head_regex = '(?:[A-Z0-9\-]+\.)+'.freeze
17 self.domain_tld_regex = '(?:[A-Z]{2}|com|org|net|edu|gov|mil|biz|info|mobi|name|aero|jobs|museum)'.freeze
18 self.email_regex = /\A#{email_name_regex}@#{domain_head_regex}#{domain_tld_regex}\z/i
19 self.bad_email_message = "should look like an email address.".freeze
20
21 def self.included(recipient)
22 recipient.extend(ModelClassMethods)
23 recipient.class_eval do
24 include ModelInstanceMethods
25 end
26 end
27
28 module ModelClassMethods
29 def secure_digest(*args)
30 Digest::SHA1.hexdigest(args.flatten.join('--'))
31 end
32
33 def make_token
34 secure_digest(Time.now, (1..10).map{ rand.to_s })
35 end
36 end # class methods
37
38 module ModelInstanceMethods
39 end # instance methods
40
41 module ByPassword
42 # Stuff directives into including module
43 def self.included(recipient)
44 recipient.extend(ModelClassMethods)
45 recipient.class_eval do
46 include ModelInstanceMethods
47
48 # Virtual attribute for the unencrypted password
49 attr_accessor :password
50 validates_presence_of :password, :if => :password_required?
51 validates_presence_of :password_confirmation, :if => :password_required?
52 validates_confirmation_of :password, :if => :password_required?
53 validates_length_of :password, :within => 6..40, :if => :password_required?
54 before_save :encrypt_password
55 end
56 end # #included directives
57
58 #
59 # Class Methods
60 #
61 module ModelClassMethods
62 # This provides a modest increased defense against a dictionary attack if
63 # your db were ever compromised, but will invalidate existing passwords.
64 # See the README and the file config/initializers/site_keys.rb
65 #
66 # It may not be obvious, but if you set REST_AUTH_SITE_KEY to nil and
67 # REST_AUTH_DIGEST_STRETCHES to 1 you'll have backwards compatibility with
68 # older versions of restful-authentication.
69 def password_digest(password, salt)
70 digest = REST_AUTH_SITE_KEY
71 REST_AUTH_DIGEST_STRETCHES.times do
72 digest = secure_digest(digest, salt, password, REST_AUTH_SITE_KEY)
73 end
74 digest
75 end
76 end # class methods
77
78 #
79 # Instance Methods
80 #
81 module ModelInstanceMethods
82
83 # Encrypts the password with the user salt
84 def encrypt(password)
85 self.class.password_digest(password, salt)
86 end
87
88 def authenticated?(password)
89 crypted_password == encrypt(password)
90 end
91
92 # before filter
93 def encrypt_password
94 return if password.blank?
95 self.salt = self.class.make_token if new_record?
96 self.crypted_password = encrypt(password)
97 end
98 def password_required?
99 crypted_password.blank? || !password.blank?
100 end
101 end # instance methods
102 end
103end \ No newline at end of file