authem 0.0.2 → 1.0.0.rc1

Sign up to get free protection for your applications and to get access to all the features.
@@ -4,7 +4,7 @@ Authem is an authentication library for Ruby web applications.
4
4
 
5
5
  ## Compatibility
6
6
 
7
- Authem is tested against Ruby 1.8.7, 1.9.2, 1.9.3, Rubinius, and REE.
7
+ Authem is tested against Ruby 1.9.2, 1.9.3, Rubinius
8
8
 
9
9
  [![Build Status](https://secure.travis-ci.org/paulelliott/authem.png)](http://travis-ci.org/paulelliott/authem)
10
10
 
data/Rakefile CHANGED
@@ -1,9 +1,6 @@
1
- require 'rake/dsl_definition'
2
- require 'bundler/setup'
1
+ require 'bundler'
3
2
 
4
- require "rspec"
5
3
  require "rspec/core/rake_task"
6
-
7
4
  RSpec::Core::RakeTask.new(:spec) do |spec|
8
5
  spec.pattern = "spec/**/*_spec.rb"
9
6
  end
@@ -1,7 +1,10 @@
1
1
  module Authem
2
+ autoload :BaseUser, 'authem/base_user'
3
+ autoload :User, 'authem/user'
4
+ autoload :SorceryUser, 'authem/sorcery_user'
5
+
2
6
  autoload :Config, 'authem/config'
3
7
  autoload :ControllerSupport, 'authem/controller_support'
4
- autoload :Model, 'authem/model'
5
8
  autoload :Token, 'authem/token'
6
9
 
7
10
  def self.configure(&block)
@@ -0,0 +1,45 @@
1
+ require 'bcrypt'
2
+
3
+ module Authem::BaseUser
4
+ extend ::ActiveSupport::Concern
5
+
6
+ included do
7
+ attr_accessible :email, :password, :password_confirmation
8
+
9
+ validates_uniqueness_of :email
10
+ validates_format_of :email, with: /^\S+@\S+$/
11
+ validates_presence_of :password, on: :create
12
+ validates_confirmation_of :password, message: 'should match confirmation'
13
+
14
+ def self.find_by_email(email)
15
+ where("LOWER(email) = ?", email.downcase).first
16
+ end
17
+
18
+ def remember_token
19
+ self[:remember_token] || generate_token(:remember)
20
+ end
21
+
22
+ def session_token
23
+ self[:session_token] || generate_token(:session)
24
+ end
25
+
26
+ def reset_password(password, confirmation)
27
+ return false unless password.present?
28
+
29
+ self.password = password
30
+ self.password_confirmation = confirmation
31
+ self.reset_password_token = nil
32
+ save
33
+ end
34
+
35
+ def reset_password_token!
36
+ generate_token(:reset_password)
37
+ end
38
+
39
+ private
40
+
41
+ def generate_token(type)
42
+ Authem::Token.generate.tap { |token| update_column("#{type}_token", token) }
43
+ end
44
+ end
45
+ end
@@ -3,34 +3,25 @@ module Authem::ControllerSupport
3
3
 
4
4
  protected
5
5
 
6
- def sign_in(email_or_user, password=nil, remember_me=nil)
7
- if email_or_user.is_a? String
8
- email_or_user = Authem::Config.user_class.authenticate(email_or_user, password)
9
- end
10
- if email_or_user.is_a? Authem::Model
11
- establish_presence(email_or_user)
12
- remember_me! if remember_me
13
- email_or_user
14
- end
6
+ def sign_in(user, remember_me=true)
7
+ cookies.permanent.signed[:remember_token] = user.remember_token if remember_me
8
+ session[:session_token] = user.session_token
15
9
  end
16
10
 
17
11
  def sign_out
18
- clear_session
19
- end
20
-
21
- def remember_me!
22
- cookies.permanent.signed[:remember_me] = current_user.id
12
+ cookies[:remember_token] = nil
13
+ reset_session
14
+ @current_user = nil
23
15
  end
24
16
 
25
17
  def current_user
26
- @current_user ||= (
27
- if session[:user_id]
28
- Authem::Config.user_class.where(id: session[:user_id]).first
29
- elsif cookies[:remember_me].present?
30
- user = Authem::Config.user_class.where(id: cookies.signed[:remember_me]).first
31
- establish_presence(user) if user
18
+ if session[:session_token]
19
+ Authem::Config.user_class.where(session_token: session[:session_token].to_s).first
20
+ elsif cookies[:remember_token].present?
21
+ Authem::Config.user_class.where(remember_token: cookies.signed[:remember_token].to_s).first.tap do |user|
22
+ session[:session_token] = user.session_token if user
32
23
  end
33
- )
24
+ end
34
25
  end
35
26
 
36
27
  def require_user
@@ -40,12 +31,8 @@ module Authem::ControllerSupport
40
31
  end
41
32
  end
42
33
 
43
- def establish_presence(user)
44
- return_to_url = session[:return_to_url]
45
- clear_session
46
- session[:return_to_url] = return_to_url
47
- session[:user_id] = user.id
48
- @current_user = user
34
+ def signed_in?
35
+ current_user.present?
49
36
  end
50
37
 
51
38
  def redirect_back_or_to(url, flash_hash = {})
@@ -54,11 +41,6 @@ module Authem::ControllerSupport
54
41
  redirect_to(url, :flash => flash_hash)
55
42
  end
56
43
 
57
- def clear_session
58
- cookies[:remember_me] = nil
59
- reset_session
60
- end
61
-
62
44
  included do
63
45
  helper_method :current_user
64
46
  end
@@ -0,0 +1,21 @@
1
+ module Authem::SorceryUser
2
+ extend ::ActiveSupport::Concern
3
+ include Authem::BaseUser
4
+
5
+ included do
6
+ attr_accessor :password, :password_confirmation
7
+
8
+ before_save :encrypt_password
9
+
10
+ def authenticate(password)
11
+ self if crypted_password.present? && ::BCrypt::Password.new(crypted_password) == [password, salt].join
12
+ end
13
+
14
+ def encrypt_password
15
+ if password.present?
16
+ self.salt = ::BCrypt::Engine.generate_salt
17
+ self.crypted_password = ::BCrypt::Password.create([password, salt].join)
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,10 @@
1
+ module Authem::User
2
+ extend ::ActiveSupport::Concern
3
+ include Authem::BaseUser
4
+
5
+ included do
6
+ Authem::Config.user_class = self
7
+
8
+ has_secure_password
9
+ end
10
+ end
@@ -1,3 +1,3 @@
1
1
  module Authem
2
- VERSION = '0.0.2'
2
+ VERSION = '1.0.0.rc1'
3
3
  end
metadata CHANGED
@@ -1,119 +1,173 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: authem
3
- version: !ruby/object:Gem::Version
4
- prerelease:
5
- version: 0.0.2
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0.rc1
5
+ prerelease: 6
6
6
  platform: ruby
7
- authors:
7
+ authors:
8
8
  - Paul Elliott
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
-
13
- date: 2012-02-01 00:00:00 Z
14
- dependencies:
15
- - !ruby/object:Gem::Dependency
12
+ date: 2012-08-22 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
16
15
  name: activesupport
17
- prerelease: false
18
- requirement: &id001 !ruby/object:Gem::Requirement
16
+ requirement: !ruby/object:Gem::Requirement
19
17
  none: false
20
- requirements:
21
- - - ">="
22
- - !ruby/object:Gem::Version
23
- version: "0"
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
24
22
  type: :runtime
25
- version_requirements: *id001
26
- - !ruby/object:Gem::Dependency
27
- name: bcrypt-ruby
28
23
  prerelease: false
29
- requirement: &id002 !ruby/object:Gem::Requirement
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: bcrypt-ruby
32
+ requirement: !ruby/object:Gem::Requirement
30
33
  none: false
31
- requirements:
32
- - - ">="
33
- - !ruby/object:Gem::Version
34
- version: "0"
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
35
38
  type: :runtime
36
- version_requirements: *id002
37
- - !ruby/object:Gem::Dependency
38
- name: actionpack
39
39
  prerelease: false
40
- requirement: &id003 !ruby/object:Gem::Requirement
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: actionpack
48
+ requirement: !ruby/object:Gem::Requirement
41
49
  none: false
42
- requirements:
43
- - - ">="
44
- - !ruby/object:Gem::Version
45
- version: "0"
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
46
54
  type: :development
47
- version_requirements: *id003
48
- - !ruby/object:Gem::Dependency
49
- name: activerecord
50
55
  prerelease: false
51
- requirement: &id004 !ruby/object:Gem::Requirement
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: activerecord
64
+ requirement: !ruby/object:Gem::Requirement
52
65
  none: false
53
- requirements:
54
- - - ">="
55
- - !ruby/object:Gem::Version
56
- version: "0"
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
57
70
  type: :development
58
- version_requirements: *id004
59
- - !ruby/object:Gem::Dependency
60
- name: database_cleaner
61
71
  prerelease: false
62
- requirement: &id005 !ruby/object:Gem::Requirement
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ - !ruby/object:Gem::Dependency
79
+ name: database_cleaner
80
+ requirement: !ruby/object:Gem::Requirement
63
81
  none: false
64
- requirements:
65
- - - ">="
66
- - !ruby/object:Gem::Version
67
- version: "0"
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
68
86
  type: :development
69
- version_requirements: *id005
70
- - !ruby/object:Gem::Dependency
71
- name: rake
72
87
  prerelease: false
73
- requirement: &id006 !ruby/object:Gem::Requirement
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ - !ruby/object:Gem::Dependency
95
+ name: rake
96
+ requirement: !ruby/object:Gem::Requirement
74
97
  none: false
75
- requirements:
76
- - - ">="
77
- - !ruby/object:Gem::Version
78
- version: "0"
98
+ requirements:
99
+ - - ! '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
79
102
  type: :development
80
- version_requirements: *id006
81
- - !ruby/object:Gem::Dependency
82
- name: rspec
83
103
  prerelease: false
84
- requirement: &id007 !ruby/object:Gem::Requirement
104
+ version_requirements: !ruby/object:Gem::Requirement
85
105
  none: false
86
- requirements:
87
- - - ">="
88
- - !ruby/object:Gem::Version
89
- version: "0"
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ - !ruby/object:Gem::Dependency
111
+ name: rspec
112
+ requirement: !ruby/object:Gem::Requirement
113
+ none: false
114
+ requirements:
115
+ - - ! '>='
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
90
118
  type: :development
91
- version_requirements: *id007
92
- - !ruby/object:Gem::Dependency
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ none: false
122
+ requirements:
123
+ - - ! '>='
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
126
+ - !ruby/object:Gem::Dependency
93
127
  name: pg
128
+ requirement: !ruby/object:Gem::Requirement
129
+ none: false
130
+ requirements:
131
+ - - ! '>='
132
+ - !ruby/object:Gem::Version
133
+ version: '0'
134
+ type: :development
94
135
  prerelease: false
95
- requirement: &id008 !ruby/object:Gem::Requirement
136
+ version_requirements: !ruby/object:Gem::Requirement
96
137
  none: false
97
- requirements:
98
- - - ">="
99
- - !ruby/object:Gem::Version
100
- version: "0"
138
+ requirements:
139
+ - - ! '>='
140
+ - !ruby/object:Gem::Version
141
+ version: '0'
142
+ - !ruby/object:Gem::Dependency
143
+ name: pry
144
+ requirement: !ruby/object:Gem::Requirement
145
+ none: false
146
+ requirements:
147
+ - - ! '>='
148
+ - !ruby/object:Gem::Version
149
+ version: '0'
101
150
  type: :development
102
- version_requirements: *id008
151
+ prerelease: false
152
+ version_requirements: !ruby/object:Gem::Requirement
153
+ none: false
154
+ requirements:
155
+ - - ! '>='
156
+ - !ruby/object:Gem::Version
157
+ version: '0'
103
158
  description: Authem provides a simple solution for email-based authentication.
104
- email:
159
+ email:
105
160
  - paul@hashrocket.com
106
161
  executables: []
107
-
108
162
  extensions: []
109
-
110
163
  extra_rdoc_files: []
111
-
112
- files:
164
+ files:
165
+ - lib/authem/base_user.rb
113
166
  - lib/authem/config.rb
114
167
  - lib/authem/controller_support.rb
115
- - lib/authem/model.rb
168
+ - lib/authem/sorcery_user.rb
116
169
  - lib/authem/token.rb
170
+ - lib/authem/user.rb
117
171
  - lib/authem/version.rb
118
172
  - lib/authem.rb
119
173
  - LICENSE
@@ -121,30 +175,26 @@ files:
121
175
  - Rakefile
122
176
  homepage: https://github.com/paulelliott/authem
123
177
  licenses: []
124
-
125
178
  post_install_message:
126
179
  rdoc_options: []
127
-
128
- require_paths:
180
+ require_paths:
129
181
  - lib
130
- required_ruby_version: !ruby/object:Gem::Requirement
182
+ required_ruby_version: !ruby/object:Gem::Requirement
131
183
  none: false
132
- requirements:
133
- - - ">="
134
- - !ruby/object:Gem::Version
135
- version: "0"
136
- required_rubygems_version: !ruby/object:Gem::Requirement
184
+ requirements:
185
+ - - ! '>='
186
+ - !ruby/object:Gem::Version
187
+ version: '0'
188
+ required_rubygems_version: !ruby/object:Gem::Requirement
137
189
  none: false
138
- requirements:
139
- - - ">="
140
- - !ruby/object:Gem::Version
141
- version: "0"
190
+ requirements:
191
+ - - ! '>'
192
+ - !ruby/object:Gem::Version
193
+ version: 1.3.1
142
194
  requirements: []
143
-
144
195
  rubyforge_project:
145
- rubygems_version: 1.8.6
196
+ rubygems_version: 1.8.24
146
197
  signing_key:
147
198
  specification_version: 3
148
199
  summary: Authem authenticates them by email
149
200
  test_files: []
150
-
@@ -1,44 +0,0 @@
1
- require 'bcrypt'
2
-
3
- module Authem::Model
4
- extend ::ActiveSupport::Concern
5
-
6
- included do
7
- Authem::Config.user_class = self
8
-
9
- attr_accessor :password, :password_confirmation
10
-
11
- attr_protected :crypted_password, :salt, :reset_password_token
12
-
13
- validates_confirmation_of :password
14
- validates :email, :presence => true, :uniqueness => true
15
-
16
- def self.find_by_email(email)
17
- where("upper(email) = ?", email.upcase).first
18
- end
19
-
20
- before_save :encrypt_password
21
-
22
- def self.authenticate(email, password)
23
- user = find_by_email(email)
24
- user if user && user.crypted_password_matches?(password)
25
- end
26
- end
27
-
28
- def crypted_password_matches?(password)
29
- crypted_password.present? && ::BCrypt::Password.new(crypted_password) == [password, salt].join
30
- end
31
-
32
- def encrypt_password
33
- if password.present?
34
- self.salt = ::BCrypt::Engine.generate_salt
35
- self.crypted_password = ::BCrypt::Password.create([password, salt].join)
36
- end
37
- end
38
-
39
- def reset_password_token
40
- update_attribute(:reset_password_token, Authem::Token.generate) if self[:reset_password_token].blank?
41
- self[:reset_password_token]
42
- end
43
-
44
- end