devise_token_auth_multitenancy 1.1.3.alpha1
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 +103 -0
- data/Rakefile +42 -0
- data/app/controllers/devise_token_auth/application_controller.rb +79 -0
- data/app/controllers/devise_token_auth/concerns/resource_finder.rb +44 -0
- data/app/controllers/devise_token_auth/concerns/set_user_by_token.rb +162 -0
- data/app/controllers/devise_token_auth/confirmations_controller.rb +82 -0
- data/app/controllers/devise_token_auth/omniauth_callbacks_controller.rb +287 -0
- data/app/controllers/devise_token_auth/passwords_controller.rb +206 -0
- data/app/controllers/devise_token_auth/registrations_controller.rb +205 -0
- data/app/controllers/devise_token_auth/sessions_controller.rb +131 -0
- data/app/controllers/devise_token_auth/token_validations_controller.rb +31 -0
- data/app/controllers/devise_token_auth/unlocks_controller.rb +89 -0
- data/app/models/devise_token_auth/concerns/active_record_support.rb +16 -0
- data/app/models/devise_token_auth/concerns/confirmable_support.rb +27 -0
- data/app/models/devise_token_auth/concerns/mongoid_support.rb +19 -0
- data/app/models/devise_token_auth/concerns/tokens_serialization.rb +19 -0
- data/app/models/devise_token_auth/concerns/user.rb +257 -0
- data/app/models/devise_token_auth/concerns/user_omniauth_callbacks.rb +28 -0
- data/app/validators/devise_token_auth_email_validator.rb +23 -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/locales/da-DK.yml +52 -0
- data/config/locales/de.yml +51 -0
- data/config/locales/en.yml +57 -0
- data/config/locales/es.yml +51 -0
- data/config/locales/fr.yml +51 -0
- data/config/locales/he.yml +52 -0
- data/config/locales/it.yml +48 -0
- data/config/locales/ja.yml +48 -0
- data/config/locales/ko.yml +51 -0
- data/config/locales/nl.yml +32 -0
- data/config/locales/pl.yml +51 -0
- data/config/locales/pt-BR.yml +48 -0
- data/config/locales/pt.yml +51 -0
- data/config/locales/ro.yml +48 -0
- data/config/locales/ru.yml +52 -0
- data/config/locales/sq.yml +48 -0
- data/config/locales/sv.yml +52 -0
- data/config/locales/uk.yml +61 -0
- data/config/locales/vi.yml +52 -0
- data/config/locales/zh-CN.yml +48 -0
- data/config/locales/zh-HK.yml +50 -0
- data/config/locales/zh-TW.yml +50 -0
- data/lib/devise_token_auth/blacklist.rb +2 -0
- data/lib/devise_token_auth/controllers/helpers.rb +161 -0
- data/lib/devise_token_auth/controllers/url_helpers.rb +10 -0
- data/lib/devise_token_auth/engine.rb +96 -0
- data/lib/devise_token_auth/errors.rb +8 -0
- data/lib/devise_token_auth/rails/routes.rb +116 -0
- data/lib/devise_token_auth/token_factory.rb +126 -0
- data/lib/devise_token_auth/url.rb +44 -0
- data/lib/devise_token_auth/version.rb +5 -0
- data/lib/devise_token_auth.rb +14 -0
- data/lib/generators/devise_token_auth/USAGE +31 -0
- data/lib/generators/devise_token_auth/install_generator.rb +91 -0
- data/lib/generators/devise_token_auth/install_generator_helpers.rb +98 -0
- data/lib/generators/devise_token_auth/install_mongoid_generator.rb +46 -0
- data/lib/generators/devise_token_auth/install_views_generator.rb +18 -0
- data/lib/generators/devise_token_auth/templates/devise_token_auth.rb +60 -0
- data/lib/generators/devise_token_auth/templates/devise_token_auth_create_users.rb.erb +49 -0
- data/lib/generators/devise_token_auth/templates/user.rb.erb +9 -0
- data/lib/generators/devise_token_auth/templates/user_mongoid.rb.erb +56 -0
- data/lib/tasks/devise_token_auth_tasks.rake +6 -0
- data/test/controllers/custom/custom_confirmations_controller_test.rb +25 -0
- data/test/controllers/custom/custom_omniauth_callbacks_controller_test.rb +33 -0
- data/test/controllers/custom/custom_passwords_controller_test.rb +79 -0
- data/test/controllers/custom/custom_registrations_controller_test.rb +63 -0
- data/test/controllers/custom/custom_sessions_controller_test.rb +39 -0
- data/test/controllers/custom/custom_token_validations_controller_test.rb +42 -0
- data/test/controllers/demo_group_controller_test.rb +151 -0
- data/test/controllers/demo_mang_controller_test.rb +284 -0
- data/test/controllers/demo_user_controller_test.rb +629 -0
- data/test/controllers/devise_token_auth/confirmations_controller_test.rb +191 -0
- data/test/controllers/devise_token_auth/omniauth_callbacks_controller_test.rb +441 -0
- data/test/controllers/devise_token_auth/passwords_controller_test.rb +780 -0
- data/test/controllers/devise_token_auth/registrations_controller_test.rb +907 -0
- data/test/controllers/devise_token_auth/sessions_controller_test.rb +503 -0
- data/test/controllers/devise_token_auth/token_validations_controller_test.rb +102 -0
- data/test/controllers/devise_token_auth/unlocks_controller_test.rb +196 -0
- data/test/controllers/overrides/confirmations_controller_test.rb +47 -0
- data/test/controllers/overrides/omniauth_callbacks_controller_test.rb +53 -0
- data/test/controllers/overrides/passwords_controller_test.rb +64 -0
- data/test/controllers/overrides/registrations_controller_test.rb +46 -0
- data/test/controllers/overrides/sessions_controller_test.rb +35 -0
- data/test/controllers/overrides/token_validations_controller_test.rb +43 -0
- data/test/dummy/README.rdoc +28 -0
- data/test/dummy/app/active_record/confirmable_user.rb +11 -0
- data/test/dummy/app/active_record/lockable_user.rb +7 -0
- data/test/dummy/app/active_record/mang.rb +5 -0
- data/test/dummy/app/active_record/only_email_user.rb +7 -0
- data/test/dummy/app/active_record/scoped_user.rb +9 -0
- data/test/dummy/app/active_record/unconfirmable_user.rb +9 -0
- data/test/dummy/app/active_record/unregisterable_user.rb +9 -0
- data/test/dummy/app/active_record/user.rb +6 -0
- data/test/dummy/app/controllers/application_controller.rb +18 -0
- data/test/dummy/app/controllers/auth_origin_controller.rb +7 -0
- data/test/dummy/app/controllers/custom/confirmations_controller.rb +13 -0
- data/test/dummy/app/controllers/custom/omniauth_callbacks_controller.rb +13 -0
- data/test/dummy/app/controllers/custom/passwords_controller.rb +39 -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 +15 -0
- data/test/dummy/app/controllers/demo_mang_controller.rb +14 -0
- data/test/dummy/app/controllers/demo_user_controller.rb +27 -0
- data/test/dummy/app/controllers/overrides/confirmations_controller.rb +28 -0
- data/test/dummy/app/controllers/overrides/omniauth_callbacks_controller.rb +16 -0
- data/test/dummy/app/controllers/overrides/passwords_controller.rb +35 -0
- data/test/dummy/app/controllers/overrides/registrations_controller.rb +29 -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 +1058 -0
- data/test/dummy/app/models/concerns/favorite_color.rb +19 -0
- data/test/dummy/app/mongoid/confirmable_user.rb +52 -0
- data/test/dummy/app/mongoid/lockable_user.rb +38 -0
- data/test/dummy/app/mongoid/mang.rb +46 -0
- data/test/dummy/app/mongoid/only_email_user.rb +33 -0
- data/test/dummy/app/mongoid/scoped_user.rb +50 -0
- data/test/dummy/app/mongoid/unconfirmable_user.rb +44 -0
- data/test/dummy/app/mongoid/unregisterable_user.rb +47 -0
- data/test/dummy/app/mongoid/user.rb +49 -0
- data/test/dummy/app/views/layouts/application.html.erb +14 -0
- data/test/dummy/config/application.rb +48 -0
- data/test/dummy/config/application.yml.bk +0 -0
- data/test/dummy/config/boot.rb +11 -0
- data/test/dummy/config/environment.rb +7 -0
- data/test/dummy/config/environments/development.rb +46 -0
- data/test/dummy/config/environments/production.rb +84 -0
- data/test/dummy/config/environments/test.rb +50 -0
- data/test/dummy/config/initializers/assets.rb +10 -0
- data/test/dummy/config/initializers/backtrace_silencers.rb +9 -0
- data/test/dummy/config/initializers/cookies_serializer.rb +5 -0
- data/test/dummy/config/initializers/devise.rb +290 -0
- data/test/dummy/config/initializers/devise_token_auth.rb +55 -0
- data/test/dummy/config/initializers/figaro.rb +3 -0
- data/test/dummy/config/initializers/filter_parameter_logging.rb +6 -0
- data/test/dummy/config/initializers/inflections.rb +18 -0
- data/test/dummy/config/initializers/mime_types.rb +6 -0
- data/test/dummy/config/initializers/omniauth.rb +11 -0
- data/test/dummy/config/initializers/session_store.rb +5 -0
- data/test/dummy/config/initializers/wrap_parameters.rb +16 -0
- data/test/dummy/config/routes.rb +57 -0
- data/test/dummy/config/spring.rb +3 -0
- data/test/dummy/config.ru +18 -0
- data/test/dummy/db/migrate/20140715061447_devise_token_auth_create_users.rb +58 -0
- data/test/dummy/db/migrate/20140715061805_devise_token_auth_create_mangs.rb +57 -0
- data/test/dummy/db/migrate/20140829044006_add_operating_thetan_to_user.rb +8 -0
- data/test/dummy/db/migrate/20140916224624_add_favorite_color_to_mangs.rb +7 -0
- data/test/dummy/db/migrate/20141222035835_devise_token_auth_create_only_email_users.rb +55 -0
- data/test/dummy/db/migrate/20141222053502_devise_token_auth_create_unregisterable_users.rb +56 -0
- data/test/dummy/db/migrate/20150708104536_devise_token_auth_create_unconfirmable_users.rb +56 -0
- data/test/dummy/db/migrate/20160103235141_devise_token_auth_create_scoped_users.rb +56 -0
- data/test/dummy/db/migrate/20160629184441_devise_token_auth_create_lockable_users.rb +56 -0
- data/test/dummy/db/migrate/20190924101113_devise_token_auth_create_confirmable_users.rb +49 -0
- data/test/dummy/db/schema.rb +198 -0
- data/test/dummy/lib/migration_database_helper.rb +43 -0
- data/test/factories/users.rb +41 -0
- data/test/lib/devise_token_auth/blacklist_test.rb +11 -0
- data/test/lib/devise_token_auth/token_factory_test.rb +191 -0
- data/test/lib/devise_token_auth/url_test.rb +26 -0
- data/test/lib/generators/devise_token_auth/install_generator_test.rb +217 -0
- data/test/lib/generators/devise_token_auth/install_generator_with_namespace_test.rb +222 -0
- data/test/lib/generators/devise_token_auth/install_views_generator_test.rb +25 -0
- data/test/models/concerns/mongoid_support_test.rb +31 -0
- data/test/models/concerns/tokens_serialization_test.rb +70 -0
- data/test/models/confirmable_user_test.rb +35 -0
- data/test/models/only_email_user_test.rb +29 -0
- data/test/models/user_test.rb +108 -0
- data/test/support/controllers/routes.rb +43 -0
- data/test/test_helper.rb +103 -0
- metadata +483 -0
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative 'install_generator_helpers'
|
|
4
|
+
|
|
5
|
+
module DeviseTokenAuth
|
|
6
|
+
class InstallGenerator < Rails::Generators::Base
|
|
7
|
+
include Rails::Generators::Migration
|
|
8
|
+
include DeviseTokenAuth::InstallGeneratorHelpers
|
|
9
|
+
|
|
10
|
+
class_option :primary_key_type, type: :string, desc: 'The type for primary key'
|
|
11
|
+
|
|
12
|
+
def copy_migrations
|
|
13
|
+
if self.class.migration_exists?('db/migrate', "devise_token_auth_create_#{user_class.pluralize.gsub('::','').underscore}")
|
|
14
|
+
say_status('skipped', "Migration 'devise_token_auth_create_#{user_class.pluralize.gsub('::','').underscore}' already exists")
|
|
15
|
+
else
|
|
16
|
+
migration_template(
|
|
17
|
+
'devise_token_auth_create_users.rb.erb',
|
|
18
|
+
"db/migrate/devise_token_auth_create_#{user_class.pluralize.gsub('::','').underscore}.rb"
|
|
19
|
+
)
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def create_user_model
|
|
24
|
+
fname = "app/models/#{user_class.underscore}.rb"
|
|
25
|
+
if File.exist?(File.join(destination_root, fname))
|
|
26
|
+
inclusion = 'include DeviseTokenAuth::Concerns::User'
|
|
27
|
+
unless parse_file_for_line(fname, inclusion)
|
|
28
|
+
|
|
29
|
+
active_record_needle = (Rails::VERSION::MAJOR == 5) ? 'ApplicationRecord' : 'ActiveRecord::Base'
|
|
30
|
+
inject_into_file fname, after: "class #{user_class} < #{active_record_needle}\n" do <<-'RUBY'
|
|
31
|
+
# Include default devise modules.
|
|
32
|
+
devise :database_authenticatable, :registerable,
|
|
33
|
+
:recoverable, :rememberable, :trackable, :validatable,
|
|
34
|
+
:confirmable, :omniauthable
|
|
35
|
+
include DeviseTokenAuth::Concerns::User
|
|
36
|
+
RUBY
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
else
|
|
40
|
+
template('user.rb.erb', fname)
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
private
|
|
45
|
+
|
|
46
|
+
def self.next_migration_number(path)
|
|
47
|
+
Time.zone.now.utc.strftime('%Y%m%d%H%M%S')
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
def json_supported_database?
|
|
51
|
+
(postgres? && postgres_correct_version?) || (mysql? && mysql_correct_version?)
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
def postgres?
|
|
55
|
+
database_name == 'ActiveRecord::ConnectionAdapters::PostgreSQLAdapter'
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
def postgres_correct_version?
|
|
59
|
+
database_version > '9.3'
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
def mysql?
|
|
63
|
+
database_name == 'ActiveRecord::ConnectionAdapters::MysqlAdapter'
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
def mysql_correct_version?
|
|
67
|
+
database_version > '5.7.7'
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
def database_name
|
|
71
|
+
ActiveRecord::Base.connection.class.name
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
def database_version
|
|
75
|
+
ActiveRecord::Base.connection.select_value('SELECT VERSION()')
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
def rails_5_or_newer?
|
|
79
|
+
Rails::VERSION::MAJOR >= 5
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
def primary_key_type
|
|
83
|
+
primary_key_string if rails_5_or_newer?
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
def primary_key_string
|
|
87
|
+
key_string = options[:primary_key_type]
|
|
88
|
+
", id: :#{key_string}" if key_string
|
|
89
|
+
end
|
|
90
|
+
end
|
|
91
|
+
end
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
module DeviseTokenAuth
|
|
2
|
+
module InstallGeneratorHelpers
|
|
3
|
+
class << self
|
|
4
|
+
def included(mod)
|
|
5
|
+
mod.class_eval do
|
|
6
|
+
source_root File.expand_path('templates', __dir__)
|
|
7
|
+
|
|
8
|
+
argument :user_class, type: :string, default: 'User'
|
|
9
|
+
argument :mount_path, type: :string, default: 'auth'
|
|
10
|
+
|
|
11
|
+
def create_initializer_file
|
|
12
|
+
copy_file('devise_token_auth.rb', 'config/initializers/devise_token_auth.rb')
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def include_controller_concerns
|
|
16
|
+
fname = 'app/controllers/application_controller.rb'
|
|
17
|
+
line = 'include DeviseTokenAuth::Concerns::SetUserByToken'
|
|
18
|
+
|
|
19
|
+
if File.exist?(File.join(destination_root, fname))
|
|
20
|
+
if parse_file_for_line(fname, line)
|
|
21
|
+
say_status('skipped', 'Concern is already included in the application controller.')
|
|
22
|
+
elsif is_rails_api?
|
|
23
|
+
inject_into_file fname, after: "class ApplicationController < ActionController::API\n" do <<-'RUBY'
|
|
24
|
+
include DeviseTokenAuth::Concerns::SetUserByToken
|
|
25
|
+
RUBY
|
|
26
|
+
end
|
|
27
|
+
else
|
|
28
|
+
inject_into_file fname, after: "class ApplicationController < ActionController::Base\n" do <<-'RUBY'
|
|
29
|
+
include DeviseTokenAuth::Concerns::SetUserByToken
|
|
30
|
+
RUBY
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
else
|
|
34
|
+
say_status('skipped', "app/controllers/application_controller.rb not found. Add 'include DeviseTokenAuth::Concerns::SetUserByToken' to any controllers that require authentication.")
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def add_route_mount
|
|
39
|
+
f = 'config/routes.rb'
|
|
40
|
+
str = "mount_devise_token_auth_for '#{user_class}', at: '#{mount_path}'"
|
|
41
|
+
|
|
42
|
+
if File.exist?(File.join(destination_root, f))
|
|
43
|
+
line = parse_file_for_line(f, 'mount_devise_token_auth_for')
|
|
44
|
+
|
|
45
|
+
if line
|
|
46
|
+
existing_user_class = true
|
|
47
|
+
else
|
|
48
|
+
line = 'Rails.application.routes.draw do'
|
|
49
|
+
existing_user_class = false
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
if parse_file_for_line(f, str)
|
|
53
|
+
say_status('skipped', "Routes already exist for #{user_class} at #{mount_path}")
|
|
54
|
+
else
|
|
55
|
+
insert_after_line(f, line, str)
|
|
56
|
+
|
|
57
|
+
if existing_user_class
|
|
58
|
+
scoped_routes = ''\
|
|
59
|
+
"as :#{user_class.underscore} do\n"\
|
|
60
|
+
" # Define routes for #{user_class} within this block.\n"\
|
|
61
|
+
" end\n"
|
|
62
|
+
insert_after_line(f, str, scoped_routes)
|
|
63
|
+
end
|
|
64
|
+
end
|
|
65
|
+
else
|
|
66
|
+
say_status('skipped', "config/routes.rb not found. Add \"mount_devise_token_auth_for '#{user_class}', at: '#{mount_path}'\" to your routes file.")
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
private
|
|
71
|
+
|
|
72
|
+
def insert_after_line(filename, line, str)
|
|
73
|
+
gsub_file filename, /(#{Regexp.escape(line)})/mi do |match|
|
|
74
|
+
"#{match}\n #{str}"
|
|
75
|
+
end
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
def parse_file_for_line(filename, str)
|
|
79
|
+
match = false
|
|
80
|
+
|
|
81
|
+
File.open(File.join(destination_root, filename)) do |f|
|
|
82
|
+
f.each_line do |line|
|
|
83
|
+
match = line if line =~ /(#{Regexp.escape(str)})/mi
|
|
84
|
+
end
|
|
85
|
+
end
|
|
86
|
+
match
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
def is_rails_api?
|
|
90
|
+
fname = 'app/controllers/application_controller.rb'
|
|
91
|
+
line = 'class ApplicationController < ActionController::API'
|
|
92
|
+
parse_file_for_line(fname, line)
|
|
93
|
+
end
|
|
94
|
+
end
|
|
95
|
+
end
|
|
96
|
+
end
|
|
97
|
+
end
|
|
98
|
+
end
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative 'install_generator_helpers'
|
|
4
|
+
|
|
5
|
+
module DeviseTokenAuth
|
|
6
|
+
class InstallMongoidGenerator < Rails::Generators::Base
|
|
7
|
+
include DeviseTokenAuth::InstallGeneratorHelpers
|
|
8
|
+
|
|
9
|
+
def create_user_model
|
|
10
|
+
fname = "app/models/#{user_class.underscore}.rb"
|
|
11
|
+
if File.exist?(File.join(destination_root, fname))
|
|
12
|
+
inclusion = 'include DeviseTokenAuth::Concerns::User'
|
|
13
|
+
unless parse_file_for_line(fname, inclusion)
|
|
14
|
+
inject_into_file fname, before: /end\s\z/ do <<-'RUBY'
|
|
15
|
+
|
|
16
|
+
include Mongoid::Locker
|
|
17
|
+
|
|
18
|
+
field :locker_locked_at, type: Time
|
|
19
|
+
field :locker_locked_until, type: Time
|
|
20
|
+
|
|
21
|
+
locker locked_at_field: :locker_locked_at,
|
|
22
|
+
locked_until_field: :locker_locked_until
|
|
23
|
+
|
|
24
|
+
## Required
|
|
25
|
+
field :provider, type: String
|
|
26
|
+
field :uid, type: String, default: ''
|
|
27
|
+
|
|
28
|
+
## Tokens
|
|
29
|
+
field :tokens, type: Hash, default: {}
|
|
30
|
+
|
|
31
|
+
# Include default devise modules. Others available are:
|
|
32
|
+
# :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
|
|
33
|
+
devise :database_authenticatable, :registerable,
|
|
34
|
+
:recoverable, :rememberable, :validatable
|
|
35
|
+
include DeviseTokenAuth::Concerns::User
|
|
36
|
+
|
|
37
|
+
index({ uid: 1, provider: 1}, { name: 'uid_provider_index', unique: true, background: true })
|
|
38
|
+
RUBY
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
else
|
|
42
|
+
template('user_mongoid.rb.erb', fname)
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
end
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module DeviseTokenAuth
|
|
4
|
+
class InstallViewsGenerator < Rails::Generators::Base
|
|
5
|
+
source_root File.expand_path('../../../app/views/devise/mailer', __dir__)
|
|
6
|
+
|
|
7
|
+
def copy_mailer_templates
|
|
8
|
+
copy_file(
|
|
9
|
+
'confirmation_instructions.html.erb',
|
|
10
|
+
'app/views/devise/mailer/confirmation_instructions.html.erb'
|
|
11
|
+
)
|
|
12
|
+
copy_file(
|
|
13
|
+
'reset_password_instructions.html.erb',
|
|
14
|
+
'app/views/devise/mailer/reset_password_instructions.html.erb'
|
|
15
|
+
)
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
end
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
DeviseTokenAuth.setup do |config|
|
|
4
|
+
# By default the authorization headers will change after each request. The
|
|
5
|
+
# client is responsible for keeping track of the changing tokens. Change
|
|
6
|
+
# this to false to prevent the Authorization header from changing after
|
|
7
|
+
# each request.
|
|
8
|
+
# config.change_headers_on_each_request = true
|
|
9
|
+
|
|
10
|
+
# By default, users will need to re-authenticate after 2 weeks. This setting
|
|
11
|
+
# determines how long tokens will remain valid after they are issued.
|
|
12
|
+
# config.token_lifespan = 2.weeks
|
|
13
|
+
|
|
14
|
+
# Limiting the token_cost to just 4 in testing will increase the performance of
|
|
15
|
+
# your test suite dramatically. The possible cost value is within range from 4
|
|
16
|
+
# to 31. It is recommended to not use a value more than 10 in other environments.
|
|
17
|
+
config.token_cost = Rails.env.test? ? 4 : 10
|
|
18
|
+
|
|
19
|
+
# Sets the max number of concurrent devices per user, which is 10 by default.
|
|
20
|
+
# After this limit is reached, the oldest tokens will be removed.
|
|
21
|
+
# config.max_number_of_devices = 10
|
|
22
|
+
|
|
23
|
+
# Sometimes it's necessary to make several requests to the API at the same
|
|
24
|
+
# time. In this case, each request in the batch will need to share the same
|
|
25
|
+
# auth token. This setting determines how far apart the requests can be while
|
|
26
|
+
# still using the same auth token.
|
|
27
|
+
# config.batch_request_buffer_throttle = 5.seconds
|
|
28
|
+
|
|
29
|
+
# This route will be the prefix for all oauth2 redirect callbacks. For
|
|
30
|
+
# example, using the default '/omniauth', the github oauth2 provider will
|
|
31
|
+
# redirect successful authentications to '/omniauth/github/callback'
|
|
32
|
+
# config.omniauth_prefix = "/omniauth"
|
|
33
|
+
|
|
34
|
+
# By default sending current password is not needed for the password update.
|
|
35
|
+
# Uncomment to enforce current_password param to be checked before all
|
|
36
|
+
# attribute updates. Set it to :password if you want it to be checked only if
|
|
37
|
+
# password is updated.
|
|
38
|
+
# config.check_current_password_before_update = :attributes
|
|
39
|
+
|
|
40
|
+
# By default we will use callbacks for single omniauth.
|
|
41
|
+
# It depends on fields like email, provider and uid.
|
|
42
|
+
# config.default_callbacks = true
|
|
43
|
+
|
|
44
|
+
# Makes it possible to change the headers names
|
|
45
|
+
# config.headers_names = {:'access-token' => 'access-token',
|
|
46
|
+
# :'client' => 'client',
|
|
47
|
+
# :'expiry' => 'expiry',
|
|
48
|
+
# :'uid' => 'uid',
|
|
49
|
+
# :'token-type' => 'token-type' }
|
|
50
|
+
|
|
51
|
+
# By default, only Bearer Token authentication is implemented out of the box.
|
|
52
|
+
# If, however, you wish to integrate with legacy Devise authentication, you can
|
|
53
|
+
# do so by enabling this flag. NOTE: This feature is highly experimental!
|
|
54
|
+
# config.enable_standard_devise_support = false
|
|
55
|
+
|
|
56
|
+
# By default DeviseTokenAuth will not send confirmation email, even when including
|
|
57
|
+
# devise confirmable module. If you want to use devise confirmable module and
|
|
58
|
+
# send email, set it to true. (This is a setting for compatibility)
|
|
59
|
+
# config.send_confirmation_email = true
|
|
60
|
+
end
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
class DeviseTokenAuthCreate<%= user_class.pluralize.gsub("::","") %> < ActiveRecord::Migration<%= "[#{Rails::VERSION::STRING[0..2]}]" if Rails::VERSION::MAJOR > 4 %>
|
|
2
|
+
def change
|
|
3
|
+
<% table_name = @user_class.pluralize.gsub("::","").underscore %>
|
|
4
|
+
create_table(:<%= table_name %><%= primary_key_type %>) do |t|
|
|
5
|
+
## Required
|
|
6
|
+
t.string :provider, :null => false, :default => "email"
|
|
7
|
+
t.string :uid, :null => false, :default => ""
|
|
8
|
+
|
|
9
|
+
## Database authenticatable
|
|
10
|
+
t.string :encrypted_password, :null => false, :default => ""
|
|
11
|
+
|
|
12
|
+
## Recoverable
|
|
13
|
+
t.string :reset_password_token
|
|
14
|
+
t.datetime :reset_password_sent_at
|
|
15
|
+
t.boolean :allow_password_change, :default => false
|
|
16
|
+
|
|
17
|
+
## Rememberable
|
|
18
|
+
t.datetime :remember_created_at
|
|
19
|
+
|
|
20
|
+
## Confirmable
|
|
21
|
+
t.string :confirmation_token
|
|
22
|
+
t.datetime :confirmed_at
|
|
23
|
+
t.datetime :confirmation_sent_at
|
|
24
|
+
t.string :unconfirmed_email # Only if using reconfirmable
|
|
25
|
+
|
|
26
|
+
## Lockable
|
|
27
|
+
# t.integer :failed_attempts, :default => 0, :null => false # Only if lock strategy is :failed_attempts
|
|
28
|
+
# t.string :unlock_token # Only if unlock strategy is :email or :both
|
|
29
|
+
# t.datetime :locked_at
|
|
30
|
+
|
|
31
|
+
## User Info
|
|
32
|
+
t.string :name
|
|
33
|
+
t.string :nickname
|
|
34
|
+
t.string :image
|
|
35
|
+
t.string :email
|
|
36
|
+
|
|
37
|
+
## Tokens
|
|
38
|
+
<%= json_supported_database? ? 't.json :tokens' : 't.text :tokens' %>
|
|
39
|
+
|
|
40
|
+
t.timestamps
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
add_index :<%= table_name %>, :email, unique: true
|
|
44
|
+
add_index :<%= table_name %>, [:uid, :provider], unique: true
|
|
45
|
+
add_index :<%= table_name %>, :reset_password_token, unique: true
|
|
46
|
+
add_index :<%= table_name %>, :confirmation_token, unique: true
|
|
47
|
+
# add_index :<%= table_name %>, :unlock_token, unique: true
|
|
48
|
+
end
|
|
49
|
+
end
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
class <%= user_class %> < ActiveRecord::Base
|
|
4
|
+
# Include default devise modules. Others available are:
|
|
5
|
+
# :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
|
|
6
|
+
devise :database_authenticatable, :registerable,
|
|
7
|
+
:recoverable, :rememberable, :validatable
|
|
8
|
+
include DeviseTokenAuth::Concerns::User
|
|
9
|
+
end
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
class <%= user_class %>
|
|
4
|
+
include Mongoid::Document
|
|
5
|
+
include Mongoid::Timestamps
|
|
6
|
+
include Mongoid::Locker
|
|
7
|
+
|
|
8
|
+
field :locker_locked_at, type: Time
|
|
9
|
+
field :locker_locked_until, type: Time
|
|
10
|
+
|
|
11
|
+
locker locked_at_field: :locker_locked_at,
|
|
12
|
+
locked_until_field: :locker_locked_until
|
|
13
|
+
|
|
14
|
+
## Database authenticatable
|
|
15
|
+
field :email, type: String, default: ''
|
|
16
|
+
field :encrypted_password, type: String, default: ''
|
|
17
|
+
|
|
18
|
+
## Recoverable
|
|
19
|
+
field :reset_password_token, type: String
|
|
20
|
+
field :reset_password_sent_at, type: Time
|
|
21
|
+
field :reset_password_redirect_url, type: String
|
|
22
|
+
field :allow_password_change, type: Boolean, default: false
|
|
23
|
+
|
|
24
|
+
## Rememberable
|
|
25
|
+
field :remember_created_at, type: Time
|
|
26
|
+
|
|
27
|
+
## Confirmable
|
|
28
|
+
field :confirmation_token, type: String
|
|
29
|
+
field :confirmed_at, type: Time
|
|
30
|
+
field :confirmation_sent_at, type: Time
|
|
31
|
+
field :unconfirmed_email, type: String # Only if using reconfirmable
|
|
32
|
+
|
|
33
|
+
## Lockable
|
|
34
|
+
# field :failed_attempts, type: Integer, default: 0 # Only if lock strategy is :failed_attempts
|
|
35
|
+
# field :unlock_token, type: String # Only if unlock strategy is :email or :both
|
|
36
|
+
# field :locked_at, type: Time
|
|
37
|
+
|
|
38
|
+
## Required
|
|
39
|
+
field :provider, type: String
|
|
40
|
+
field :uid, type: String, default: ''
|
|
41
|
+
|
|
42
|
+
## Tokens
|
|
43
|
+
field :tokens, type: Hash, default: {}
|
|
44
|
+
|
|
45
|
+
# Include default devise modules. Others available are:
|
|
46
|
+
# :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
|
|
47
|
+
devise :database_authenticatable, :registerable,
|
|
48
|
+
:recoverable, :rememberable, :validatable
|
|
49
|
+
include DeviseTokenAuth::Concerns::User
|
|
50
|
+
|
|
51
|
+
index({ email: 1 }, { name: 'email_index', unique: true, background: true })
|
|
52
|
+
index({ reset_password_token: 1 }, { name: 'reset_password_token_index', unique: true, sparse: true, background: true })
|
|
53
|
+
index({ confirmation_token: 1 }, { name: 'confirmation_token_index', unique: true, sparse: true, background: true })
|
|
54
|
+
index({ uid: 1, provider: 1}, { name: 'uid_provider_index', unique: true, background: true })
|
|
55
|
+
# index({ unlock_token: 1 }, { name: 'unlock_token_index', unique: true, sparse: true, background: true })
|
|
56
|
+
end
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'test_helper'
|
|
4
|
+
|
|
5
|
+
class Custom::ConfirmationsControllerTest < ActionController::TestCase
|
|
6
|
+
describe Custom::ConfirmationsController do
|
|
7
|
+
include CustomControllersRoutes
|
|
8
|
+
|
|
9
|
+
before do
|
|
10
|
+
@redirect_url = Faker::Internet.url
|
|
11
|
+
@new_user = create(:user)
|
|
12
|
+
@new_user.send_confirmation_instructions(redirect_url: @redirect_url)
|
|
13
|
+
@mail = ActionMailer::Base.deliveries.last
|
|
14
|
+
@token = @mail.body.match(/confirmation_token=([^&]*)&/)[1]
|
|
15
|
+
@client_config = @mail.body.match(/config=([^&]*)&/)[1]
|
|
16
|
+
|
|
17
|
+
get :show,
|
|
18
|
+
params: { confirmation_token: @token, redirect_url: @redirect_url }
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
test 'yield resource to block on show success' do
|
|
22
|
+
assert @controller.show_block_called?, 'show failed to yield resource to provided block'
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'test_helper'
|
|
4
|
+
|
|
5
|
+
class Custom::OmniauthCallbacksControllerTest < ActionDispatch::IntegrationTest
|
|
6
|
+
describe Custom::OmniauthCallbacksController do
|
|
7
|
+
include CustomControllersRoutes
|
|
8
|
+
|
|
9
|
+
setup do
|
|
10
|
+
OmniAuth.config.test_mode = true
|
|
11
|
+
OmniAuth.config.mock_auth[:facebook] = OmniAuth::AuthHash.new(
|
|
12
|
+
provider: 'facebook',
|
|
13
|
+
uid: '123545',
|
|
14
|
+
info: {
|
|
15
|
+
name: 'swong',
|
|
16
|
+
email: 'swongsong@yandex.ru'
|
|
17
|
+
}
|
|
18
|
+
)
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
test 'yield resource to block on omniauth_success success' do
|
|
22
|
+
@redirect_url = 'http://ng-token-auth.dev/'
|
|
23
|
+
get '/nice_user_auth/facebook',
|
|
24
|
+
params: { auth_origin_url: @redirect_url,
|
|
25
|
+
omniauth_window_type: 'newWindow' }
|
|
26
|
+
|
|
27
|
+
follow_all_redirects!
|
|
28
|
+
|
|
29
|
+
assert @controller.omniauth_success_block_called?,
|
|
30
|
+
'omniauth_success failed to yield resource to provided block'
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
end
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'test_helper'
|
|
4
|
+
|
|
5
|
+
class Custom::PasswordsControllerTest < ActionController::TestCase
|
|
6
|
+
describe Custom::PasswordsController do
|
|
7
|
+
include CustomControllersRoutes
|
|
8
|
+
|
|
9
|
+
before do
|
|
10
|
+
@resource = create(:user, :confirmed)
|
|
11
|
+
@redirect_url = 'http://ng-token-auth.dev'
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
test 'yield resource to block on create success' do
|
|
15
|
+
post :create,
|
|
16
|
+
params: { email: @resource.email,
|
|
17
|
+
redirect_url: @redirect_url }
|
|
18
|
+
|
|
19
|
+
@mail = ActionMailer::Base.deliveries.last
|
|
20
|
+
@resource.reload
|
|
21
|
+
|
|
22
|
+
@mail_config_name = CGI.unescape(@mail.body.match(/config=([^&]*)&/)[1])
|
|
23
|
+
@mail_redirect_url = CGI.unescape(@mail.body.match(/redirect_url=([^&]*)&/)[1])
|
|
24
|
+
@mail_reset_token = @mail.body.match(/reset_password_token=(.*)\"/)[1]
|
|
25
|
+
|
|
26
|
+
assert @controller.create_block_called?,
|
|
27
|
+
'create failed to yield resource to provided block'
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
test 'yield resource to block on edit success' do
|
|
31
|
+
@resource = create(:user)
|
|
32
|
+
@redirect_url = 'http://ng-token-auth.dev'
|
|
33
|
+
|
|
34
|
+
post :create,
|
|
35
|
+
params: { email: @resource.email,
|
|
36
|
+
redirect_url: @redirect_url },
|
|
37
|
+
xhr: true
|
|
38
|
+
|
|
39
|
+
@mail = ActionMailer::Base.deliveries.last
|
|
40
|
+
@resource.reload
|
|
41
|
+
|
|
42
|
+
@mail_config_name = CGI.unescape(@mail.body.match(/config=([^&]*)&/)[1])
|
|
43
|
+
@mail_redirect_url = CGI.unescape(@mail.body.match(/redirect_url=([^&]*)&/)[1])
|
|
44
|
+
@mail_reset_token = @mail.body.match(/reset_password_token=(.*)\"/)[1]
|
|
45
|
+
|
|
46
|
+
get :edit,
|
|
47
|
+
params: { reset_password_token: @mail_reset_token,
|
|
48
|
+
redirect_url: @mail_redirect_url },
|
|
49
|
+
xhr: true
|
|
50
|
+
@resource.reload
|
|
51
|
+
assert @controller.edit_block_called?,
|
|
52
|
+
'edit failed to yield resource to provided block'
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
test 'yield resource to block on update success' do
|
|
56
|
+
@auth_headers = @resource.create_new_auth_token
|
|
57
|
+
request.headers.merge!(@auth_headers)
|
|
58
|
+
@new_password = Faker::Internet.password
|
|
59
|
+
put :update,
|
|
60
|
+
params: { password: @new_password,
|
|
61
|
+
password_confirmation: @new_password }
|
|
62
|
+
assert @controller.update_block_called?, 'update failed to yield resource to provided block'
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
test 'yield resource to block on update success with custom json' do
|
|
66
|
+
@auth_headers = @resource.create_new_auth_token
|
|
67
|
+
request.headers.merge!(@auth_headers)
|
|
68
|
+
@new_password = Faker::Internet.password
|
|
69
|
+
put :update,
|
|
70
|
+
params: { password: @new_password,
|
|
71
|
+
password_confirmation: @new_password }
|
|
72
|
+
|
|
73
|
+
@data = JSON.parse(response.body)
|
|
74
|
+
|
|
75
|
+
assert @controller.update_block_called?, 'update failed to yield resource to provided block'
|
|
76
|
+
assert_equal @data['custom'], 'foo'
|
|
77
|
+
end
|
|
78
|
+
end
|
|
79
|
+
end
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'test_helper'
|
|
4
|
+
|
|
5
|
+
class Custom::RegistrationsControllerTest < ActionDispatch::IntegrationTest
|
|
6
|
+
describe Custom::RegistrationsController do
|
|
7
|
+
include CustomControllersRoutes
|
|
8
|
+
|
|
9
|
+
before do
|
|
10
|
+
@create_params = attributes_for(:user,
|
|
11
|
+
confirm_success_url: Faker::Internet.url,
|
|
12
|
+
unpermitted_param: '(x_x)')
|
|
13
|
+
|
|
14
|
+
@existing_user = create(:user, :confirmed)
|
|
15
|
+
@auth_headers = @existing_user.create_new_auth_token
|
|
16
|
+
@client_id = @auth_headers['client']
|
|
17
|
+
|
|
18
|
+
# ensure request is not treated as batch request
|
|
19
|
+
age_token(@existing_user, @client_id)
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
test 'yield resource to block on create success' do
|
|
23
|
+
post '/nice_user_auth', params: @create_params
|
|
24
|
+
assert @controller.create_block_called?,
|
|
25
|
+
'create failed to yield resource to provided block'
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
test 'yield resource to block on create success with custom json' do
|
|
29
|
+
post '/nice_user_auth', params: @create_params
|
|
30
|
+
|
|
31
|
+
@data = JSON.parse(response.body)
|
|
32
|
+
|
|
33
|
+
assert @controller.create_block_called?,
|
|
34
|
+
'create failed to yield resource to provided block'
|
|
35
|
+
assert_equal @data['custom'], 'foo'
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
test 'yield resource to block on update success' do
|
|
39
|
+
put '/nice_user_auth',
|
|
40
|
+
params: {
|
|
41
|
+
nickname: "Ol' Sunshine-face"
|
|
42
|
+
},
|
|
43
|
+
headers: @auth_headers
|
|
44
|
+
assert @controller.update_block_called?,
|
|
45
|
+
'update failed to yield resource to provided block'
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
test 'yield resource to block on destroy success' do
|
|
49
|
+
delete '/nice_user_auth', headers: @auth_headers
|
|
50
|
+
assert @controller.destroy_block_called?,
|
|
51
|
+
'destroy failed to yield resource to provided block'
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
describe 'when overriding #build_resource' do
|
|
55
|
+
test 'it fails' do
|
|
56
|
+
Custom::RegistrationsController.any_instance.stubs(:build_resource).returns(nil)
|
|
57
|
+
assert_raises DeviseTokenAuth::Errors::NoResourceDefinedError do
|
|
58
|
+
post '/nice_user_auth', params: @create_params
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
end
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'test_helper'
|
|
4
|
+
|
|
5
|
+
class Custom::SessionsControllerTest < ActionController::TestCase
|
|
6
|
+
describe Custom::SessionsController do
|
|
7
|
+
include CustomControllersRoutes
|
|
8
|
+
|
|
9
|
+
before do
|
|
10
|
+
@existing_user = create(:user, :confirmed)
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
test 'yield resource to block on create success' do
|
|
14
|
+
post :create,
|
|
15
|
+
params: {
|
|
16
|
+
email: @existing_user.email,
|
|
17
|
+
password: @existing_user.password
|
|
18
|
+
}
|
|
19
|
+
assert @controller.create_block_called?,
|
|
20
|
+
'create failed to yield resource to provided block'
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
test 'yield resource to block on destroy success' do
|
|
24
|
+
@auth_headers = @existing_user.create_new_auth_token
|
|
25
|
+
request.headers.merge!(@auth_headers)
|
|
26
|
+
delete :destroy, format: :json
|
|
27
|
+
assert @controller.destroy_block_called?,
|
|
28
|
+
'destroy failed to yield resource to provided block'
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
test 'render method override' do
|
|
32
|
+
post :create,
|
|
33
|
+
params: { email: @existing_user.email,
|
|
34
|
+
password: @existing_user.password }
|
|
35
|
+
@data = JSON.parse(response.body)
|
|
36
|
+
assert_equal @data['custom'], 'foo'
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
end
|