xing_backend_token_auth 0.1.31
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.
- checksums.yaml +7 -0
- data/LICENSE +13 -0
- data/README.md +679 -0
- data/Rakefile +34 -0
- data/app/controllers/devise_token_auth/application_controller.rb +22 -0
- data/app/controllers/devise_token_auth/concerns/set_user_by_token.rb +110 -0
- data/app/controllers/devise_token_auth/confirmations_controller.rb +31 -0
- data/app/controllers/devise_token_auth/omniauth_callbacks_controller.rb +169 -0
- data/app/controllers/devise_token_auth/passwords_controller.rb +107 -0
- data/app/controllers/devise_token_auth/registrations_controller.rb +99 -0
- data/app/controllers/devise_token_auth/sessions_controller.rb +50 -0
- data/app/controllers/devise_token_auth/token_validations_controller.rb +22 -0
- data/app/serializers/devise_token_auth/error_messages_serializer.rb +16 -0
- data/app/serializers/devise_token_auth/resource_errors_serializer.rb +24 -0
- data/app/serializers/devise_token_auth/resource_serializer.rb +17 -0
- data/app/serializers/devise_token_auth/success_message_serializer.rb +15 -0
- data/app/views/devise/mailer/confirmation_instructions.html.erb +5 -0
- data/app/views/devise/mailer/reset_password_instructions.html.erb +8 -0
- data/app/views/devise/mailer/unlock_instructions.html.erb +7 -0
- data/app/views/devise_token_auth/omniauth_failure.html.erb +2 -0
- data/app/views/devise_token_auth/omniauth_success.html.erb +8 -0
- data/app/views/layouts/omniauth_response.html.erb +31 -0
- data/config/initializers/devise.rb +207 -0
- data/config/initializers/token_auth_failure_app.rb +7 -0
- data/config/locales/devise.en.yml +59 -0
- data/config/routes.rb +5 -0
- data/lib/devise_token_auth.rb +9 -0
- data/lib/devise_token_auth/controllers/helpers.rb +129 -0
- data/lib/devise_token_auth/controllers/url_helpers.rb +8 -0
- data/lib/devise_token_auth/engine.rb +32 -0
- data/lib/devise_token_auth/models/token_authenticatable.rb +195 -0
- data/lib/devise_token_auth/rails/routes.rb +65 -0
- data/lib/generators/devise_token_auth/USAGE +31 -0
- data/lib/generators/devise_token_auth/install_generator.rb +100 -0
- data/lib/generators/devise_token_auth/install_views_generator.rb +16 -0
- data/lib/generators/devise_token_auth/templates/devise_token_auth.rb +22 -0
- data/lib/generators/devise_token_auth/templates/devise_token_auth_add_token_info_to_users.rb.erb +14 -0
- data/lib/tasks/devise_token_auth_tasks.rake +4 -0
- data/lib/xing_backend_token_auth.rb +1 -0
- data/test/controllers/demo_group_controller_test.rb +126 -0
- data/test/controllers/demo_mang_controller_test.rb +263 -0
- data/test/controllers/demo_user_controller_test.rb +262 -0
- data/test/controllers/devise_token_auth/confirmations_controller_test.rb +107 -0
- data/test/controllers/devise_token_auth/omniauth_callbacks_controller_test.rb +144 -0
- data/test/controllers/devise_token_auth/passwords_controller_test.rb +275 -0
- data/test/controllers/devise_token_auth/registrations_controller_test.rb +405 -0
- data/test/controllers/devise_token_auth/registrations_controller_test.rb.orig +494 -0
- data/test/controllers/devise_token_auth/sessions_controller_test.rb +169 -0
- data/test/controllers/overrides/confirmations_controller_test.rb +44 -0
- data/test/controllers/overrides/omniauth_callbacks_controller_test.rb +44 -0
- data/test/controllers/overrides/passwords_controller_test.rb +64 -0
- data/test/controllers/overrides/registrations_controller_test.rb +42 -0
- data/test/controllers/overrides/sessions_controller_test.rb +35 -0
- data/test/controllers/overrides/token_validations_controller_test.rb +38 -0
- data/test/dummy/README.rdoc +28 -0
- data/test/dummy/Rakefile +6 -0
- data/test/dummy/app/assets/images/logo.jpg +0 -0
- data/test/dummy/app/assets/images/omniauth-provider-settings.png +0 -0
- data/test/dummy/app/assets/javascripts/application.js +13 -0
- data/test/dummy/app/assets/stylesheets/application.css +15 -0
- data/test/dummy/app/controllers/application_controller.rb +16 -0
- data/test/dummy/app/controllers/demo_group_controller.rb +13 -0
- data/test/dummy/app/controllers/demo_mang_controller.rb +12 -0
- data/test/dummy/app/controllers/demo_user_controller.rb +12 -0
- data/test/dummy/app/controllers/overrides/confirmations_controller.rb +32 -0
- data/test/dummy/app/controllers/overrides/omniauth_callbacks_controller.rb +14 -0
- data/test/dummy/app/controllers/overrides/passwords_controller.rb +39 -0
- data/test/dummy/app/controllers/overrides/registrations_controller.rb +27 -0
- data/test/dummy/app/controllers/overrides/sessions_controller.rb +26 -0
- data/test/dummy/app/controllers/overrides/token_validations_controller.rb +23 -0
- data/test/dummy/app/controllers/registrations_controller.rb +2 -0
- data/test/dummy/app/helpers/application_helper.rb +1065 -0
- data/test/dummy/app/models/evil_user.rb +5 -0
- data/test/dummy/app/models/mang.rb +5 -0
- data/test/dummy/app/models/user.rb +20 -0
- data/test/dummy/app/views/layouts/application.html.erb +14 -0
- data/test/dummy/bin/bundle +3 -0
- data/test/dummy/bin/rails +8 -0
- data/test/dummy/bin/rake +8 -0
- data/test/dummy/bin/spring +18 -0
- data/test/dummy/config.ru +16 -0
- data/test/dummy/config/application.rb +23 -0
- data/test/dummy/config/boot.rb +5 -0
- data/test/dummy/config/database.yml +31 -0
- data/test/dummy/config/environment.rb +5 -0
- data/test/dummy/config/environments/development.rb +44 -0
- data/test/dummy/config/environments/production.rb +82 -0
- data/test/dummy/config/environments/test.rb +40 -0
- data/test/dummy/config/initializers/assets.rb +8 -0
- data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
- data/test/dummy/config/initializers/cookies_serializer.rb +3 -0
- data/test/dummy/config/initializers/devise_token_auth.rb +22 -0
- data/test/dummy/config/initializers/figaro.rb +1 -0
- data/test/dummy/config/initializers/filter_parameter_logging.rb +4 -0
- data/test/dummy/config/initializers/inflections.rb +16 -0
- data/test/dummy/config/initializers/mime_types.rb +4 -0
- data/test/dummy/config/initializers/omniauth.rb +8 -0
- data/test/dummy/config/initializers/session_store.rb +3 -0
- data/test/dummy/config/initializers/wrap_parameters.rb +14 -0
- data/test/dummy/config/locales/en.yml +23 -0
- data/test/dummy/config/routes.rb +32 -0
- data/test/dummy/config/secrets.yml +22 -0
- data/test/dummy/config/spring.rb +1 -0
- data/test/dummy/db/migrate/20140715061447_devise_token_auth_create_users.rb +56 -0
- data/test/dummy/db/migrate/20140715061805_devise_token_auth_create_mangs.rb +56 -0
- data/test/dummy/db/migrate/20140829044006_add_operating_thetan_to_user.rb +6 -0
- data/test/dummy/db/migrate/20140916224624_add_favorite_color_to_mangs.rb +5 -0
- data/test/dummy/db/migrate/20140928231203_devise_token_auth_create_evil_users.rb +57 -0
- data/test/dummy/db/schema.rb +111 -0
- data/test/dummy/public/404.html +67 -0
- data/test/dummy/public/422.html +67 -0
- data/test/dummy/public/500.html +66 -0
- data/test/dummy/public/favicon.ico +0 -0
- data/test/fixtures/evil_users.yml +29 -0
- data/test/fixtures/mangs.yml +29 -0
- data/test/fixtures/users.yml +29 -0
- data/test/integration/navigation_test.rb +10 -0
- data/test/lib/generators/devise_token_auth/install_generator_test.rb +131 -0
- data/test/lib/generators/devise_token_auth/install_views_generator_test.rb +23 -0
- data/test/models/user_test.rb +81 -0
- data/test/test_helper.rb +60 -0
- metadata +320 -0
@@ -0,0 +1,59 @@
|
|
1
|
+
# Additional translations at https://github.com/plataformatec/devise/wiki/I18n
|
2
|
+
|
3
|
+
en:
|
4
|
+
devise:
|
5
|
+
confirmations:
|
6
|
+
confirmed: "Your account was successfully confirmed."
|
7
|
+
send_instructions: "You will receive an email with instructions about how to confirm your account in a few minutes."
|
8
|
+
send_paranoid_instructions: "If your email address exists in our database, you will receive an email with instructions about how to confirm your account in a few minutes."
|
9
|
+
failure:
|
10
|
+
already_authenticated: "You are already signed in."
|
11
|
+
inactive: "Your account is not activated yet."
|
12
|
+
invalid: "Invalid email or password."
|
13
|
+
locked: "Your account is locked."
|
14
|
+
last_attempt: "You have one more attempt before your account will be locked."
|
15
|
+
not_found_in_database: "Invalid email or password."
|
16
|
+
timeout: "Your session expired. Please sign in again to continue."
|
17
|
+
unauthenticated: "You need to sign in or sign up before continuing."
|
18
|
+
unconfirmed: "You have to confirm your account before continuing."
|
19
|
+
mailer:
|
20
|
+
confirmation_instructions:
|
21
|
+
subject: "Confirmation instructions"
|
22
|
+
reset_password_instructions:
|
23
|
+
subject: "Reset password instructions"
|
24
|
+
unlock_instructions:
|
25
|
+
subject: "Unlock Instructions"
|
26
|
+
omniauth_callbacks:
|
27
|
+
failure: "Could not authenticate you from %{kind} because \"%{reason}\"."
|
28
|
+
success: "Successfully authenticated from %{kind} account."
|
29
|
+
passwords:
|
30
|
+
no_token: "You can't access this page without coming from a password reset email. If you do come from a password reset email, please make sure you used the full URL provided."
|
31
|
+
send_instructions: "You will receive an email with instructions on how to reset your password in a few minutes."
|
32
|
+
send_paranoid_instructions: "If your email address exists in our database, you will receive a password recovery link at your email address in a few minutes."
|
33
|
+
updated: "Your password was changed successfully. You are now signed in."
|
34
|
+
updated_not_active: "Your password was changed successfully."
|
35
|
+
registrations:
|
36
|
+
destroyed: "Bye! Your account was successfully cancelled. We hope to see you again soon."
|
37
|
+
signed_up: "Welcome! You have signed up successfully."
|
38
|
+
signed_up_but_inactive: "You have signed up successfully. However, we could not sign you in because your account is not yet activated."
|
39
|
+
signed_up_but_locked: "You have signed up successfully. However, we could not sign you in because your account is locked."
|
40
|
+
signed_up_but_unconfirmed: "A message with a confirmation link has been sent to your email address. Please open the link to activate your account."
|
41
|
+
update_needs_confirmation: "You updated your account successfully, but we need to verify your new email address. Please check your email and click on the confirm link to finalize confirming your new email address."
|
42
|
+
updated: "You updated your account successfully."
|
43
|
+
sessions:
|
44
|
+
signed_in: "Signed in successfully."
|
45
|
+
signed_out: "Signed out successfully."
|
46
|
+
unlocks:
|
47
|
+
send_instructions: "You will receive an email with instructions about how to unlock your account in a few minutes."
|
48
|
+
send_paranoid_instructions: "If your account exists, you will receive an email with instructions about how to unlock it in a few minutes."
|
49
|
+
unlocked: "Your account has been unlocked successfully. Please sign in to continue."
|
50
|
+
errors:
|
51
|
+
messages:
|
52
|
+
already_confirmed: "was already confirmed, please try signing in"
|
53
|
+
confirmation_period_expired: "needs to be confirmed within %{period}, please request a new one"
|
54
|
+
expired: "has expired, please request a new one"
|
55
|
+
not_found: "not found"
|
56
|
+
not_locked: "was not locked"
|
57
|
+
not_saved:
|
58
|
+
one: "1 error prohibited this %{resource} from being saved:"
|
59
|
+
other: "%{count} errors prohibited this %{resource} from being saved:"
|
data/config/routes.rb
ADDED
@@ -0,0 +1,9 @@
|
|
1
|
+
require "devise"
|
2
|
+
require "devise_token_auth/engine"
|
3
|
+
require "devise_token_auth/controllers/helpers"
|
4
|
+
require "devise_token_auth/controllers/url_helpers"
|
5
|
+
|
6
|
+
module DeviseTokenAuth
|
7
|
+
end
|
8
|
+
|
9
|
+
Devise.add_module :token_authenticatable, :model => 'devise_token_auth/models/token_authenticatable'
|
@@ -0,0 +1,129 @@
|
|
1
|
+
module DeviseTokenAuth
|
2
|
+
module Controllers
|
3
|
+
module Helpers
|
4
|
+
extend ActiveSupport::Concern
|
5
|
+
|
6
|
+
module ClassMethods
|
7
|
+
# Define authentication filters and accessor helpers for a group of mappings.
|
8
|
+
# These methods are useful when you are working with multiple mappings that
|
9
|
+
# share some functionality. They are pretty much the same as the ones
|
10
|
+
# defined for normal mappings.
|
11
|
+
#
|
12
|
+
# Example:
|
13
|
+
#
|
14
|
+
# inside BlogsController (or any other controller, it doesn't matter which):
|
15
|
+
# devise_group :blogger, contains: [:user, :admin]
|
16
|
+
#
|
17
|
+
# Generated methods:
|
18
|
+
# authenticate_blogger! # Redirects unless user or admin are signed in
|
19
|
+
# blogger_signed_in? # Checks whether there is either a user or an admin signed in
|
20
|
+
# current_blogger # Currently signed in user or admin
|
21
|
+
# current_bloggers # Currently signed in user and admin
|
22
|
+
#
|
23
|
+
# Use:
|
24
|
+
# before_filter :authenticate_blogger! # Redirects unless either a user or an admin are authenticated
|
25
|
+
# before_filter ->{ authenticate_blogger! :admin } # Redirects to the admin login page
|
26
|
+
# current_blogger :user # Preferably returns a User if one is signed in
|
27
|
+
#
|
28
|
+
def devise_token_auth_group(group_name, opts={})
|
29
|
+
mappings = "[#{ opts[:contains].map { |m| ":#{m}" }.join(',') }]"
|
30
|
+
|
31
|
+
class_eval <<-METHODS, __FILE__, __LINE__ + 1
|
32
|
+
def authenticate_#{group_name}!(favourite=nil, opts={})
|
33
|
+
unless #{group_name}_signed_in?
|
34
|
+
mappings = #{mappings}
|
35
|
+
mappings.unshift mappings.delete(favourite.to_sym) if favourite
|
36
|
+
mappings.each do |mapping|
|
37
|
+
set_user_by_token(mapping)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def #{group_name}_signed_in?
|
43
|
+
#{mappings}.any? do |mapping|
|
44
|
+
set_user_by_token(mapping)
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
def current_#{group_name}(favourite=nil)
|
49
|
+
mappings = #{mappings}
|
50
|
+
mappings.unshift mappings.delete(favourite.to_sym) if favourite
|
51
|
+
mappings.each do |mapping|
|
52
|
+
current = set_user_by_token(mapping)
|
53
|
+
return current if current
|
54
|
+
end
|
55
|
+
nil
|
56
|
+
end
|
57
|
+
|
58
|
+
def current_#{group_name.to_s.pluralize}
|
59
|
+
#{mappings}.map do |mapping|
|
60
|
+
set_user_by_token(mapping)
|
61
|
+
end.compact
|
62
|
+
end
|
63
|
+
|
64
|
+
helper_method "current_#{group_name}", "current_#{group_name.to_s.pluralize}", "#{group_name}_signed_in?"
|
65
|
+
METHODS
|
66
|
+
end
|
67
|
+
|
68
|
+
def log_process_action(payload)
|
69
|
+
payload[:status] ||= 401 unless payload[:exception]
|
70
|
+
super
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
# Define authentication filters and accessor helpers based on mappings.
|
75
|
+
# These filters should be used inside the controllers as before_filters,
|
76
|
+
# so you can control the scope of the user who should be signed in to
|
77
|
+
# access that specific controller/action.
|
78
|
+
# Example:
|
79
|
+
#
|
80
|
+
# Roles:
|
81
|
+
# User
|
82
|
+
# Admin
|
83
|
+
#
|
84
|
+
# Generated methods:
|
85
|
+
# authenticate_user! # Signs user in or 401
|
86
|
+
# authenticate_admin! # Signs admin in or 401
|
87
|
+
# user_signed_in? # Checks whether there is a user signed in or not
|
88
|
+
# admin_signed_in? # Checks whether there is an admin signed in or not
|
89
|
+
# current_user # Current signed in user
|
90
|
+
# current_admin # Current signed in admin
|
91
|
+
# user_session # Session data available only to the user scope
|
92
|
+
# admin_session # Session data available only to the admin scope
|
93
|
+
#
|
94
|
+
# Use:
|
95
|
+
# before_filter :authenticate_user! # Tell devise to use :user map
|
96
|
+
# before_filter :authenticate_admin! # Tell devise to use :admin map
|
97
|
+
#
|
98
|
+
def self.define_helpers(mapping) #:nodoc:
|
99
|
+
mapping = mapping.name
|
100
|
+
|
101
|
+
class_eval <<-METHODS, __FILE__, __LINE__ + 1
|
102
|
+
def authenticate_#{mapping}!(opts={})
|
103
|
+
unless current_#{mapping}
|
104
|
+
return render json: {
|
105
|
+
errors: ["Authorized users only."]
|
106
|
+
}, status: 401
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
110
|
+
def #{mapping}_signed_in?
|
111
|
+
!!current_#{mapping}
|
112
|
+
end
|
113
|
+
|
114
|
+
def current_#{mapping}
|
115
|
+
@current_#{mapping} ||= set_user_by_token(:#{mapping})
|
116
|
+
end
|
117
|
+
|
118
|
+
def #{mapping}_session
|
119
|
+
current_#{mapping} && warden.session(:#{mapping})
|
120
|
+
end
|
121
|
+
METHODS
|
122
|
+
|
123
|
+
ActiveSupport.on_load(:action_controller) do
|
124
|
+
helper_method "current_#{mapping}", "#{mapping}_signed_in?", "#{mapping}_session"
|
125
|
+
end
|
126
|
+
end
|
127
|
+
end
|
128
|
+
end
|
129
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require 'devise_token_auth/rails/routes'
|
2
|
+
|
3
|
+
module DeviseTokenAuth
|
4
|
+
class Engine < ::Rails::Engine
|
5
|
+
isolate_namespace DeviseTokenAuth
|
6
|
+
|
7
|
+
initializer "devise_token_auth.url_helpers" do
|
8
|
+
Devise.helpers << DeviseTokenAuth::Controllers::Helpers
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
mattr_accessor :change_headers_on_each_request,
|
13
|
+
:token_lifespan,
|
14
|
+
:batch_request_buffer_throttle,
|
15
|
+
:omniauth_prefix,
|
16
|
+
:session_serializer,
|
17
|
+
:registration_serializer,
|
18
|
+
:token_validation_serializer,
|
19
|
+
:password_serializer,
|
20
|
+
:error_serializer,
|
21
|
+
:error_messages_serializer,
|
22
|
+
:success_message_serializer
|
23
|
+
|
24
|
+
self.change_headers_on_each_request = true
|
25
|
+
self.token_lifespan = 2.weeks
|
26
|
+
self.batch_request_buffer_throttle = 5.seconds
|
27
|
+
self.omniauth_prefix = '/omniauth'
|
28
|
+
|
29
|
+
def self.setup(&block)
|
30
|
+
yield self
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,195 @@
|
|
1
|
+
module Devise
|
2
|
+
module Models
|
3
|
+
module TokenAuthenticatable
|
4
|
+
extend ActiveSupport::Concern
|
5
|
+
|
6
|
+
included do
|
7
|
+
|
8
|
+
serialize :tokens, JSON
|
9
|
+
# can't set default on text fields in mysql, simulate here instead.
|
10
|
+
after_save :set_empty_token_hash
|
11
|
+
after_initialize :set_empty_token_hash
|
12
|
+
before_save :destroy_expired_tokens
|
13
|
+
|
14
|
+
# override devise method to include additional info as opts hash
|
15
|
+
def send_confirmation_instructions(opts=nil)
|
16
|
+
unless @raw_confirmation_token
|
17
|
+
generate_confirmation_token!
|
18
|
+
end
|
19
|
+
|
20
|
+
opts ||= {}
|
21
|
+
|
22
|
+
# fall back to "default" config name
|
23
|
+
opts[:client_config] ||= "default"
|
24
|
+
|
25
|
+
if pending_reconfirmation?
|
26
|
+
opts[:to] = unconfirmed_email
|
27
|
+
end
|
28
|
+
|
29
|
+
send_devise_notification(:confirmation_instructions, @raw_confirmation_token, opts)
|
30
|
+
end
|
31
|
+
|
32
|
+
|
33
|
+
# override devise method to include additional info as opts hash
|
34
|
+
def send_reset_password_instructions(opts=nil)
|
35
|
+
token = set_reset_password_token
|
36
|
+
|
37
|
+
opts ||= {}
|
38
|
+
|
39
|
+
# fall back to "default" config name
|
40
|
+
opts[:client_config] ||= "default"
|
41
|
+
|
42
|
+
if pending_reconfirmation?
|
43
|
+
opts[:to] = unconfirmed_email
|
44
|
+
else
|
45
|
+
opts[:to] = email
|
46
|
+
end
|
47
|
+
|
48
|
+
send_devise_notification(:reset_password_instructions, token, opts)
|
49
|
+
|
50
|
+
token
|
51
|
+
end
|
52
|
+
|
53
|
+
end
|
54
|
+
|
55
|
+
|
56
|
+
|
57
|
+
|
58
|
+
# this must be done from the controller so that additional params
|
59
|
+
# can be passed on from the client
|
60
|
+
def send_confirmation_notification?
|
61
|
+
false
|
62
|
+
end
|
63
|
+
|
64
|
+
def valid_token?(token, client_id='default')
|
65
|
+
client_id ||= 'default'
|
66
|
+
|
67
|
+
return false unless self.tokens[client_id]
|
68
|
+
|
69
|
+
return true if token_is_current?(token, client_id)
|
70
|
+
return true if token_can_be_reused?(token, client_id)
|
71
|
+
|
72
|
+
# return false if none of the above conditions are met
|
73
|
+
return false
|
74
|
+
end
|
75
|
+
|
76
|
+
|
77
|
+
def token_is_current?(token, client_id)
|
78
|
+
return true if (
|
79
|
+
# ensure that expiry and token are set
|
80
|
+
self.tokens[client_id]['expiry'] and
|
81
|
+
self.tokens[client_id]['token'] and
|
82
|
+
|
83
|
+
# ensure that the token was created within the last two weeks
|
84
|
+
DateTime.strptime(self.tokens[client_id]['expiry'].to_s, '%s') > Time.now and
|
85
|
+
|
86
|
+
# ensure that the token is valid
|
87
|
+
BCrypt::Password.new(self.tokens[client_id]['token']) == token
|
88
|
+
)
|
89
|
+
end
|
90
|
+
|
91
|
+
|
92
|
+
# allow batch requests to use the previous token
|
93
|
+
def token_can_be_reused?(token, client_id)
|
94
|
+
return true if (
|
95
|
+
# ensure that the last token and its creation time exist
|
96
|
+
self.tokens[client_id]['updated_at'] and
|
97
|
+
self.tokens[client_id]['last_token'] and
|
98
|
+
|
99
|
+
# ensure that previous token falls within the batch buffer throttle time of the last request
|
100
|
+
Time.parse(self.tokens[client_id]['updated_at']) > Time.now - DeviseTokenAuth.batch_request_buffer_throttle and
|
101
|
+
|
102
|
+
# ensure that the token is valid
|
103
|
+
BCrypt::Password.new(self.tokens[client_id]['last_token']) == token
|
104
|
+
)
|
105
|
+
end
|
106
|
+
|
107
|
+
|
108
|
+
# update user's auth token (should happen on each request)
|
109
|
+
def create_new_auth_token(client_id=nil)
|
110
|
+
client_id ||= SecureRandom.urlsafe_base64(nil, false)
|
111
|
+
last_token ||= nil
|
112
|
+
token = SecureRandom.urlsafe_base64(nil, false)
|
113
|
+
token_hash = BCrypt::Password.create(token)
|
114
|
+
expiry = (Time.now + DeviseTokenAuth.token_lifespan).to_i
|
115
|
+
|
116
|
+
if self.tokens[client_id] and self.tokens[client_id]['token']
|
117
|
+
last_token = self.tokens[client_id]['token']
|
118
|
+
end
|
119
|
+
|
120
|
+
self.tokens[client_id] = {
|
121
|
+
token: token_hash,
|
122
|
+
expiry: expiry,
|
123
|
+
last_token: last_token,
|
124
|
+
updated_at: Time.now
|
125
|
+
}
|
126
|
+
|
127
|
+
self.save!
|
128
|
+
|
129
|
+
return build_auth_header(token, client_id)
|
130
|
+
end
|
131
|
+
|
132
|
+
|
133
|
+
def build_auth_header(token, client_id='default')
|
134
|
+
client_id ||= 'default'
|
135
|
+
|
136
|
+
# client may use expiry to prevent validation request if expired
|
137
|
+
# must be cast as string or headers will break
|
138
|
+
expiry = self.tokens[client_id]['expiry'].to_s
|
139
|
+
|
140
|
+
return {
|
141
|
+
"access-token" => token,
|
142
|
+
"token-type" => "Bearer",
|
143
|
+
"client" => client_id,
|
144
|
+
"expiry" => expiry,
|
145
|
+
"uid" => self.uid
|
146
|
+
}
|
147
|
+
end
|
148
|
+
|
149
|
+
|
150
|
+
def build_auth_url(base_url, args)
|
151
|
+
args[:uid] = self.uid
|
152
|
+
args[:expiry] = self.tokens[args[:client_id]]['expiry']
|
153
|
+
|
154
|
+
generate_url(base_url, args)
|
155
|
+
end
|
156
|
+
|
157
|
+
|
158
|
+
def extend_batch_buffer(token, client_id)
|
159
|
+
self.tokens[client_id]['updated_at'] = Time.now
|
160
|
+
self.save!
|
161
|
+
|
162
|
+
return build_auth_header(token, client_id)
|
163
|
+
end
|
164
|
+
|
165
|
+
|
166
|
+
protected
|
167
|
+
|
168
|
+
|
169
|
+
# ensure that fragment comes AFTER querystring for proper $location
|
170
|
+
# parsing using AngularJS.
|
171
|
+
def generate_url(url, params = {})
|
172
|
+
uri = URI(url)
|
173
|
+
|
174
|
+
res = "#{uri.scheme}://#{uri.host}"
|
175
|
+
res += ":#{uri.port}" if (uri.port and uri.port != 80 and uri.port != 443)
|
176
|
+
res += "#{uri.path}#" if uri.path
|
177
|
+
res += "#{uri.fragment}" if uri.fragment
|
178
|
+
res += "?#{params.to_query}"
|
179
|
+
|
180
|
+
return res
|
181
|
+
end
|
182
|
+
|
183
|
+
def set_empty_token_hash
|
184
|
+
self.tokens ||= {} if has_attribute?(:tokens)
|
185
|
+
end
|
186
|
+
|
187
|
+
def destroy_expired_tokens
|
188
|
+
self.tokens.delete_if{|cid,v|
|
189
|
+
expiry = v[:expiry] || v["expiry"]
|
190
|
+
DateTime.strptime(expiry.to_s, '%s') < Time.now
|
191
|
+
}
|
192
|
+
end
|
193
|
+
end
|
194
|
+
end
|
195
|
+
end
|