gds-sso 21.0.0 → 22.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 +4 -4
- data/config/routes.rb +4 -3
- data/lib/gds-sso/api_access.rb +6 -1
- data/lib/gds-sso/bearer_token.rb +1 -0
- data/lib/gds-sso/config.rb +5 -0
- data/lib/gds-sso/failure_app.rb +8 -1
- data/lib/gds-sso/version.rb +1 -1
- data/spec/internal/config/routes.rb +1 -1
- data/spec/request/api/user_spec.rb +144 -0
- data/spec/request/authentication_spec.rb +77 -0
- data/spec/request/demo_app_spec.rb +264 -0
- data/spec/spec_helper.rb +16 -7
- data/spec/support/request_helpers.rb +45 -0
- data/spec/unit/api_access_spec.rb +37 -19
- data/spec/unit/failure_app_spec.rb +31 -0
- data/spec/unit/mock_bearer_token_spec.rb +6 -0
- data/spec/unit/session_serialisation_spec.rb +5 -4
- metadata +20 -30
- data/spec/controller/api_user_controller_spec.rb +0 -114
- data/spec/support/timecop.rb +0 -7
- data/spec/system/authentication_and_authorisation_spec.rb +0 -245
- /data/spec/{controller → unit}/controller_methods_spec.rb +0 -0
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: cc434aa156074fb389c79d10a05993a5493d692a06af85e7d1409b6329ad296e
|
4
|
+
data.tar.gz: 2a4625eae41416c741ed0c0288eb7f4f4d4cb19d78338e5e967ea2761149e6f1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 35c3637ecf9de2b951b2367924f4b4aff1538a84e2ba0c318ef79dc95eccdd075d9f1c176feaed71dc60a03f8b2d6891bcf08148d85571cfadff303ccaf904ae
|
7
|
+
data.tar.gz: f9c2193558be493ce2f58414c72cb9ec240f835e2e3d5005cfa271716d9b504bbfbba080e487443b4905071209876acd5886f0280ce83630305ae1d2d254d7ae
|
data/config/routes.rb
CHANGED
@@ -1,9 +1,10 @@
|
|
1
1
|
Rails.application.routes.draw do
|
2
|
+
put "/auth/gds/api/users/:uid", to: "api/user#update"
|
3
|
+
post "/auth/gds/api/users/:uid/reauth", to: "api/user#reauth"
|
4
|
+
|
2
5
|
next if GDS::SSO::Config.api_only
|
3
6
|
|
4
7
|
get "/auth/gds/callback", to: "authentications#callback", as: :gds_sign_in
|
5
8
|
get "/auth/gds/sign_out", to: "authentications#sign_out", as: :gds_sign_out
|
6
|
-
get
|
7
|
-
put "/auth/gds/api/users/:uid", to: "api/user#update"
|
8
|
-
post "/auth/gds/api/users/:uid/reauth", to: "api/user#reauth"
|
9
|
+
get "/auth/failure", to: "authentications#failure", as: :auth_failure
|
9
10
|
end
|
data/lib/gds-sso/api_access.rb
CHANGED
@@ -8,7 +8,12 @@ module GDS
|
|
8
8
|
return true if GDS::SSO::Config.api_only
|
9
9
|
|
10
10
|
if GDS::SSO::Config.api_request_matcher
|
11
|
-
|
11
|
+
request = Rack::Request.new(env)
|
12
|
+
|
13
|
+
gds_sso_api_request_matcher = GDS::SSO::Config.gds_sso_api_request_matcher
|
14
|
+
return true if gds_sso_api_request_matcher&.call(request)
|
15
|
+
|
16
|
+
return GDS::SSO::Config.api_request_matcher.call(request)
|
12
17
|
end
|
13
18
|
|
14
19
|
!bearer_token(env).nil?
|
data/lib/gds-sso/bearer_token.rb
CHANGED
@@ -58,6 +58,7 @@ module GDS
|
|
58
58
|
|
59
59
|
module MockBearerToken
|
60
60
|
def self.locate(_token_string)
|
61
|
+
return unless ENV["GDS_SSO_MOCK_INVALID"].to_s.empty?
|
61
62
|
return GDS::SSO.test_user if GDS::SSO.test_user
|
62
63
|
|
63
64
|
dummy_api_user = GDS::SSO::Config.user_klass.where(email: "dummyapiuser@domain.com").first
|
data/lib/gds-sso/config.rb
CHANGED
@@ -31,6 +31,11 @@ module GDS
|
|
31
31
|
|
32
32
|
mattr_accessor :api_request_matcher
|
33
33
|
|
34
|
+
# A matcher for GDS SSO's own API for updating user details. Shouldn't
|
35
|
+
# need configuring unless you mount the API at a different location.
|
36
|
+
mattr_accessor :gds_sso_api_request_matcher
|
37
|
+
@@gds_sso_api_request_matcher = ->(request) { request.path.start_with?("/auth/gds/api/") }
|
38
|
+
|
34
39
|
mattr_accessor :intercept_401_responses
|
35
40
|
@@intercept_401_responses = true
|
36
41
|
|
data/lib/gds-sso/failure_app.rb
CHANGED
@@ -6,6 +6,8 @@ require "rails"
|
|
6
6
|
module GDS
|
7
7
|
module SSO
|
8
8
|
class FailureApp < ActionController::Metal
|
9
|
+
MAX_RETURN_TO_PATH_SIZE = 2048
|
10
|
+
|
9
11
|
include ActionController::Redirecting
|
10
12
|
include AbstractController::Rendering
|
11
13
|
include ActionController::Rendering
|
@@ -44,7 +46,12 @@ module GDS
|
|
44
46
|
|
45
47
|
# TOTALLY NOT DOING THE SCOPE THING. PROBABLY SHOULD.
|
46
48
|
def store_location!
|
47
|
-
|
49
|
+
return unless request.get?
|
50
|
+
|
51
|
+
attempted_path = request.env["warden.options"][:attempted_path]
|
52
|
+
return if attempted_path.bytesize > MAX_RETURN_TO_PATH_SIZE
|
53
|
+
|
54
|
+
session["return_to"] = attempted_path
|
48
55
|
end
|
49
56
|
|
50
57
|
private
|
data/lib/gds-sso/version.rb
CHANGED
@@ -5,7 +5,7 @@ Rails.application.routes.draw do
|
|
5
5
|
get "/restricted" => "example#restricted"
|
6
6
|
get "/this-requires-execute-permission" => "example#this_requires_execute_permission"
|
7
7
|
|
8
|
-
constraints(GDS::SSO::AuthorisedUserConstraint.new("
|
8
|
+
constraints(GDS::SSO::AuthorisedUserConstraint.new("constraint")) do
|
9
9
|
get "/constraint-restricted" => "example#constraint_restricted"
|
10
10
|
end
|
11
11
|
end
|
@@ -0,0 +1,144 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe "Api::UserController", type: :request do
|
4
|
+
shared_examples "rejects a request from an unauthenticated user" do |method, path|
|
5
|
+
it "rejects the request when a user is unauthenticated" do
|
6
|
+
stub_failed_signon_user_request
|
7
|
+
public_send(method, path, headers: { "Authorization" => "Bearer anything" })
|
8
|
+
expect(response).to have_http_status(:unauthorized)
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
shared_examples "rejects a request from an authenticated user lacking permission" do |method, path|
|
13
|
+
it "rejects the request when a user is authenticated but lacking permission" do
|
14
|
+
stub_successful_signon_user_request(permissions: [])
|
15
|
+
public_send(method, path, headers: { "Authorization" => "Bearer anything" })
|
16
|
+
expect(response).to have_http_status(:forbidden)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
shared_examples "operates as an API endpoint if api_request_matcher doesn't match it" do |method, path|
|
21
|
+
it "doesn't redirect to /auth/gds because it's recognised as an API request" do
|
22
|
+
allow(GDS::SSO::Config)
|
23
|
+
.to receive(:api_request_matcher)
|
24
|
+
.and_return(->(_request) { false })
|
25
|
+
|
26
|
+
stub_failed_signon_user_request
|
27
|
+
public_send(method, path, headers: { "Authorization" => "Bearer anything" })
|
28
|
+
expect(response.media_type).to eq("application/json")
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
shared_examples "redirects to /auth/gds if gds_sso_api_request_matcher is configured to not match" do |method, path|
|
33
|
+
it "fails if gds_sso_api_request_matcher is configured to not match" do
|
34
|
+
allow(GDS::SSO::Config)
|
35
|
+
.to receive(:api_request_matcher)
|
36
|
+
.and_return(->(_request) { false })
|
37
|
+
|
38
|
+
allow(GDS::SSO::Config)
|
39
|
+
.to receive(:gds_sso_api_request_matcher)
|
40
|
+
.and_return(nil)
|
41
|
+
|
42
|
+
public_send(method, path, headers: { "Authorization" => "Bearer anything" })
|
43
|
+
expect(response).to redirect_to("/auth/gds")
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
describe "PUT /auth/gds/api/users/:uid" do
|
48
|
+
shared_params = [:put, "/auth/gds/api/users/#{SecureRandom.uuid}"]
|
49
|
+
it_behaves_like "rejects a request from an unauthenticated user", *shared_params
|
50
|
+
it_behaves_like "rejects a request from an authenticated user lacking permission", *shared_params
|
51
|
+
it_behaves_like "operates as an API endpoint if api_request_matcher doesn't match it", *shared_params
|
52
|
+
it_behaves_like "redirects to /auth/gds if gds_sso_api_request_matcher is configured to not match", *shared_params
|
53
|
+
|
54
|
+
it "updates an existing user" do
|
55
|
+
stub_successful_signon_user_request(permissions: %w[user_update_permission])
|
56
|
+
|
57
|
+
user = User.create!(
|
58
|
+
uid: SecureRandom.uuid,
|
59
|
+
email: "user@example.com",
|
60
|
+
name: "Example User",
|
61
|
+
permissions: [],
|
62
|
+
)
|
63
|
+
|
64
|
+
put "/auth/gds/api/users/#{user.uid}",
|
65
|
+
headers: { "Authorization" => "Bearer anything" },
|
66
|
+
params: user_update_params(user, { "name" => "John Matrix" }),
|
67
|
+
as: :json
|
68
|
+
|
69
|
+
expect(response).to have_http_status(:success)
|
70
|
+
expect(response.body).to eq("")
|
71
|
+
expect(user.reload.name).to eq("John Matrix")
|
72
|
+
end
|
73
|
+
|
74
|
+
it "creates a new user if a user does not exist" do
|
75
|
+
stub_successful_signon_user_request(permissions: %w[user_update_permission])
|
76
|
+
|
77
|
+
user = User.new(
|
78
|
+
uid: SecureRandom.uuid,
|
79
|
+
email: "user@example.com",
|
80
|
+
name: "Example User",
|
81
|
+
permissions: [],
|
82
|
+
)
|
83
|
+
|
84
|
+
put "/auth/gds/api/users/#{user.uid}",
|
85
|
+
headers: { "Authorization" => "Bearer anything" },
|
86
|
+
params: user_update_params(user),
|
87
|
+
as: :json
|
88
|
+
|
89
|
+
expect(response).to have_http_status(:success)
|
90
|
+
expect(response.body).to eq("")
|
91
|
+
expect(User.last.uid).to eq(user.uid)
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
describe "POST /auth/gds/api/users/:uid/reauth" do
|
96
|
+
shared_params = [:post, "/auth/gds/api/users/#{SecureRandom.uuid}/reauth"]
|
97
|
+
it_behaves_like "rejects a request from an unauthenticated user", *shared_params
|
98
|
+
it_behaves_like "rejects a request from an authenticated user lacking permission", *shared_params
|
99
|
+
it_behaves_like "operates as an API endpoint if api_request_matcher doesn't match it", *shared_params
|
100
|
+
it_behaves_like "redirects to /auth/gds if gds_sso_api_request_matcher is configured to not match", *shared_params
|
101
|
+
|
102
|
+
it "flags a user that exists as remotely signed out" do
|
103
|
+
stub_successful_signon_user_request(permissions: %w[user_update_permission])
|
104
|
+
|
105
|
+
user = User.create!(
|
106
|
+
uid: SecureRandom.uuid,
|
107
|
+
email: "user@example.com",
|
108
|
+
name: "Example User",
|
109
|
+
permissions: [],
|
110
|
+
)
|
111
|
+
|
112
|
+
expect {
|
113
|
+
post "/auth/gds/api/users/#{user.uid}/reauth",
|
114
|
+
headers: { "Authorization" => "Bearer anything" }
|
115
|
+
}.to change { user.reload.remotely_signed_out }.to(true)
|
116
|
+
|
117
|
+
expect(response).to have_http_status(:success)
|
118
|
+
expect(response.body).to eq("")
|
119
|
+
end
|
120
|
+
|
121
|
+
it "responds successfully even if the user doesn't exist" do
|
122
|
+
stub_successful_signon_user_request(permissions: %w[user_update_permission])
|
123
|
+
|
124
|
+
user = User.new(
|
125
|
+
uid: SecureRandom.uuid,
|
126
|
+
email: "user@example.com",
|
127
|
+
name: "Example User",
|
128
|
+
permissions: [],
|
129
|
+
)
|
130
|
+
|
131
|
+
post "/auth/gds/api/users/#{user.uid}/reauth",
|
132
|
+
headers: { "Authorization" => "Bearer anything" }
|
133
|
+
|
134
|
+
expect(response).to have_http_status(:success)
|
135
|
+
expect(response.body).to eq("")
|
136
|
+
end
|
137
|
+
end
|
138
|
+
|
139
|
+
def user_update_params(user, modifications = {})
|
140
|
+
fields = %i[uid name email permissions organisation_slug organisation_content_id disabled]
|
141
|
+
user_details = user.as_json(only: fields).merge(modifications)
|
142
|
+
{ "user" => user_details }
|
143
|
+
end
|
144
|
+
end
|
@@ -0,0 +1,77 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe "AuthenticationController", type: :request do
|
4
|
+
describe "GET /auth/gds/callback" do
|
5
|
+
it "fails without a valid state param" do
|
6
|
+
get "/auth/gds/callback"
|
7
|
+
|
8
|
+
expect(response).to redirect_to("/auth/failure?message=csrf_detected&strategy=gds")
|
9
|
+
end
|
10
|
+
|
11
|
+
it "redirects to the attempted url if a user was restricted earlier in the session" do
|
12
|
+
get "/restricted"
|
13
|
+
|
14
|
+
state = request_to_establish_oauth_state
|
15
|
+
|
16
|
+
stub_signon_oauth_token_request
|
17
|
+
stub_successful_signon_user_request
|
18
|
+
|
19
|
+
get "/auth/gds/callback?state=#{state}"
|
20
|
+
expect(response).to redirect_to("/restricted")
|
21
|
+
end
|
22
|
+
|
23
|
+
it "redirects to the root path if the user hadn't tried to access a restricted url" do
|
24
|
+
state = request_to_establish_oauth_state
|
25
|
+
|
26
|
+
stub_signon_oauth_token_request
|
27
|
+
stub_successful_signon_user_request
|
28
|
+
|
29
|
+
get "/auth/gds/callback?state=#{state}"
|
30
|
+
expect(response).to redirect_to("/")
|
31
|
+
end
|
32
|
+
|
33
|
+
it "uses the OAuth2 proof key for code exchange feature for increased security" do
|
34
|
+
get "/auth/gds"
|
35
|
+
|
36
|
+
expect(response).to have_http_status(:redirect)
|
37
|
+
location = URI.parse(response.location)
|
38
|
+
query = Rack::Utils.parse_query(location.query)
|
39
|
+
|
40
|
+
expect(location.path).to eq("/oauth/authorize")
|
41
|
+
expect(query).to include("code_challenge", "code_challenge_method")
|
42
|
+
|
43
|
+
token_request = stub_request(:post, "http://signon/oauth/access_token")
|
44
|
+
.with(body: hash_including("code_verifier"))
|
45
|
+
|
46
|
+
get "/auth/gds/callback?state=#{query['state']}"
|
47
|
+
|
48
|
+
expect(token_request).to have_been_made
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
describe "GET /auth/failure" do
|
53
|
+
it "responds successfully" do
|
54
|
+
get "/auth/failure"
|
55
|
+
|
56
|
+
expect(response).to have_http_status(:success)
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
describe "GET /auth/gds/sign_out" do
|
61
|
+
it "redirects to signon sign out" do
|
62
|
+
get "/auth/gds/sign_out"
|
63
|
+
|
64
|
+
expect(response).to redirect_to("http://signon/users/sign_out")
|
65
|
+
end
|
66
|
+
|
67
|
+
it "logs an authenticated user out" do
|
68
|
+
authenticate_with_stub_signon
|
69
|
+
|
70
|
+
get "/auth/gds/sign_out"
|
71
|
+
|
72
|
+
# access a restricted route to assert we're logged out
|
73
|
+
get "/restricted"
|
74
|
+
expect(response).to redirect_to("/auth/gds")
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
@@ -0,0 +1,264 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe "Integration tests with a demo app", type: :request do
|
4
|
+
describe "accessing a route that doesn't require authentication" do
|
5
|
+
it "allows access" do
|
6
|
+
get "/not-restricted"
|
7
|
+
expect(response).to have_http_status(:success)
|
8
|
+
expect(response.body).to eq("jabberwocky")
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
describe "accessing a route the requires authentication" do
|
13
|
+
context "when GDS::SSO isn't configured to treat this as an explicit API request" do
|
14
|
+
it "redirects an unauthenticated request to sign-in" do
|
15
|
+
get "/restricted"
|
16
|
+
expect(response).to redirect_to("/auth/gds")
|
17
|
+
end
|
18
|
+
|
19
|
+
it "responds successfully for an authenticated user" do
|
20
|
+
authenticate_with_stub_signon
|
21
|
+
|
22
|
+
get "/restricted"
|
23
|
+
expect(response).to have_http_status(:success)
|
24
|
+
expect(response.body).to eq("restricted kablooie")
|
25
|
+
end
|
26
|
+
|
27
|
+
it "redirects to sign-in if a user is authenticated but remotely signed out" do
|
28
|
+
authenticate_with_stub_signon
|
29
|
+
User.last.set_remotely_signed_out!
|
30
|
+
|
31
|
+
get "/restricted"
|
32
|
+
expect(response).to redirect_to("/auth/gds")
|
33
|
+
end
|
34
|
+
|
35
|
+
it "redirects to sign-in if a user's session has expired" do
|
36
|
+
authenticate_with_stub_signon
|
37
|
+
|
38
|
+
travel_to(Time.now.utc + GDS::SSO::Config.auth_valid_for + 1.second) do
|
39
|
+
get "/restricted"
|
40
|
+
expect(response).to redirect_to("/auth/gds")
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
it "allows access when given a valid bearer token" do
|
45
|
+
stub_successful_signon_user_request
|
46
|
+
|
47
|
+
get "/restricted", headers: { "Authorization" => "Bearer 123" }
|
48
|
+
expect(response).to have_http_status(:success)
|
49
|
+
expect(response.body).to eq("restricted kablooie")
|
50
|
+
end
|
51
|
+
|
52
|
+
it "restricts access when given an invalid bearer token" do
|
53
|
+
stub_failed_signon_user_request
|
54
|
+
|
55
|
+
get "/restricted", headers: { "Authorization" => "Bearer invalid" }
|
56
|
+
expect(response).to have_http_status(:unauthorized)
|
57
|
+
expect_invalid_bearer_token_response(response)
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
context "when an application is configured as API only" do
|
62
|
+
before { allow(GDS::SSO::Config).to receive(:api_only).and_return(true) }
|
63
|
+
|
64
|
+
it "allows access when given a valid bearer token" do
|
65
|
+
stub_successful_signon_user_request
|
66
|
+
|
67
|
+
get "/restricted", headers: { "Authorization" => "Bearer 123" }
|
68
|
+
expect(response).to have_http_status(:success)
|
69
|
+
expect(response.body).to eq("restricted kablooie")
|
70
|
+
end
|
71
|
+
|
72
|
+
it "rejects a request without a bearer token" do
|
73
|
+
get "/restricted"
|
74
|
+
expect(response).to have_http_status(:unauthorized)
|
75
|
+
expect_missing_bearer_token_response(response)
|
76
|
+
end
|
77
|
+
|
78
|
+
it "restricts access for an invalid bearer token" do
|
79
|
+
stub_failed_signon_user_request
|
80
|
+
|
81
|
+
get "/restricted", headers: { "Authorization" => "Bearer invalid" }
|
82
|
+
expect(response).to have_http_status(:unauthorized)
|
83
|
+
expect_invalid_bearer_token_response(response)
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
context "when API requests are differentiated by api_request_matcher" do
|
88
|
+
it "treats a match as an API request" do
|
89
|
+
allow(GDS::SSO::Config)
|
90
|
+
.to receive(:api_request_matcher)
|
91
|
+
.and_return(->(request) { request.path == "/restricted" })
|
92
|
+
|
93
|
+
get "/restricted"
|
94
|
+
expect(response).to have_http_status(:unauthorized)
|
95
|
+
expect_missing_bearer_token_response(response)
|
96
|
+
end
|
97
|
+
|
98
|
+
it "treats a non-match as a non-API request" do
|
99
|
+
allow(GDS::SSO::Config)
|
100
|
+
.to receive(:api_request_matcher)
|
101
|
+
.and_return(->(_request) { false })
|
102
|
+
|
103
|
+
get "/restricted"
|
104
|
+
expect(response).to redirect_to("/auth/gds")
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
109
|
+
describe "when accessing routes without authentication and using the mock strategies" do
|
110
|
+
before { use_mock_strategies }
|
111
|
+
|
112
|
+
it "allows a user access without authentication" do
|
113
|
+
# non-bearer token mock requests require a user to exist
|
114
|
+
User.create!(
|
115
|
+
uid: SecureRandom.uuid,
|
116
|
+
email: "user@example.com",
|
117
|
+
name: "Example User",
|
118
|
+
permissions: [],
|
119
|
+
)
|
120
|
+
|
121
|
+
get "/restricted"
|
122
|
+
expect(response).to have_http_status(:success)
|
123
|
+
expect(response.body).to eq("restricted kablooie")
|
124
|
+
end
|
125
|
+
|
126
|
+
it "can be configured to fail authentication with an env var" do
|
127
|
+
ClimateControl.modify("GDS_SSO_MOCK_INVALID" => "1") do
|
128
|
+
get "/restricted"
|
129
|
+
expect(response).to redirect_to("/auth/gds")
|
130
|
+
end
|
131
|
+
end
|
132
|
+
|
133
|
+
it "allows an API request without a bearer token" do
|
134
|
+
allow(GDS::SSO::Config).to receive(:api_only).and_return(true)
|
135
|
+
|
136
|
+
get "/restricted"
|
137
|
+
expect(response).to have_http_status(:success)
|
138
|
+
expect(response.body).to eq("restricted kablooie")
|
139
|
+
end
|
140
|
+
|
141
|
+
it "can be configured to fail API authentication with an env var" do
|
142
|
+
allow(GDS::SSO::Config).to receive(:api_only).and_return(true)
|
143
|
+
|
144
|
+
ClimateControl.modify("GDS_SSO_MOCK_INVALID" => "1") do
|
145
|
+
get "/restricted"
|
146
|
+
expect(response).to have_http_status(:unauthorized)
|
147
|
+
end
|
148
|
+
end
|
149
|
+
end
|
150
|
+
|
151
|
+
describe "when accessing a route that requires permission" do
|
152
|
+
context "when GDS::SSO isn't configured to treat this as an explicit API request" do
|
153
|
+
it "allows a user with the permission to access the resource" do
|
154
|
+
authenticate_with_stub_signon(permissions: %w[execute])
|
155
|
+
|
156
|
+
get "/this-requires-execute-permission"
|
157
|
+
expect(response).to have_http_status(:success)
|
158
|
+
expect(response.body).to eq("you have execute permission")
|
159
|
+
end
|
160
|
+
|
161
|
+
it "restricts a user lacking the permission" do
|
162
|
+
authenticate_with_stub_signon
|
163
|
+
|
164
|
+
get "/this-requires-execute-permission"
|
165
|
+
expect(response).to have_http_status(:forbidden)
|
166
|
+
expect(response.body).to include("Sorry, you don't seem to have the execute permission for this app.")
|
167
|
+
end
|
168
|
+
end
|
169
|
+
|
170
|
+
context "when GDS::SSO is configured to treat the request as an API request" do
|
171
|
+
before { allow(GDS::SSO::Config).to receive(:api_only).and_return(true) }
|
172
|
+
|
173
|
+
it "allows a user with the permission to access the resource" do
|
174
|
+
stub_successful_signon_user_request(permissions: %w[execute])
|
175
|
+
|
176
|
+
get "/this-requires-execute-permission", headers: { "Authorization" => "Bearer 123" }
|
177
|
+
expect(response).to have_http_status(:success)
|
178
|
+
expect(response.body).to eq("you have execute permission")
|
179
|
+
end
|
180
|
+
|
181
|
+
it "restricts a user lacking the permission" do
|
182
|
+
stub_successful_signon_user_request
|
183
|
+
|
184
|
+
get "/this-requires-execute-permission", headers: { "Authorization" => "Bearer 123" }
|
185
|
+
expect(response).to have_http_status(:forbidden)
|
186
|
+
expect_json_response(response, { "message" => "Sorry, you don't seem to have the execute permission for this app." })
|
187
|
+
end
|
188
|
+
end
|
189
|
+
|
190
|
+
context "when using bearer token for auth with the mock strategy" do
|
191
|
+
before { use_mock_strategies }
|
192
|
+
|
193
|
+
it "automatically grants permissions configured in GDS:SSO::Config.additional_mock_permissions_required" do
|
194
|
+
allow(GDS::SSO::Config).to receive(:additional_mock_permissions_required).and_return(%w[execute])
|
195
|
+
|
196
|
+
stub_successful_signon_user_request
|
197
|
+
|
198
|
+
get "/this-requires-execute-permission", headers: { "Authorization" => "Bearer 123" }
|
199
|
+
expect(response).to have_http_status(:success)
|
200
|
+
expect(response.body).to eq("you have execute permission")
|
201
|
+
end
|
202
|
+
|
203
|
+
it "doesn't grant access without that config" do
|
204
|
+
allow(GDS::SSO::Config).to receive(:additional_mock_permissions_required).and_return(nil)
|
205
|
+
|
206
|
+
stub_successful_signon_user_request
|
207
|
+
|
208
|
+
get "/this-requires-execute-permission", headers: { "Authorization" => "Bearer 123" }
|
209
|
+
expect(response).to have_http_status(:forbidden)
|
210
|
+
expect_json_response(response, { "message" => "Sorry, you don't seem to have the execute permission for this app." })
|
211
|
+
end
|
212
|
+
end
|
213
|
+
end
|
214
|
+
|
215
|
+
context "when accessing a route that is restricted by the authorised user constraint" do
|
216
|
+
it "allows access when an authenticated user has correct permissions" do
|
217
|
+
authenticate_with_stub_signon(permissions: %w[constraint])
|
218
|
+
|
219
|
+
get "/constraint-restricted"
|
220
|
+
expect(response).to have_http_status(:success)
|
221
|
+
expect(response.body).to eq("constraint restricted")
|
222
|
+
end
|
223
|
+
|
224
|
+
it "redirects an unauthenticated request to signon" do
|
225
|
+
get "/constraint-restricted"
|
226
|
+
|
227
|
+
expect(response).to redirect_to("/auth/gds")
|
228
|
+
end
|
229
|
+
|
230
|
+
it "restricts access when an authenticated user does not have the correct permissions" do
|
231
|
+
authenticate_with_stub_signon
|
232
|
+
|
233
|
+
get "/constraint-restricted"
|
234
|
+
expect(response).to have_http_status(:forbidden)
|
235
|
+
end
|
236
|
+
end
|
237
|
+
|
238
|
+
def expect_missing_bearer_token_response(response)
|
239
|
+
expect(response.headers).to include("WWW-Authenticate" => 'Bearer error="invalid_request"')
|
240
|
+
expect_json_response(response, { "message" => "No bearer token was provided" })
|
241
|
+
end
|
242
|
+
|
243
|
+
def expect_invalid_bearer_token_response(response)
|
244
|
+
expect(response.headers).to include("WWW-Authenticate" => 'Bearer error="invalid_token"')
|
245
|
+
expect_json_response(response, { "message" => "Bearer token does not appear to be valid" })
|
246
|
+
end
|
247
|
+
|
248
|
+
def expect_json_response(response, json)
|
249
|
+
expect(response.media_type).to eq("application/json")
|
250
|
+
expect(response.parsed_body).to eq(json)
|
251
|
+
end
|
252
|
+
|
253
|
+
def use_mock_strategies
|
254
|
+
# Using allow_any_instance_of because it's hard to access the instance
|
255
|
+
# of the class used within the Rails middleware
|
256
|
+
allow_any_instance_of(Warden::Config).to receive(:[]).and_call_original
|
257
|
+
allow_any_instance_of(Warden::Config)
|
258
|
+
.to receive(:[])
|
259
|
+
.with(:default_strategies)
|
260
|
+
.and_return({ _all: %i[mock_gds_sso gds_bearer_token] })
|
261
|
+
|
262
|
+
allow(Warden::OAuth2.config).to receive(:token_model).and_return(GDS::SSO::MockBearerToken)
|
263
|
+
end
|
264
|
+
end
|
data/spec/spec_helper.rb
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
# Bad things happen if we don't ;-)
|
3
3
|
ENV["GDS_SSO_STRATEGY"] = "real"
|
4
4
|
|
5
|
-
require "
|
5
|
+
require "climate_control"
|
6
6
|
require "webmock/rspec"
|
7
7
|
require "combustion"
|
8
8
|
|
@@ -12,15 +12,10 @@ Combustion.initialize! :all do
|
|
12
12
|
end
|
13
13
|
|
14
14
|
require "rspec/rails"
|
15
|
-
require "capybara/rails"
|
16
15
|
WebMock.disable_net_connect!
|
17
16
|
|
18
17
|
Dir[File.join(File.dirname(__FILE__), "support/**/*.rb")].sort.each { |f| require f }
|
19
18
|
|
20
|
-
Capybara.register_driver :rack_test do |app|
|
21
|
-
Capybara::RackTest::Driver.new(app, follow_redirects: false)
|
22
|
-
end
|
23
|
-
|
24
19
|
RSpec.configure do |config|
|
25
20
|
config.run_all_when_everything_filtered = true
|
26
21
|
config.filter_run :focus
|
@@ -32,6 +27,20 @@ RSpec.configure do |config|
|
|
32
27
|
# --seed 1234
|
33
28
|
config.order = "random"
|
34
29
|
|
30
|
+
config.include ActiveSupport::Testing::TimeHelpers
|
35
31
|
config.include Warden::Test::Helpers
|
36
|
-
config.include
|
32
|
+
config.include RequestHelpers, type: :request
|
33
|
+
config.before(:each, type: :request) do
|
34
|
+
# we reload routes each test as GDS::SSO::Config affects what routes are
|
35
|
+
# available, we only want to run this once routes are loaded otherwise
|
36
|
+
# we can lose app routes
|
37
|
+
routes_reloader = Rails.application.routes_reloader
|
38
|
+
|
39
|
+
# Routes changed in Rails 8 to be lazily loaded so this wasn't a problem
|
40
|
+
# before Rails 8.
|
41
|
+
# TODO: remove this line once Rails 7 support is removed
|
42
|
+
next unless routes_reloader.respond_to?(:loaded)
|
43
|
+
|
44
|
+
routes_reloader.reload! if routes_reloader.loaded
|
45
|
+
end
|
37
46
|
end
|