action_controller-stashed_redirects 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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 57726ac99490f85375fd07300740792ad9ce85efda8c4eb728794c4243391636
4
+ data.tar.gz: 2a3059590ed5e4032477d5600304668a5af6c4b4b55a51a31a3246a59be64e5e
5
+ SHA512:
6
+ metadata.gz: bf260715badd07d6a091254fecbc3890ff21b69317fd0c05c8d50971a18f47be87a182da0f9ea6ffe2c4140d71b101f30003c24fe4ab28feb8671945aa260def
7
+ data.tar.gz: 949bf794275255b14aeb5b7e46e6f0558218cf736eb7a1b686063c02af2c545128e8dc55f2e0984a097b844bb8f0dcc7ea89575e67181b1a06793f239d40294a
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2022-04-21
4
+
5
+ - Initial release
data/Gemfile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in action_controller-stashed_redirects.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ gem "debug"
11
+ gem "minitest", "~> 5.0"
12
+ gem "railties"
data/Gemfile.lock ADDED
@@ -0,0 +1,89 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ action_controller-stashed_redirects (0.1.0)
5
+ actionpack (>= 7.0)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ actionpack (7.0.2.3)
11
+ actionview (= 7.0.2.3)
12
+ activesupport (= 7.0.2.3)
13
+ rack (~> 2.0, >= 2.2.0)
14
+ rack-test (>= 0.6.3)
15
+ rails-dom-testing (~> 2.0)
16
+ rails-html-sanitizer (~> 1.0, >= 1.2.0)
17
+ actionview (7.0.2.3)
18
+ activesupport (= 7.0.2.3)
19
+ builder (~> 3.1)
20
+ erubi (~> 1.4)
21
+ rails-dom-testing (~> 2.0)
22
+ rails-html-sanitizer (~> 1.1, >= 1.2.0)
23
+ activesupport (7.0.2.3)
24
+ concurrent-ruby (~> 1.0, >= 1.0.2)
25
+ i18n (>= 1.6, < 2)
26
+ minitest (>= 5.1)
27
+ tzinfo (~> 2.0)
28
+ builder (3.2.4)
29
+ concurrent-ruby (1.1.10)
30
+ crass (1.0.6)
31
+ debug (1.5.0)
32
+ irb (>= 1.3.6)
33
+ reline (>= 0.2.7)
34
+ erubi (1.10.0)
35
+ i18n (1.10.0)
36
+ concurrent-ruby (~> 1.0)
37
+ io-console (0.5.11)
38
+ irb (1.4.1)
39
+ reline (>= 0.3.0)
40
+ loofah (2.16.0)
41
+ crass (~> 1.0.2)
42
+ nokogiri (>= 1.5.9)
43
+ method_source (1.0.0)
44
+ minitest (5.15.0)
45
+ nokogiri (1.13.4-aarch64-linux)
46
+ racc (~> 1.4)
47
+ nokogiri (1.13.4-arm64-darwin)
48
+ racc (~> 1.4)
49
+ nokogiri (1.13.4-x86-linux)
50
+ racc (~> 1.4)
51
+ nokogiri (1.13.4-x86_64-linux)
52
+ racc (~> 1.4)
53
+ racc (1.6.0)
54
+ rack (2.2.3)
55
+ rack-test (1.1.0)
56
+ rack (>= 1.0, < 3)
57
+ rails-dom-testing (2.0.3)
58
+ activesupport (>= 4.2.0)
59
+ nokogiri (>= 1.6)
60
+ rails-html-sanitizer (1.4.2)
61
+ loofah (~> 2.3)
62
+ railties (7.0.2.3)
63
+ actionpack (= 7.0.2.3)
64
+ activesupport (= 7.0.2.3)
65
+ method_source
66
+ rake (>= 12.2)
67
+ thor (~> 1.0)
68
+ zeitwerk (~> 2.5)
69
+ rake (13.0.6)
70
+ reline (0.3.1)
71
+ io-console (~> 0.5)
72
+ thor (1.2.1)
73
+ tzinfo (2.0.4)
74
+ concurrent-ruby (~> 1.0)
75
+ zeitwerk (2.5.4)
76
+
77
+ PLATFORMS
78
+ arm64-darwin-20
79
+ linux
80
+
81
+ DEPENDENCIES
82
+ action_controller-stashed_redirects!
83
+ debug
84
+ minitest (~> 5.0)
85
+ railties
86
+ rake (~> 13.0)
87
+
88
+ BUNDLED WITH
89
+ 2.3.11
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2022 Kasper Timm Hansen
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,62 @@
1
+ # ActionController::StashedRedirects
2
+
3
+ Pass between different controller flows via stashed redirects
4
+
5
+ Stash a redirect to execute a controller flow within another and return to the original flow later.
6
+
7
+ ## Usage
8
+
9
+ ```ruby
10
+ class ApplicationController < ActionController::Base
11
+ before_action :authenticate
12
+
13
+ private
14
+ def authenticate
15
+ redirect_to new_session_url unless Current.user
16
+ end
17
+ end
18
+
19
+ class SessionsController < ApplicationController
20
+ # Stash a redirect at the start of the session authentication flow,
21
+ # from either params[:redirect_url] or request.referer in that order.
22
+ stash_redirect_for :sign_in, on: :new
23
+
24
+ def new
25
+ end
26
+
27
+ def create
28
+ if User.authenticate_by(session_params)
29
+ # On success, redirect the user back to where they first tried to access before being authenticated.
30
+ redirect_from_stashed :sign_in
31
+ end
32
+ end
33
+ end
34
+ ```
35
+
36
+ See the internal documentation for more usage information.
37
+
38
+ Only internal redirects are allowed, so attackers can't pass an external `redirect_url`.
39
+
40
+ ## Installation
41
+
42
+ Install the gem and add to the application's Gemfile by executing:
43
+
44
+ $ bundle add action_controller-stashed_redirects
45
+
46
+ If bundler is not being used to manage dependencies, install the gem by executing:
47
+
48
+ $ gem install action_controller-stashed_redirects
49
+
50
+ ## Development
51
+
52
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
53
+
54
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
55
+
56
+ ## Contributing
57
+
58
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/action_controller-stashed_redirects.
59
+
60
+ ## License
61
+
62
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rake/testtask"
5
+
6
+ Rake::TestTask.new(:test) do |t|
7
+ t.libs << "test"
8
+ t.libs << "lib"
9
+ t.test_files = FileList["test/**/test_*.rb"]
10
+ end
11
+
12
+ task default: :test
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/action_controller/stashed_redirects/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "action_controller-stashed_redirects"
7
+ spec.version = ActionController::StashedRedirects::VERSION
8
+ spec.authors = ["Kasper Timm Hansen"]
9
+ spec.email = ["hey@kaspth.com"]
10
+
11
+ spec.summary = "Embed a controller flow within another by stashing the final redirect upfront and performing it after completing."
12
+ spec.homepage = "https://github.com/kaspth/action_controller-stashed_redirects"
13
+ spec.license = "MIT"
14
+ spec.required_ruby_version = ">= 2.7.0"
15
+
16
+ spec.metadata["homepage_uri"] = spec.homepage
17
+ spec.metadata["source_code_uri"] = spec.homepage
18
+ spec.metadata["changelog_uri"] = "#{spec.homepage}/blob/main/CHANGELOG.md"
19
+
20
+ # Specify which files should be added to the gem when it is released.
21
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
22
+ spec.files = Dir.chdir(__dir__) do
23
+ `git ls-files -z`.split("\x0").reject do |f|
24
+ (f == __FILE__) || f.match(%r{\A(?:(?:bin|test|spec|features)/|\.(?:git|travis|circleci)|appveyor)})
25
+ end
26
+ end
27
+ spec.bindir = "exe"
28
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
29
+ spec.require_paths = ["lib"]
30
+
31
+ # Uncomment to register a new dependency of your gem
32
+ spec.add_dependency "actionpack", ">= 7.0"
33
+
34
+ # For more information and examples about making a new gem, check out our
35
+ # guide at: https://bundler.io/guides/creating_gem.html
36
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActionController
4
+ module StashedRedirects
5
+ VERSION = "0.1.0"
6
+ end
7
+ end
@@ -0,0 +1,75 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "active_support"
4
+ require_relative "stashed_redirects/version"
5
+
6
+ # Pass between different controller flows via stashed redirects
7
+ #
8
+ # Stash a redirect to execute a controller flow within another and return to the original flow later.
9
+ module ActionController::StashedRedirects
10
+ extend ActiveSupport::Concern
11
+
12
+ class_methods do
13
+ # Adds a before_action to stash a redirect for a given `on:` action.
14
+ #
15
+ # stash_redirect_for :sudo_authentication, on: :new
16
+ # stash_redirect_for :sign_in, from: :referer, on: :new
17
+ # stash_redirect_for :sign_in, from: -> { update_post_path(@post) }
18
+ def stash_redirect_for(purpose, on:, from: nil)
19
+ before_action(-> { stash_redirect_for(purpose, from: from.respond_to?(:call) ? instance_exec(&from) : from) }, only: on)
20
+ end
21
+ end
22
+
23
+ # Stashes a redirect URL in the `session` under the given +purpose+.
24
+ #
25
+ # An explicit +redirect_url+ can be passsed, otherwise the redirect URL is
26
+ # derived from `params[:redirect_url]` then falling back to `request.referer`.
27
+ #
28
+ # stash_redirect_for :sign_in
29
+ # stash_redirect_for :sign_in, from: url_from(params[:redirect_url]) || root_url
30
+ # stash_redirect_for :sign_in, from: :param # Only derive the redirect URL from `params[:redirect_url]`.
31
+ # stash_redirect_for :sign_in, from: :referer # Only derive the redirect URL from `request.referer`.
32
+ def stash_redirect_for(purpose, from: nil)
33
+ if url = derive_stash_redirect_url_from(from)
34
+ session[KEY_GENERATOR.(purpose)] = url
35
+ else
36
+ raise ArgumentError, "missing a redirect_url to stash, pass one via from: or via a redirect_url URL param"
37
+ end
38
+ end
39
+
40
+ # Finds and deletes the redirect stashed in `session` under the given +purpose+, then redirects.
41
+ #
42
+ # redirect_from_stashed :login
43
+ #
44
+ # Raises if no stashed redirect is found under the given +purpose+.
45
+ #
46
+ # Relies on +redirect_to+'s open redirect protection, see it's documentation for more.
47
+ def redirect_from_stashed(purpose)
48
+ redirect_to stashed_redirect_url_for(purpose)
49
+ end
50
+
51
+ # Deletes the redirect stashed in the `session` under the given +purpose+ and returns it if any.
52
+ #
53
+ # discard_stashed_redirect_for :login # => the login redirect URL or nil.
54
+ def discard_stashed_redirect_for(purpose)
55
+ session.delete(KEY_GENERATOR.(purpose))
56
+ end
57
+
58
+ private
59
+ KEY_GENERATOR = ->(purpose) { "__url_stash_#{purpose}" }
60
+ private_constant :KEY_GENERATOR
61
+
62
+ def stashed_redirect_url_for(purpose)
63
+ raise ArgumentError, "can't extract a stashed redirect_url from session, none found" \
64
+ unless redirect_url = discard_stashed_redirect_for(purpose)
65
+
66
+ url_from(redirect_url)
67
+ end
68
+
69
+ def derive_stash_redirect_url_from(from)
70
+ from ||= %i[ param referer ]
71
+ { param: params[:redirect_url], referer: request.get? && request.referer }.values_at(*from).find(&:present?) || from
72
+ end
73
+ end
74
+
75
+ ActiveSupport.on_load(:action_controller) { include ActionController::StashedRedirects }
metadata ADDED
@@ -0,0 +1,70 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: action_controller-stashed_redirects
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Kasper Timm Hansen
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2022-04-21 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: actionpack
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '7.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '7.0'
27
+ description:
28
+ email:
29
+ - hey@kaspth.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - CHANGELOG.md
35
+ - Gemfile
36
+ - Gemfile.lock
37
+ - LICENSE.txt
38
+ - README.md
39
+ - Rakefile
40
+ - action_controller-stashed_redirects.gemspec
41
+ - lib/action_controller/stashed_redirects.rb
42
+ - lib/action_controller/stashed_redirects/version.rb
43
+ homepage: https://github.com/kaspth/action_controller-stashed_redirects
44
+ licenses:
45
+ - MIT
46
+ metadata:
47
+ homepage_uri: https://github.com/kaspth/action_controller-stashed_redirects
48
+ source_code_uri: https://github.com/kaspth/action_controller-stashed_redirects
49
+ changelog_uri: https://github.com/kaspth/action_controller-stashed_redirects/blob/main/CHANGELOG.md
50
+ post_install_message:
51
+ rdoc_options: []
52
+ require_paths:
53
+ - lib
54
+ required_ruby_version: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: 2.7.0
59
+ required_rubygems_version: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: '0'
64
+ requirements: []
65
+ rubygems_version: 3.3.11
66
+ signing_key:
67
+ specification_version: 4
68
+ summary: Embed a controller flow within another by stashing the final redirect upfront
69
+ and performing it after completing.
70
+ test_files: []