NanoAuth 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. data/lib/nano_auth.rb +63 -0
  2. metadata +64 -0
@@ -0,0 +1,63 @@
1
+ # NanoAuth
2
+ #
3
+ #
4
+
5
+ require 'bcrypt'
6
+
7
+ module NanoAuth
8
+
9
+ def self.included(base)
10
+
11
+ # Virtual attributes
12
+ attr_accessor :password
13
+
14
+ # AR Callbacks
15
+ base.before_save :encrypt_password
16
+
17
+ # Password validation
18
+ base.validates_presence_of :password, :if => Proc.new { |u| u.password_required? }
19
+ base.validates_confirmation_of :password, :if => Proc.new { |u| u.password_required? }, :allow_nil => true
20
+ base.validates_length_of :password, :minimum => 6, :if => Proc.new { |u| u.password_required? }, :allow_nil => true
21
+
22
+ # Class methods
23
+ base.class_eval do
24
+
25
+ # Authenticates a user by their email name and unencrypted password. Returns the user or nil.
26
+ def self.authenticate(email, password)
27
+ return nil unless email && password
28
+ u = find_by_email(email)
29
+ u && u.authenticated?(password) ? u : nil
30
+ end
31
+
32
+ end
33
+
34
+ end
35
+
36
+ # Encrypts the password with the user salt
37
+ def encrypt(password)
38
+ BCrypt::Engine.hash_secret(password,password_salt)
39
+ end
40
+
41
+ # The big question - are we equivalent?
42
+ def authenticated?(password)
43
+ password_hash == encrypt(password)
44
+ end
45
+
46
+ # Tell us if there's a password to work with
47
+ def password_required?
48
+ password_hash.blank? || !password.blank?
49
+ end
50
+
51
+ protected
52
+
53
+ # Encrypts the password with the user salt
54
+ def encrypt_password
55
+
56
+ if password.present?
57
+ self.password_salt = BCrypt::Engine.generate_salt
58
+ self.password_hash = BCrypt::Engine.hash_secret(password, password_salt)
59
+ end
60
+
61
+ end
62
+
63
+ end
metadata ADDED
@@ -0,0 +1,64 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: NanoAuth
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Jason Lee
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-11-13 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bcrypt-ruby
16
+ requirement: &70236360970520 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *70236360970520
25
+ description: ! "NanoAuth is a super stripped down Rails authentication module. It's
26
+ comprised (so far) of one file that provides some additional methods to be mixed
27
+ in to your User model. Over the years I kept tweaking code in various projects and
28
+ continued to see the same boiler plate code for:\n\n * authentication a user via
29
+ User.authenticate(email,password)\n * encrypt(password)\n * authenticated?(password)\n\n
30
+ \ So I decided on some down time to pull all this stuff out and make a nice little
31
+ gem of it."
32
+ email: jasonlee9@gmail.com
33
+ executables: []
34
+ extensions: []
35
+ extra_rdoc_files: []
36
+ files:
37
+ - lib/nano_auth.rb
38
+ homepage: https://github.com/jasonbits/nano_auth/blob/master
39
+ licenses: []
40
+ post_install_message:
41
+ rdoc_options: []
42
+ require_paths:
43
+ - lib
44
+ required_ruby_version: !ruby/object:Gem::Requirement
45
+ none: false
46
+ requirements:
47
+ - - ! '>='
48
+ - !ruby/object:Gem::Version
49
+ version: '0'
50
+ required_rubygems_version: !ruby/object:Gem::Requirement
51
+ none: false
52
+ requirements:
53
+ - - ! '>='
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ requirements: []
57
+ rubyforge_project:
58
+ rubygems_version: 1.8.8
59
+ signing_key:
60
+ specification_version: 3
61
+ summary: NanoAuth is a super stripped down Rails authentication module. It's comprised
62
+ (so far) of one file that provides some additional methods to be mixed in to your
63
+ User model.
64
+ test_files: []