public
Description: Generates common user authentication code for Rails/Merb, with a full test/unit and rspec suite and optional Acts as State Machine support built-in.
Homepage: http://weblog.techno-weenie.net
Clone URL: git://github.com/technoweenie/restful-authentication.git
Click here to lend your support to: restful-authentication and make a donation at www.pledgie.com !
restful-authentication / lib / authentication.rb
100644 41 lines (32 sloc) 1.546 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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 \\&gt;&lt;&amp;/ 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
end