easy_auth 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -5,6 +5,9 @@ module EasyAuthHelper
5
5
  end
6
6
  end
7
7
 
8
+ # Access the current account the users is authenticated with
9
+ #
10
+ # @return [Account] instance
8
11
  def current_account
9
12
  if session[:session_token] && session[:account_class]
10
13
  begin
@@ -29,11 +32,13 @@ module EasyAuthHelper
29
32
  end
30
33
  alias :current_user :current_account
31
34
 
35
+ # Should be used to test if user is authenticated
32
36
  def account_signed_in?
33
37
  current_account
34
38
  end
35
39
  alias :user_signed_in? :account_signed_in?
36
40
 
41
+ # Should be used to test if user is not authenticated
37
42
  def account_not_signed_in?
38
43
  !account_signed_in?
39
44
  end
@@ -0,0 +1,3 @@
1
+ class Identity < ActiveRecord::Base
2
+ include EasyAuth::Models::Identity
3
+ end
@@ -14,7 +14,7 @@ module EasyAuth::Controllers::Sessions
14
14
  end
15
15
  after_successful_sign_in(identity)
16
16
  else
17
- @identity = EasyAuth.find_identity_model(self).new(params[params[:identity]])
17
+ @identity = EasyAuth.find_identity_model(params).new(params[params[:identity]])
18
18
  after_failed_sign_in(@identity)
19
19
  end
20
20
  end
@@ -73,13 +73,13 @@ module EasyAuth::Controllers::Sessions
73
73
 
74
74
  def method_missing(method_name, *args)
75
75
  # Swallow exceptions for identity callbacks
76
- unless method_name =~ /after_\w+_with_\w+/
76
+ unless method_name.to_s =~ /after_\w+_with_\w+/
77
77
  super
78
78
  end
79
79
  end
80
80
 
81
81
  def set_remember(identity)
82
- if identity_attributes = params[ActiveModel::Naming.param_key(EasyAuth.find_identity_model(self).new)]
82
+ if identity_attributes = params[ActiveModel::Naming.param_key(EasyAuth.find_identity_model(params).new)]
83
83
  identity.remember = identity_attributes[:remember]
84
84
  end
85
85
  end
@@ -5,7 +5,7 @@ module EasyAuth::Models::Account
5
5
 
6
6
  reverse_included do
7
7
  # Relationships
8
- has_many :identities, :class_name => 'EasyAuth::Identity', :as => :account, :dependent => :destroy
8
+ has_many :identities, :class_name => 'Identity', :as => :account, :dependent => :destroy
9
9
 
10
10
  def identity_username_attribute
11
11
  self.send(self.class.identity_username_attribute)
@@ -13,6 +13,14 @@ module EasyAuth::Models::Account
13
13
  end
14
14
 
15
15
  module ClassMethods
16
+ # Will attempt to find the username attribute
17
+ #
18
+ # First will check to see if #identity_username_attribute is already defined in the model.
19
+ #
20
+ # If not, will check to see if `username` exists as a column on the record
21
+ # If not, will check to see if `email` exists as a column on the record
22
+ #
23
+ # @return [Symbol]
16
24
  def identity_username_attribute
17
25
  if respond_to?(:super)
18
26
  super
@@ -26,11 +34,17 @@ module EasyAuth::Models::Account
26
34
  end
27
35
  end
28
36
 
37
+ # Generates a new session token and updates the record
38
+ #
39
+ # @return [String]
29
40
  def generate_session_token!
30
41
  self.update_column(:session_token, _generate_token(:session))
31
42
  self.session_token
32
43
  end
33
44
 
45
+ # Used to set the session for the authenticated account
46
+ #
47
+ # @param [Rack::Session::Abstract::SessionHash] session controller session
34
48
  def set_session(session)
35
49
  session[:session_token] = generate_session_token!
36
50
  session[:account_class] = self.class.to_s
@@ -4,39 +4,59 @@ module EasyAuth::Models::Identity
4
4
 
5
5
  def self.included(base)
6
6
  base.class_eval do
7
- self.table_name = :identities
8
7
  belongs_to :account, :polymorphic => true
9
8
  extend ClassMethods
10
9
  end
11
10
  end
12
11
 
13
12
  module ClassMethods
13
+ # Base authentication method, should be implemented by child identities
14
+ #
15
+ # @param [ActionController::Base] controller instance of the controller
14
16
  def authenticate(controller = nil)
15
17
  raise NotImplementedError
16
18
  end
17
19
 
20
+ # Base assumption for new sessions on the controller, will set `@identity` to a new instance of the identity
21
+ #
22
+ # @param [ActionController::Base] controller instance of the controller
18
23
  def new_session(controller)
19
24
  controller.instance_variable_set(:@identity, self.new)
20
25
  end
21
26
  end
22
27
 
28
+ # Sets the session for the association account
29
+ #
30
+ # @param [Rack::Session::Abstract::SessionHash] session controller session
23
31
  def set_account_session(session)
24
32
  account.set_session(session)
25
33
  end
26
34
 
35
+ # Getter for the remember flag
27
36
  def remember
28
37
  @remember
29
38
  end
30
39
 
40
+ # Setter for the remember flag
41
+ #
42
+ # @param [Boolean] value
31
43
  def remember=(value)
32
44
  @remember = ::ActiveRecord::ConnectionAdapters::Column.value_to_boolean(value)
33
45
  end
34
46
 
47
+ # Generates a new remember token and updates it on the identity record
48
+ #
49
+ # @return [String]
35
50
  def generate_remember_token!
36
51
  update_column(:remember_token, _generate_token(:remember))
37
52
  remember_token
38
53
  end
39
54
 
55
+ # The time used for remembering how long to stay signed in
56
+ #
57
+ # Defaults to 1 year, override in the model to set your own custom remember time
58
+ #
59
+ # @return [DateTime]
40
60
  def remember_time
41
61
  1.year
42
62
  end
@@ -1,3 +1,3 @@
1
1
  module EasyAuth
2
- VERSION = '0.1.0'
2
+ VERSION = '0.2.0'
3
3
  end
data/lib/easy_auth.rb CHANGED
@@ -13,37 +13,66 @@ module EasyAuth
13
13
  autoload :Routes
14
14
  autoload :TokenGenerator
15
15
 
16
+ # The top-level model that should be inherited from to build EasyAuth identities
17
+ #
18
+ # @return [Class]
16
19
  def self.identity_model
17
- EasyAuth::Identity
20
+ ::Identity
18
21
  end
19
22
 
23
+ # The assumed account class
24
+ #
25
+ # @return [Class]
20
26
  def self.account_model
21
27
  User
22
28
  end
23
29
 
30
+ # Main authenticate method to delegate to the proper identity's .authenticate method
31
+ # Should handle any redirects necessary
32
+ #
33
+ # @param [ActionController::Base] controller instance of the controller
24
34
  def self.authenticate(controller)
25
- if identity_model = find_identity_model(controller)
35
+ if identity_model = find_identity_model(controller.params)
26
36
  identity_model.authenticate(controller)
27
37
  end
28
38
  end
29
39
 
40
+ # Main new_session method to delegate to the proper identity's .new_session method
41
+ # Should handle any redirects necessary
42
+ #
43
+ # @param [ActionController::Base] controller instance of the controller
30
44
  def self.new_session(controller)
31
- identity_model = find_identity_model(controller)
45
+ identity_model = find_identity_model(controller.params)
32
46
  identity_model.new_session(controller)
33
47
  end
34
48
 
49
+ # EasyAuth config
50
+ #
51
+ # @param [&block] block
35
52
  def self.config(&block)
36
53
  yield self
37
54
  end
38
55
 
39
- private
40
-
41
- def self.find_identity_model(controller)
42
- method_name = "#{controller.params[:identity]}_identity_model"
56
+ # Find the proper identity model
57
+ # Will use params[:identity] to first see if the identity's model method exists:
58
+ #
59
+ # i.e. password_identity_model
60
+ #
61
+ # If that method doesn't exist, will see if `Identities::#{camelcased_identity_name}` is defined
62
+ #
63
+ # If that fails will finally check to see if the camelcased identity name exists in the top-leve namespace
64
+ #
65
+ # @param [Hash] params must contain an `identity` key
66
+ # @return [Class]
67
+ def self.find_identity_model(params)
68
+ method_name = "#{params[:identity]}_identity_model"
69
+ camelcased_identity_name = params[:identity].to_s.camelcase
43
70
  if respond_to?(method_name)
44
- send(method_name, controller)
71
+ send(method_name, params)
72
+ elsif eval("defined?(Identities::#{camelcased_identity_name})")
73
+ eval("Identities::#{camelcased_identity_name}")
45
74
  else
46
- controller.params[:identity].to_s.camelcase.constantize
75
+ camelcased_identity_name.constantize
47
76
  end
48
77
  end
49
78
  end
@@ -0,0 +1,16 @@
1
+ module EasyAuth
2
+ class SetupGenerator < Rails::Generators::Base
3
+ def copy_initializer
4
+ source_paths << File.expand_path('../../templates/easy_auth', __FILE__)
5
+ copy_file 'initializer.rb', 'config/initializers/easy_auth.rb'
6
+ end
7
+
8
+ private
9
+
10
+ def self.installation_message
11
+ 'Copies initializer into config/initializers'
12
+ end
13
+
14
+ desc installation_message
15
+ end
16
+ end
@@ -0,0 +1,3 @@
1
+ EasyAuth.config do |c|
2
+ # put your config options in here
3
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: easy_auth
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2012-11-02 00:00:00.000000000 Z
13
+ date: 2012-11-05 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rails
@@ -188,6 +188,22 @@ dependencies:
188
188
  - - ! '>='
189
189
  - !ruby/object:Gem::Version
190
190
  version: '0'
191
+ - !ruby/object:Gem::Dependency
192
+ name: generator_spec
193
+ requirement: !ruby/object:Gem::Requirement
194
+ none: false
195
+ requirements:
196
+ - - ! '>='
197
+ - !ruby/object:Gem::Version
198
+ version: '0'
199
+ type: :development
200
+ prerelease: false
201
+ version_requirements: !ruby/object:Gem::Requirement
202
+ none: false
203
+ requirements:
204
+ - - ! '>='
205
+ - !ruby/object:Gem::Version
206
+ version: '0'
191
207
  description: EasyAuth
192
208
  email:
193
209
  - brian@dockyard.com
@@ -200,7 +216,7 @@ files:
200
216
  - app/controllers/authenticated_controller.rb
201
217
  - app/controllers/sessions_controller.rb
202
218
  - app/helpers/easy_auth_helper.rb
203
- - app/models/easy_auth/identity.rb
219
+ - app/models/identity.rb
204
220
  - config/locales/en.yml
205
221
  - config/routes.rb
206
222
  - db/migrate/20120227014023_create_easy_auth_identities.rb
@@ -218,7 +234,8 @@ files:
218
234
  - lib/easy_auth/token_generator.rb
219
235
  - lib/easy_auth/version.rb
220
236
  - lib/easy_auth.rb
221
- - lib/tasks/easy_auth_tasks.rake
237
+ - lib/generators/easy_auth/setup_generator.rb
238
+ - lib/generators/templates/easy_auth/initializer.rb
222
239
  - Rakefile
223
240
  - README.md
224
241
  homepage: https://github.com/dockyard/easy_auth
@@ -235,7 +252,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
235
252
  version: '0'
236
253
  segments:
237
254
  - 0
238
- hash: -3015113123042150927
255
+ hash: 2413348689923643998
239
256
  required_rubygems_version: !ruby/object:Gem::Requirement
240
257
  none: false
241
258
  requirements:
@@ -244,7 +261,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
244
261
  version: '0'
245
262
  segments:
246
263
  - 0
247
- hash: -3015113123042150927
264
+ hash: 2413348689923643998
248
265
  requirements: []
249
266
  rubyforge_project:
250
267
  rubygems_version: 1.8.23
@@ -1,3 +0,0 @@
1
- class EasyAuth::Identity < ActiveRecord::Base
2
- include EasyAuth::Models::Identity
3
- end
@@ -1,4 +0,0 @@
1
- # desc "Explaining what the task does"
2
- # task :easy_auth do
3
- # # Task goes here
4
- # end