prx_auth-rails 4.0.0 → 4.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (37) hide show
  1. checksums.yaml +4 -4
  2. data/.git-blame-ignore-revs +2 -0
  3. data/.github/workflows/check-project-std.yml +23 -0
  4. data/Gemfile +1 -1
  5. data/Guardfile +5 -5
  6. data/Rakefile +3 -3
  7. data/app/controllers/prx_auth/rails/sessions_controller.rb +25 -14
  8. data/app/views/prx_auth/rails/sessions/auth_error.html.erb +0 -7
  9. data/app/views/prx_auth/rails/sessions/show.html.erb +4 -1
  10. data/config/initializers/assets.rb +1 -1
  11. data/config/routes.rb +3 -3
  12. data/lib/prx_auth/rails/configuration.rb +16 -14
  13. data/lib/prx_auth/rails/engine.rb +1 -1
  14. data/lib/prx_auth/rails/ext/controller.rb +23 -19
  15. data/lib/prx_auth/rails/railtie.rb +3 -3
  16. data/lib/prx_auth/rails/token.rb +17 -4
  17. data/lib/prx_auth/rails/version.rb +1 -1
  18. data/lib/prx_auth/rails.rb +3 -3
  19. data/prx_auth-rails.gemspec +25 -26
  20. data/test/dummy/app/controllers/application_controller.rb +3 -3
  21. data/test/dummy/app/mailers/application_mailer.rb +2 -2
  22. data/test/dummy/bin/rails +1 -1
  23. data/test/dummy/bin/setup +7 -7
  24. data/test/dummy/config/boot.rb +2 -2
  25. data/test/dummy/config/environments/development.rb +2 -2
  26. data/test/dummy/config/environments/production.rb +5 -5
  27. data/test/dummy/config/environments/test.rb +2 -2
  28. data/test/dummy/config/initializers/assets.rb +1 -1
  29. data/test/dummy/config/initializers/prx_auth.rb +7 -7
  30. data/test/dummy/config/routes.rb +2 -2
  31. data/test/prx_auth/rails/configuration_test.rb +15 -13
  32. data/test/prx_auth/rails/ext/controller_test.rb +87 -81
  33. data/test/prx_auth/rails/sessions_controller_test.rb +39 -39
  34. data/test/prx_auth/rails/token_test.rb +44 -16
  35. data/test/prx_auth/rails_test.rb +11 -12
  36. data/test/test_helper.rb +15 -17
  37. metadata +21 -68
@@ -13,7 +13,7 @@ Rails.application.configure do
13
13
  config.eager_load = true
14
14
 
15
15
  # Full error reports are disabled and caching is turned on.
16
- config.consider_all_requests_local = false
16
+ config.consider_all_requests_local = false
17
17
  config.action_controller.perform_caching = true
18
18
 
19
19
  # Ensures that a master key has been made available in either ENV["RAILS_MASTER_KEY"]
@@ -22,7 +22,7 @@ Rails.application.configure do
22
22
 
23
23
  # Disable serving static files from the `/public` folder by default since
24
24
  # Apache or NGINX already handles this.
25
- config.public_file_server.enabled = ENV['RAILS_SERVE_STATIC_FILES'].present?
25
+ config.public_file_server.enabled = ENV["RAILS_SERVE_STATIC_FILES"].present?
26
26
 
27
27
  # Compress CSS using a preprocessor.
28
28
  # config.assets.css_compressor = :sass
@@ -53,7 +53,7 @@ Rails.application.configure do
53
53
  config.log_level = :info
54
54
 
55
55
  # Prepend all log lines with the following tags.
56
- config.log_tags = [ :request_id ]
56
+ config.log_tags = [:request_id]
57
57
 
58
58
  # Use a different cache store in production.
59
59
  # config.cache_store = :mem_cache_store
@@ -89,9 +89,9 @@ Rails.application.configure do
89
89
  # config.logger = ActiveSupport::TaggedLogging.new(Syslog::Logger.new 'app-name')
90
90
 
91
91
  if ENV["RAILS_LOG_TO_STDOUT"].present?
92
- logger = ActiveSupport::Logger.new(STDOUT)
92
+ logger = ActiveSupport::Logger.new($stdout)
93
93
  logger.formatter = config.log_formatter
94
- config.logger = ActiveSupport::TaggedLogging.new(logger)
94
+ config.logger = ActiveSupport::TaggedLogging.new(logger)
95
95
  end
96
96
 
97
97
  # Do not dump schema after migrations.
@@ -19,11 +19,11 @@ Rails.application.configure do
19
19
  # Configure public file server for tests with Cache-Control for performance.
20
20
  config.public_file_server.enabled = true
21
21
  config.public_file_server.headers = {
22
- 'Cache-Control' => "public, max-age=#{1.hour.to_i}"
22
+ "Cache-Control" => "public, max-age=#{1.hour.to_i}"
23
23
  }
24
24
 
25
25
  # Show full error reports and disable caching.
26
- config.consider_all_requests_local = true
26
+ config.consider_all_requests_local = true
27
27
  config.action_controller.perform_caching = false
28
28
  config.cache_store = :null_store
29
29
 
@@ -1,7 +1,7 @@
1
1
  # Be sure to restart your server when you modify this file.
2
2
 
3
3
  # Version of your assets, change this if you want to expire all your assets.
4
- Rails.application.config.assets.version = '1.0'
4
+ Rails.application.config.assets.version = "1.0"
5
5
 
6
6
  # Add additional assets to the asset load path.
7
7
  # Rails.application.config.assets.paths << Emoji.images_path
@@ -1,8 +1,8 @@
1
- require 'prx_auth/rails'
1
+ require "prx_auth/rails"
2
2
 
3
- PrxAuth::Rails.configure do |config|
4
- config.install_middleware = true
5
- config.namespace = :test_app
6
- config.prx_client_id = '1234'
7
- config.id_host = 'id.prx.test'
8
- end
3
+ PrxAuth::Rails.configure do |config|
4
+ config.install_middleware = true
5
+ config.namespace = :test_app
6
+ config.prx_client_id = "1234"
7
+ config.id_host = "id.prx.test"
8
+ end
@@ -1,5 +1,5 @@
1
1
  Rails.application.routes.draw do
2
- get 'index', to: 'application#index'
3
- put 'index', to: 'application#index'
2
+ get "index", to: "application#index"
3
+ put "index", to: "application#index"
4
4
  mount PrxAuth::Rails::Engine => "/prx_auth-rails"
5
5
  end
@@ -1,35 +1,37 @@
1
- require 'test_helper'
1
+ require "test_helper"
2
2
 
3
3
  describe PrxAuth::Rails::Configuration do
4
-
5
4
  subject { PrxAuth::Rails::Configuration.new }
6
5
 
7
- it 'initializes with defaults' do
6
+ it "initializes with defaults" do
8
7
  assert subject.install_middleware
9
8
  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
+ assert_nil subject.prx_scope
10
+ assert_equal "id.prx.org", subject.id_host
11
+ assert_equal "api/v1/certs", subject.cert_path
12
12
  end
13
13
 
14
- it 'infers the default namespace from the rails app name' do
14
+ it "infers the default namespace from the rails app name" do
15
15
  assert_equal :dummy, subject.namespace
16
16
  end
17
17
 
18
- it 'is updated by the prxauth configure block' do
18
+ it "is updated by the prxauth configure block" do
19
19
  PrxAuth::Rails.stub(:configuration, subject) do
20
20
  PrxAuth::Rails.configure do |config|
21
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'
22
+ config.prx_client_id = "some-id"
23
+ config.prx_scope = "appname:*"
24
+ config.id_host = "id.prx.blah"
25
+ config.cert_path = "cert/path"
25
26
  config.namespace = :new_test
26
27
  end
27
28
  end
28
29
 
29
30
  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
31
+ assert_equal "some-id", subject.prx_client_id
32
+ assert_equal "appname:*", subject.prx_scope
33
+ assert_equal "id.prx.blah", subject.id_host
34
+ assert_equal "cert/path", subject.cert_path
33
35
  assert_equal :new_test, subject.namespace
34
36
  end
35
37
  end
@@ -1,142 +1,144 @@
1
- require 'test_helper'
1
+ require "test_helper"
2
2
 
3
3
  module PrxAuth::Rails::Ext
4
4
  class ControllerTest < ActionController::TestCase
5
-
6
5
  setup do
7
6
  @controller = ApplicationController.new
8
7
  @jwt_session_key = ApplicationController::PRX_JWT_SESSION_KEY
9
8
  @user_info_key = ApplicationController::PRX_USER_INFO_SESSION_KEY
10
9
  @account_mapping_key = ApplicationController::PRX_ACCOUNT_MAPPING_SESSION_KEY
11
- @stub_claims = {'iat' => Time.now.to_i, 'exp' => Time.now.to_i + 3600}
10
+ @stub_claims = {"iat" => Time.now.to_i, "exp" => Time.now.to_i + 3600}
12
11
  end
13
12
 
14
13
  # stub auth and init controller+session by getting a page
15
14
  def with_stubbed_auth(jwt)
16
- session[@jwt_session_key] = 'some-jwt'
15
+ session[@jwt_session_key] = "some-jwt"
17
16
  @controller.stub(:prx_auth_needs_refresh?, false) do
18
17
  get :index
19
- assert_equal response.code, '200'
18
+ assert_equal response.code, "200"
20
19
  yield
21
20
  end
22
21
  end
23
22
 
24
- test 'redirects unless you are authenticated' do
23
+ test "redirects unless you are authenticated" do
25
24
  get :index
26
- assert_equal response.code, '302'
27
- assert response.headers['Location'].ends_with?('/sessions/new')
25
+ assert_equal response.code, "302"
26
+ assert response.headers["Location"].ends_with?("/sessions/new")
28
27
  end
29
28
 
30
- test 'uses a valid session token' do
31
- session[@jwt_session_key] = 'some-jwt'
29
+ test "uses a valid session token" do
30
+ session[@jwt_session_key] = "some-jwt"
32
31
  JSON::JWT.stub(:decode, @stub_claims) do
33
32
  get :index
34
- assert_equal response.code, '200'
35
- assert response.body.include?('the controller index!')
33
+ assert_equal response.code, "200"
34
+ assert response.body.include?("the controller index!")
36
35
  assert @controller.current_user.is_a?(PrxAuth::Rails::Token)
37
36
  end
38
37
  end
39
38
 
40
- test 'redirects if your token is nearing expiration' do
41
- session[@jwt_session_key] = 'some-jwt'
42
- @stub_claims['exp'] = Time.now.to_i + 10
39
+ test "redirects if your token is nearing expiration" do
40
+ session[@jwt_session_key] = "some-jwt"
41
+ @stub_claims["exp"] = Time.now.to_i + 10
43
42
  JSON::JWT.stub(:decode, @stub_claims) do
44
43
  get :index
45
- assert_equal response.code, '302'
46
- assert response.headers['Location'].ends_with?('/sessions/new')
44
+ assert_equal response.code, "302"
45
+ assert response.headers["Location"].ends_with?("/sessions/new")
47
46
  end
48
47
  end
49
48
 
50
- test 'does not redirect if your token has expired on a non-GET request' do
51
- session[@jwt_session_key] = 'some-jwt'
52
- @stub_claims['exp'] = Time.now.to_i + 10
49
+ test "does not redirect if your token has expired on a non-GET request" do
50
+ session[@jwt_session_key] = "some-jwt"
51
+ @stub_claims["exp"] = Time.now.to_i + 10
53
52
  JSON::JWT.stub(:decode, @stub_claims) do
54
53
  put :index
55
- assert_equal response.code, '200'
56
- assert response.body.include?('the controller index!')
54
+ assert_equal response.code, "200"
55
+ assert response.body.include?("the controller index!")
57
56
  end
58
57
  end
59
58
 
60
- test 'fetches current user info' do
61
- with_stubbed_auth('some-jwt') do
59
+ test "fetches current user info" do
60
+ with_stubbed_auth("some-jwt") do
62
61
  body = {
63
- 'name' => 'Some Username',
64
- 'apps' => {'publish.prx.test' => 'https://publish.prx.test'},
65
- 'other' => 'stuff'
62
+ "name" => "Some Username",
63
+ "apps" => {"publish.prx.test" => "https://publish.prx.test"},
64
+ "other" => "stuff"
66
65
  }
67
66
 
68
67
  id_host = PrxAuth::Rails.configuration.id_host
69
- stub_request(:get, "https://#{id_host}/userinfo?scope=apps%20email%20profile").
70
- with(headers: {'Authorization' => 'Bearer some-jwt'}).
71
- to_return(status: 200, body: JSON.generate(body))
72
-
73
- assert session[@user_info_key] == nil
74
- assert_equal @controller.current_user_info, body
75
- refute session[@user_info_key] == nil
76
- assert_equal @controller.current_user_name, 'Some Username'
77
- assert_equal @controller.current_user_apps, {'PRX Publish' => 'https://publish.prx.test'}
68
+ stub_request(:get, "https://#{id_host}/userinfo?scope=apps%20email%20profile")
69
+ .with(headers: {"Authorization" => "Bearer some-jwt"})
70
+ .to_return(status: 200, body: JSON.generate(body))
71
+
72
+ assert session[@user_info_key].nil?
73
+ assert_equal @controller.current_user_info, body.slice("name", "apps")
74
+ refute session[@user_info_key].nil?
75
+ assert_equal @controller.current_user_name, "Some Username"
76
+ assert_equal @controller.current_user_apps, {"PRX Publish" => "https://publish.prx.test"}
78
77
  end
79
78
  end
80
79
 
81
- test 'has user name fallbacks' do
82
- with_stubbed_auth('some-jwt') do
83
- session[@user_info_key] = {'name' => 'one', 'preferred_username' => 'two', 'email' => 'three'}
84
- assert_equal @controller.current_user_name, 'one'
80
+ test "has user name fallbacks" do
81
+ with_stubbed_auth("some-jwt") do
82
+ session[@user_info_key] = {"name" => "one", "preferred_username" => "two", "email" => "three"}
83
+ assert_equal @controller.current_user_name, "one"
85
84
 
86
- session[@user_info_key] = {'preferred_username' => 'two', 'email' => 'three'}
87
- assert_equal @controller.current_user_name, 'two'
85
+ session[@user_info_key] = {"preferred_username" => "two", "email" => "three"}
86
+ assert_equal @controller.current_user_name, "two"
88
87
 
89
- session[@user_info_key] = {'email' => 'three'}
90
- assert_equal @controller.current_user_name, 'three'
88
+ session[@user_info_key] = {"email" => "three"}
89
+ assert_equal @controller.current_user_name, "three"
91
90
  end
92
91
  end
93
92
 
94
- test 'filters apps displayed in production' do
95
- with_stubbed_auth('some-jwt') do
93
+ test "filters apps displayed in production" do
94
+ with_stubbed_auth("some-jwt") do
96
95
  Rails.env.stub(:production?, true) do
97
96
  session[@user_info_key] = {
98
- 'apps' => {
99
- 'localhost stuff' => 'http://localhost:4000/path1',
100
- 'publish.prx.test' => 'https://publish.prx.test/path2',
101
- 'metrics.prx.tech' => 'https://metrics.prx.tech/path3',
102
- 'augury.prx.org' => 'https://augury.prx.org/path4',
97
+ "apps" => {
98
+ "localhost stuff" => "http://localhost:4000/path1",
99
+ "publish.prx.test" => "https://publish.prx.test/path2",
100
+ "metrics.prx.tech" => "https://metrics.prx.tech/path3",
101
+ "augury.prx.org" => "https://augury.prx.org/path4"
103
102
  }
104
103
  }
105
104
 
106
105
  assert_equal @controller.current_user_apps, {
107
- 'PRX Metrics' => 'https://metrics.prx.tech/path3',
108
- 'PRX Augury' => 'https://augury.prx.org/path4',
106
+ "PRX Metrics" => "https://metrics.prx.tech/path3",
107
+ "PRX Augury" => "https://augury.prx.org/path4"
109
108
  }
110
109
  end
111
110
  end
112
111
  end
113
112
 
114
- test 'fetches accounts' do
115
- with_stubbed_auth('some-jwt') do
116
- one = {'id' => 1, 'type' => 'IndividualAccount', 'name' => 'One'}
117
- three = {'id' => 3, 'type' => 'GroupAccount', 'name' => 'Three'}
118
- body = {'_embedded' => {'prx:items' => [one, three]}}
113
+ test "fetches accounts" do
114
+ with_stubbed_auth("some-jwt") do
115
+ one = {"id" => 1, "type" => "IndividualAccount", "name" => "One"}
116
+ three = {"id" => 3, "type" => "GroupAccount", "name" => "Three"}
117
+ body = {"_embedded" => {"prx:items" => [one, three]}}
118
+
119
+ min_one = one.slice("name", "type")
120
+ min_three = three.slice("name", "type")
119
121
 
120
122
  id_host = PrxAuth::Rails.configuration.id_host
121
- stub_request(:get, "https://#{id_host}/api/v1/accounts?account_ids=1,2,3").
122
- to_return(status: 200, body: JSON.generate(body))
123
+ stub_request(:get, "https://#{id_host}/api/v1/accounts?account_ids=1,2,3")
124
+ .to_return(status: 200, body: JSON.generate(body))
123
125
 
124
126
  assert_nil session[@account_mapping_key]
125
- assert_equal @controller.accounts_for([1, 2, 3]), [one, nil, three]
127
+ assert_equal @controller.accounts_for([1, 2, 3]), [min_one, nil, min_three]
126
128
  refute_nil session[@account_mapping_key]
127
- assert_equal @controller.account_for(1), one
128
- assert_equal @controller.account_for(3), three
129
- assert_equal @controller.account_name_for(1), 'One'
130
- assert_equal @controller.account_name_for(3), 'Three'
129
+ assert_equal @controller.account_for(1), min_one
130
+ assert_equal @controller.account_for(3), min_three
131
+ assert_equal @controller.account_name_for(1), "One"
132
+ assert_equal @controller.account_name_for(3), "Three"
131
133
  end
132
134
  end
133
135
 
134
- test 'handles unknown account ids' do
135
- with_stubbed_auth('some-jwt') do
136
+ test "handles unknown account ids" do
137
+ with_stubbed_auth("some-jwt") do
136
138
  id_host = PrxAuth::Rails.configuration.id_host
137
- stub_request(:get, "https://#{id_host}/api/v1/accounts?account_ids=2").
138
- to_return(status: 200, body: JSON.generate({'_embedded' => {'prx:items' => []}})).
139
- times(3)
139
+ stub_request(:get, "https://#{id_host}/api/v1/accounts?account_ids=2")
140
+ .to_return(status: 200, body: JSON.generate({"_embedded" => {"prx:items" => []}}))
141
+ .times(3)
140
142
 
141
143
  assert_equal @controller.accounts_for([2]), [nil]
142
144
  assert_nil @controller.account_for(2)
@@ -144,21 +146,25 @@ module PrxAuth::Rails::Ext
144
146
  end
145
147
  end
146
148
 
147
- test 'only fetches only missing accounts' do
148
- with_stubbed_auth('some-jwt') do
149
- one = {'name' => 'One'}
150
- two = {'id' => 2, 'type' => 'StationAccount', 'name' => 'Two'}
151
- three = {'name' => 'Three'}
149
+ test "only fetches only missing accounts" do
150
+ with_stubbed_auth("some-jwt") do
151
+ one = {"name" => "One"}
152
+ two = {"id" => 2, "type" => "StationAccount", "name" => "Two"}
153
+ three = {"name" => "Three"}
152
154
  session[@account_mapping_key] = {1 => one, 3 => three}
153
- body = {'_embedded' => {'prx:items' => [two]}}
155
+ body = {"_embedded" => {"prx:items" => [two]}}
156
+
157
+ min_one = one.slice("name", "type")
158
+ min_two = two.slice("name", "type")
159
+ min_three = three.slice("name", "type")
154
160
 
155
161
  id_host = PrxAuth::Rails.configuration.id_host
156
- stub_request(:get, "https://#{id_host}/api/v1/accounts?account_ids=2").
157
- to_return(status: 200, body: JSON.generate(body))
162
+ stub_request(:get, "https://#{id_host}/api/v1/accounts?account_ids=2")
163
+ .to_return(status: 200, body: JSON.generate(body))
158
164
 
159
- assert_equal @controller.accounts_for([1, 2, 3]), [one, two, three]
160
- assert_equal @controller.account_for(2), two
161
- assert_equal @controller.account_name_for(2), 'Two'
165
+ assert_equal @controller.accounts_for([1, 2, 3]), [min_one, min_two, min_three]
166
+ assert_equal @controller.account_for(2), min_two
167
+ assert_equal @controller.account_name_for(2), "Two"
162
168
  end
163
169
  end
164
170
  end
@@ -2,19 +2,18 @@ require "test_helper"
2
2
 
3
3
  module PrxAuth::Rails
4
4
  class SessionsControllerTest < ActionController::TestCase
5
-
6
5
  setup do
7
6
  @routes = PrxAuth::Rails::Engine.routes
8
7
  @nonce_session_key = SessionsController::ID_NONCE_SESSION_KEY
9
8
  @refresh_back_key = SessionsController::PRX_REFRESH_BACK_KEY
10
- @token_params = {id_token: 'idtok', access_token: 'accesstok'}
11
- @stub_claims = {'nonce' => '123', 'sub' => '1'}
12
- @stub_token = PrxAuth::Rails::Token.new(Rack::PrxAuth::TokenData.new())
9
+ @token_params = {id_token: "idtok", access_token: "accesstok"}
10
+ @stub_claims = {"nonce" => "123", "sub" => "1"}
11
+ @stub_token = PrxAuth::Rails::Token.new(Rack::PrxAuth::TokenData.new)
13
12
  end
14
13
 
15
14
  test "new creates nonce" do
16
15
  nonce = session[@nonce_session_key]
17
- assert nonce == nil
16
+ assert nonce.nil?
18
17
 
19
18
  get :new
20
19
 
@@ -23,7 +22,7 @@ module PrxAuth::Rails
23
22
  assert nonce.length == 32
24
23
  end
25
24
 
26
- test 'new should should not overwrite the saved nonce' do
25
+ test "new should should not overwrite the saved nonce" do
27
26
  get :new
28
27
  nonce1 = session[@nonce_session_key]
29
28
 
@@ -32,92 +31,93 @@ module PrxAuth::Rails
32
31
  assert nonce1 == nonce2
33
32
  end
34
33
 
35
- test 'create should validate a token and set the session variable' do
34
+ test "create should validate a token and set the session variable" do
36
35
  session[SessionsController::PRX_JWT_SESSION_KEY] = nil
37
36
  @controller.stub(:validate_token, @stub_claims) do
38
37
  @controller.stub(:session_token, @stub_token) do
39
- session[@nonce_session_key] = '123'
38
+ session[@nonce_session_key] = "123"
40
39
  post :create, params: @token_params, format: :json
41
- assert session[SessionsController::PRX_JWT_SESSION_KEY] == 'accesstok'
40
+ assert session[SessionsController::PRX_JWT_SESSION_KEY] == "accesstok"
42
41
  end
43
42
  end
44
43
  end
45
44
 
46
- test 'create should call test_nonce! if upon verification' do
47
- @controller.stub(:validate_token, {'nonce' => 'not matching', 'aud' => '1'}) do
48
- session[@nonce_session_key] = 'nonce'
45
+ test "create should call test_nonce! if upon verification" do
46
+ @controller.stub(:validate_token, {"nonce" => "not matching", "aud" => "1"}) do
47
+ session[@nonce_session_key] = "nonce"
49
48
  post :create, params: @token_params, format: :json
50
- assert session[@nonce_session_key] == nil
49
+ assert session[@nonce_session_key].nil?
51
50
  end
52
51
  end
53
52
 
54
- test 'create should reset the nonce after consumed' do
53
+ test "create should reset the nonce after consumed" do
55
54
  @controller.stub(:validate_token, @stub_claims) do
56
55
  @controller.stub(:session_token, @stub_token) do
57
- session[@nonce_session_key] = '123'
56
+ session[@nonce_session_key] = "123"
58
57
  post :create, params: @token_params, format: :json
59
58
 
60
- assert session[@nonce_session_key] == nil
61
- assert response.code == '302'
59
+ assert session[@nonce_session_key].nil?
60
+ assert response.code == "302"
62
61
  assert response.body.match?(/after-sign-in-path/)
63
62
  end
64
63
  end
65
64
  end
66
65
 
67
- test 'redirects to a back-path after refresh' do
66
+ test "redirects to a back-path after refresh" do
68
67
  @controller.stub(:validate_token, @stub_claims) do
69
68
  @controller.stub(:session_token, @stub_token) do
70
- session[@nonce_session_key] = '123'
71
- session[@refresh_back_key] = '/lets/go/here?okay'
69
+ session[@nonce_session_key] = "123"
70
+ session[@refresh_back_key] = "/lets/go/here?okay"
72
71
  post :create, params: @token_params, format: :json
73
72
 
74
73
  # A trailing log of the 'last' page
75
- assert session[@refresh_back_key] == '/lets/go/here?okay'
74
+ assert session[@refresh_back_key] == "/lets/go/here?okay"
76
75
 
77
- assert response.code == '302'
78
- assert response.headers['Location'].ends_with?('/lets/go/here?okay')
76
+ assert response.code == "302"
77
+ assert response.headers["Location"].ends_with?("/lets/go/here?okay")
79
78
  end
80
79
  end
81
80
  end
82
81
 
83
- test 'should respond with redirect to the auth error page / code if the nonce does not match' do
82
+ test "should respond with redirect to the auth error page / code if the nonce does not match" do
84
83
  @controller.stub(:validate_token, @stub_claims) do
85
- session[@nonce_session_key] = 'nonce-does-not-match'
84
+ @token_params[:error] = "verification_failed"
85
+ session[@nonce_session_key] = "nonce-does-not-match"
86
86
  post :create, params: @token_params, format: :json
87
- assert response.code == '302'
87
+ assert response.code == "302"
88
88
  assert response.body.match(/auth_error\?error=verification_failed/)
89
89
  end
90
90
  end
91
91
 
92
- test 'auth_error should return a formatted error message to the user' do
93
- get :auth_error, params: {error: 'error_message'}
94
- assert response.code == '200'
95
- assert response.body.match?(/Message was: <pre>error_message/)
92
+ test "auth_error should return a formatted error message to the user" do
93
+ get :auth_error, params: {error: "error_message"}
94
+ assert response.code == "200"
95
+ assert response.body.match?(/Not authorized/)
96
96
  end
97
97
 
98
- test 'auth_error should expect the error param' do
98
+ test "auth_error should expect the error param" do
99
99
  assert_raises ActionController::ParameterMissing do
100
100
  get :auth_error, params: {}
101
101
  end
102
102
  end
103
103
 
104
- test 'validates that the user id matches in both tokens' do
104
+ test "validates that the user id matches in both tokens" do
105
105
  @controller.stub(:id_claims, @stub_claims) do
106
- @controller.stub(:access_claims, @stub_claims.merge('sub' => '444')) do
107
-
108
- session[@nonce_session_key] = '123'
106
+ @controller.stub(:access_claims, @stub_claims.merge("sub" => "444")) do
107
+ @token_params[:error] = "verification_failed"
108
+ session[@nonce_session_key] = "123"
109
109
  post :create, params: @token_params, format: :json
110
110
 
111
- assert response.code == '302'
111
+ assert response.code == "302"
112
112
  assert response.body.match?(/error=verification_failed/)
113
113
  end
114
114
  end
115
115
  end
116
116
 
117
- test 'should clear the user token on sign out' do
118
- session[SessionsController::PRX_JWT_SESSION_KEY] = 'some-token'
117
+ test "should clear the user token on sign out" do
118
+ session[SessionsController::PRX_JWT_SESSION_KEY] = "some-token"
119
119
  post :destroy
120
- assert session[SessionsController::PRX_JWT_SESSION_KEY] == nil
120
+ assert session[SessionsController::PRX_JWT_SESSION_KEY].nil?
121
121
  end
122
122
  end
123
123
  end
@@ -1,35 +1,35 @@
1
- require 'test_helper'
1
+ require "test_helper"
2
2
 
3
3
  describe PrxAuth::Rails::Token do
4
- let (:aur) { { "123" => "test_app:read other_namespace:write", "*" => "test_app:add" } }
5
- let (:sub) { "123" }
6
- let (:scope) { "one two three" }
7
- let (:token_data) { Rack::PrxAuth::TokenData.new("aur" => aur, "scope" => scope, "sub" => sub)}
8
- let (:mock_token_data) { Minitest::Mock.new(token_data) }
9
- let (:token) { PrxAuth::Rails::Token.new(mock_token_data) }
10
-
11
- it 'automatically namespaces requests' do
4
+ let(:aur) { {"123" => "test_app:read other_namespace:write", "*" => "test_app:add"} }
5
+ let(:sub) { "123" }
6
+ let(:scope) { "one two three" }
7
+ let(:token_data) { Rack::PrxAuth::TokenData.new("aur" => aur, "scope" => scope, "sub" => sub) }
8
+ let(:mock_token_data) { Minitest::Mock.new(token_data) }
9
+ let(:token) { PrxAuth::Rails::Token.new(mock_token_data) }
10
+
11
+ it "automatically namespaces requests" do
12
12
  mock_token_data.expect(:authorized?, true, ["123", :test_app, :read])
13
13
  assert token.authorized?("123", :read)
14
14
 
15
15
  mock_token_data.expect(:resources, ["123"], [:test_app, :read])
16
- assert token.resources(:read) === ['123']
16
+ assert token.resources(:read) === ["123"]
17
17
 
18
18
  mock_token_data.expect(:globally_authorized?, true, [:test_app, :add])
19
- assert token.globally_authorized?(:add)
19
+ assert token.globally_authorized?(:add)
20
20
 
21
21
  mock_token_data.verify
22
22
  end
23
23
 
24
- it 'allows unscoped calls to authorized?' do
24
+ it "allows unscoped calls to authorized?" do
25
25
  assert token.authorized?("123")
26
26
  end
27
27
 
28
- it 'allows unscoped calls to resources' do
29
- assert token.resources == [ "123" ]
28
+ it "allows unscoped calls to resources" do
29
+ assert token.resources == ["123"]
30
30
  end
31
31
 
32
- it 'allows manual setting of namespace' do
32
+ it "allows manual setting of namespace" do
33
33
  assert token.authorized?("123", :other_namespace, :write)
34
34
  assert !token.authorized?("123", :other_namespace, :read)
35
35
 
@@ -41,5 +41,33 @@ describe PrxAuth::Rails::Token do
41
41
  assert !token.globally_authorized?(:other_namespace, :add)
42
42
  end
43
43
 
44
-
44
+ it "returns a token except resources" do
45
+ token2 = token.except("123")
46
+
47
+ assert token.authorized?("123", :read)
48
+ refute token2.authorized?("123", :read)
49
+
50
+ # BUT cannot remove wildcard resources
51
+ assert token.authorized?("123", :add)
52
+ assert token2.authorized?("123", :add)
53
+
54
+ # the ! version modifies
55
+ token.except!("123")
56
+ refute token.authorized?("123", :read)
57
+ end
58
+
59
+ it "checks for empty resources" do
60
+ # wilcard tokens are never empty
61
+ refute token.empty_resources?
62
+ refute token.except("123").empty_resources?
63
+
64
+ # non-wildcard token
65
+ aur2 = {"123" => "anything"}
66
+ token_data2 = Rack::PrxAuth::TokenData.new("aur" => aur2, "scope" => scope, "sub" => sub)
67
+ mock_token_data2 = Minitest::Mock.new(token_data2)
68
+ token2 = PrxAuth::Rails::Token.new(mock_token_data2)
69
+
70
+ refute token2.empty_resources?
71
+ assert token2.except("123").empty_resources?
72
+ end
45
73
  end