katalyst-basic-auth 1.0.1 → 2.0.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e0690a58df78d87a4b6ec2a4399953e9e42a3a6cad751dd80e7583881cc9a620
4
- data.tar.gz: 56c1bd702be0b16cabc4b66291724ee344ec6ae6130011bb50aa71aad04d31ac
3
+ metadata.gz: d8a5aad0435c2f6a2b342329168f0ed68d0c541803f80bbf1fab1993545545d7
4
+ data.tar.gz: 4906fb1519f090f47d5b5a5ac93b4cdeebd0bcc633b45ade268299f4150f9ee5
5
5
  SHA512:
6
- metadata.gz: 7475af04fa7c36c0dd6a09bc7713880807c64678d480a13184bad2db6ae1fc2867c26e92a3bbfbbd337a9161d2be94642f8c6a68716731bda81450557efa7b1b
7
- data.tar.gz: aaea9752360f625d4aaaaac20fa29358553098f08e03ed7a85ae7a75b1b3e80a7f561d69aa1d8fc5b90c79afa8949f259a1ac651685c206efad5fe4206fb1d9b
6
+ metadata.gz: 1617b9b1a07663056906985e029d413fb765e8b42b6031d559f2b4fba95235c339fa4ae95682a94a1d90c656bf706021d294680a08ba29b6dbd82427af075795
7
+ data.tar.gz: ffe15df4fdd8a3eda37ac7c11a53d7fd6a1c6c3a702c4c7a8ad2c57c16da28a1a9b913426d1a25b1ae3d95cdb55d6b45bea7609416ffecad1780d1797a4428ac
data/CHANGELOG.md CHANGED
@@ -1,7 +1,19 @@
1
+ ## [2.0.0] - 2026-11-26
2
+
3
+ - Breaking: when more than one rule matches a request, the rule added last applies; previously the most specific path
4
+ applied regardless of order
5
+ - Breaking: drops support for Ruby < 4
6
+ - Add support for `Regexp` paths, e.g. `exclude(%r{/clients/[^/]+/webhooks/[^/]+})`; patterns must match the whole
7
+ request path
8
+ - String paths are matched literally, as in 1.0.1; apps that relied on regular expressions in string paths should
9
+ upgrade to 2.0 and pass a `Regexp` instead
10
+
1
11
  ## [1.0.1] - 2026-11-26
2
12
 
3
13
  - Fix: normalise repeated slashes before matching paths, e.g. `//admin` is matched as `/admin`
4
14
  - Fix: match configured paths on segment boundaries, e.g. excluding `/up` no longer excludes `/uploads`
15
+ - Breaking (unintentional): string paths are matched literally, so regular expressions in string paths such as
16
+ `/clients/.*/webhooks/.*` no longer match; upgrade to 2.0 and use a `Regexp` for patterns
5
17
 
6
18
  ## [1.0.0] - 2025-08-04
7
19
 
data/README.md CHANGED
@@ -48,6 +48,18 @@ Paths can also be excluded from basic auth. e.g. to allow access to `/public` wi
48
48
 
49
49
  Katalyst::Basic::Auth.exclude("/public")
50
50
 
51
+ Paths match themselves and anything below them, e.g. `/public` matches `/public` and `/public/robots.txt` but not
52
+ `/publications`. Use a `Regexp` to match other paths. A pattern must match the whole request path:
53
+
54
+ Katalyst::Basic::Auth.exclude(%r{/clients/[^/]+/webhooks/[^/]+})
55
+
56
+ When more than one rule matches a request, the rule added last applies, so add general rules before their exceptions:
57
+
58
+ Katalyst::Basic::Auth.exclude("/admin")
59
+ Katalyst::Basic::Auth.add("/admin/secure", username: "user", password: "pass")
60
+
61
+ Requests that match no rule use the global settings.
62
+
51
63
  ## Development
52
64
 
53
65
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
@@ -15,12 +15,10 @@ module Katalyst
15
15
  include Enumerable
16
16
 
17
17
  # @param path [String] Request path
18
- # @return [Config] The config for the given path
18
+ # @return [Config] The last rule added that matches the given path
19
19
  def for_path(path)
20
20
  path = normalize_path(path)
21
- all.sort_by(&:path)
22
- .reverse
23
- .detect { |i| i.match?(path) } || global
21
+ all.rfind { |i| i.match?(path) }
24
22
  end
25
23
 
26
24
  # Compare paths the way the Rails router will see them, e.g. "//admin" routes to "/admin"
@@ -32,10 +30,11 @@ module Katalyst
32
30
 
33
31
  # @return [Config] The global configuration
34
32
  def global
35
- all[0]
33
+ all.first
36
34
  end
37
35
 
38
- # @param path [String] Relative path
36
+ # Adds a rule that takes priority over the rules added before it
37
+ # @param path [String, Regexp] Relative path, or a pattern that must match the whole request path
39
38
  # @param username [String] Basic auth user name
40
39
  # @param password [String] Basic auth password
41
40
  # @param enabled [Boolean] True to enable basic auth for this path
@@ -48,7 +47,6 @@ module Katalyst
48
47
  enabled: enabled,
49
48
  ip_allowlist: ip_allowlist,
50
49
  )
51
- all.delete(all.detect { |i| i.path == config.path })
52
50
  all << config
53
51
  config
54
52
  end
@@ -57,12 +55,14 @@ module Katalyst
57
55
  new(path: path, enabled: false)
58
56
  end
59
57
 
58
+ # @return [Array<Config>] Rules in the order they were added, starting with the global configuration and
59
+ # the Rails health check
60
60
  def all
61
61
  @all ||= [new, up]
62
62
  end
63
63
 
64
64
  def reset!
65
- @all = [new, up]
65
+ @all = nil
66
66
  end
67
67
 
68
68
  def each(&)
@@ -133,12 +133,18 @@ module Katalyst
133
133
  path == ROOT_PATH
134
134
  end
135
135
 
136
- # Matches the configured path and anything below it, on path segment boundaries,
137
- # e.g. "/up" matches "/up" and "/up/db" but not "/uploads"
136
+ def pattern?
137
+ path.is_a?(Regexp)
138
+ end
139
+
140
+ # Paths match themselves and anything below them, on path segment boundaries,
141
+ # e.g. "/up" matches "/up" and "/up/db" but not "/uploads".
142
+ # Patterns must match the whole request path, e.g. %r{/clients/[^/]+/webhooks/[^/]+}.
138
143
  # @param request_path [String] Normalized request path
139
144
  def match?(request_path)
140
- prefix = path.chomp("/")
141
- request_path == prefix || request_path.start_with?("#{prefix}/")
145
+ return /\A(?:#{path})\z/.match?(request_path) if pattern?
146
+
147
+ request_path == path || request_path.start_with?("#{path.chomp('/')}/")
142
148
  end
143
149
 
144
150
  def allow_ip?(env)
@@ -151,7 +157,7 @@ module Katalyst
151
157
 
152
158
  def description
153
159
  output = []
154
- output << "path: #{root_path? ? '(global)' : path}"
160
+ output << "path: #{root_path? ? '(global)' : path_description}"
155
161
  output << "enabled: #{enabled?}"
156
162
  output << "username: #{username}"
157
163
  output << "password: #{password}"
@@ -161,7 +167,11 @@ module Katalyst
161
167
 
162
168
  private
163
169
 
164
- # @param path [String] Relative path
170
+ def path_description
171
+ pattern? ? path.inspect : path
172
+ end
173
+
174
+ # @param path [String, Regexp] Relative path, or a pattern that must match the whole request path
165
175
  # @param username [String] Basic auth user name
166
176
  # @param password [String] Basic auth password
167
177
  # @param enabled [Boolean] True to enable basic auth for this path
@@ -176,9 +186,10 @@ module Katalyst
176
186
 
177
187
  def sanitize_path(path)
178
188
  return ROOT_PATH if path.nil?
189
+ return path if path.is_a?(Regexp)
179
190
 
180
191
  path = "/#{path}" unless path.start_with?("/")
181
- path
192
+ path == ROOT_PATH ? path : path.chomp("/")
182
193
  end
183
194
 
184
195
  def initialize_ip_allowlist(ip_allowlist)
@@ -9,7 +9,7 @@ module Katalyst
9
9
  module Auth
10
10
  class << self
11
11
  # Add a path to be protected by basic authentication
12
- # @param path [String] Relative path
12
+ # @param path [String, Regexp] Relative path, or a pattern that must match the whole request path
13
13
  # @param username [String] Basic auth user name
14
14
  # @param password [String] Basic auth password
15
15
  # @param ip_allowlist [Array<String>] List of IP addresses or network ranges to allow without basic auth
@@ -22,7 +22,7 @@ module Katalyst
22
22
  end
23
23
 
24
24
  # Add a path to be excluded from basic authentication
25
- # @param path [String] Relative path
25
+ # @param path [String, Regexp] Relative path, or a pattern that must match the whole request path
26
26
  def exclude(path)
27
27
  Config.add(path: path, enabled: false)
28
28
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: katalyst-basic-auth
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Katalyst Interactive
@@ -41,7 +41,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
41
41
  requirements:
42
42
  - - ">="
43
43
  - !ruby/object:Gem::Version
44
- version: '3.3'
44
+ version: '4.0'
45
45
  required_rubygems_version: !ruby/object:Gem::Requirement
46
46
  requirements:
47
47
  - - ">="