diff options
| author | simon <simon@zagal.(none)> | 2009-02-08 23:15:11 +0100 |
|---|---|---|
| committer | hukl <hukl@eight.local> | 2009-02-15 20:22:01 +0100 |
| commit | 9f94a70c3e3d9bf766cb9663b0a904d30a190d85 (patch) | |
| tree | 4b4bbf567ec60a939d024b083b478d72476700a5 /lib/authentication.rb | |
| parent | 48ffd4eb446bcaeba7651758ec3002f342702249 (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/authentication.rb')
| -rw-r--r-- | lib/authentication.rb | 103 |
1 files changed, 103 insertions, 0 deletions
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 @@ | |||
| 1 | module 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 \\><&/ 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 | ||
| 103 | end \ No newline at end of file | ||
