namxam-authlogic 2.1.3.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +9 -0
- data/CHANGELOG.rdoc +345 -0
- data/LICENSE +20 -0
- data/README.rdoc +246 -0
- data/Rakefile +42 -0
- data/VERSION.yml +5 -0
- data/authlogic.gemspec +217 -0
- data/generators/session/session_generator.rb +9 -0
- data/generators/session/templates/session.rb +2 -0
- data/init.rb +1 -0
- data/lib/authlogic.rb +64 -0
- data/lib/authlogic/acts_as_authentic/base.rb +107 -0
- data/lib/authlogic/acts_as_authentic/email.rb +110 -0
- data/lib/authlogic/acts_as_authentic/logged_in_status.rb +60 -0
- data/lib/authlogic/acts_as_authentic/login.rb +141 -0
- data/lib/authlogic/acts_as_authentic/magic_columns.rb +24 -0
- data/lib/authlogic/acts_as_authentic/password.rb +355 -0
- data/lib/authlogic/acts_as_authentic/perishable_token.rb +105 -0
- data/lib/authlogic/acts_as_authentic/persistence_token.rb +68 -0
- data/lib/authlogic/acts_as_authentic/restful_authentication.rb +61 -0
- data/lib/authlogic/acts_as_authentic/session_maintenance.rb +139 -0
- data/lib/authlogic/acts_as_authentic/single_access_token.rb +65 -0
- data/lib/authlogic/acts_as_authentic/validations_scope.rb +32 -0
- data/lib/authlogic/authenticates_many/association.rb +42 -0
- data/lib/authlogic/authenticates_many/base.rb +55 -0
- data/lib/authlogic/controller_adapters/abstract_adapter.rb +67 -0
- data/lib/authlogic/controller_adapters/merb_adapter.rb +30 -0
- data/lib/authlogic/controller_adapters/rails_adapter.rb +48 -0
- data/lib/authlogic/controller_adapters/sinatra_adapter.rb +61 -0
- data/lib/authlogic/crypto_providers/aes256.rb +43 -0
- data/lib/authlogic/crypto_providers/bcrypt.rb +90 -0
- data/lib/authlogic/crypto_providers/md5.rb +34 -0
- data/lib/authlogic/crypto_providers/sha1.rb +35 -0
- data/lib/authlogic/crypto_providers/sha256.rb +50 -0
- data/lib/authlogic/crypto_providers/sha512.rb +50 -0
- data/lib/authlogic/crypto_providers/wordpress.rb +43 -0
- data/lib/authlogic/i18n.rb +83 -0
- data/lib/authlogic/i18n/translator.rb +15 -0
- data/lib/authlogic/random.rb +33 -0
- data/lib/authlogic/regex.rb +25 -0
- data/lib/authlogic/session/activation.rb +58 -0
- data/lib/authlogic/session/active_record_trickery.rb +64 -0
- data/lib/authlogic/session/base.rb +37 -0
- data/lib/authlogic/session/brute_force_protection.rb +96 -0
- data/lib/authlogic/session/callbacks.rb +99 -0
- data/lib/authlogic/session/cookies.rb +130 -0
- data/lib/authlogic/session/existence.rb +93 -0
- data/lib/authlogic/session/foundation.rb +63 -0
- data/lib/authlogic/session/http_auth.rb +58 -0
- data/lib/authlogic/session/id.rb +41 -0
- data/lib/authlogic/session/klass.rb +78 -0
- data/lib/authlogic/session/magic_columns.rb +95 -0
- data/lib/authlogic/session/magic_states.rb +59 -0
- data/lib/authlogic/session/params.rb +101 -0
- data/lib/authlogic/session/password.rb +240 -0
- data/lib/authlogic/session/perishable_token.rb +18 -0
- data/lib/authlogic/session/persistence.rb +70 -0
- data/lib/authlogic/session/priority_record.rb +34 -0
- data/lib/authlogic/session/scopes.rb +101 -0
- data/lib/authlogic/session/session.rb +62 -0
- data/lib/authlogic/session/timeout.rb +82 -0
- data/lib/authlogic/session/unauthorized_record.rb +50 -0
- data/lib/authlogic/session/validation.rb +82 -0
- data/lib/authlogic/test_case.rb +120 -0
- data/lib/authlogic/test_case/mock_controller.rb +45 -0
- data/lib/authlogic/test_case/mock_cookie_jar.rb +14 -0
- data/lib/authlogic/test_case/mock_logger.rb +10 -0
- data/lib/authlogic/test_case/mock_request.rb +19 -0
- data/lib/authlogic/test_case/rails_request_adapter.rb +30 -0
- data/rails/init.rb +1 -0
- data/shoulda_macros/authlogic.rb +69 -0
- data/test/acts_as_authentic_test/base_test.rb +18 -0
- data/test/acts_as_authentic_test/email_test.rb +97 -0
- data/test/acts_as_authentic_test/logged_in_status_test.rb +36 -0
- data/test/acts_as_authentic_test/login_test.rb +109 -0
- data/test/acts_as_authentic_test/magic_columns_test.rb +27 -0
- data/test/acts_as_authentic_test/password_test.rb +236 -0
- data/test/acts_as_authentic_test/perishable_token_test.rb +90 -0
- data/test/acts_as_authentic_test/persistence_token_test.rb +55 -0
- data/test/acts_as_authentic_test/restful_authentication_test.rb +40 -0
- data/test/acts_as_authentic_test/session_maintenance_test.rb +84 -0
- data/test/acts_as_authentic_test/single_access_test.rb +44 -0
- data/test/authenticates_many_test.rb +16 -0
- data/test/crypto_provider_test/aes256_test.rb +14 -0
- data/test/crypto_provider_test/bcrypt_test.rb +14 -0
- data/test/crypto_provider_test/sha1_test.rb +23 -0
- data/test/crypto_provider_test/sha256_test.rb +14 -0
- data/test/crypto_provider_test/sha512_test.rb +14 -0
- data/test/fixtures/companies.yml +5 -0
- data/test/fixtures/employees.yml +17 -0
- data/test/fixtures/projects.yml +3 -0
- data/test/fixtures/users.yml +24 -0
- data/test/i18n_test.rb +33 -0
- data/test/libs/affiliate.rb +7 -0
- data/test/libs/company.rb +6 -0
- data/test/libs/employee.rb +7 -0
- data/test/libs/employee_session.rb +2 -0
- data/test/libs/ldaper.rb +3 -0
- data/test/libs/ordered_hash.rb +9 -0
- data/test/libs/project.rb +3 -0
- data/test/libs/user.rb +5 -0
- data/test/libs/user_session.rb +6 -0
- data/test/random_test.rb +49 -0
- data/test/session_test/activation_test.rb +43 -0
- data/test/session_test/active_record_trickery_test.rb +36 -0
- data/test/session_test/brute_force_protection_test.rb +101 -0
- data/test/session_test/callbacks_test.rb +6 -0
- data/test/session_test/cookies_test.rb +112 -0
- data/test/session_test/credentials_test.rb +0 -0
- data/test/session_test/existence_test.rb +64 -0
- data/test/session_test/http_auth_test.rb +28 -0
- data/test/session_test/id_test.rb +17 -0
- data/test/session_test/klass_test.rb +40 -0
- data/test/session_test/magic_columns_test.rb +62 -0
- data/test/session_test/magic_states_test.rb +60 -0
- data/test/session_test/params_test.rb +53 -0
- data/test/session_test/password_test.rb +106 -0
- data/test/session_test/perishability_test.rb +15 -0
- data/test/session_test/persistence_test.rb +21 -0
- data/test/session_test/scopes_test.rb +60 -0
- data/test/session_test/session_test.rb +59 -0
- data/test/session_test/timeout_test.rb +52 -0
- data/test/session_test/unauthorized_record_test.rb +13 -0
- data/test/session_test/validation_test.rb +23 -0
- data/test/test_helper.rb +182 -0
- metadata +248 -0
@@ -0,0 +1,63 @@
|
|
1
|
+
module Authlogic
|
2
|
+
module Session
|
3
|
+
# Sort of like an interface, it sets the foundation for the class, such as the required methods. This also allows
|
4
|
+
# other modules to overwrite methods and call super on them. It's also a place to put "utility" methods used
|
5
|
+
# throughout Authlogic.
|
6
|
+
module Foundation
|
7
|
+
def self.included(klass)
|
8
|
+
klass.class_eval do
|
9
|
+
extend ClassMethods
|
10
|
+
include InstanceMethods
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
module ClassMethods
|
15
|
+
private
|
16
|
+
def rw_config(key, value, default_value = nil, read_value = nil)
|
17
|
+
if value == read_value
|
18
|
+
return read_inheritable_attribute(key) if inheritable_attributes.include?(key)
|
19
|
+
write_inheritable_attribute(key, default_value)
|
20
|
+
else
|
21
|
+
write_inheritable_attribute(key, value)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
module InstanceMethods
|
27
|
+
def initialize(*args)
|
28
|
+
self.credentials = args
|
29
|
+
end
|
30
|
+
|
31
|
+
# The credentials you passed to create your session. See credentials= for more info.
|
32
|
+
def credentials
|
33
|
+
[]
|
34
|
+
end
|
35
|
+
|
36
|
+
# Set your credentials before you save your session. You can pass a hash of credentials:
|
37
|
+
#
|
38
|
+
# session.credentials = {:login => "my login", :password => "my password", :remember_me => true}
|
39
|
+
#
|
40
|
+
# or you can pass an array of objects:
|
41
|
+
#
|
42
|
+
# session.credentials = [my_user_object, true]
|
43
|
+
#
|
44
|
+
# and if you need to set an id, just pass it last. This value need be the last item in the array you pass, since the id is something that
|
45
|
+
# you control yourself, it should never be set from a hash or a form. Examples:
|
46
|
+
#
|
47
|
+
# session.credentials = [{:login => "my login", :password => "my password", :remember_me => true}, :my_id]
|
48
|
+
# session.credentials = [my_user_object, true, :my_id]
|
49
|
+
def credentials=(values)
|
50
|
+
end
|
51
|
+
|
52
|
+
def inspect
|
53
|
+
"#<#{self.class.name}: #{credentials.blank? ? "no credentials provided" : credentials.inspect}>"
|
54
|
+
end
|
55
|
+
|
56
|
+
private
|
57
|
+
def build_key(last_part)
|
58
|
+
last_part
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
module Authlogic
|
2
|
+
module Session
|
3
|
+
# Handles all authentication that deals with basic HTTP auth. Which is authentication built into the HTTP protocol:
|
4
|
+
#
|
5
|
+
# http://username:password@whatever.com
|
6
|
+
#
|
7
|
+
# Also, if you are not comfortable letting users pass their raw username and password you can always use the single
|
8
|
+
# access token. See Authlogic::Session::Params for more info.
|
9
|
+
module HttpAuth
|
10
|
+
def self.included(klass)
|
11
|
+
klass.class_eval do
|
12
|
+
extend Config
|
13
|
+
include InstanceMethods
|
14
|
+
persist :persist_by_http_auth, :if => :persist_by_http_auth?
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
# Configuration for the HTTP basic auth feature of Authlogic.
|
19
|
+
module Config
|
20
|
+
# Do you want to allow your users to log in via HTTP basic auth?
|
21
|
+
#
|
22
|
+
# I recommend keeping this enabled. The only time I feel this should be disabled is if you are not comfortable
|
23
|
+
# having your users provide their raw username and password. Whatever the reason, you can disable it here.
|
24
|
+
#
|
25
|
+
# * <tt>Default:</tt> true
|
26
|
+
# * <tt>Accepts:</tt> Boolean
|
27
|
+
def allow_http_basic_auth(value = nil)
|
28
|
+
rw_config(:allow_http_basic_auth, value, true)
|
29
|
+
end
|
30
|
+
alias_method :allow_http_basic_auth=, :allow_http_basic_auth
|
31
|
+
end
|
32
|
+
|
33
|
+
# Instance methods for the HTTP basic auth feature of authlogic.
|
34
|
+
module InstanceMethods
|
35
|
+
private
|
36
|
+
def persist_by_http_auth?
|
37
|
+
allow_http_basic_auth? && login_field && password_field
|
38
|
+
end
|
39
|
+
|
40
|
+
def persist_by_http_auth
|
41
|
+
controller.authenticate_with_http_basic do |login, password|
|
42
|
+
if !login.blank? && !password.blank?
|
43
|
+
send("#{login_field}=", login)
|
44
|
+
send("#{password_field}=", password)
|
45
|
+
return valid?
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
false
|
50
|
+
end
|
51
|
+
|
52
|
+
def allow_http_basic_auth?
|
53
|
+
self.class.allow_http_basic_auth == true
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
module Authlogic
|
2
|
+
module Session
|
3
|
+
# Allows you to separate sessions with an id, ultimately letting you create multiple sessions for the same user.
|
4
|
+
module Id
|
5
|
+
def self.included(klass)
|
6
|
+
klass.class_eval do
|
7
|
+
attr_writer :id
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
# Setting the id if it is passed in the credentials.
|
12
|
+
def credentials=(value)
|
13
|
+
super
|
14
|
+
values = value.is_a?(Array) ? value : [value]
|
15
|
+
self.id = values.last if values.last.is_a?(Symbol)
|
16
|
+
end
|
17
|
+
|
18
|
+
# Allows you to set a unique identifier for your session, so that you can have more than 1 session at a time.
|
19
|
+
# A good example when this might be needed is when you want to have a normal user session and a "secure" user session.
|
20
|
+
# The secure user session would be created only when they want to modify their billing information, or other sensitive
|
21
|
+
# information. Similar to me.com. This requires 2 user sessions. Just use an id for the "secure" session and you should be good.
|
22
|
+
#
|
23
|
+
# You can set the id during initialization (see initialize for more information), or as an attribute:
|
24
|
+
#
|
25
|
+
# session.id = :my_id
|
26
|
+
#
|
27
|
+
# Just be sure and set your id before you save your session.
|
28
|
+
#
|
29
|
+
# Lastly, to retrieve your session with the id check out the find class method.
|
30
|
+
def id
|
31
|
+
@id
|
32
|
+
end
|
33
|
+
|
34
|
+
private
|
35
|
+
# Used for things like cookie_key, session_key, etc.
|
36
|
+
def build_key(last_part)
|
37
|
+
[id, super].compact.join("_")
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,78 @@
|
|
1
|
+
module Authlogic
|
2
|
+
module Session
|
3
|
+
# Handles authenticating via a traditional username and password.
|
4
|
+
module Klass
|
5
|
+
def self.included(klass)
|
6
|
+
klass.class_eval do
|
7
|
+
extend Config
|
8
|
+
include InstanceMethods
|
9
|
+
|
10
|
+
class << self
|
11
|
+
attr_accessor :configured_klass_methods
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
module Config
|
17
|
+
# Lets you change which model to use for authentication.
|
18
|
+
#
|
19
|
+
# * <tt>Default:</tt> inferred from the class name. UserSession would automatically try User
|
20
|
+
# * <tt>Accepts:</tt> an ActiveRecord class
|
21
|
+
def authenticate_with(klass)
|
22
|
+
@klass_name = klass.name
|
23
|
+
@klass = klass
|
24
|
+
end
|
25
|
+
alias_method :authenticate_with=, :authenticate_with
|
26
|
+
|
27
|
+
# The name of the class that this session is authenticating with. For example, the UserSession class will
|
28
|
+
# authenticate with the User class unless you specify otherwise in your configuration. See authenticate_with
|
29
|
+
# for information on how to change this value.
|
30
|
+
def klass
|
31
|
+
@klass ||=
|
32
|
+
if klass_name
|
33
|
+
klass_name.constantize
|
34
|
+
else
|
35
|
+
nil
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
# Same as klass, just returns a string instead of the actual constant.
|
40
|
+
def klass_name
|
41
|
+
@klass_name ||= guessed_klass_name
|
42
|
+
end
|
43
|
+
|
44
|
+
# The string of the model name class guessed from the actual session class name.
|
45
|
+
def guessed_klass_name
|
46
|
+
guessed_name = name.scan(/(.*)Session/)[0]
|
47
|
+
guessed_name[0] if guessed_name
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
module InstanceMethods
|
52
|
+
# Creating an alias method for the "record" method based on the klass name, so that we can do:
|
53
|
+
#
|
54
|
+
# session.user
|
55
|
+
#
|
56
|
+
# instead of:
|
57
|
+
#
|
58
|
+
# session.record
|
59
|
+
def initialize(*args)
|
60
|
+
if !self.class.configured_klass_methods
|
61
|
+
self.class.send(:alias_method, klass_name.demodulize.underscore.to_sym, :record)
|
62
|
+
self.class.configured_klass_methods = true
|
63
|
+
end
|
64
|
+
super
|
65
|
+
end
|
66
|
+
|
67
|
+
private
|
68
|
+
def klass
|
69
|
+
self.class.klass
|
70
|
+
end
|
71
|
+
|
72
|
+
def klass_name
|
73
|
+
self.class.klass_name
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
@@ -0,0 +1,95 @@
|
|
1
|
+
module Authlogic
|
2
|
+
module Session
|
3
|
+
# Just like ActiveRecord has "magic" columns, such as: created_at and updated_at. Authlogic has its own "magic" columns too:
|
4
|
+
#
|
5
|
+
# Column name Description
|
6
|
+
# login_count Increased every time an explicit login is made. This will *NOT* increase if logging in by a session, cookie, or basic http auth
|
7
|
+
# failed_login_count This increases for each consecutive failed login. See Authlogic::Session::BruteForceProtection and the consecutive_failed_logins_limit config option for more details.
|
8
|
+
# last_request_at Updates every time the user logs in, either by explicitly logging in, or logging in by cookie, session, or http auth
|
9
|
+
# current_login_at Updates with the current time when an explicit login is made.
|
10
|
+
# last_login_at Updates with the value of current_login_at before it is reset.
|
11
|
+
# current_login_ip Updates with the request remote_ip when an explicit login is made.
|
12
|
+
# last_login_ip Updates with the value of current_login_ip before it is reset.
|
13
|
+
module MagicColumns
|
14
|
+
def self.included(klass)
|
15
|
+
klass.class_eval do
|
16
|
+
extend Config
|
17
|
+
include InstanceMethods
|
18
|
+
after_persisting :set_last_request_at, :if => :set_last_request_at?
|
19
|
+
validate :increase_failed_login_count
|
20
|
+
before_save :update_info
|
21
|
+
before_save :set_last_request_at, :if => :set_last_request_at?
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
# Configuration for the magic columns feature.
|
26
|
+
module Config
|
27
|
+
# Every time a session is found the last_request_at field for that record is updatd with the current time, if that field exists.
|
28
|
+
# If you want to limit how frequent that field is updated specify the threshold here. For example, if your user is making a
|
29
|
+
# request every 5 seconds, and you feel this is too frequent, and feel a minute is a good threashold. Set this to 1.minute.
|
30
|
+
# Once a minute has passed in between requests the field will be updated.
|
31
|
+
#
|
32
|
+
# * <tt>Default:</tt> 0
|
33
|
+
# * <tt>Accepts:</tt> integer representing time in seconds
|
34
|
+
def last_request_at_threshold(value = nil)
|
35
|
+
rw_config(:last_request_at_threshold, value, 0)
|
36
|
+
end
|
37
|
+
alias_method :last_request_at_threshold=, :last_request_at_threshold
|
38
|
+
end
|
39
|
+
|
40
|
+
# The methods available for an Authlogic::Session::Base object that make up the magic columns feature.
|
41
|
+
module InstanceMethods
|
42
|
+
private
|
43
|
+
def increase_failed_login_count
|
44
|
+
if invalid_password? && attempted_record.respond_to?(:failed_login_count)
|
45
|
+
attempted_record.failed_login_count ||= 0
|
46
|
+
attempted_record.failed_login_count += 1
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
def update_info
|
51
|
+
record.login_count = (record.login_count.blank? ? 1 : record.login_count + 1) if record.respond_to?(:login_count)
|
52
|
+
record.failed_login_count = 0 if record.respond_to?(:failed_login_count)
|
53
|
+
|
54
|
+
if record.respond_to?(:current_login_at)
|
55
|
+
record.last_login_at = record.current_login_at if record.respond_to?(:last_login_at)
|
56
|
+
record.current_login_at = klass.default_timezone == :utc ? Time.now.utc : Time.now
|
57
|
+
end
|
58
|
+
|
59
|
+
if record.respond_to?(:current_login_ip)
|
60
|
+
record.last_login_ip = record.current_login_ip if record.respond_to?(:last_login_ip)
|
61
|
+
record.current_login_ip = controller.request.remote_ip
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
# This method lets authlogic know whether it should allow the last_request_at field to be updated
|
66
|
+
# with the current time (Time.now). One thing to note here is that it also checks for the existence of a
|
67
|
+
# last_request_update_allowed? method in your controller. This allows you to control this method pragmatically
|
68
|
+
# in your controller.
|
69
|
+
#
|
70
|
+
# For example, what if you had a javascript function that polled the server updating how much time is left in their
|
71
|
+
# session before it times out. Obviously you would want to ignore this request, because then the user would never time out.
|
72
|
+
# So you can do something like this in your controller:
|
73
|
+
#
|
74
|
+
# def last_request_update_allowed?
|
75
|
+
# action_name =! "update_session_time_left"
|
76
|
+
# end
|
77
|
+
#
|
78
|
+
# You can do whatever you want with that method.
|
79
|
+
def set_last_request_at? # :doc:
|
80
|
+
return false if !record || !klass.column_names.include?("last_request_at")
|
81
|
+
return controller.last_request_update_allowed? if controller.responds_to_last_request_update_allowed?
|
82
|
+
record.last_request_at.blank? || last_request_at_threshold.to_i.seconds.ago >= record.last_request_at
|
83
|
+
end
|
84
|
+
|
85
|
+
def set_last_request_at
|
86
|
+
record.last_request_at = klass.default_timezone == :utc ? Time.now.utc : Time.now
|
87
|
+
end
|
88
|
+
|
89
|
+
def last_request_at_threshold
|
90
|
+
self.class.last_request_at_threshold
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
@@ -0,0 +1,59 @@
|
|
1
|
+
module Authlogic
|
2
|
+
module Session
|
3
|
+
# Authlogic tries to check the state of the record before creating the session. If your record responds to the following methods and any of them return false, validation will fail:
|
4
|
+
#
|
5
|
+
# Method name Description
|
6
|
+
# active? Is the record marked as active?
|
7
|
+
# approved? Has the record been approved?
|
8
|
+
# confirmed? Has the record been conirmed?
|
9
|
+
#
|
10
|
+
# Authlogic does nothing to define these methods for you, its up to you to define what they mean. If your object responds to these methods Authlogic will use them, otherwise they are ignored.
|
11
|
+
#
|
12
|
+
# What's neat about this is that these are checked upon any type of login. When logging in explicitly, by cookie, session, or basic http auth.
|
13
|
+
# So if you mark a user inactive in the middle of their session they wont be logged back in next time they refresh the page. Giving you complete control.
|
14
|
+
#
|
15
|
+
# Need Authlogic to check your own "state"? No problem, check out the hooks section below. Add in a before_validation to do your own checking. The sky is the limit.
|
16
|
+
module MagicStates
|
17
|
+
def self.included(klass)
|
18
|
+
klass.class_eval do
|
19
|
+
extend Config
|
20
|
+
include InstanceMethods
|
21
|
+
validate :validate_magic_states, :unless => :disable_magic_states?
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
# Configuration for the magic states feature.
|
26
|
+
module Config
|
27
|
+
# Set this to true if you want to disable the checking of active?, approved?, and confirmed? on your record. This is more or less of a
|
28
|
+
# convenience feature, since 99% of the time if those methods exist and return false you will not want the user logging in. You could
|
29
|
+
# easily accomplish this same thing with a before_validation method or other callbacks.
|
30
|
+
#
|
31
|
+
# * <tt>Default:</tt> false
|
32
|
+
# * <tt>Accepts:</tt> Boolean
|
33
|
+
def disable_magic_states(value = nil)
|
34
|
+
rw_config(:disable_magic_states, value, false)
|
35
|
+
end
|
36
|
+
alias_method :disable_magic_states=, :disable_magic_states
|
37
|
+
end
|
38
|
+
|
39
|
+
# The methods available for an Authlogic::Session::Base object that make up the magic states feature.
|
40
|
+
module InstanceMethods
|
41
|
+
private
|
42
|
+
def disable_magic_states?
|
43
|
+
self.class.disable_magic_states == true
|
44
|
+
end
|
45
|
+
|
46
|
+
def validate_magic_states
|
47
|
+
return true if attempted_record.nil?
|
48
|
+
[:active, :approved, :confirmed].each do |required_status|
|
49
|
+
if attempted_record.respond_to?("#{required_status}?") && !attempted_record.send("#{required_status}?")
|
50
|
+
errors.add(:base, I18n.t("error_messages.not_#{required_status}", :default => "Your account is not #{required_status}"))
|
51
|
+
return false
|
52
|
+
end
|
53
|
+
end
|
54
|
+
true
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
@@ -0,0 +1,101 @@
|
|
1
|
+
module Authlogic
|
2
|
+
module Session
|
3
|
+
# This module is responsible for authenticating the user via params, which ultimately allows the user to log in using a URL like the following:
|
4
|
+
#
|
5
|
+
# https://www.domain.com?user_credentials=4LiXF7FiGUppIPubBPey
|
6
|
+
#
|
7
|
+
# Notice the token in the URL, this is a single access token. A single access token is used for single access only, it is not persisted. Meaning the user
|
8
|
+
# provides it, Authlogic grants them access, and that's it. If they want access again they need to provide the token again. Authlogic will
|
9
|
+
# *NEVER* try to persist the session after authenticating through this method.
|
10
|
+
#
|
11
|
+
# For added security, this token is *ONLY* allowed for RSS and ATOM requests. You can change this with the configuration. You can also define if
|
12
|
+
# it is allowed dynamically by defining a single_access_allowed? method in your controller. For example:
|
13
|
+
#
|
14
|
+
# class UsersController < ApplicationController
|
15
|
+
# private
|
16
|
+
# def single_access_allowed?
|
17
|
+
# action_name == "index"
|
18
|
+
# end
|
19
|
+
#
|
20
|
+
# Also, by default, this token is permanent. Meaning if the user changes their password, this token will remain the same. It will only change
|
21
|
+
# when it is explicitly reset.
|
22
|
+
#
|
23
|
+
# You can modify all of this behavior with the Config sub module.
|
24
|
+
module Params
|
25
|
+
def self.included(klass)
|
26
|
+
klass.class_eval do
|
27
|
+
extend Config
|
28
|
+
include InstanceMethods
|
29
|
+
attr_accessor :single_access
|
30
|
+
persist :persist_by_params
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
# Configuration for the params / single access feature.
|
35
|
+
module Config
|
36
|
+
# Works exactly like cookie_key, but for params. So a user can login via params just like a cookie or a session. Your URL would look like:
|
37
|
+
#
|
38
|
+
# http://www.domain.com?user_credentials=my_single_access_key
|
39
|
+
#
|
40
|
+
# You can change the "user_credentials" key above with this configuration option. Keep in mind, just like cookie_key, if you supply an id
|
41
|
+
# the id will be appended to the front. Check out cookie_key for more details. Also checkout the "Single Access / Private Feeds Access" section in the README.
|
42
|
+
#
|
43
|
+
# * <tt>Default:</tt> cookie_key
|
44
|
+
# * <tt>Accepts:</tt> String
|
45
|
+
def params_key(value = nil)
|
46
|
+
rw_config(:params_key, value, cookie_key)
|
47
|
+
end
|
48
|
+
alias_method :params_key=, :params_key
|
49
|
+
|
50
|
+
# Authentication is allowed via a single access token, but maybe this is something you don't want for your application as a whole. Maybe this is
|
51
|
+
# something you only want for specific request types. Specify a list of allowed request types and single access authentication will only be
|
52
|
+
# allowed for the ones you specify.
|
53
|
+
#
|
54
|
+
# * <tt>Default:</tt> ["application/rss+xml", "application/atom+xml"]
|
55
|
+
# * <tt>Accepts:</tt> String of a request type, or :all or :any to allow single access authentication for any and all request types
|
56
|
+
def single_access_allowed_request_types(value = nil)
|
57
|
+
rw_config(:single_access_allowed_request_types, value, ["application/rss+xml", "application/atom+xml"])
|
58
|
+
end
|
59
|
+
alias_method :single_access_allowed_request_types=, :single_access_allowed_request_types
|
60
|
+
end
|
61
|
+
|
62
|
+
# The methods available for an Authlogic::Session::Base object that make up the params / single access feature.
|
63
|
+
module InstanceMethods
|
64
|
+
private
|
65
|
+
def persist_by_params
|
66
|
+
return false if !params_enabled?
|
67
|
+
self.unauthorized_record = search_for_record("find_by_single_access_token", params_credentials)
|
68
|
+
self.single_access = valid?
|
69
|
+
end
|
70
|
+
|
71
|
+
def params_enabled?
|
72
|
+
return false if !params_credentials || !klass.column_names.include?("single_access_token")
|
73
|
+
return controller.single_access_allowed? if controller.responds_to_single_access_allowed?
|
74
|
+
|
75
|
+
case single_access_allowed_request_types
|
76
|
+
when Array
|
77
|
+
single_access_allowed_request_types.include?(controller.request_content_type) || single_access_allowed_request_types.include?(:all)
|
78
|
+
else
|
79
|
+
[:all, :any].include?(single_access_allowed_request_types)
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
def params_key
|
84
|
+
build_key(self.class.params_key)
|
85
|
+
end
|
86
|
+
|
87
|
+
def single_access?
|
88
|
+
single_access == true
|
89
|
+
end
|
90
|
+
|
91
|
+
def single_access_allowed_request_types
|
92
|
+
self.class.single_access_allowed_request_types
|
93
|
+
end
|
94
|
+
|
95
|
+
def params_credentials
|
96
|
+
controller.params[params_key]
|
97
|
+
end
|
98
|
+
end
|
99
|
+
end
|
100
|
+
end
|
101
|
+
end
|