prx_auth-rails 3.0.0 → 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: 5be902ae2b7abfd4c01d28511d99931969fb3dce4aaff815fab35df005b99e07
4
- data.tar.gz: dd3f5cf76251b17ceccaf307275401939e3d361ec01e215325674bff27aa5313
3
+ metadata.gz: 1eed8329985438f59a1adc529c8e33748cbfca9becbd285475385c16b25639e6
4
+ data.tar.gz: 0a065d8fdf1e4d077fdd43da82cc37c3110ada401d31e6eadb5e154ae7001c6f
5
5
  SHA512:
6
- metadata.gz: '09589b75ff43d4da201e8ee6ebe7caca75dcada77e8c959588a7cc2832998d71eae72bea7bc44e8d0b02db5fa2dceb5284841930502bfbc8926096bb47cf9b63'
7
- data.tar.gz: 1bf07c4a6edb477714297edbd2e6a1ba9e3289ef28babe973816eaf6b8984157b534b4556ec5840b5c6d4d7c7dc134fab415a404cc3bfbf2716b91c994f466c2
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.
@@ -4,7 +4,7 @@ module PrxAuth::Rails
4
4
  class SessionsController < ApplicationController
5
5
  include PrxAuth::Rails::Engine.routes.url_helpers
6
6
 
7
- skip_before_action :authenticate!
7
+ skip_before_action :authenticate!, raise: false
8
8
 
9
9
  before_action :set_nonce!, only: [:new, :show]
10
10
  before_action :set_after_sign_in_path
@@ -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
@@ -1,5 +1,7 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module PrxAuth
2
4
  module Rails
3
- VERSION = "3.0.0"
5
+ VERSION = '4.0.0'
4
6
  end
5
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.0
4
+ version: 4.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chris Rhoden
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-08-29 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,6 +265,7 @@ 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:
@@ -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