rodauth-rails 0.8.1 → 0.11.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,40 @@
1
+ require "rodauth"
2
+ require "rodauth/rails/feature"
3
+
4
+ module Rodauth
5
+ module Rails
6
+ # Base auth class that applies some default configuration and supports
7
+ # multi-level inheritance.
8
+ class Auth < Rodauth::Auth
9
+ class << self
10
+ attr_writer :features
11
+ attr_writer :routes
12
+ attr_accessor :configuration
13
+ end
14
+
15
+ def self.inherited(auth_class)
16
+ super
17
+ auth_class.roda_class = Rodauth::Rails.app
18
+ auth_class.features = features.dup
19
+ auth_class.routes = routes.dup
20
+ auth_class.route_hash = route_hash.dup
21
+ auth_class.configuration = configuration.clone
22
+ auth_class.configuration.instance_variable_set(:@auth, auth_class)
23
+ end
24
+
25
+ # apply default configuration
26
+ configure do
27
+ enable :rails
28
+
29
+ # database functions are more complex to set up, so disable them by default
30
+ use_database_authentication_functions? false
31
+
32
+ # avoid having to set deadline values in column default values
33
+ set_deadline_values? true
34
+
35
+ # use HMACs for additional security
36
+ hmac_secret { Rodauth::Rails.secret_key_base }
37
+ end
38
+ end
39
+ end
40
+ end
@@ -9,11 +9,7 @@ module Rodauth
9
9
  end
10
10
 
11
11
  def rodauth(name = nil)
12
- if name
13
- request.env["rodauth.#{name}"]
14
- else
15
- request.env["rodauth"]
16
- end
12
+ request.env.fetch ["rodauth", *name].join(".")
17
13
  end
18
14
  end
19
15
  end
@@ -44,6 +44,11 @@ module Rodauth
44
44
  true
45
45
  end
46
46
 
47
+ # Reset Rails session to protect from session fixation attacks.
48
+ def clear_session
49
+ rails_controller_instance.reset_session
50
+ end
51
+
47
52
  # Default the flash error key to Rails' default :alert.
48
53
  def flash_error_key
49
54
  :alert
@@ -58,24 +63,33 @@ module Rodauth
58
63
  super.html_safe
59
64
  end
60
65
 
66
+ delegate :rails_routes, :rails_request, to: :scope
67
+
61
68
  private
62
69
 
63
70
  # Runs controller callbacks and rescue handlers around Rodauth actions.
64
71
  def _around_rodauth(&block)
65
72
  result = nil
66
73
 
67
- rails_controller_rescue do
68
- rails_controller_callbacks do
69
- result = catch(:halt) { super(&block) }
74
+ rails_instrument_request do
75
+ rails_controller_rescue do
76
+ rails_controller_callbacks do
77
+ result = catch(:halt) { super(&block) }
78
+ end
70
79
  end
80
+
81
+ result = handle_rails_controller_response(result)
71
82
  end
72
83
 
84
+ throw :halt, result if result
85
+ end
86
+
87
+ # Handles controller rendering a response or setting response headers.
88
+ def handle_rails_controller_response(result)
73
89
  if rails_controller_instance.performed?
74
90
  rails_controller_response
75
91
  elsif result
76
92
  result[1].merge!(rails_controller_instance.response.headers)
77
- throw :halt, result
78
- else
79
93
  result
80
94
  end
81
95
  end
@@ -104,6 +118,20 @@ module Rodauth
104
118
  end
105
119
  end
106
120
 
121
+ def rails_instrument_request
122
+ ActiveSupport::Notifications.instrument("start_processing.rodauth", rodauth: self)
123
+ ActiveSupport::Notifications.instrument("process_request.rodauth", rodauth: self) do |payload|
124
+ begin
125
+ status, headers, body = yield
126
+ payload[:status] = status || 404
127
+ payload[:headers] = headers
128
+ payload[:body] = body
129
+ ensure
130
+ rails_controller_instance.send(:append_info_to_payload, payload)
131
+ end
132
+ end
133
+ end
134
+
107
135
  # Returns Roda response from controller response if set.
108
136
  def rails_controller_response
109
137
  controller_response = rails_controller_instance.response
@@ -112,7 +140,7 @@ module Rodauth
112
140
  response.headers.merge! controller_response.headers
113
141
  response.write controller_response.body
114
142
 
115
- request.halt
143
+ response.finish
116
144
  end
117
145
 
118
146
  # Create emails with ActionMailer which uses configured delivery method.
@@ -163,11 +191,8 @@ module Rodauth
163
191
 
164
192
  # Instances of the configured controller with current request's env hash.
165
193
  def _rails_controller_instance
166
- controller = rails_controller.new
167
- rails_request = ActionDispatch::Request.new(scope.env)
168
-
194
+ controller = rails_controller.new
169
195
  prepare_rails_controller(controller, rails_request)
170
-
171
196
  controller
172
197
  end
173
198
 
@@ -187,6 +212,14 @@ module Rodauth
187
212
  defined?(ActionController::API) && rails_controller <= ActionController::API
188
213
  end
189
214
 
215
+ def rails_controller
216
+ if only_json? && Rodauth::Rails.api_only?
217
+ ActionController::API
218
+ else
219
+ ActionController::Base
220
+ end
221
+ end
222
+
190
223
  # ActionMailer subclass for correct email delivering.
191
224
  class Mailer < ActionMailer::Base
192
225
  def create_email(**options)
@@ -0,0 +1,34 @@
1
+ module Rodauth
2
+ module Rails
3
+ class LogSubscriber < ActiveSupport::LogSubscriber
4
+ def start_processing(event)
5
+ rodauth = event.payload[:rodauth]
6
+ app_class = rodauth.scope.class.superclass
7
+ format = rodauth.rails_request.format.ref
8
+ format = format.to_s.upcase if format.is_a?(Symbol)
9
+ format = "*/*" if format.nil?
10
+
11
+ info "Processing by #{app_class} as #{format}"
12
+ end
13
+
14
+ def process_request(event)
15
+ status = event.payload[:status]
16
+
17
+ additions = ActionController::Base.log_process_action(event.payload)
18
+ if ::Rails.gem_version >= Gem::Version.new("6.0")
19
+ additions << "Allocations: #{event.allocations}"
20
+ end
21
+
22
+ message = "Completed #{status} #{Rack::Utils::HTTP_STATUS_CODES[status]} in #{event.duration.round}ms"
23
+ message << " (#{additions.join(" | ")})"
24
+ message << "\n\n" if defined?(::Rails.env) && ::Rails.env.development?
25
+
26
+ info message
27
+ end
28
+
29
+ def logger
30
+ ::Rails.logger
31
+ end
32
+ end
33
+ end
34
+ end
@@ -1,5 +1,6 @@
1
1
  require "rodauth/rails/middleware"
2
2
  require "rodauth/rails/controller_methods"
3
+ require "rodauth/rails/log_subscriber"
3
4
 
4
5
  require "rails"
5
6
 
@@ -16,6 +17,10 @@ module Rodauth
16
17
  end
17
18
  end
18
19
 
20
+ initializer "rodauth.log_subscriber" do
21
+ Rodauth::Rails::LogSubscriber.attach_to :rodauth
22
+ end
23
+
19
24
  initializer "rodauth.test" do
20
25
  # Rodauth uses RACK_ENV to set the default bcrypt hash cost
21
26
  ENV["RACK_ENV"] = "test" if ::Rails.env.test?
@@ -1,5 +1,5 @@
1
1
  module Rodauth
2
2
  module Rails
3
- VERSION = "0.8.1"
3
+ VERSION = "0.11.0"
4
4
  end
5
5
  end
@@ -17,7 +17,7 @@ Gem::Specification.new do |spec|
17
17
  spec.require_paths = ["lib"]
18
18
 
19
19
  spec.add_dependency "railties", ">= 4.2", "< 7"
20
- spec.add_dependency "rodauth", "~> 2.7"
20
+ spec.add_dependency "rodauth", "~> 2.11"
21
21
  spec.add_dependency "sequel-activerecord_connection", "~> 1.1"
22
22
  spec.add_dependency "tilt"
23
23
  spec.add_dependency "bcrypt"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rodauth-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.1
4
+ version: 0.11.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Janko Marohnić
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-01-04 00:00:00.000000000 Z
11
+ date: 2021-05-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: railties
@@ -36,14 +36,14 @@ dependencies:
36
36
  requirements:
37
37
  - - "~>"
38
38
  - !ruby/object:Gem::Version
39
- version: '2.7'
39
+ version: '2.11'
40
40
  type: :runtime
41
41
  prerelease: false
42
42
  version_requirements: !ruby/object:Gem::Requirement
43
43
  requirements:
44
44
  - - "~>"
45
45
  - !ruby/object:Gem::Version
46
- version: '2.7'
46
+ version: '2.11'
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: sequel-activerecord_connection
49
49
  requirement: !ruby/object:Gem::Requirement
@@ -111,7 +111,6 @@ files:
111
111
  - LICENSE.txt
112
112
  - README.md
113
113
  - lib/generators/rodauth/install_generator.rb
114
- - lib/generators/rodauth/mailer_generator.rb
115
114
  - lib/generators/rodauth/migration/account_expiration.erb
116
115
  - lib/generators/rodauth/migration/active_sessions.erb
117
116
  - lib/generators/rodauth/migration/audit_logging.erb
@@ -205,8 +204,10 @@ files:
205
204
  - lib/rodauth/rails/app.rb
206
205
  - lib/rodauth/rails/app/flash.rb
207
206
  - lib/rodauth/rails/app/middleware.rb
207
+ - lib/rodauth/rails/auth.rb
208
208
  - lib/rodauth/rails/controller_methods.rb
209
209
  - lib/rodauth/rails/feature.rb
210
+ - lib/rodauth/rails/log_subscriber.rb
210
211
  - lib/rodauth/rails/middleware.rb
211
212
  - lib/rodauth/rails/railtie.rb
212
213
  - lib/rodauth/rails/tasks.rake
@@ -216,7 +217,7 @@ homepage: https://github.com/janko/rodauth-rails
216
217
  licenses:
217
218
  - MIT
218
219
  metadata: {}
219
- post_install_message:
220
+ post_install_message:
220
221
  rdoc_options: []
221
222
  require_paths:
222
223
  - lib
@@ -231,8 +232,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
231
232
  - !ruby/object:Gem::Version
232
233
  version: '0'
233
234
  requirements: []
234
- rubygems_version: 3.1.4
235
- signing_key:
235
+ rubygems_version: 3.2.3
236
+ signing_key:
236
237
  specification_version: 4
237
238
  summary: Provides Rails integration for Rodauth.
238
239
  test_files: []
@@ -1,37 +0,0 @@
1
- require "rails/generators/base"
2
-
3
- module Rodauth
4
- module Rails
5
- module Generators
6
- class MailerGenerator < ::Rails::Generators::Base
7
- source_root "#{__dir__}/templates"
8
- namespace "rodauth:mailer"
9
-
10
- VIEWS = %w[
11
- email_auth
12
- password_changed
13
- reset_password
14
- unlock_account
15
- verify_account
16
- verify_login_change
17
- ]
18
-
19
- class_option :name,
20
- desc: "The name for the mailer and the views directory",
21
- default: "rodauth"
22
-
23
- def copy_mailer
24
- template "app/mailers/rodauth_mailer.rb",
25
- "app/mailers/#{options[:name].underscore}_mailer.rb"
26
- end
27
-
28
- def copy_mailer_views
29
- VIEWS.each do |view|
30
- template "app/views/rodauth_mailer/#{view}.text.erb",
31
- "app/views/#{options[:name].underscore}_mailer/#{view}.text.erb"
32
- end
33
- end
34
- end
35
- end
36
- end
37
- end