Skip to content

Commit 67fb840

Browse files
authored
Merge pull request #2909 from OpenC3/sessions
Logout when changing password to clear sessions
2 parents d02ac06 + 17b055a commit 67fb840

File tree

3 files changed

+31
-0
lines changed

3 files changed

+31
-0
lines changed

openc3-cosmos-cmd-tlm-api/spec/controllers/auth_controller_spec.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,28 @@
5757
end
5858
end
5959

60+
describe "set" do
61+
it "revokes old sessions and issues a new token on password change" do
62+
# Set initial password and get a session token
63+
post :set, params: { password: 'PASSWORD' }
64+
expect(response).to have_http_status(:ok)
65+
old_token = response.body
66+
expect(old_token).not_to be_empty
67+
68+
# Change password
69+
post :set, params: { password: 'PASSWORD2', old_password: 'PASSWORD' }
70+
expect(response).to have_http_status(:ok)
71+
new_token = response.body
72+
expect(new_token).not_to be_empty
73+
expect(new_token).not_to eq(old_token)
74+
75+
# Old token should be invalid
76+
expect(OpenC3::AuthModel.verify(old_token)).to eq(false)
77+
# New token should be valid
78+
expect(OpenC3::AuthModel.verify(new_token)).to eq(true)
79+
end
80+
end
81+
6082
describe "verify" do
6183
it "requires token" do
6284
post :verify

openc3/lib/openc3/models/auth_model.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ def self.set(password, old_password, key = PRIMARY_KEY)
109109
Store.set(key, pw_hash)
110110
@@pw_hash_cache = nil
111111
@@pw_hash_cache_time = nil
112+
logout
112113
end
113114

114115
# Creates a new session token. DO NOT CALL BEFORE VERIFYING.

openc3/spec/models/auth_model_spec.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,14 @@ module OpenC3
6161
expect(AuthModel.verify(token)).to eq(false)
6262
end
6363

64+
it "revokes all sessions on password change" do
65+
token = AuthModel.generate_session
66+
expect(AuthModel.verify(token)).to eq(true)
67+
68+
AuthModel.set('newpassword', AUTH_INITIAL_PASSWORD)
69+
expect(AuthModel.verify(token)).to eq(false)
70+
end
71+
6472
it "raises when stored password hash is SHA256" do
6573
@redis.set(PW_HASH_PRIMARY_KEY, Digest::SHA256.hexdigest(AUTH_INITIAL_PASSWORD))
6674
expect{ AuthModel.verify_no_service(AUTH_INITIAL_PASSWORD, mode: :any) }.to \

0 commit comments

Comments
 (0)