Skip to content

Commit 0b31793

Browse files
Revert "Connect the token authentication mechanism to Devise session tracking"
This reverts commit a69bbc1 which was causing a 403 error on every request from the reporting app, triggering a reauthentication. This was also breaking filtering in the reporting app due to every request hitting an unrelated bug in the OAuth redirect code in Mavis.
1 parent 7478171 commit 0b31793

4 files changed

Lines changed: 19 additions & 40 deletions

File tree

app/controllers/concerns/reporting_api/token_authentication_concern.rb

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,8 @@ def authenticate_user_by_jwt!
4343
)
4444
)
4545
if @current_user
46+
session["user"] = data["user"]
4647
session["cis2_info"] = data["cis2_info"]
47-
48-
# Establish a Warden session with activity tracking
49-
# which enables Devise's timeoutable module
50-
sign_in @current_user, event: :authentication
51-
5248
authenticate_user!
5349
else
5450
session.clear

spec/controllers/concerns/reporting_api/token_authentication_concern_spec.rb

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,6 @@
1212
def authenticate_user!
1313
end
1414

15-
def sign_in(user, options = {})
16-
end
17-
1815
def initialize(request: nil, session: {})
1916
@request = request
2017
@session = session
@@ -204,20 +201,20 @@ def current_user
204201
context "when a User exists with the values of id, session_token and reporting_api_session_token" do
205202
let(:user_id) { user.id }
206203

204+
it "copies the user key into session['user']" do
205+
an_object_which_includes_the_concern.send(:authenticate_user_by_jwt!)
206+
expect(an_object_which_includes_the_concern.session["user"]).to eq(
207+
user_info.first["data"]["user"]
208+
)
209+
end
210+
207211
it "copies the cis2_info key into session['cis2_info']" do
208212
an_object_which_includes_the_concern.send(:authenticate_user_by_jwt!)
209213
expect(
210214
an_object_which_includes_the_concern.session["cis2_info"]
211215
).to eq(user_info.first["data"]["cis2_info"])
212216
end
213217

214-
it "signs in the user through Devise/Warden" do
215-
expect(an_object_which_includes_the_concern).to receive(
216-
:sign_in
217-
).with(user, event: :authentication)
218-
an_object_which_includes_the_concern.send(:authenticate_user_by_jwt!)
219-
end
220-
221218
it "calls authenticate_user!" do
222219
expect(an_object_which_includes_the_concern).to receive(
223220
:authenticate_user!

spec/support/reporting_api_helper.rb

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,6 @@ module ReportingAPIHelper
44
def valid_jwt_payload
55
team = create(:team, :with_one_nurse)
66
user = team.users.first
7-
8-
# Ensure the user has session tokens required by Warden callbacks
9-
user.update!(
10-
session_token: SecureRandom.hex(32),
11-
reporting_api_session_token: SecureRandom.hex(32)
12-
)
13-
147
{
158
data: {
169
user: user.as_json,
@@ -23,8 +16,12 @@ def valid_jwt_payload
2316
}
2417
end
2518

26-
def valid_jwt(payload = valid_jwt_payload)
27-
JWT.encode(payload, Settings.reporting_api.client_app.secret, "HS512")
19+
def valid_jwt
20+
JWT.encode(
21+
valid_jwt_payload,
22+
Settings.reporting_api.client_app.secret,
23+
"HS512"
24+
)
2825
end
2926

3027
def invalid_jwt_payload

spec/support/shared_examples/a_reporting_api_controller.rb

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
# frozen_string_literal: true
22

33
shared_examples "a ReportingAPI controller" do
4-
include ReportingAPIHelper
4+
let(:team) { create(:team, :with_one_nurse) }
5+
let(:user) { team.users.first }
56

6-
# Extract the user from the JWT payload so we're testing with the same user
7-
# that was authenticated via JWT
8-
let(:jwt_payload) { valid_jwt_payload }
9-
let(:user) { User.find(jwt_payload[:data][:user]["id"]) }
10-
let(:team) { user.teams.first }
7+
include ReportingAPIHelper
118

129
context "when the reporting_api feature flag is disabled" do
1310
before { Flipper.disable(:reporting_api) }
@@ -17,7 +14,7 @@
1714
let(:params) { { jwt: jwt } }
1815

1916
context "which is valid" do
20-
let(:jwt) { valid_jwt(jwt_payload) }
17+
let(:jwt) { valid_jwt }
2118

2219
it "responds with status :forbidden" do
2320
get :index, params: { jwt: jwt }
@@ -36,20 +33,12 @@
3633
let(:params) { { jwt: jwt } }
3734

3835
context "which is valid" do
39-
let(:jwt) { valid_jwt(jwt_payload) }
36+
let(:jwt) { valid_jwt }
4037

4138
it "responds with status 200" do
4239
get :index, params: { jwt: jwt }
4340
expect(response.status).to eq(200)
4441
end
45-
46-
it "establishes a Warden session with activity tracking" do
47-
get :index, params: { jwt: jwt }
48-
49-
expect(
50-
request.session.dig("warden.user.user.session", "last_request_at")
51-
).to be_a(Integer)
52-
end
5342
end
5443

5544
context "which is not valid" do

0 commit comments

Comments
 (0)