From 9f94a70c3e3d9bf766cb9663b0a904d30a190d85 Mon Sep 17 00:00:00 2001 From: simon Date: Sun, 8 Feb 2009 23:15:11 +0100 Subject: * 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 --- lib/authentication.rb | 103 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 103 insertions(+) create mode 100644 lib/authentication.rb (limited to 'lib/authentication.rb') 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 @@ +module Authentication + mattr_accessor :login_regex, :bad_login_message, + :name_regex, :bad_name_message, + :email_name_regex, :domain_head_regex, :domain_tld_regex, :email_regex, :bad_email_message + + self.login_regex = /\A\w[\w\.\-_@]+\z/ # ASCII, strict + # self.login_regex = /\A[[:alnum:]][[:alnum:]\.\-_@]+\z/ # Unicode, strict + # self.login_regex = /\A[^[:cntrl:]\\<>\/&]*\z/ # Unicode, permissive + + self.bad_login_message = "use only letters, numbers, and .-_@ please.".freeze + + self.name_regex = /\A[^[:cntrl:]\\<>\/&]*\z/ # Unicode, permissive + self.bad_name_message = "avoid non-printing characters and \\><&/ please.".freeze + + self.email_name_regex = '[\w\.%\+\-]+'.freeze + self.domain_head_regex = '(?:[A-Z0-9\-]+\.)+'.freeze + self.domain_tld_regex = '(?:[A-Z]{2}|com|org|net|edu|gov|mil|biz|info|mobi|name|aero|jobs|museum)'.freeze + self.email_regex = /\A#{email_name_regex}@#{domain_head_regex}#{domain_tld_regex}\z/i + self.bad_email_message = "should look like an email address.".freeze + + def self.included(recipient) + recipient.extend(ModelClassMethods) + recipient.class_eval do + include ModelInstanceMethods + end + end + + module ModelClassMethods + def secure_digest(*args) + Digest::SHA1.hexdigest(args.flatten.join('--')) + end + + def make_token + secure_digest(Time.now, (1..10).map{ rand.to_s }) + end + end # class methods + + module ModelInstanceMethods + end # instance methods + + module ByPassword + # Stuff directives into including module + def self.included(recipient) + recipient.extend(ModelClassMethods) + recipient.class_eval do + include ModelInstanceMethods + + # Virtual attribute for the unencrypted password + attr_accessor :password + validates_presence_of :password, :if => :password_required? + validates_presence_of :password_confirmation, :if => :password_required? + validates_confirmation_of :password, :if => :password_required? + validates_length_of :password, :within => 6..40, :if => :password_required? + before_save :encrypt_password + end + end # #included directives + + # + # Class Methods + # + module ModelClassMethods + # This provides a modest increased defense against a dictionary attack if + # your db were ever compromised, but will invalidate existing passwords. + # See the README and the file config/initializers/site_keys.rb + # + # It may not be obvious, but if you set REST_AUTH_SITE_KEY to nil and + # REST_AUTH_DIGEST_STRETCHES to 1 you'll have backwards compatibility with + # older versions of restful-authentication. + def password_digest(password, salt) + digest = REST_AUTH_SITE_KEY + REST_AUTH_DIGEST_STRETCHES.times do + digest = secure_digest(digest, salt, password, REST_AUTH_SITE_KEY) + end + digest + end + end # class methods + + # + # Instance Methods + # + module ModelInstanceMethods + + # Encrypts the password with the user salt + def encrypt(password) + self.class.password_digest(password, salt) + end + + def authenticated?(password) + crypted_password == encrypt(password) + end + + # before filter + def encrypt_password + return if password.blank? + self.salt = self.class.make_token if new_record? + self.crypted_password = encrypt(password) + end + def password_required? + crypted_password.blank? || !password.blank? + end + end # instance methods + end +end \ No newline at end of file -- cgit v1.3