rodauth-rails 0.1.0

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.
@@ -0,0 +1,20 @@
1
+ module Rodauth
2
+ module Rails
3
+ module ControllerMethods
4
+ def self.included(controller)
5
+ # ActionController::API doesn't have helper methods
6
+ if controller.respond_to?(:helper_method)
7
+ controller.helper_method :rodauth
8
+ end
9
+ end
10
+
11
+ def rodauth(name = nil)
12
+ if name
13
+ request.env["rodauth.#{name}"]
14
+ else
15
+ request.env["rodauth"]
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,118 @@
1
+ module Rodauth
2
+ Feature.define(:rails) do
3
+ depends :email_base
4
+
5
+ # List of overridable methods.
6
+ auth_methods(
7
+ :rails_render,
8
+ :rails_csrf_tag,
9
+ :rails_csrf_param,
10
+ :rails_csrf_token,
11
+ :rails_check_csrf!,
12
+ :rails_controller_instance,
13
+ :rails_controller,
14
+ )
15
+
16
+ # Renders templates with layout. First tries to render a user-defined
17
+ # template, otherwise falls back to Rodauth's template.
18
+ def view(page, *)
19
+ rails_render(action: page.tr("-", "_"), layout: true) ||
20
+ rails_render(html: super.html_safe, layout: true)
21
+ end
22
+
23
+ # Renders templates without layout. First tries to render a user-defined
24
+ # template or partial, otherwise falls back to Rodauth's template.
25
+ def render(page)
26
+ rails_render(partial: page.tr("-", "_"), layout: false) ||
27
+ rails_render(action: page.tr("-", "_"), layout: false) ||
28
+ super
29
+ end
30
+
31
+ # Render Rails CSRF tags in Rodauth templates.
32
+ def csrf_tag(*)
33
+ rails_csrf_tag
34
+ end
35
+
36
+ # Default the flash error key to Rails' default :alert.
37
+ def flash_error_key
38
+ :alert
39
+ end
40
+
41
+ private
42
+
43
+ # Verify Rails' authenticity token before each Rodauth route.
44
+ def before_rodauth
45
+ rails_check_csrf!
46
+ super
47
+ end
48
+
49
+ # Create emails with ActionMailer which uses configured delivery method.
50
+ def create_email_to(to, subject, body)
51
+ Mailer.create_email(to: to, from: email_from, subject: "#{email_subject_prefix}#{subject}", body: body)
52
+ end
53
+
54
+ # Delivers the given email.
55
+ def send_email(email)
56
+ email.deliver_now
57
+ end
58
+
59
+ # Calls the Rails renderer, returning nil if a template is missing.
60
+ def rails_render(*args)
61
+ rails_controller_instance.render_to_string(*args)
62
+ rescue ActionView::MissingTemplate
63
+ nil
64
+ end
65
+
66
+ # Hidden tag with Rails CSRF token inserted into Rodauth templates.
67
+ def rails_csrf_tag
68
+ %(<input type="hidden" name="#{rails_csrf_param}" value="#{rails_csrf_token}">)
69
+ end
70
+
71
+ # The request parameter under which to send the Rails CSRF token.
72
+ def rails_csrf_param
73
+ rails_controller.request_forgery_protection_token
74
+ end
75
+
76
+ # The Rails CSRF token value inserted into Rodauth templates.
77
+ def rails_csrf_token
78
+ rails_controller_instance.send(:form_authenticity_token)
79
+ end
80
+
81
+ # Calls the controller to verify the authenticity token.
82
+ def rails_check_csrf!
83
+ rails_controller_instance.send(:verify_authenticity_token)
84
+ end
85
+
86
+ # Instances of the configured controller with current request's env hash.
87
+ def rails_controller_instance
88
+ request = ActionDispatch::Request.new(scope.env)
89
+ instance = rails_controller.new
90
+
91
+ if ActionPack.version >= Gem::Version.new("5.0.0")
92
+ instance.set_request! request
93
+ instance.set_response! rails_controller.make_response!(request)
94
+ else
95
+ instance.send(:set_response!, request)
96
+ instance.instance_variable_set(:@_request, request)
97
+ end
98
+
99
+ instance
100
+ end
101
+
102
+ # Controller class to use for rendering and CSRF protection.
103
+ def rails_controller
104
+ ActionController::Base
105
+ end
106
+
107
+ # ActionMailer subclass for correct email delivering.
108
+ class Mailer < ActionMailer::Base
109
+ def create_email(**options)
110
+ mail(**options)
111
+ end
112
+ end
113
+ end
114
+
115
+ # Assign feature and feature configuration to constants for introspection.
116
+ Rails::Feature = FEATURES[:rails]
117
+ Rails::FeatureConfiguration = FEATURES[:rails].configuration
118
+ end
@@ -0,0 +1,21 @@
1
+ module Rodauth
2
+ module Rails
3
+ # Middleware that's added to the Rails middleware stack. Normally the main
4
+ # Roda app could be used directly, but this trick allows the app class to
5
+ # be reloadable.
6
+ class Middleware
7
+ def initialize(app)
8
+ @app = app
9
+ end
10
+
11
+ def call(env)
12
+ app = Rodauth::Rails.app.new(@app)
13
+
14
+ # allow the Rails app to call Rodauth methods that throw :halt
15
+ catch(:halt) do
16
+ app.call(env)
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,18 @@
1
+ require "rodauth/rails/middleware"
2
+ require "rodauth/rails/controller_methods"
3
+
4
+ module Rodauth
5
+ module Rails
6
+ class Railtie < ::Rails::Railtie
7
+ initializer "rodauth.middleware" do |app|
8
+ app.middleware.use Rodauth::Rails::Middleware if Rodauth::Rails.middleware?
9
+ end
10
+
11
+ initializer "rodauth.controller" do
12
+ ActiveSupport.on_load(:action_controller) do
13
+ include Rodauth::Rails::ControllerMethods
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,33 @@
1
+ require "rodauth/rails/railtie"
2
+
3
+ module Rodauth
4
+ module Rails
5
+ class Error < StandardError
6
+ end
7
+
8
+ # This allows the developer to avoid loading Rodauth at boot time.
9
+ autoload :App, "rodauth/rails/app"
10
+
11
+ def self.configure
12
+ yield self
13
+ end
14
+
15
+ @app = nil
16
+ @middleware = true
17
+
18
+ class << self
19
+ attr_writer :app
20
+ attr_writer :middleware
21
+
22
+ def app
23
+ fail Rodauth::Rails::Error, "app was not configured" unless @app
24
+
25
+ @app.constantize
26
+ end
27
+
28
+ def middleware?
29
+ @middleware
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1 @@
1
+ require "rodauth/rails"
@@ -0,0 +1,22 @@
1
+ Gem::Specification.new do |spec|
2
+ spec.name = "rodauth-rails"
3
+ spec.version = "0.1.0"
4
+ spec.authors = ["Janko Marohnić"]
5
+ spec.email = ["janko.marohnic@gmail.com"]
6
+
7
+ spec.summary = %q{Provides Rails integration for Rodauth.}
8
+ spec.description = %q{Provides Rails integration for Rodauth.}
9
+ spec.homepage = "https://github.com/janko/rodauth-rails"
10
+ spec.license = "MIT"
11
+
12
+ spec.required_ruby_version = ">= 2.2.0"
13
+
14
+ spec.files = Dir["README.md", "LICENSE.txt", "CHANGELOG.md", "lib/**/*.rb", "*.gemspec"]
15
+ spec.require_paths = ["lib"]
16
+
17
+ spec.add_dependency "railties", ">= 4.2", "< 7"
18
+ spec.add_dependency "rodauth", ">= 1.23", "< 3"
19
+ spec.add_dependency "sequel-activerecord_connection", "~> 0.2"
20
+ spec.add_dependency "tilt"
21
+ spec.add_dependency "bcrypt"
22
+ end
metadata ADDED
@@ -0,0 +1,147 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rodauth-rails
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Janko Marohnić
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-05-06 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: railties
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '4.2'
20
+ - - "<"
21
+ - !ruby/object:Gem::Version
22
+ version: '7'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ version: '4.2'
30
+ - - "<"
31
+ - !ruby/object:Gem::Version
32
+ version: '7'
33
+ - !ruby/object:Gem::Dependency
34
+ name: rodauth
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '1.23'
40
+ - - "<"
41
+ - !ruby/object:Gem::Version
42
+ version: '3'
43
+ type: :runtime
44
+ prerelease: false
45
+ version_requirements: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: '1.23'
50
+ - - "<"
51
+ - !ruby/object:Gem::Version
52
+ version: '3'
53
+ - !ruby/object:Gem::Dependency
54
+ name: sequel-activerecord_connection
55
+ requirement: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - "~>"
58
+ - !ruby/object:Gem::Version
59
+ version: '0.2'
60
+ type: :runtime
61
+ prerelease: false
62
+ version_requirements: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - "~>"
65
+ - !ruby/object:Gem::Version
66
+ version: '0.2'
67
+ - !ruby/object:Gem::Dependency
68
+ name: tilt
69
+ requirement: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ version: '0'
74
+ type: :runtime
75
+ prerelease: false
76
+ version_requirements: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ version: '0'
81
+ - !ruby/object:Gem::Dependency
82
+ name: bcrypt
83
+ requirement: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ type: :runtime
89
+ prerelease: false
90
+ version_requirements: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ description: Provides Rails integration for Rodauth.
96
+ email:
97
+ - janko.marohnic@gmail.com
98
+ executables: []
99
+ extensions: []
100
+ extra_rdoc_files: []
101
+ files:
102
+ - LICENSE.txt
103
+ - README.md
104
+ - lib/generators/rodauth/install_generator.rb
105
+ - lib/generators/rodauth/mailer_generator.rb
106
+ - lib/generators/rodauth/templates/app/controllers/rodauth_controller.rb
107
+ - lib/generators/rodauth/templates/app/mailers/rodauth_mailer.rb
108
+ - lib/generators/rodauth/templates/app/models/account.rb
109
+ - lib/generators/rodauth/templates/config/initializers/rodauth.rb
110
+ - lib/generators/rodauth/templates/config/initializers/sequel.rb
111
+ - lib/generators/rodauth/templates/db/migrate/create_rodauth.rb
112
+ - lib/generators/rodauth/templates/lib/rodauth_app.rb
113
+ - lib/generators/rodauth/views_generator.rb
114
+ - lib/rodauth-rails.rb
115
+ - lib/rodauth/features/rails.rb
116
+ - lib/rodauth/rails.rb
117
+ - lib/rodauth/rails/app.rb
118
+ - lib/rodauth/rails/app/flash.rb
119
+ - lib/rodauth/rails/controller_methods.rb
120
+ - lib/rodauth/rails/feature.rb
121
+ - lib/rodauth/rails/middleware.rb
122
+ - lib/rodauth/rails/railtie.rb
123
+ - rodauth-rails.gemspec
124
+ homepage: https://github.com/janko/rodauth-rails
125
+ licenses:
126
+ - MIT
127
+ metadata: {}
128
+ post_install_message:
129
+ rdoc_options: []
130
+ require_paths:
131
+ - lib
132
+ required_ruby_version: !ruby/object:Gem::Requirement
133
+ requirements:
134
+ - - ">="
135
+ - !ruby/object:Gem::Version
136
+ version: 2.2.0
137
+ required_rubygems_version: !ruby/object:Gem::Requirement
138
+ requirements:
139
+ - - ">="
140
+ - !ruby/object:Gem::Version
141
+ version: '0'
142
+ requirements: []
143
+ rubygems_version: 3.1.1
144
+ signing_key:
145
+ specification_version: 4
146
+ summary: Provides Rails integration for Rodauth.
147
+ test_files: []