digix_devise_token_auth 0.1.44
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 +952 -0
- data/Rakefile +35 -0
- data/app/controllers/devise_token_auth/application_controller.rb +76 -0
- data/app/controllers/devise_token_auth/concerns/resource_finder.rb +43 -0
- data/app/controllers/devise_token_auth/concerns/set_user_by_token.rb +165 -0
- data/app/controllers/devise_token_auth/confirmations_controller.rb +30 -0
- data/app/controllers/devise_token_auth/omniauth_callbacks_controller.rb +243 -0
- data/app/controllers/devise_token_auth/passwords_controller.rb +202 -0
- data/app/controllers/devise_token_auth/registrations_controller.rb +205 -0
- data/app/controllers/devise_token_auth/sessions_controller.rb +133 -0
- data/app/controllers/devise_token_auth/token_validations_controller.rb +29 -0
- data/app/controllers/devise_token_auth/unlocks_controller.rb +89 -0
- data/app/models/devise_token_auth/concerns/user.rb +260 -0
- data/app/models/devise_token_auth/concerns/user_omniauth_callbacks.rb +26 -0
- data/app/validators/email_validator.rb +21 -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_external_window.html.erb +38 -0
- data/config/initializers/devise.rb +196 -0
- data/config/locales/da-DK.yml +50 -0
- data/config/locales/de.yml +49 -0
- data/config/locales/en.yml +50 -0
- data/config/locales/es.yml +49 -0
- data/config/locales/fr.yml +49 -0
- data/config/locales/it.yml +46 -0
- data/config/locales/ja.yml +46 -0
- data/config/locales/nl.yml +30 -0
- data/config/locales/pl.yml +48 -0
- data/config/locales/pt-BR.yml +46 -0
- data/config/locales/pt.yml +48 -0
- data/config/locales/ro.yml +46 -0
- data/config/locales/ru.yml +50 -0
- data/config/locales/sq.yml +46 -0
- data/config/locales/uk.yml +59 -0
- data/config/locales/vi.yml +50 -0
- data/config/locales/zh-CN.yml +46 -0
- data/config/locales/zh-HK.yml +48 -0
- data/config/locales/zh-TW.yml +48 -0
- data/lib/devise_token_auth.rb +8 -0
- data/lib/devise_token_auth/controllers/helpers.rb +149 -0
- data/lib/devise_token_auth/controllers/url_helpers.rb +8 -0
- data/lib/devise_token_auth/engine.rb +90 -0
- data/lib/devise_token_auth/rails/routes.rb +114 -0
- data/lib/devise_token_auth/url.rb +37 -0
- data/lib/devise_token_auth/version.rb +3 -0
- data/lib/generators/devise_token_auth/USAGE +31 -0
- data/lib/generators/devise_token_auth/install_generator.rb +160 -0
- data/lib/generators/devise_token_auth/install_views_generator.rb +16 -0
- data/lib/generators/devise_token_auth/templates/devise_token_auth.rb +48 -0
- data/lib/generators/devise_token_auth/templates/devise_token_auth_create_users.rb.erb +55 -0
- data/lib/generators/devise_token_auth/templates/user.rb +7 -0
- data/lib/tasks/devise_token_auth_tasks.rake +4 -0
- data/test/controllers/custom/custom_confirmations_controller_test.rb +21 -0
- data/test/controllers/custom/custom_omniauth_callbacks_controller_test.rb +29 -0
- data/test/controllers/custom/custom_passwords_controller_test.rb +75 -0
- data/test/controllers/custom/custom_registrations_controller_test.rb +54 -0
- data/test/controllers/custom/custom_sessions_controller_test.rb +37 -0
- data/test/controllers/custom/custom_token_validations_controller_test.rb +40 -0
- data/test/controllers/demo_group_controller_test.rb +153 -0
- data/test/controllers/demo_mang_controller_test.rb +284 -0
- data/test/controllers/demo_user_controller_test.rb +601 -0
- data/test/controllers/devise_token_auth/confirmations_controller_test.rb +129 -0
- data/test/controllers/devise_token_auth/omniauth_callbacks_controller_test.rb +371 -0
- data/test/controllers/devise_token_auth/passwords_controller_test.rb +649 -0
- data/test/controllers/devise_token_auth/registrations_controller_test.rb +878 -0
- data/test/controllers/devise_token_auth/sessions_controller_test.rb +500 -0
- data/test/controllers/devise_token_auth/token_validations_controller_test.rb +90 -0
- data/test/controllers/devise_token_auth/unlocks_controller_test.rb +194 -0
- data/test/controllers/overrides/confirmations_controller_test.rb +43 -0
- data/test/controllers/overrides/omniauth_callbacks_controller_test.rb +49 -0
- data/test/controllers/overrides/passwords_controller_test.rb +66 -0
- data/test/controllers/overrides/registrations_controller_test.rb +40 -0
- data/test/controllers/overrides/sessions_controller_test.rb +33 -0
- data/test/controllers/overrides/token_validations_controller_test.rb +41 -0
- data/test/dummy/README.rdoc +28 -0
- data/test/dummy/app/controllers/application_controller.rb +16 -0
- data/test/dummy/app/controllers/auth_origin_controller.rb +5 -0
- data/test/dummy/app/controllers/custom/confirmations_controller.rb +13 -0
- data/test/dummy/app/controllers/custom/omniauth_callbacks_controller.rb +11 -0
- data/test/dummy/app/controllers/custom/passwords_controller.rb +40 -0
- data/test/dummy/app/controllers/custom/registrations_controller.rb +39 -0
- data/test/dummy/app/controllers/custom/sessions_controller.rb +29 -0
- data/test/dummy/app/controllers/custom/token_validations_controller.rb +19 -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 +25 -0
- data/test/dummy/app/controllers/overrides/confirmations_controller.rb +26 -0
- data/test/dummy/app/controllers/overrides/omniauth_callbacks_controller.rb +14 -0
- data/test/dummy/app/controllers/overrides/passwords_controller.rb +33 -0
- data/test/dummy/app/controllers/overrides/registrations_controller.rb +27 -0
- data/test/dummy/app/controllers/overrides/sessions_controller.rb +36 -0
- data/test/dummy/app/controllers/overrides/token_validations_controller.rb +23 -0
- data/test/dummy/app/helpers/application_helper.rb +1065 -0
- data/test/dummy/app/models/evil_user.rb +3 -0
- data/test/dummy/app/models/lockable_user.rb +5 -0
- data/test/dummy/app/models/mang.rb +3 -0
- data/test/dummy/app/models/nice_user.rb +7 -0
- data/test/dummy/app/models/only_email_user.rb +5 -0
- data/test/dummy/app/models/scoped_user.rb +7 -0
- data/test/dummy/app/models/unconfirmable_user.rb +8 -0
- data/test/dummy/app/models/unregisterable_user.rb +7 -0
- data/test/dummy/app/models/user.rb +18 -0
- data/test/dummy/app/views/layouts/application.html.erb +14 -0
- data/test/dummy/config.ru +16 -0
- data/test/dummy/config/application.rb +24 -0
- data/test/dummy/config/application.yml.bk +0 -0
- data/test/dummy/config/boot.rb +5 -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 +48 -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.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/routes.rb +72 -0
- data/test/dummy/config/spring.rb +1 -0
- data/test/dummy/db/migrate/20140715061447_devise_token_auth_create_users.rb +63 -0
- data/test/dummy/db/migrate/20140715061805_devise_token_auth_create_mangs.rb +62 -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 +64 -0
- data/test/dummy/db/migrate/20141222035835_devise_token_auth_create_only_email_users.rb +60 -0
- data/test/dummy/db/migrate/20141222053502_devise_token_auth_create_unregisterable_users.rb +61 -0
- data/test/dummy/db/migrate/20150409095712_devise_token_auth_create_nice_users.rb +61 -0
- data/test/dummy/db/migrate/20150708104536_devise_token_auth_create_unconfirmable_users.rb +61 -0
- data/test/dummy/db/migrate/20160103235141_devise_token_auth_create_scoped_users.rb +61 -0
- data/test/dummy/db/migrate/20160629184441_devise_token_auth_create_lockable_users.rb +61 -0
- data/test/dummy/db/schema.rb +258 -0
- data/test/dummy/lib/migration_database_helper.rb +29 -0
- data/test/integration/navigation_test.rb +10 -0
- data/test/lib/devise_token_auth/url_test.rb +24 -0
- data/test/lib/generators/devise_token_auth/install_generator_test.rb +187 -0
- data/test/lib/generators/devise_token_auth/install_views_generator_test.rb +23 -0
- data/test/models/only_email_user_test.rb +35 -0
- data/test/models/user_test.rb +169 -0
- data/test/test_helper.rb +77 -0
- metadata +342 -0
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
module ActionDispatch::Routing
|
|
2
|
+
class Mapper
|
|
3
|
+
def mount_devise_token_auth_for(resource, opts)
|
|
4
|
+
# ensure objects exist to simplify attr checks
|
|
5
|
+
opts[:controllers] ||= {}
|
|
6
|
+
opts[:skip] ||= []
|
|
7
|
+
|
|
8
|
+
# check for ctrl overrides, fall back to defaults
|
|
9
|
+
sessions_ctrl = opts[:controllers][:sessions] || "devise_token_auth/sessions"
|
|
10
|
+
registrations_ctrl = opts[:controllers][:registrations] || "devise_token_auth/registrations"
|
|
11
|
+
passwords_ctrl = opts[:controllers][:passwords] || "devise_token_auth/passwords"
|
|
12
|
+
confirmations_ctrl = opts[:controllers][:confirmations] || "devise_token_auth/confirmations"
|
|
13
|
+
token_validations_ctrl = opts[:controllers][:token_validations] || "devise_token_auth/token_validations"
|
|
14
|
+
omniauth_ctrl = opts[:controllers][:omniauth_callbacks] || "devise_token_auth/omniauth_callbacks"
|
|
15
|
+
unlocks_ctrl = opts[:controllers][:unlocks] || "devise_token_auth/unlocks"
|
|
16
|
+
|
|
17
|
+
# define devise controller mappings
|
|
18
|
+
controllers = {:sessions => sessions_ctrl,
|
|
19
|
+
:registrations => registrations_ctrl,
|
|
20
|
+
:passwords => passwords_ctrl,
|
|
21
|
+
:confirmations => confirmations_ctrl}
|
|
22
|
+
|
|
23
|
+
controllers[:unlocks] = unlocks_ctrl if unlocks_ctrl
|
|
24
|
+
|
|
25
|
+
# remove any unwanted devise modules
|
|
26
|
+
opts[:skip].each{|item| controllers.delete(item)}
|
|
27
|
+
|
|
28
|
+
devise_for resource.pluralize.underscore.gsub('/', '_').to_sym,
|
|
29
|
+
:class_name => resource,
|
|
30
|
+
:module => :devise,
|
|
31
|
+
:path => "#{opts[:at]}",
|
|
32
|
+
:controllers => controllers,
|
|
33
|
+
:skip => opts[:skip] + [:omniauth_callbacks]
|
|
34
|
+
|
|
35
|
+
unnest_namespace do
|
|
36
|
+
# get full url path as if it were namespaced
|
|
37
|
+
full_path = "#{@scope[:path]}/#{opts[:at]}"
|
|
38
|
+
|
|
39
|
+
# get namespace name
|
|
40
|
+
namespace_name = @scope[:as]
|
|
41
|
+
|
|
42
|
+
# clear scope so controller routes aren't namespaced
|
|
43
|
+
@scope = ActionDispatch::Routing::Mapper::Scope.new(
|
|
44
|
+
path: "",
|
|
45
|
+
shallow_path: "",
|
|
46
|
+
constraints: {},
|
|
47
|
+
defaults: {},
|
|
48
|
+
options: {},
|
|
49
|
+
parent: nil
|
|
50
|
+
)
|
|
51
|
+
|
|
52
|
+
mapping_name = resource.underscore.gsub('/', '_')
|
|
53
|
+
mapping_name = "#{namespace_name}_#{mapping_name}" if namespace_name
|
|
54
|
+
|
|
55
|
+
devise_scope mapping_name.to_sym do
|
|
56
|
+
# path to verify token validity
|
|
57
|
+
get "#{full_path}/validate_token", controller: "#{token_validations_ctrl}", action: "validate_token"
|
|
58
|
+
|
|
59
|
+
# omniauth routes. only define if omniauth is installed and not skipped.
|
|
60
|
+
if defined?(::OmniAuth) && !opts[:skip].include?(:omniauth_callbacks)
|
|
61
|
+
match "#{full_path}/failure", controller: omniauth_ctrl, action: "omniauth_failure", via: [:get]
|
|
62
|
+
match "#{full_path}/:provider/callback", controller: omniauth_ctrl, action: "omniauth_success", via: [:get]
|
|
63
|
+
|
|
64
|
+
match "#{DeviseTokenAuth.omniauth_prefix}/:provider/callback", controller: omniauth_ctrl, action: "redirect_callbacks", via: [:get, :post]
|
|
65
|
+
match "#{DeviseTokenAuth.omniauth_prefix}/failure", controller: omniauth_ctrl, action: "omniauth_failure", via: [:get, :post]
|
|
66
|
+
|
|
67
|
+
# preserve the resource class thru oauth authentication by setting name of
|
|
68
|
+
# resource as "resource_class" param
|
|
69
|
+
match "#{full_path}/:provider", to: redirect{|params, request|
|
|
70
|
+
# get the current querystring
|
|
71
|
+
qs = CGI::parse(request.env["QUERY_STRING"])
|
|
72
|
+
|
|
73
|
+
# append name of current resource
|
|
74
|
+
qs["resource_class"] = [resource]
|
|
75
|
+
qs["namespace_name"] = [namespace_name] if namespace_name
|
|
76
|
+
|
|
77
|
+
set_omniauth_path_prefix!(DeviseTokenAuth.omniauth_prefix)
|
|
78
|
+
|
|
79
|
+
redirect_params = {}.tap {|hash| qs.each{|k, v| hash[k] = v.first}}
|
|
80
|
+
|
|
81
|
+
if DeviseTokenAuth.redirect_whitelist
|
|
82
|
+
redirect_url = request.params['auth_origin_url']
|
|
83
|
+
unless DeviseTokenAuth::Url.whitelisted?(redirect_url)
|
|
84
|
+
message = I18n.t(
|
|
85
|
+
'devise_token_auth.registrations.redirect_url_not_allowed',
|
|
86
|
+
redirect_url: redirect_url
|
|
87
|
+
)
|
|
88
|
+
redirect_params['message'] = message
|
|
89
|
+
next "#{::OmniAuth.config.path_prefix}/failure?#{redirect_params.to_param}"
|
|
90
|
+
end
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
# re-construct the path for omniauth
|
|
94
|
+
"#{::OmniAuth.config.path_prefix}/#{params[:provider]}?#{redirect_params.to_param}"
|
|
95
|
+
}, via: [:get]
|
|
96
|
+
end
|
|
97
|
+
end
|
|
98
|
+
end
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
# this allows us to use namespaced paths without namespacing the routes
|
|
102
|
+
def unnest_namespace
|
|
103
|
+
current_scope = @scope.dup
|
|
104
|
+
yield
|
|
105
|
+
ensure
|
|
106
|
+
@scope = current_scope
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
# ignore error about omniauth/multiple model support
|
|
110
|
+
def set_omniauth_path_prefix!(path_prefix)
|
|
111
|
+
::OmniAuth.config.path_prefix = path_prefix
|
|
112
|
+
end
|
|
113
|
+
end
|
|
114
|
+
end
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
module DeviseTokenAuth::Url
|
|
2
|
+
|
|
3
|
+
def self.generate(url, params = {})
|
|
4
|
+
uri = URI(url)
|
|
5
|
+
|
|
6
|
+
res = "#{uri.scheme}://#{uri.host}"
|
|
7
|
+
res += ":#{uri.port}" if (uri.port && uri.port != 80 && uri.port != 443)
|
|
8
|
+
res += "#{uri.path}" if uri.path
|
|
9
|
+
query = [uri.query, params.to_query].reject(&:blank?).join('&')
|
|
10
|
+
res += "?#{query}"
|
|
11
|
+
res += "##{uri.fragment}" if uri.fragment
|
|
12
|
+
|
|
13
|
+
return res
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def self.whitelisted?(url)
|
|
17
|
+
url.nil? || !!DeviseTokenAuth.redirect_whitelist.find { |pattern| !!Wildcat.new(pattern).match(url) }
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
# wildcard convenience class
|
|
22
|
+
class Wildcat
|
|
23
|
+
def self.parse_to_regex(str)
|
|
24
|
+
escaped = Regexp.escape(str).gsub('\*','.*?')
|
|
25
|
+
Regexp.new("^#{escaped}$", Regexp::IGNORECASE)
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def initialize(str)
|
|
29
|
+
@regex = self.class.parse_to_regex(str)
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def match(str)
|
|
33
|
+
!!@regex.match(str)
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
end
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
Description:
|
|
2
|
+
This generator will install all the necessary configuration and migration
|
|
3
|
+
files for the devise_token_auth gem. See
|
|
4
|
+
https://github.com/lynndylanhurley/devise_token_auth for more information.
|
|
5
|
+
|
|
6
|
+
Arguments:
|
|
7
|
+
USER_CLASS # The name of the class to use for user authentication. Default is
|
|
8
|
+
# 'User'
|
|
9
|
+
MOUNT_PATH # The path at which to mount the authentication routes. Default is
|
|
10
|
+
# 'auth'. More detail documentation is here:
|
|
11
|
+
# https://github.com/lynndylanhurley/devise_token_auth#usage-tldr
|
|
12
|
+
|
|
13
|
+
Example:
|
|
14
|
+
rails generate devise_token_auth:install User auth
|
|
15
|
+
|
|
16
|
+
This will create:
|
|
17
|
+
config/initializers/devise_token_auth.rb
|
|
18
|
+
db/migrate/<%= Time.now.utc.strftime("%Y%m%d%H%M%S") %>_create_devise_token_auth_create_users.rb
|
|
19
|
+
app/models/user.rb
|
|
20
|
+
|
|
21
|
+
If 'app/models/user.rb' already exists, the following line will be inserted
|
|
22
|
+
after the class definition:
|
|
23
|
+
include DeviseTokenAuth::Concerns::User
|
|
24
|
+
|
|
25
|
+
The following line will be inserted into your application controller at
|
|
26
|
+
app/controllers/application_controller.rb:
|
|
27
|
+
include DeviseTokenAuth::Concerns::SetUserByToken
|
|
28
|
+
|
|
29
|
+
The following line will be inserted at the top of 'config/routes.rb' if it
|
|
30
|
+
does not already exist:
|
|
31
|
+
mount_devise_token_auth_for "User", at: 'auth'
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
module DeviseTokenAuth
|
|
2
|
+
class InstallGenerator < Rails::Generators::Base
|
|
3
|
+
include Rails::Generators::Migration
|
|
4
|
+
|
|
5
|
+
source_root File.expand_path('../templates', __FILE__)
|
|
6
|
+
|
|
7
|
+
argument :user_class, type: :string, default: "User"
|
|
8
|
+
argument :mount_path, type: :string, default: 'auth'
|
|
9
|
+
|
|
10
|
+
def create_initializer_file
|
|
11
|
+
copy_file("devise_token_auth.rb", "config/initializers/devise_token_auth.rb")
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def copy_migrations
|
|
15
|
+
if self.class.migration_exists?("db/migrate", "devise_token_auth_create_#{ user_class.underscore }")
|
|
16
|
+
say_status("skipped", "Migration 'devise_token_auth_create_#{ user_class.underscore }' already exists")
|
|
17
|
+
else
|
|
18
|
+
migration_template(
|
|
19
|
+
"devise_token_auth_create_users.rb.erb",
|
|
20
|
+
"db/migrate/devise_token_auth_create_#{ user_class.pluralize.underscore }.rb"
|
|
21
|
+
)
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def create_user_model
|
|
26
|
+
fname = "app/models/#{ user_class.underscore }.rb"
|
|
27
|
+
unless File.exist?(File.join(destination_root, fname))
|
|
28
|
+
template("user.rb", fname)
|
|
29
|
+
else
|
|
30
|
+
inclusion = "include DeviseTokenAuth::Concerns::User"
|
|
31
|
+
unless parse_file_for_line(fname, inclusion)
|
|
32
|
+
|
|
33
|
+
active_record_needle = (Rails::VERSION::MAJOR == 5) ? 'ApplicationRecord' : 'ActiveRecord::Base'
|
|
34
|
+
inject_into_file fname, after: "class #{user_class} < #{active_record_needle}\n" do <<-'RUBY'
|
|
35
|
+
# Include default devise modules.
|
|
36
|
+
devise :database_authenticatable, :registerable,
|
|
37
|
+
:recoverable, :rememberable, :trackable, :validatable,
|
|
38
|
+
:confirmable, :omniauthable
|
|
39
|
+
include DeviseTokenAuth::Concerns::User
|
|
40
|
+
RUBY
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def include_controller_concerns
|
|
47
|
+
fname = "app/controllers/application_controller.rb"
|
|
48
|
+
line = "include DeviseTokenAuth::Concerns::SetUserByToken"
|
|
49
|
+
|
|
50
|
+
if File.exist?(File.join(destination_root, fname))
|
|
51
|
+
if parse_file_for_line(fname, line)
|
|
52
|
+
say_status("skipped", "Concern is already included in the application controller.")
|
|
53
|
+
elsif is_rails_api?
|
|
54
|
+
inject_into_file fname, after: "class ApplicationController < ActionController::API\n" do <<-'RUBY'
|
|
55
|
+
include DeviseTokenAuth::Concerns::SetUserByToken
|
|
56
|
+
RUBY
|
|
57
|
+
end
|
|
58
|
+
else
|
|
59
|
+
inject_into_file fname, after: "class ApplicationController < ActionController::Base\n" do <<-'RUBY'
|
|
60
|
+
include DeviseTokenAuth::Concerns::SetUserByToken
|
|
61
|
+
RUBY
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
else
|
|
65
|
+
say_status("skipped", "app/controllers/application_controller.rb not found. Add 'include DeviseTokenAuth::Concerns::SetUserByToken' to any controllers that require authentication.")
|
|
66
|
+
end
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
def add_route_mount
|
|
70
|
+
f = "config/routes.rb"
|
|
71
|
+
str = "mount_devise_token_auth_for '#{user_class}', at: '#{mount_path}'"
|
|
72
|
+
|
|
73
|
+
if File.exist?(File.join(destination_root, f))
|
|
74
|
+
line = parse_file_for_line(f, "mount_devise_token_auth_for")
|
|
75
|
+
|
|
76
|
+
unless line
|
|
77
|
+
line = "Rails.application.routes.draw do"
|
|
78
|
+
existing_user_class = false
|
|
79
|
+
else
|
|
80
|
+
existing_user_class = true
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
if parse_file_for_line(f, str)
|
|
84
|
+
say_status("skipped", "Routes already exist for #{user_class} at #{mount_path}")
|
|
85
|
+
else
|
|
86
|
+
insert_after_line(f, line, str)
|
|
87
|
+
|
|
88
|
+
if existing_user_class
|
|
89
|
+
scoped_routes = ""+
|
|
90
|
+
"as :#{user_class.underscore} do\n"+
|
|
91
|
+
" # Define routes for #{user_class} within this block.\n"+
|
|
92
|
+
" end\n"
|
|
93
|
+
insert_after_line(f, str, scoped_routes)
|
|
94
|
+
end
|
|
95
|
+
end
|
|
96
|
+
else
|
|
97
|
+
say_status("skipped", "config/routes.rb not found. Add \"mount_devise_token_auth_for '#{user_class}', at: '#{mount_path}'\" to your routes file.")
|
|
98
|
+
end
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
private
|
|
102
|
+
|
|
103
|
+
def self.next_migration_number(path)
|
|
104
|
+
Time.now.utc.strftime("%Y%m%d%H%M%S")
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
def insert_after_line(filename, line, str)
|
|
108
|
+
gsub_file filename, /(#{Regexp.escape(line)})/mi do |match|
|
|
109
|
+
"#{match}\n #{str}"
|
|
110
|
+
end
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
def parse_file_for_line(filename, str)
|
|
114
|
+
match = false
|
|
115
|
+
|
|
116
|
+
File.open(File.join(destination_root, filename)) do |f|
|
|
117
|
+
f.each_line do |line|
|
|
118
|
+
if line =~ /(#{Regexp.escape(str)})/mi
|
|
119
|
+
match = line
|
|
120
|
+
end
|
|
121
|
+
end
|
|
122
|
+
end
|
|
123
|
+
match
|
|
124
|
+
end
|
|
125
|
+
|
|
126
|
+
def is_rails_api?
|
|
127
|
+
fname = "app/controllers/application_controller.rb"
|
|
128
|
+
line = "class ApplicationController < ActionController::API"
|
|
129
|
+
parse_file_for_line(fname, line)
|
|
130
|
+
end
|
|
131
|
+
|
|
132
|
+
def json_supported_database?
|
|
133
|
+
(postgres? && postgres_correct_version?) || (mysql? && mysql_correct_version?)
|
|
134
|
+
end
|
|
135
|
+
|
|
136
|
+
def postgres?
|
|
137
|
+
database_name == 'ActiveRecord::ConnectionAdapters::PostgreSQLAdapter'
|
|
138
|
+
end
|
|
139
|
+
|
|
140
|
+
def postgres_correct_version?
|
|
141
|
+
database_version > '9.3'
|
|
142
|
+
end
|
|
143
|
+
|
|
144
|
+
def mysql?
|
|
145
|
+
database_name == 'ActiveRecord::ConnectionAdapters::MysqlAdapter'
|
|
146
|
+
end
|
|
147
|
+
|
|
148
|
+
def mysql_correct_version?
|
|
149
|
+
database_version > '5.7.7'
|
|
150
|
+
end
|
|
151
|
+
|
|
152
|
+
def database_name
|
|
153
|
+
ActiveRecord::Base.connection.class.name
|
|
154
|
+
end
|
|
155
|
+
|
|
156
|
+
def database_version
|
|
157
|
+
ActiveRecord::Base.connection.select_value('SELECT VERSION()')
|
|
158
|
+
end
|
|
159
|
+
end
|
|
160
|
+
end
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
module DeviseTokenAuth
|
|
2
|
+
class InstallViewsGenerator < Rails::Generators::Base
|
|
3
|
+
source_root File.expand_path('../../../../app/views/devise/mailer', __FILE__)
|
|
4
|
+
|
|
5
|
+
def copy_mailer_templates
|
|
6
|
+
copy_file(
|
|
7
|
+
"confirmation_instructions.html.erb",
|
|
8
|
+
"app/views/devise/mailer/confirmation_instructions.html.erb"
|
|
9
|
+
)
|
|
10
|
+
copy_file(
|
|
11
|
+
"reset_password_instructions.html.erb",
|
|
12
|
+
"app/views/devise/mailer/reset_password_instructions.html.erb"
|
|
13
|
+
)
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
end
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
DeviseTokenAuth.setup do |config|
|
|
2
|
+
# By default the authorization headers will change after each request. The
|
|
3
|
+
# client is responsible for keeping track of the changing tokens. Change
|
|
4
|
+
# this to false to prevent the Authorization header from changing after
|
|
5
|
+
# each request.
|
|
6
|
+
# config.change_headers_on_each_request = true
|
|
7
|
+
|
|
8
|
+
# By default, users will need to re-authenticate after 2 weeks. This setting
|
|
9
|
+
# determines how long tokens will remain valid after they are issued.
|
|
10
|
+
# config.token_lifespan = 2.weeks
|
|
11
|
+
|
|
12
|
+
# Sets the max number of concurrent devices per user, which is 10 by default.
|
|
13
|
+
# After this limit is reached, the oldest tokens will be removed.
|
|
14
|
+
# config.max_number_of_devices = 10
|
|
15
|
+
|
|
16
|
+
# Sometimes it's necessary to make several requests to the API at the same
|
|
17
|
+
# time. In this case, each request in the batch will need to share the same
|
|
18
|
+
# auth token. This setting determines how far apart the requests can be while
|
|
19
|
+
# still using the same auth token.
|
|
20
|
+
# config.batch_request_buffer_throttle = 5.seconds
|
|
21
|
+
|
|
22
|
+
# This route will be the prefix for all oauth2 redirect callbacks. For
|
|
23
|
+
# example, using the default '/omniauth', the github oauth2 provider will
|
|
24
|
+
# redirect successful authentications to '/omniauth/github/callback'
|
|
25
|
+
# config.omniauth_prefix = "/omniauth"
|
|
26
|
+
|
|
27
|
+
# By default sending current password is not needed for the password update.
|
|
28
|
+
# Uncomment to enforce current_password param to be checked before all
|
|
29
|
+
# attribute updates. Set it to :password if you want it to be checked only if
|
|
30
|
+
# password is updated.
|
|
31
|
+
# config.check_current_password_before_update = :attributes
|
|
32
|
+
|
|
33
|
+
# By default we will use callbacks for single omniauth.
|
|
34
|
+
# It depends on fields like email, provider and uid.
|
|
35
|
+
# config.default_callbacks = true
|
|
36
|
+
|
|
37
|
+
# Makes it possible to change the headers names
|
|
38
|
+
# config.headers_names = {:'access-token' => 'access-token',
|
|
39
|
+
# :'client' => 'client',
|
|
40
|
+
# :'expiry' => 'expiry',
|
|
41
|
+
# :'uid' => 'uid',
|
|
42
|
+
# :'token-type' => 'token-type' }
|
|
43
|
+
|
|
44
|
+
# By default, only Bearer Token authentication is implemented out of the box.
|
|
45
|
+
# If, however, you wish to integrate with legacy Devise authentication, you can
|
|
46
|
+
# do so by enabling this flag. NOTE: This feature is highly experimental!
|
|
47
|
+
# config.enable_standard_devise_support = false
|
|
48
|
+
end
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
class DeviseTokenAuthCreate<%= user_class.pluralize %> < ActiveRecord::Migration<%= "[#{Rails::VERSION::STRING[0..2]}]" if Rails::VERSION::MAJOR > 4 %>
|
|
2
|
+
def change
|
|
3
|
+
create_table(:<%= user_class.pluralize.underscore %>) do |t|
|
|
4
|
+
## Required
|
|
5
|
+
t.string :provider, :null => false, :default => "email"
|
|
6
|
+
t.string :uid, :null => false, :default => ""
|
|
7
|
+
|
|
8
|
+
## Database authenticatable
|
|
9
|
+
t.string :encrypted_password, :null => false, :default => ""
|
|
10
|
+
|
|
11
|
+
## Recoverable
|
|
12
|
+
t.string :reset_password_token
|
|
13
|
+
t.datetime :reset_password_sent_at
|
|
14
|
+
t.boolean :allow_password_change, :default => false
|
|
15
|
+
|
|
16
|
+
## Rememberable
|
|
17
|
+
t.datetime :remember_created_at
|
|
18
|
+
|
|
19
|
+
## Trackable
|
|
20
|
+
t.integer :sign_in_count, :default => 0, :null => false
|
|
21
|
+
t.datetime :current_sign_in_at
|
|
22
|
+
t.datetime :last_sign_in_at
|
|
23
|
+
t.string :current_sign_in_ip
|
|
24
|
+
t.string :last_sign_in_ip
|
|
25
|
+
|
|
26
|
+
## Confirmable
|
|
27
|
+
t.string :confirmation_token
|
|
28
|
+
t.datetime :confirmed_at
|
|
29
|
+
t.datetime :confirmation_sent_at
|
|
30
|
+
t.string :unconfirmed_email # Only if using reconfirmable
|
|
31
|
+
|
|
32
|
+
## Lockable
|
|
33
|
+
# t.integer :failed_attempts, :default => 0, :null => false # Only if lock strategy is :failed_attempts
|
|
34
|
+
# t.string :unlock_token # Only if unlock strategy is :email or :both
|
|
35
|
+
# t.datetime :locked_at
|
|
36
|
+
|
|
37
|
+
## User Info
|
|
38
|
+
t.string :name
|
|
39
|
+
t.string :nickname
|
|
40
|
+
t.string :image
|
|
41
|
+
t.string :email
|
|
42
|
+
|
|
43
|
+
## Tokens
|
|
44
|
+
<%= json_supported_database? ? 't.json :tokens' : 't.text :tokens' %>
|
|
45
|
+
|
|
46
|
+
t.timestamps
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
add_index :<%= user_class.pluralize.underscore %>, :email, unique: true
|
|
50
|
+
add_index :<%= user_class.pluralize.underscore %>, [:uid, :provider], unique: true
|
|
51
|
+
add_index :<%= user_class.pluralize.underscore %>, :reset_password_token, unique: true
|
|
52
|
+
add_index :<%= user_class.pluralize.underscore %>, :confirmation_token, unique: true
|
|
53
|
+
# add_index :<%= user_class.pluralize.underscore %>, :unlock_token, unique: true
|
|
54
|
+
end
|
|
55
|
+
end
|