prx_auth-rails 4.0.0 → 4.2.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 +4 -4
- data/.git-blame-ignore-revs +2 -0
- data/.github/workflows/check-project-std.yml +23 -0
- data/Gemfile +1 -1
- data/Guardfile +5 -5
- data/Rakefile +3 -3
- data/app/controllers/prx_auth/rails/sessions_controller.rb +25 -14
- data/app/views/prx_auth/rails/sessions/auth_error.html.erb +0 -7
- data/app/views/prx_auth/rails/sessions/show.html.erb +4 -1
- data/config/initializers/assets.rb +1 -1
- data/config/routes.rb +3 -3
- data/lib/prx_auth/rails/configuration.rb +16 -14
- data/lib/prx_auth/rails/engine.rb +1 -1
- data/lib/prx_auth/rails/ext/controller.rb +23 -19
- data/lib/prx_auth/rails/railtie.rb +3 -3
- data/lib/prx_auth/rails/token.rb +17 -4
- data/lib/prx_auth/rails/version.rb +1 -1
- data/lib/prx_auth/rails.rb +3 -3
- data/prx_auth-rails.gemspec +25 -26
- data/test/dummy/app/controllers/application_controller.rb +3 -3
- data/test/dummy/app/mailers/application_mailer.rb +2 -2
- data/test/dummy/bin/rails +1 -1
- data/test/dummy/bin/setup +7 -7
- data/test/dummy/config/boot.rb +2 -2
- data/test/dummy/config/environments/development.rb +2 -2
- data/test/dummy/config/environments/production.rb +5 -5
- data/test/dummy/config/environments/test.rb +2 -2
- data/test/dummy/config/initializers/assets.rb +1 -1
- data/test/dummy/config/initializers/prx_auth.rb +7 -7
- data/test/dummy/config/routes.rb +2 -2
- data/test/prx_auth/rails/configuration_test.rb +15 -13
- data/test/prx_auth/rails/ext/controller_test.rb +87 -81
- data/test/prx_auth/rails/sessions_controller_test.rb +39 -39
- data/test/prx_auth/rails/token_test.rb +44 -16
- data/test/prx_auth/rails_test.rb +11 -12
- data/test/test_helper.rb +15 -17
- 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
|
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[
|
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 = [
|
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
|
92
|
+
logger = ActiveSupport::Logger.new($stdout)
|
93
93
|
logger.formatter = config.log_formatter
|
94
|
-
config.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
|
-
|
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
|
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 =
|
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
|
-
|
1
|
+
require "prx_auth/rails"
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
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
|
data/test/dummy/config/routes.rb
CHANGED
@@ -1,35 +1,37 @@
|
|
1
|
-
require
|
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
|
6
|
+
it "initializes with defaults" do
|
8
7
|
assert subject.install_middleware
|
9
8
|
assert_nil subject.prx_client_id
|
10
|
-
|
11
|
-
assert_equal
|
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
|
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
|
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 =
|
23
|
-
config.
|
24
|
-
config.
|
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
|
31
|
-
assert_equal
|
32
|
-
assert_equal
|
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
|
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 = {
|
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] =
|
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,
|
18
|
+
assert_equal response.code, "200"
|
20
19
|
yield
|
21
20
|
end
|
22
21
|
end
|
23
22
|
|
24
|
-
test
|
23
|
+
test "redirects unless you are authenticated" do
|
25
24
|
get :index
|
26
|
-
assert_equal response.code,
|
27
|
-
assert response.headers[
|
25
|
+
assert_equal response.code, "302"
|
26
|
+
assert response.headers["Location"].ends_with?("/sessions/new")
|
28
27
|
end
|
29
28
|
|
30
|
-
test
|
31
|
-
session[@jwt_session_key] =
|
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,
|
35
|
-
assert response.body.include?(
|
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
|
41
|
-
session[@jwt_session_key] =
|
42
|
-
@stub_claims[
|
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,
|
46
|
-
assert response.headers[
|
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
|
51
|
-
session[@jwt_session_key] =
|
52
|
-
@stub_claims[
|
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,
|
56
|
-
assert response.body.include?(
|
54
|
+
assert_equal response.code, "200"
|
55
|
+
assert response.body.include?("the controller index!")
|
57
56
|
end
|
58
57
|
end
|
59
58
|
|
60
|
-
test
|
61
|
-
with_stubbed_auth(
|
59
|
+
test "fetches current user info" do
|
60
|
+
with_stubbed_auth("some-jwt") do
|
62
61
|
body = {
|
63
|
-
|
64
|
-
|
65
|
-
|
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: {
|
71
|
-
to_return(status: 200, body: JSON.generate(body))
|
72
|
-
|
73
|
-
assert session[@user_info_key]
|
74
|
-
assert_equal @controller.current_user_info, body
|
75
|
-
refute session[@user_info_key]
|
76
|
-
assert_equal @controller.current_user_name,
|
77
|
-
assert_equal @controller.current_user_apps, {
|
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
|
82
|
-
with_stubbed_auth(
|
83
|
-
session[@user_info_key] = {
|
84
|
-
assert_equal @controller.current_user_name,
|
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] = {
|
87
|
-
assert_equal @controller.current_user_name,
|
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] = {
|
90
|
-
assert_equal @controller.current_user_name,
|
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
|
95
|
-
with_stubbed_auth(
|
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
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
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
|
-
|
108
|
-
|
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
|
115
|
-
with_stubbed_auth(
|
116
|
-
one = {
|
117
|
-
three = {
|
118
|
-
body = {
|
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]), [
|
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),
|
128
|
-
assert_equal @controller.account_for(3),
|
129
|
-
assert_equal @controller.account_name_for(1),
|
130
|
-
assert_equal @controller.account_name_for(3),
|
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
|
135
|
-
with_stubbed_auth(
|
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({
|
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
|
148
|
-
with_stubbed_auth(
|
149
|
-
one = {
|
150
|
-
two = {
|
151
|
-
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 = {
|
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]), [
|
160
|
-
assert_equal @controller.account_for(2),
|
161
|
-
assert_equal @controller.account_name_for(2),
|
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:
|
11
|
-
@stub_claims = {
|
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
|
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
|
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
|
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] =
|
38
|
+
session[@nonce_session_key] = "123"
|
40
39
|
post :create, params: @token_params, format: :json
|
41
|
-
assert session[SessionsController::PRX_JWT_SESSION_KEY] ==
|
40
|
+
assert session[SessionsController::PRX_JWT_SESSION_KEY] == "accesstok"
|
42
41
|
end
|
43
42
|
end
|
44
43
|
end
|
45
44
|
|
46
|
-
test
|
47
|
-
@controller.stub(:validate_token, {
|
48
|
-
session[@nonce_session_key] =
|
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]
|
49
|
+
assert session[@nonce_session_key].nil?
|
51
50
|
end
|
52
51
|
end
|
53
52
|
|
54
|
-
test
|
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] =
|
56
|
+
session[@nonce_session_key] = "123"
|
58
57
|
post :create, params: @token_params, format: :json
|
59
58
|
|
60
|
-
assert session[@nonce_session_key]
|
61
|
-
assert response.code ==
|
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
|
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] =
|
71
|
-
session[@refresh_back_key] =
|
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] ==
|
74
|
+
assert session[@refresh_back_key] == "/lets/go/here?okay"
|
76
75
|
|
77
|
-
assert response.code ==
|
78
|
-
assert response.headers[
|
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
|
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
|
-
|
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 ==
|
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
|
93
|
-
get :auth_error, params: {error:
|
94
|
-
assert response.code ==
|
95
|
-
assert response.body.match?(/
|
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
|
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
|
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(
|
107
|
-
|
108
|
-
session[@nonce_session_key] =
|
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 ==
|
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
|
118
|
-
session[SessionsController::PRX_JWT_SESSION_KEY] =
|
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]
|
120
|
+
assert session[SessionsController::PRX_JWT_SESSION_KEY].nil?
|
121
121
|
end
|
122
122
|
end
|
123
123
|
end
|
@@ -1,35 +1,35 @@
|
|
1
|
-
require
|
1
|
+
require "test_helper"
|
2
2
|
|
3
3
|
describe PrxAuth::Rails::Token do
|
4
|
-
let
|
5
|
-
let
|
6
|
-
let
|
7
|
-
let
|
8
|
-
let
|
9
|
-
let
|
10
|
-
|
11
|
-
it
|
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) === [
|
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
|
24
|
+
it "allows unscoped calls to authorized?" do
|
25
25
|
assert token.authorized?("123")
|
26
26
|
end
|
27
27
|
|
28
|
-
it
|
29
|
-
assert token.resources == [
|
28
|
+
it "allows unscoped calls to resources" do
|
29
|
+
assert token.resources == ["123"]
|
30
30
|
end
|
31
31
|
|
32
|
-
it
|
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
|