prx_auth-rails 3.0.1 → 4.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3212721cc8a3569576581017749f0af4ebca8c3c6c50684f9d88587057e9517b
4
- data.tar.gz: 41f92ebcf2c167cb48d00c39319a1a6663d9a7203aa9c6c7d69007f8230ba011
3
+ metadata.gz: 1eed8329985438f59a1adc529c8e33748cbfca9becbd285475385c16b25639e6
4
+ data.tar.gz: 0a065d8fdf1e4d077fdd43da82cc37c3110ada401d31e6eadb5e154ae7001c6f
5
5
  SHA512:
6
- metadata.gz: c3fa282bf2f549e40761da8b04ddb3944c20492818598bc2d391e7cc5e79b032a41c2d5914b5b2f7f70a3401492548149eb308b08b030efbcd87d817b05f7698
7
- data.tar.gz: 6e25421712eb18d89fa5aa6fda01eb1641262f41ae294aa39630755ef342a743ecbd6d02bd985469eb56db25d20e4e0121696b5de8338219edf75d9597f16363
6
+ metadata.gz: 9f45b17435edca7e49910164e330eea45df6c466514b700af6f04182e7df99748d3978911cd777325fb9142e4e9f0e1723bec10917eeea8c04b54b4c98c521b1
7
+ data.tar.gz: 1dffecbaef3bf75a75759f6312a9acfb3442e5a6ff7b4354abc9e8b19816618f5fe57fd515317cfa03eaf7da0b231c2489b30c3a1f1aab2eca93f9a3e3b17d6b
data/README.md CHANGED
@@ -46,7 +46,10 @@ In your rails app, add a file to config/initializers called
46
46
  PrxAuth::Rails.configure do |config|
47
47
 
48
48
  # enables automatic installation of token parser middleware
49
- config.install_middleware = false # default: true
49
+ config.install_middleware = true # default: true
50
+
51
+ # set the ID host
52
+ config.id_host = 'id.staging.prx.tech' # default: id.prx.org
50
53
 
51
54
  # automatically adds namespace to all scoped queries, e.g. .authorized?(:foo) will be treated
52
55
  # as .authorized?(:my_great_ns, :foo). Has no impact on unscoped queries.
@@ -2,27 +2,34 @@ class PrxAuth::Rails::Configuration
2
2
  attr_accessor :install_middleware,
3
3
  :namespace,
4
4
  :prx_client_id,
5
- :id_host
5
+ :id_host,
6
+ :cert_path
6
7
 
8
+ DEFAULT_ID_HOST = 'id.prx.org'
9
+ DEFAULT_CERT_PATH = 'api/v1/certs'
7
10
 
8
11
  def initialize
9
12
  @install_middleware = true
10
- if defined?(::Rails)
11
- klass = ::Rails.application.class
12
- parent_name = if ::Rails::VERSION::MAJOR >= 6
13
- klass.module_parent_name
14
- else
15
- klass.parent_name
16
- end
17
- klass_name = if parent_name.present?
18
- parent_name
19
- else
20
- klass.name
21
- end
13
+ @prx_client_id = nil
14
+ @id_host = DEFAULT_ID_HOST
15
+ @cert_path = DEFAULT_CERT_PATH
22
16
 
23
- @namespace = klass_name.underscore.intern
24
- @prx_client_id = nil
25
- @id_host = nil
26
- end
17
+ # infer default namespace from app name
18
+ @namespace =
19
+ if defined?(::Rails)
20
+ klass = ::Rails.application.class
21
+ parent_name = if ::Rails::VERSION::MAJOR >= 6
22
+ klass.module_parent_name
23
+ else
24
+ klass.parent_name
25
+ end
26
+ klass_name = if parent_name.present?
27
+ parent_name
28
+ else
29
+ klass.name
30
+ end
31
+
32
+ klass_name.underscore.intern
33
+ end
27
34
  end
28
35
  end
@@ -7,11 +7,5 @@ module PrxAuth::Rails
7
7
  config.to_prepare do
8
8
  ApplicationController.send(:include, PrxAuth::Rails::Controller)
9
9
  end
10
-
11
- initializer 'prx_auth.insert_middleware' do |app|
12
- if PrxAuth::Rails.configuration.install_middleware
13
- app.config.middleware.insert_after Rack::Head, Rack::PrxAuth
14
- end
15
- end
16
10
  end
17
11
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module PrxAuth
4
4
  module Rails
5
- VERSION = '3.0.1'
5
+ VERSION = '4.0.0'
6
6
  end
7
7
  end
@@ -6,10 +6,36 @@ require "prx_auth/rails/engine" if defined?(Rails)
6
6
  module PrxAuth
7
7
  module Rails
8
8
  class << self
9
- attr_accessor :configuration
9
+ attr_accessor :configuration, :installed_middleware
10
10
 
11
11
  def configure
12
- yield configuration
12
+ yield configuration if block_given?
13
+
14
+ # only install from first call to configure block
15
+ if configuration.install_middleware && !installed_middleware
16
+ install_middleware!
17
+ self.installed_middleware = true
18
+ end
19
+ end
20
+
21
+ def install_middleware!(app = nil)
22
+ app ||= ::Rails.application if defined?(::Rails)
23
+
24
+ return false unless app
25
+
26
+ # guess protocol from host
27
+ host = configuration.id_host
28
+ path = configuration.cert_path
29
+ protocol =
30
+ if host.include?('localhost') || host.include?('127.0.0.1')
31
+ 'http'
32
+ else
33
+ 'https'
34
+ end
35
+
36
+ app.middleware.insert_after Rack::Head, Rack::PrxAuth,
37
+ cert_location: "#{protocol}://#{host}/#{path}",
38
+ issuer: host
13
39
  end
14
40
  end
15
41
 
@@ -4,33 +4,32 @@ describe PrxAuth::Rails::Configuration do
4
4
 
5
5
  subject { PrxAuth::Rails::Configuration.new }
6
6
 
7
- it 'initializes with a namespace defined by rails app name' do
8
- assert subject.namespace == :dummy
7
+ it 'initializes with defaults' do
8
+ assert subject.install_middleware
9
+ assert_nil subject.prx_client_id
10
+ assert_equal 'id.prx.org', subject.id_host
11
+ assert_equal 'api/v1/certs', subject.cert_path
9
12
  end
10
13
 
11
- it 'can be reconfigured using the namespace attr' do
12
- PrxAuth::Rails.stub(:configuration, subject) do
13
- PrxAuth::Rails.configure do |config|
14
- config.namespace = :new_test
15
- end
16
-
17
- assert PrxAuth::Rails.configuration.namespace == :new_test
18
- end
14
+ it 'infers the default namespace from the rails app name' do
15
+ assert_equal :dummy, subject.namespace
19
16
  end
20
17
 
21
- it 'defaults to enabling the middleware' do
22
- PrxAuth::Rails.stub(:configuration, subject) do
23
- assert PrxAuth::Rails.configuration.install_middleware
24
- end
25
- end
26
-
27
- it 'allows overriding of the middleware automatic installation' do
18
+ it 'is updated by the prxauth configure block' do
28
19
  PrxAuth::Rails.stub(:configuration, subject) do
29
20
  PrxAuth::Rails.configure do |config|
30
21
  config.install_middleware = false
22
+ config.prx_client_id = 'some-id'
23
+ config.id_host = 'id.prx.blah'
24
+ config.cert_path = 'cert/path'
25
+ config.namespace = :new_test
31
26
  end
32
-
33
- assert !PrxAuth::Rails.configuration.install_middleware
34
27
  end
28
+
29
+ refute subject.install_middleware
30
+ assert_equal 'some-id', subject.prx_client_id
31
+ assert_equal 'id.prx.blah', subject.id_host
32
+ assert_equal 'cert/path', subject.cert_path
33
+ assert_equal :new_test, subject.namespace
35
34
  end
36
35
  end
@@ -0,0 +1,64 @@
1
+ require 'test_helper'
2
+ require 'pry'
3
+
4
+ describe PrxAuth::Rails do
5
+
6
+ subject { PrxAuth::Rails }
7
+
8
+ it 'gets a configuration' do
9
+ assert_equal :test_app, subject.configuration.namespace
10
+ assert_equal '1234', subject.configuration.prx_client_id
11
+ assert_equal 'id.prx.test', subject.configuration.id_host
12
+ assert_equal 'api/v1/certs', subject.configuration.cert_path
13
+ end
14
+
15
+ it 'installs and configures prx_auth middleware' do
16
+ mw = MiniTest::Mock.new
17
+ mw.expect :insert_after, nil do |c1, c2, cert_location:, issuer:|
18
+ assert_equal Rack::Head, c1
19
+ assert_equal Rack::PrxAuth, c2
20
+ assert_equal 'https://id.prx.test/api/v1/certs', cert_location
21
+ assert_equal 'id.prx.test', issuer
22
+ end
23
+
24
+ app = MiniTest::Mock.new
25
+ app.expect :middleware, mw
26
+
27
+ subject.install_middleware!(app)
28
+ mw.verify
29
+ end
30
+
31
+ it 'installs middleware after configuration' do
32
+ called = false
33
+ spy = -> { called = true }
34
+
35
+ PrxAuth::Rails.stub(:install_middleware!, spy) do
36
+ PrxAuth::Rails.installed_middleware = false
37
+
38
+ PrxAuth::Rails.configure do |config|
39
+ config.install_middleware = true
40
+ end
41
+
42
+ assert PrxAuth::Rails.installed_middleware
43
+ end
44
+
45
+ assert called
46
+ end
47
+
48
+ it 'allows overriding of the middleware automatic installation' do
49
+ called = false
50
+ spy = -> { called = true }
51
+
52
+ PrxAuth::Rails.stub(:install_middleware!, spy) do
53
+ PrxAuth::Rails.installed_middleware = false
54
+
55
+ PrxAuth::Rails.configure do |config|
56
+ config.install_middleware = false
57
+ end
58
+
59
+ refute PrxAuth::Rails.installed_middleware
60
+ end
61
+
62
+ refute called
63
+ end
64
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: prx_auth-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.0.1
4
+ version: 4.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chris Rhoden
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-12-05 00:00:00.000000000 Z
11
+ date: 2023-01-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: actionpack
@@ -265,12 +265,13 @@ files:
265
265
  - test/prx_auth/rails/ext/controller_test.rb
266
266
  - test/prx_auth/rails/sessions_controller_test.rb
267
267
  - test/prx_auth/rails/token_test.rb
268
+ - test/prx_auth/rails_test.rb
268
269
  - test/test_helper.rb
269
270
  homepage: https://github.com/PRX/prx_auth-rails
270
271
  licenses:
271
272
  - MIT
272
273
  metadata: {}
273
- post_install_message:
274
+ post_install_message:
274
275
  rdoc_options: []
275
276
  require_paths:
276
277
  - lib
@@ -285,8 +286,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
285
286
  - !ruby/object:Gem::Version
286
287
  version: '0'
287
288
  requirements: []
288
- rubygems_version: 3.1.4
289
- signing_key:
289
+ rubygems_version: 3.3.3
290
+ signing_key:
290
291
  specification_version: 4
291
292
  summary: Rails integration for next generation PRX Authorization system.
292
293
  test_files:
@@ -351,4 +352,5 @@ test_files:
351
352
  - test/prx_auth/rails/ext/controller_test.rb
352
353
  - test/prx_auth/rails/sessions_controller_test.rb
353
354
  - test/prx_auth/rails/token_test.rb
355
+ - test/prx_auth/rails_test.rb
354
356
  - test/test_helper.rb