devise_token 0.1.1

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.
Files changed (37) hide show
  1. checksums.yaml +7 -0
  2. data/MIT-LICENSE +20 -0
  3. data/README.md +69 -0
  4. data/Rakefile +36 -0
  5. data/app/assets/config/devise_token_manifest.js +2 -0
  6. data/app/assets/javascripts/devise_token/application.js +13 -0
  7. data/app/assets/stylesheets/devise_token/application.css +15 -0
  8. data/app/controllers/devise_token/application_controller.rb +77 -0
  9. data/app/controllers/devise_token/authentications_controller.rb +87 -0
  10. data/app/controllers/devise_token/concerns/authenticate_token.rb +71 -0
  11. data/app/controllers/devise_token/confirmations_controller.rb +18 -0
  12. data/app/controllers/devise_token/registrations_controller.rb +189 -0
  13. data/app/controllers/devise_token/token_validations_controller.rb +29 -0
  14. data/app/helpers/devise_token/application_helper.rb +4 -0
  15. data/app/jobs/devise_token/application_job.rb +4 -0
  16. data/app/mailers/devise_token/application_mailer.rb +6 -0
  17. data/app/models/devise_token/application_record.rb +5 -0
  18. data/app/models/devise_token/concerns/user.rb +98 -0
  19. data/app/models/devise_token/json_web_token.rb +80 -0
  20. data/app/views/devise/mailer/confirmation_instructions.html.erb +5 -0
  21. data/app/views/devise/mailer/reset_password_instructions.html.erb +8 -0
  22. data/app/views/devise/mailer/unlock_instructions.html.erb +7 -0
  23. data/config/locales/en.yml +50 -0
  24. data/config/routes.rb +2 -0
  25. data/lib/devise_token.rb +8 -0
  26. data/lib/devise_token/authenticable.rb +23 -0
  27. data/lib/devise_token/controllers/helpers.rb +70 -0
  28. data/lib/devise_token/controllers/url_helpers.rb +8 -0
  29. data/lib/devise_token/engine.rb +44 -0
  30. data/lib/devise_token/rails/routes.rb +66 -0
  31. data/lib/devise_token/version.rb +3 -0
  32. data/lib/generators/devise_token/install_generator.rb +160 -0
  33. data/lib/generators/devise_token/install_views_generator.rb +16 -0
  34. data/lib/generators/devise_token/templates/devise_token.rb +16 -0
  35. data/lib/generators/devise_token/templates/devise_token_create_users.rb.erb +55 -0
  36. data/lib/tasks/devise_token_tasks.rake +4 -0
  37. metadata +175 -0
@@ -0,0 +1,66 @@
1
+ module ActionDispatch::Routing
2
+ class Mapper
3
+ def devise_token_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
+ authentications_ctrl = opts[:controllers][:sessions] || "devise_token/authentications"
10
+ registrations_ctrl = opts[:controllers][:registrations] || "devise_token/registrations"
11
+ confirmations_ctrl = opts[:controllers][:confirmations] || "devise_token/confirmations"
12
+ token_validations_ctrl = opts[:controllers][:token_validations] || "devise_token/token_validations"
13
+
14
+ # define devise controller mappings
15
+ controllers = {:sessions => authentications_ctrl,
16
+ :registrations => registrations_ctrl,
17
+ :confirmations => confirmations_ctrl}
18
+
19
+ opts[:skip].each{|item| controllers.delete(item)}
20
+
21
+ devise_for resource.pluralize.underscore.gsub('/', '_').to_sym,
22
+ :class_name => resource,
23
+ :module => :devise,
24
+ :path => "#{opts[:at]}",
25
+ :controllers => controllers,
26
+ :skip => opts[:skip]
27
+
28
+ unnest_namespace do
29
+ # get full url path as if it were namespaced
30
+ full_path = "#{@scope[:path]}/#{opts[:at]}"
31
+
32
+ # get namespace name
33
+ namespace_name = @scope[:as]
34
+
35
+ # clear scope so controller routes aren't namespaced
36
+ @scope = ActionDispatch::Routing::Mapper::Scope.new(
37
+ path: "",
38
+ shallow_path: "",
39
+ constraints: {},
40
+ defaults: {},
41
+ options: {},
42
+ parent: nil
43
+ )
44
+
45
+ mapping_name = resource.underscore.gsub('/', '_')
46
+ mapping_name = "#{namespace_name}_#{mapping_name}" if namespace_name
47
+
48
+ devise_scope mapping_name.to_sym do
49
+ # path to verify token validity
50
+ get "#{full_path}/validate_token", controller: "#{token_validations_ctrl}", action: "validate_token"
51
+
52
+
53
+ end
54
+ end
55
+ end
56
+
57
+ # this allows us to use namespaced paths without namespacing the routes
58
+ def unnest_namespace
59
+ current_scope = @scope.dup
60
+ yield
61
+ ensure
62
+ @scope = current_scope
63
+ end
64
+
65
+ end
66
+ end
@@ -0,0 +1,3 @@
1
+ module DeviseToken
2
+ VERSION = '0.1.1'
3
+ end
@@ -0,0 +1,160 @@
1
+ module DeviseToken
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.rb", "config/initializers/devise_token.rb")
12
+ end
13
+
14
+ def copy_migrations
15
+ if self.class.migration_exists?("db/migrate", "devise_token_create_#{ user_class.underscore }")
16
+ say_status("skipped", "Migration 'devise_token_create_#{ user_class.underscore }' already exists")
17
+ else
18
+ migration_template(
19
+ "devise_token_create_users.rb.erb",
20
+ "db/migrate/devise_token_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 DeviseToken::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 DeviseToken::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 DeviseToken::Concerns::AuthenticateToken"
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 DeviseToken::Concerns::AuthenticateToken
56
+ RUBY
57
+ end
58
+ else
59
+ inject_into_file fname, after: "class ApplicationController < ActionController::Base\n" do <<-'RUBY'
60
+ include DeviseToken::Concerns::AuthenticateToken
61
+ RUBY
62
+ end
63
+ end
64
+ else
65
+ say_status("skipped", "app/controllers/application_controller.rb not found. Add 'include DeviseToken::Concerns::AuthenticateToken' to any controllers that require authentication.")
66
+ end
67
+ end
68
+
69
+ def add_route_mount
70
+ f = "config/routes.rb"
71
+ str = "devise_token_for '#{user_class}', at: '#{mount_path}'"
72
+
73
+ if File.exist?(File.join(destination_root, f))
74
+ line = parse_file_for_line(f, "devise_token_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 \"devise_token_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 DeviseToken
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,16 @@
1
+ DeviseToken.setup do |config|
2
+
3
+ # By default, users will need to re-authenticate after 2 weeks. This setting
4
+ # determines how long tokens will remain valid after they are issued.
5
+ # config.token_lifespan = 2.weeks
6
+
7
+ # By default sending current password is not needed for the password update.
8
+ # Uncomment to enforce current_password param to be checked before all
9
+ # attribute updates. Set it to :password if you want it to be checked only if
10
+ # password is updated.
11
+ # config.check_current_password_before_update = :attributes
12
+
13
+ # By default we will use callbacks for single omniauth.
14
+ # It depends on fields like email, provider and uid.
15
+ # config.default_callbacks = true
16
+ end
@@ -0,0 +1,55 @@
1
+ class DeviseTokenCreate<%= 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
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :devise_token do
3
+ # # Task goes here
4
+ # end
metadata ADDED
@@ -0,0 +1,175 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: devise_token
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Samuel Akrah
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-12-06 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '5.1'
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 5.1.4
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - "~>"
28
+ - !ruby/object:Gem::Version
29
+ version: '5.1'
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: 5.1.4
33
+ - !ruby/object:Gem::Dependency
34
+ name: devise
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">"
38
+ - !ruby/object:Gem::Version
39
+ version: 3.5.2
40
+ - - "<"
41
+ - !ruby/object:Gem::Version
42
+ version: '4.4'
43
+ type: :runtime
44
+ prerelease: false
45
+ version_requirements: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">"
48
+ - !ruby/object:Gem::Version
49
+ version: 3.5.2
50
+ - - "<"
51
+ - !ruby/object:Gem::Version
52
+ version: '4.4'
53
+ - !ruby/object:Gem::Dependency
54
+ name: jwt
55
+ requirement: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - "~>"
58
+ - !ruby/object:Gem::Version
59
+ version: '2.1'
60
+ type: :runtime
61
+ prerelease: false
62
+ version_requirements: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - "~>"
65
+ - !ruby/object:Gem::Version
66
+ version: '2.1'
67
+ - !ruby/object:Gem::Dependency
68
+ name: sqlite3
69
+ requirement: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - "~>"
72
+ - !ruby/object:Gem::Version
73
+ version: '1.3'
74
+ type: :development
75
+ prerelease: false
76
+ version_requirements: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - "~>"
79
+ - !ruby/object:Gem::Version
80
+ version: '1.3'
81
+ - !ruby/object:Gem::Dependency
82
+ name: pg
83
+ requirement: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ type: :development
89
+ prerelease: false
90
+ version_requirements: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ - !ruby/object:Gem::Dependency
96
+ name: mysql2
97
+ requirement: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - ">="
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ type: :development
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - ">="
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ description: Authentication for all Rails Api applications
110
+ email:
111
+ - akrahdan@gmail.com
112
+ executables: []
113
+ extensions: []
114
+ extra_rdoc_files: []
115
+ files:
116
+ - MIT-LICENSE
117
+ - README.md
118
+ - Rakefile
119
+ - app/assets/config/devise_token_manifest.js
120
+ - app/assets/javascripts/devise_token/application.js
121
+ - app/assets/stylesheets/devise_token/application.css
122
+ - app/controllers/devise_token/application_controller.rb
123
+ - app/controllers/devise_token/authentications_controller.rb
124
+ - app/controllers/devise_token/concerns/authenticate_token.rb
125
+ - app/controllers/devise_token/confirmations_controller.rb
126
+ - app/controllers/devise_token/registrations_controller.rb
127
+ - app/controllers/devise_token/token_validations_controller.rb
128
+ - app/helpers/devise_token/application_helper.rb
129
+ - app/jobs/devise_token/application_job.rb
130
+ - app/mailers/devise_token/application_mailer.rb
131
+ - app/models/devise_token/application_record.rb
132
+ - app/models/devise_token/concerns/user.rb
133
+ - app/models/devise_token/json_web_token.rb
134
+ - app/views/devise/mailer/confirmation_instructions.html.erb
135
+ - app/views/devise/mailer/reset_password_instructions.html.erb
136
+ - app/views/devise/mailer/unlock_instructions.html.erb
137
+ - config/locales/en.yml
138
+ - config/routes.rb
139
+ - lib/devise_token.rb
140
+ - lib/devise_token/authenticable.rb
141
+ - lib/devise_token/controllers/helpers.rb
142
+ - lib/devise_token/controllers/url_helpers.rb
143
+ - lib/devise_token/engine.rb
144
+ - lib/devise_token/rails/routes.rb
145
+ - lib/devise_token/version.rb
146
+ - lib/generators/devise_token/install_generator.rb
147
+ - lib/generators/devise_token/install_views_generator.rb
148
+ - lib/generators/devise_token/templates/devise_token.rb
149
+ - lib/generators/devise_token/templates/devise_token_create_users.rb.erb
150
+ - lib/tasks/devise_token_tasks.rake
151
+ homepage: https://github.com/akrahdan/devise_token
152
+ licenses:
153
+ - MIT
154
+ metadata: {}
155
+ post_install_message:
156
+ rdoc_options: []
157
+ require_paths:
158
+ - lib
159
+ required_ruby_version: !ruby/object:Gem::Requirement
160
+ requirements:
161
+ - - ">="
162
+ - !ruby/object:Gem::Version
163
+ version: '0'
164
+ required_rubygems_version: !ruby/object:Gem::Requirement
165
+ requirements:
166
+ - - ">="
167
+ - !ruby/object:Gem::Version
168
+ version: '0'
169
+ requirements: []
170
+ rubyforge_project:
171
+ rubygems_version: 2.6.13
172
+ signing_key:
173
+ specification_version: 4
174
+ summary: JWT Token based authentication with devise.
175
+ test_files: []