workarea-basic_auth 1.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 (77) hide show
  1. checksums.yaml +7 -0
  2. data/.editorconfig +20 -0
  3. data/.github/ISSUE_TEMPLATE/bug_report.md +37 -0
  4. data/.github/ISSUE_TEMPLATE/documentation-request.md +17 -0
  5. data/.github/ISSUE_TEMPLATE/feature_request.md +20 -0
  6. data/.gitignore +25 -0
  7. data/CHANGELOG.md +371 -0
  8. data/CODE_OF_CONDUCT.md +3 -0
  9. data/CONTRIBUTING.md +3 -0
  10. data/Gemfile +5 -0
  11. data/LICENSE +52 -0
  12. data/README.md +105 -0
  13. data/Rakefile +60 -0
  14. data/bin/rails +20 -0
  15. data/config/initializers/access_control.rb +23 -0
  16. data/lib/tasks/basic_auth_tasks.rake +4 -0
  17. data/lib/workarea/basic_auth.rb +23 -0
  18. data/lib/workarea/basic_auth/engine.rb +8 -0
  19. data/lib/workarea/basic_auth/middleware.rb +82 -0
  20. data/lib/workarea/basic_auth/path.rb +37 -0
  21. data/lib/workarea/basic_auth/railtie.rb +16 -0
  22. data/lib/workarea/basic_auth/simple_route_set.rb +23 -0
  23. data/lib/workarea/basic_auth/version.rb +5 -0
  24. data/test/dummy/Rakefile +6 -0
  25. data/test/dummy/app/assets/config/manifest.js +4 -0
  26. data/test/dummy/app/assets/images/.keep +0 -0
  27. data/test/dummy/app/assets/javascripts/application.js +13 -0
  28. data/test/dummy/app/assets/stylesheets/application.css +15 -0
  29. data/test/dummy/app/controllers/application_controller.rb +3 -0
  30. data/test/dummy/app/controllers/concerns/.keep +0 -0
  31. data/test/dummy/app/helpers/application_helper.rb +2 -0
  32. data/test/dummy/app/jobs/application_job.rb +2 -0
  33. data/test/dummy/app/mailers/application_mailer.rb +4 -0
  34. data/test/dummy/app/models/concerns/.keep +0 -0
  35. data/test/dummy/app/views/layouts/application.html.erb +14 -0
  36. data/test/dummy/app/views/layouts/mailer.html.erb +13 -0
  37. data/test/dummy/app/views/layouts/mailer.text.erb +1 -0
  38. data/test/dummy/bin/bundle +3 -0
  39. data/test/dummy/bin/rails +4 -0
  40. data/test/dummy/bin/rake +4 -0
  41. data/test/dummy/bin/setup +34 -0
  42. data/test/dummy/bin/update +29 -0
  43. data/test/dummy/config.ru +5 -0
  44. data/test/dummy/config/application.rb +17 -0
  45. data/test/dummy/config/boot.rb +5 -0
  46. data/test/dummy/config/cable.yml +9 -0
  47. data/test/dummy/config/environment.rb +5 -0
  48. data/test/dummy/config/environments/development.rb +56 -0
  49. data/test/dummy/config/environments/production.rb +86 -0
  50. data/test/dummy/config/environments/test.rb +43 -0
  51. data/test/dummy/config/initializers/application_controller_renderer.rb +6 -0
  52. data/test/dummy/config/initializers/assets.rb +11 -0
  53. data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
  54. data/test/dummy/config/initializers/cookies_serializer.rb +5 -0
  55. data/test/dummy/config/initializers/filter_parameter_logging.rb +4 -0
  56. data/test/dummy/config/initializers/inflections.rb +16 -0
  57. data/test/dummy/config/initializers/mime_types.rb +4 -0
  58. data/test/dummy/config/initializers/new_framework_defaults.rb +21 -0
  59. data/test/dummy/config/initializers/session_store.rb +3 -0
  60. data/test/dummy/config/initializers/workarea.rb +4 -0
  61. data/test/dummy/config/initializers/wrap_parameters.rb +14 -0
  62. data/test/dummy/config/locales/en.yml +23 -0
  63. data/test/dummy/config/puma.rb +47 -0
  64. data/test/dummy/config/routes.rb +5 -0
  65. data/test/dummy/config/secrets.yml +22 -0
  66. data/test/dummy/config/spring.rb +6 -0
  67. data/test/dummy/db/seeds.rb +2 -0
  68. data/test/dummy/lib/assets/.keep +0 -0
  69. data/test/dummy/log/.keep +0 -0
  70. data/test/factories/workarea/testing/basic_auth_helper.rb +21 -0
  71. data/test/lib/workarea/basic_auth/middleware_test.rb +149 -0
  72. data/test/lib/workarea/basic_auth/path_test.rb +50 -0
  73. data/test/lib/workarea/basic_auth/simple_route_set_test.rb +64 -0
  74. data/test/lib/workarea/basic_auth_test.rb +12 -0
  75. data/test/test_helper.rb +9 -0
  76. data/workarea-basic_auth.gemspec +39 -0
  77. metadata +226 -0
@@ -0,0 +1,3 @@
1
+ View this plugin's code of conduct here:
2
+
3
+ <https://github.com/workarea-commerce/workarea/blob/master/CODE_OF_CONDUCT.md>
data/CONTRIBUTING.md ADDED
@@ -0,0 +1,3 @@
1
+ View this plugin's contribution guidelines here:
2
+
3
+ <https://github.com/workarea-commerce/workarea/blob/master/CONTRIBUTING.md>
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source "https://rubygems.org"
2
+
3
+ gemspec
4
+
5
+ gem 'workarea'
data/LICENSE ADDED
@@ -0,0 +1,52 @@
1
+ WebLinc
2
+ Business Source License
3
+
4
+ Licensor: WebLinc Corporation, 22 S. 3rd Street, 2nd Floor, Philadelphia PA 19106
5
+
6
+ Licensed Work: Workarea Commerce Platform
7
+ The Licensed Work is (c) 2019 WebLinc Corporation
8
+
9
+ Additional Use Grant:
10
+ You may make production use of the Licensed Work without an additional license agreement with WebLinc so long as you do not use the Licensed Work for a Commerce Service.
11
+
12
+ A "Commerce Service" is a commercial offering that allows third parties (other than your employees and contractors) to access the functionality of the Licensed Work by creating or managing commerce functionality, the products, taxonomy, assets and/or content of which are controlled by such third parties.
13
+
14
+ For information about obtaining an additional license agreement with WebLinc, contact licensing@workarea.com.
15
+
16
+ Change Date: 2019-08-20
17
+
18
+ Change License: Version 2.0 or later of the GNU General Public License as published by the Free Software Foundation
19
+
20
+ Terms
21
+
22
+ The Licensor hereby grants you the right to copy, modify, create derivative works, redistribute, and make non-production use of the Licensed Work. The Licensor may make an Additional Use Grant, above, permitting limited production use.
23
+
24
+ Effective on the Change Date, or the fourth anniversary of the first publicly available distribution of a specific version of the Licensed Work under this License, whichever comes first, the Licensor hereby grants you rights under the terms of the Change License, and the rights granted in the paragraph above terminate.
25
+
26
+ If your use of the Licensed Work does not comply with the requirements currently in effect as described in this License, you must purchase a commercial license from the Licensor, its affiliated entities, or authorized resellers, or you must refrain from using the Licensed Work.
27
+
28
+ All copies of the original and modified Licensed Work, and derivative works of the Licensed Work, are subject to this License. This License applies separately for each version of the Licensed Work and the Change Date may vary for each version of the Licensed Work released by Licensor.
29
+
30
+ You must conspicuously display this License on each original or modified copy of the Licensed Work. If you receive the Licensed Work in original or modified form from a third party, the terms and conditions set forth in this License apply to your use of that work.
31
+
32
+ Any use of the Licensed Work in violation of this License will automatically terminate your rights under this License for the current and all other versions of the Licensed Work.
33
+
34
+ This License does not grant you any right in any trademark or logo of Licensor or its affiliates (provided that you may use a trademark or logo of Licensor as expressly required by this License). TO THE EXTENT PERMITTED BY APPLICABLE LAW, THE LICENSED WORK IS PROVIDED ON AN "AS IS" BASIS. LICENSOR HEREBY DISCLAIMS ALL WARRANTIES AND CONDITIONS, EXPRESS OR IMPLIED, INCLUDING (WITHOUT LIMITATION) WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT, AND TITLE. MariaDB hereby grants you permission to use this License’s text to license your works and to refer to it using the trademark "Business Source License" as long as you comply with the Covenants of Licensor below.
35
+
36
+ Covenants of Licensor
37
+ In consideration of the right to use this License’s text and the "Business Source License" name and trademark, Licensor covenants to MariaDB, and to all other recipients of the licensed work to be provided by Licensor:
38
+
39
+ To specify as the Change License the GPL Version 2.0 or any later version, or a license that is compatible with GPL Version 2.0 or a later version, where "compatible" means that software provided under the Change License can be included in a program with software provided under GPL Version 2.0 or a later version. Licensor may specify additional Change Licenses without limitation.
40
+
41
+ To either: (a) specify an additional grant of rights to use that does not impose any additional restriction on the right granted in this License, as the Additional Use Grant; or (b) insert the text "None."
42
+
43
+ To specify a Change Date.
44
+
45
+ Not to modify this License in any other way.
46
+
47
+ Notice
48
+ The Business Source License (this document, or the "License") is not an Open Source license. However, the Licensed Work will eventually be made available under an Open Source License, as stated in this License.
49
+
50
+ For more information on the use of the Business Source License generally, please visit the Adopting and Developing Business Source License FAQ.
51
+
52
+ License text copyright (c) 2017 MariaDB Corporation Ab, All Rights Reserved. "Business Source License" is a trademark of MariaDB Corporation Ab.
data/README.md ADDED
@@ -0,0 +1,105 @@
1
+ Workarea Basic Auth
2
+ ================================================================================
3
+
4
+ A Workarea Commerce plugin that adds middleware to enable HTTP Basic Auth for the Workarea Commerce platform, preventing undesired traffic (like bots) on staging and QA environments.
5
+
6
+ Getting Started
7
+ --------------------------------------------------------------------------------
8
+
9
+ Add the gem to your application's Gemfile:
10
+
11
+ ```ruby
12
+ # ...
13
+ gem 'workarea-basic_auth'
14
+ # ...
15
+ ```
16
+
17
+ Update your application's bundle.
18
+
19
+ ```bash
20
+ cd path/to/application
21
+ bundle
22
+ ```
23
+
24
+ Configuration
25
+ --------------------------------------------------------------------------------
26
+
27
+ By default, `workarea-basic-auth` is disabled. To require basic authentication for routes in your application, set the following properties in `Workarea.config`:
28
+
29
+ ```ruby
30
+ Workarea.configure do |config|
31
+ config.basic_auth.enabled = true
32
+ config.basic_auth.user = 'my_username'
33
+ config.basic_auth.pass = 'my_password'
34
+ config.basic_auth.protect_routes.add('/products')
35
+ config.basic_auth.protect_routes.add('/categories')
36
+ config.basic_auth.protect_routes.add('/contact')
37
+ config.basic_auth.exclude_routes.add('/api*')
38
+ config.basic_auth.exclude_routes.add('/products/my-sweet-product')
39
+ end
40
+ ```
41
+
42
+ The configuration above will require HTTP basic auth for all routes and HTTP methods other than those that start with `/api` or `/products/my-sweet-product`.
43
+
44
+ ### Excluding Assets
45
+
46
+ Need to exclude assets from http basic auth?
47
+
48
+ ```ruby
49
+ Workarea.configure do |config|
50
+ config.basic_auth.enabled = true
51
+ config.basic_auth.user = 'my_username'
52
+ config.basic_auth.pass = 'my_password'
53
+ config.basic_auth.exclude_routes.add('/assets/*')
54
+ end
55
+ ```
56
+
57
+
58
+ ### Excluding routes based off HTTP method
59
+
60
+ You can also specify protecting or excluding protection of routes for specific
61
+ HTTP methods:
62
+
63
+ ```ruby
64
+ Workarea.configure do |config|
65
+ config.basic_auth.enabled = true
66
+ config.basic_auth.user = 'my_username'
67
+ config.basic_auth.pass = 'my_password'
68
+ config.basic_auth.protect_routes.add('/login', :post, :option)
69
+ config.basic_auth.protect_routes.add('/contact', :put)
70
+ end
71
+ ```
72
+
73
+ The configuration above will require HTTP basic auth when sending a
74
+ `POST` or `OPTION` request to `/login` or a `PUT` request to `/contact`.
75
+
76
+ ### Excluding routes based off a Proc
77
+
78
+ Sometimes you may run into a case where path/method just won't cut it. You can
79
+ pass a protect or exclude route a proc that will be passed a Rack::Request object.
80
+ Anytime the proc returns true, that path will match for either protection or exclusion.
81
+
82
+ For example, in order to allow the AWS ElasticLoadBalancer the ability to check
83
+ an instances health, we exclude basic auth protection from any request where the
84
+ User Agent contains 'ELB-HealthChecker':
85
+
86
+ ```ruby
87
+ Workarea.configure do |config|
88
+ config.basic_auth.enabled = true
89
+ config.basic_auth.user = 'my_username'
90
+ config.basic_auth.pass = 'my_password'
91
+ config.basic_auth.exclude_routes.add('/*', ->(request) {
92
+ request.env['HTTP_USER_AGENT'].include?('ELB-HealthChecker')
93
+ })
94
+ end
95
+ ```
96
+
97
+ Workarea Commerce Documentation
98
+ --------------------------------------------------------------------------------
99
+
100
+ See [https://developer.workarea.com](https://developer.workarea.com) for Workarea Commerce documentation.
101
+
102
+ License
103
+ --------------------------------------------------------------------------------
104
+
105
+ Workarea Basic Auth is released under the [Business Software License](LICENSE)
data/Rakefile ADDED
@@ -0,0 +1,60 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+ RDoc::Task.new(:rdoc) do |rdoc|
9
+ rdoc.rdoc_dir = 'rdoc'
10
+ rdoc.title = 'BasicAuth'
11
+ rdoc.options << '--line-numbers'
12
+ rdoc.rdoc_files.include('README.md')
13
+ rdoc.rdoc_files.include('lib/**/*.rb')
14
+ end
15
+
16
+ APP_RAKEFILE = File.expand_path("../test/dummy/Rakefile", __FILE__)
17
+ load 'rails/tasks/engine.rake'
18
+ load 'rails/tasks/statistics.rake'
19
+ load 'workarea/changelog.rake'
20
+
21
+ require 'rake/testtask'
22
+ Rake::TestTask.new(:test) do |t|
23
+ t.libs << 'lib'
24
+ t.libs << 'test'
25
+ t.pattern = 'test/**/*_test.rb'
26
+ t.verbose = false
27
+ end
28
+ task default: :test
29
+
30
+ $LOAD_PATH.unshift File.expand_path('../lib', __FILE__)
31
+ require 'workarea/basic_auth/version'
32
+
33
+ desc "Release version #{Workarea::BasicAuth::VERSION} of the gem"
34
+ task :release do
35
+ host = "https://#{ENV['BUNDLE_GEMS__WEBLINC__COM']}@gems.weblinc.com"
36
+
37
+ #Rake::Task['workarea:changelog'].execute
38
+ #system 'git add CHANGELOG.md'
39
+ #system 'git commit -m "Update CHANGELOG"'
40
+ #system 'git push origin HEAD'
41
+
42
+ system "git tag -a v#{Workarea::BasicAuth::VERSION} -m 'Tagging #{Workarea::BasicAuth::VERSION}'"
43
+ system 'git push --tags'
44
+
45
+ system "gem build workarea-basic_auth.gemspec"
46
+ system "gem push workarea-basic_auth-#{Workarea::BasicAuth::VERSION}.gem"
47
+ system "gem push workarea-basic_auth-#{Workarea::BasicAuth::VERSION}.gem --host #{host}"
48
+ system "rm workarea-basic_auth-#{Workarea::BasicAuth::VERSION}.gem"
49
+ end
50
+
51
+ desc 'Run the JavaScript tests'
52
+ ENV['TEASPOON_RAILS_ENV'] = File.expand_path('../test/dummy/config/environment', __FILE__)
53
+ task teaspoon: 'app:teaspoon'
54
+
55
+ desc 'Start a server at http://localhost:3000/teaspoon for JavaScript tests'
56
+ task :teaspoon_server do
57
+ Dir.chdir("test/dummy")
58
+ teaspoon_env = File.expand_path('../test/teaspoon_env.rb', __FILE__)
59
+ system "RAILS_ENV=test TEASPOON_ENV=#{teaspoon_env} rails s"
60
+ end
data/bin/rails ADDED
@@ -0,0 +1,20 @@
1
+ #!/usr/bin/env ruby
2
+ # This command will automatically be run when you run "rails" with Rails gems
3
+ # installed from the root of your application.
4
+
5
+ ENGINE_ROOT = File.expand_path("../..", __FILE__)
6
+ ENGINE_PATH = File.expand_path("../../lib/basic_auth/engine", __FILE__)
7
+ APP_PATH = File.expand_path('../test/dummy/config/application', __dir__)
8
+
9
+ # Set up gems listed in the Gemfile.
10
+ ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile", __FILE__)
11
+ require "bundler/setup" if File.exist?(ENV["BUNDLE_GEMFILE"])
12
+
13
+ require 'action_controller/railtie'
14
+ require 'action_view/railtie'
15
+ require 'action_mailer/railtie'
16
+ require 'rails/test_unit/railtie'
17
+ require 'sprockets/railtie'
18
+ require 'teaspoon-mocha'
19
+
20
+ require 'rails/engine/commands'
@@ -0,0 +1,23 @@
1
+ Workarea.configure do |config|
2
+ basic_auth = ActiveSupport::Configurable::Configuration.new
3
+ basic_auth.enabled = false
4
+
5
+ basic_auth.protect_routes = Workarea::BasicAuth::SimpleRouteSet.new
6
+ basic_auth.exclude_routes = Workarea::BasicAuth::SimpleRouteSet.new
7
+
8
+ basic_auth.exclude_routes.add("/*", ->(request) {
9
+ request.env["HTTP_USER_AGENT"].to_s.include?("ELB-HealthChecker")
10
+ })
11
+
12
+ basic_auth.protect_routes.add("/*")
13
+ basic_auth.exclude_routes.add("/api*")
14
+ basic_auth.exclude_routes.add("/assets/*")
15
+ basic_auth.exclude_routes.add("/media/*")
16
+ basic_auth.exclude_routes.add("/product_images/*")
17
+
18
+ basic_auth.whitelisted_ips = Rack::Attack::ALERT_LOGIC_IP_ADDRESSES
19
+
20
+ Workarea.config.basic_auth = basic_auth
21
+
22
+ Rails.application.config.middleware.insert_before 0, Workarea::BasicAuth::Middleware
23
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :basic_auth do
3
+ # # Task goes here
4
+ # end
@@ -0,0 +1,23 @@
1
+ require "rack"
2
+ require "workarea"
3
+ require "workarea/admin"
4
+ require "workarea/storefront"
5
+ require "workarea/basic_auth/engine"
6
+ require "workarea/basic_auth/version"
7
+
8
+ module Workarea
9
+ module BasicAuth
10
+ def self.enabled?
11
+ config.enabled
12
+ end
13
+
14
+ def self.config
15
+ Workarea.config.basic_auth
16
+ end
17
+ end
18
+ end
19
+
20
+ require "workarea/basic_auth/path"
21
+ require "workarea/basic_auth/simple_route_set"
22
+ require "workarea/basic_auth/middleware"
23
+ require "workarea/basic_auth/railtie"
@@ -0,0 +1,8 @@
1
+ module Workarea
2
+ module BasicAuth
3
+ class Engine < ::Rails::Engine
4
+ include Workarea::Plugin
5
+ isolate_namespace Workarea::BasicAuth
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,82 @@
1
+ require "rack/auth/abstract/handler"
2
+ require "rack/auth/abstract/request"
3
+
4
+ module Workarea
5
+ module BasicAuth
6
+ class Middleware < Rack::Auth::AbstractHandler
7
+ def initialize(app)
8
+ @app = app
9
+ @realm = "workarea-basic-auth"
10
+ @authenticator = Proc.new do |user, pass|
11
+ (Workarea.config.basic_auth.user == user &&
12
+ Workarea.config.basic_auth.pass == pass)
13
+ end
14
+ end
15
+
16
+ def call(env)
17
+ return @app.call(env) unless Workarea::BasicAuth.enabled?
18
+
19
+ auth = Middleware::Request.new(env)
20
+
21
+ return @app.call(env) unless auth.required?
22
+ return unauthorized unless auth.provided?
23
+ return bad_request unless auth.basic?
24
+ return unauthorized unless valid?(auth)
25
+
26
+ env["REMOTE_USER"] = auth.username
27
+ @app.call(env)
28
+ end
29
+
30
+ private
31
+
32
+ def challenge
33
+ 'Basic realm="%s"' % realm
34
+ end
35
+
36
+ def valid?(auth)
37
+ @authenticator.call(*auth.credentials)
38
+ end
39
+
40
+ class Request < Rack::Auth::AbstractRequest
41
+ def required?
42
+ !ip_whitelisted? && path_is_protected?
43
+ end
44
+
45
+ def path_is_protected?
46
+ return false if excluded_routes.matches?(request)
47
+ protected_routes.matches?(request)
48
+ end
49
+
50
+ def ip_whitelisted?
51
+ whitelisted_ips.any? { |ip| ip.include?(request.ip) }
52
+ end
53
+
54
+ def basic?
55
+ "basic" == scheme
56
+ end
57
+
58
+ def credentials
59
+ @credentials ||= params.unpack("m*").first.split(/:/, 2)
60
+ end
61
+
62
+ def username
63
+ credentials.first
64
+ end
65
+
66
+ private
67
+
68
+ def protected_routes
69
+ Workarea.config.basic_auth.protect_routes
70
+ end
71
+
72
+ def excluded_routes
73
+ Workarea.config.basic_auth.exclude_routes
74
+ end
75
+
76
+ def whitelisted_ips
77
+ Workarea.config.basic_auth.whitelisted_ips
78
+ end
79
+ end
80
+ end
81
+ end
82
+ end
@@ -0,0 +1,37 @@
1
+ module Workarea
2
+ module BasicAuth
3
+ class Path
4
+ attr_reader :regexp
5
+
6
+ def initialize(string, *http_methods)
7
+ path = Regexp.escape(string).gsub('\*', ".*?")
8
+ @regexp = Regexp.new("^#{path}$", true)
9
+
10
+ if http_methods && http_methods.first.is_a?(Proc)
11
+ @proc = http_methods.first
12
+ end
13
+
14
+ @http_methods = http_methods
15
+ end
16
+
17
+ def matches?(request)
18
+ path_matches?(request) && request_matches?(request)
19
+ end
20
+
21
+ private
22
+
23
+ def path_matches?(request)
24
+ !@regexp.match(request.path_info).nil?
25
+ end
26
+
27
+ def request_matches?(request)
28
+ if @proc
29
+ @proc.call(request)
30
+ else
31
+ method = request.env["REQUEST_METHOD"].downcase.to_sym
32
+ @http_methods.empty? || @http_methods.include?(method)
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end