2019-04-29 20:27:42 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-10-25 05:45:31 -04:00
|
|
|
require "discourse_ip_info"
|
2017-01-31 17:21:37 -05:00
|
|
|
|
2022-07-27 22:27:38 -04:00
|
|
|
RSpec.describe UserAuthToken do
|
2021-12-22 12:09:43 -05:00
|
|
|
fab!(:user) { Fabricate(:user) }
|
2017-01-31 17:21:37 -05:00
|
|
|
|
|
|
|
it "can remove old expired tokens" do
|
2017-02-13 14:01:01 -05:00
|
|
|
SiteSetting.verbose_auth_token_logging = true
|
|
|
|
|
2017-01-31 17:21:37 -05:00
|
|
|
freeze_time Time.zone.now
|
|
|
|
SiteSetting.maximum_session_age = 1
|
|
|
|
|
|
|
|
token =
|
|
|
|
UserAuthToken.generate!(
|
|
|
|
user_id: user.id,
|
|
|
|
user_agent: "some user agent 2",
|
|
|
|
client_ip: "1.1.2.3",
|
|
|
|
)
|
|
|
|
|
|
|
|
freeze_time 1.hour.from_now
|
|
|
|
UserAuthToken.cleanup!
|
|
|
|
|
|
|
|
expect(UserAuthToken.where(id: token.id).count).to eq(1)
|
|
|
|
|
|
|
|
freeze_time 1.second.from_now
|
|
|
|
UserAuthToken.cleanup!
|
|
|
|
|
|
|
|
expect(UserAuthToken.where(id: token.id).count).to eq(1)
|
|
|
|
|
|
|
|
freeze_time UserAuthToken::ROTATE_TIME.from_now
|
|
|
|
UserAuthToken.cleanup!
|
|
|
|
|
|
|
|
expect(UserAuthToken.where(id: token.id).count).to eq(0)
|
|
|
|
end
|
|
|
|
|
2018-05-03 20:11:58 -04:00
|
|
|
it "can lookup hashed" do
|
2017-01-31 17:21:37 -05:00
|
|
|
token =
|
|
|
|
UserAuthToken.generate!(
|
|
|
|
user_id: user.id,
|
|
|
|
user_agent: "some user agent 2",
|
|
|
|
client_ip: "1.1.2.3",
|
|
|
|
)
|
|
|
|
|
|
|
|
lookup_token = UserAuthToken.lookup(token.unhashed_auth_token)
|
|
|
|
|
|
|
|
expect(user.id).to eq(lookup_token.user.id)
|
|
|
|
|
|
|
|
lookup_token = UserAuthToken.lookup(token.auth_token)
|
|
|
|
|
|
|
|
expect(lookup_token).to eq(nil)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "can validate token was seen at lookup time" do
|
|
|
|
user_token =
|
|
|
|
UserAuthToken.generate!(
|
|
|
|
user_id: user.id,
|
|
|
|
user_agent: "some user agent 2",
|
|
|
|
client_ip: "1.1.2.3",
|
|
|
|
)
|
|
|
|
|
|
|
|
expect(user_token.auth_token_seen).to eq(false)
|
|
|
|
|
|
|
|
UserAuthToken.lookup(user_token.unhashed_auth_token, seen: true)
|
|
|
|
|
|
|
|
user_token.reload
|
|
|
|
expect(user_token.auth_token_seen).to eq(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "can rotate with no params maintaining data" do
|
|
|
|
user_token =
|
|
|
|
UserAuthToken.generate!(
|
|
|
|
user_id: user.id,
|
|
|
|
user_agent: "some user agent 2",
|
|
|
|
client_ip: "1.1.2.3",
|
|
|
|
)
|
|
|
|
|
|
|
|
user_token.update_columns(auth_token_seen: true)
|
|
|
|
expect(user_token.rotate!).to eq(true)
|
|
|
|
user_token.reload
|
|
|
|
expect(user_token.client_ip.to_s).to eq("1.1.2.3")
|
|
|
|
expect(user_token.user_agent).to eq("some user agent 2")
|
|
|
|
end
|
|
|
|
|
2017-02-26 17:09:57 -05:00
|
|
|
it "expires correctly" do
|
2020-03-10 17:13:17 -04:00
|
|
|
freeze_time Time.zone.now
|
2017-02-26 17:09:57 -05:00
|
|
|
user_token =
|
|
|
|
UserAuthToken.generate!(
|
|
|
|
user_id: user.id,
|
|
|
|
user_agent: "some user agent 2",
|
|
|
|
client_ip: "1.1.2.3",
|
|
|
|
)
|
|
|
|
|
|
|
|
UserAuthToken.lookup(user_token.unhashed_auth_token, seen: true)
|
|
|
|
|
2020-03-10 17:13:17 -04:00
|
|
|
freeze_time SiteSetting.maximum_session_age.hours.from_now - 1.second
|
2017-02-26 17:09:57 -05:00
|
|
|
|
|
|
|
user_token.reload
|
|
|
|
user_token.rotate!
|
|
|
|
UserAuthToken.lookup(user_token.unhashed_auth_token, seen: true)
|
|
|
|
|
2020-03-10 17:13:17 -04:00
|
|
|
freeze_time SiteSetting.maximum_session_age.hours.from_now - 1.second
|
2017-02-26 17:09:57 -05:00
|
|
|
|
|
|
|
still_good = UserAuthToken.lookup(user_token.unhashed_auth_token, seen: true)
|
|
|
|
expect(still_good).not_to eq(nil)
|
|
|
|
|
|
|
|
freeze_time 2.hours.from_now
|
|
|
|
|
|
|
|
not_good = UserAuthToken.lookup(user_token.unhashed_auth_token, seen: true)
|
|
|
|
expect(not_good).to eq(nil)
|
|
|
|
end
|
|
|
|
|
2017-01-31 17:21:37 -05:00
|
|
|
it "can properly rotate tokens" do
|
2020-03-10 17:13:17 -04:00
|
|
|
freeze_time 3.days.ago
|
2017-01-31 17:21:37 -05:00
|
|
|
|
|
|
|
user_token =
|
|
|
|
UserAuthToken.generate!(
|
|
|
|
user_id: user.id,
|
|
|
|
user_agent: "some user agent 2",
|
|
|
|
client_ip: "1.1.2.3",
|
|
|
|
)
|
|
|
|
|
|
|
|
prev_auth_token = user_token.auth_token
|
|
|
|
unhashed_prev = user_token.unhashed_auth_token
|
|
|
|
|
|
|
|
rotated = user_token.rotate!(user_agent: "a new user agent", client_ip: "1.1.2.4")
|
|
|
|
expect(rotated).to eq(false)
|
|
|
|
|
|
|
|
user_token.update_columns(auth_token_seen: true)
|
|
|
|
|
2020-03-10 17:13:17 -04:00
|
|
|
rotation_time = freeze_time 1.day.from_now
|
|
|
|
|
2017-01-31 17:21:37 -05:00
|
|
|
rotated = user_token.rotate!(user_agent: "a new user agent", client_ip: "1.1.2.4")
|
|
|
|
expect(rotated).to eq(true)
|
|
|
|
|
|
|
|
user_token.reload
|
|
|
|
|
2020-03-10 17:13:17 -04:00
|
|
|
expect(user_token.rotated_at).to eq_time(rotation_time)
|
2017-01-31 17:21:37 -05:00
|
|
|
expect(user_token.client_ip).to eq("1.1.2.4")
|
|
|
|
expect(user_token.user_agent).to eq("a new user agent")
|
|
|
|
expect(user_token.auth_token_seen).to eq(false)
|
2017-02-15 10:58:18 -05:00
|
|
|
expect(user_token.seen_at).to eq(nil)
|
2017-01-31 17:21:37 -05:00
|
|
|
expect(user_token.prev_auth_token).to eq(prev_auth_token)
|
|
|
|
|
|
|
|
# ability to auth using an old token
|
2020-03-10 17:13:17 -04:00
|
|
|
seen_at = freeze_time 1.day.from_now
|
2017-02-15 10:58:18 -05:00
|
|
|
|
|
|
|
looked_up = UserAuthToken.lookup(user_token.unhashed_auth_token, seen: true)
|
|
|
|
expect(looked_up.id).to eq(user_token.id)
|
|
|
|
expect(looked_up.auth_token_seen).to eq(true)
|
2020-03-10 17:13:17 -04:00
|
|
|
expect(looked_up.seen_at).to eq_time(seen_at)
|
2017-02-15 10:58:18 -05:00
|
|
|
|
|
|
|
looked_up = UserAuthToken.lookup(unhashed_prev, seen: true)
|
2017-01-31 17:21:37 -05:00
|
|
|
expect(looked_up.id).to eq(user_token.id)
|
|
|
|
|
2020-03-10 17:13:17 -04:00
|
|
|
freeze_time 2.minutes.from_now
|
2017-02-15 10:58:18 -05:00
|
|
|
|
|
|
|
looked_up = UserAuthToken.lookup(unhashed_prev)
|
2017-02-26 17:09:57 -05:00
|
|
|
expect(looked_up).not_to eq(nil)
|
|
|
|
|
|
|
|
looked_up.reload
|
|
|
|
expect(looked_up.auth_token_seen).to eq(false)
|
2017-02-15 10:58:18 -05:00
|
|
|
|
|
|
|
rotated = user_token.rotate!(user_agent: "a new user agent", client_ip: "1.1.2.4")
|
|
|
|
expect(rotated).to eq(true)
|
|
|
|
user_token.reload
|
|
|
|
expect(user_token.seen_at).to eq(nil)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "keeps prev token valid for 1 minute after it is confirmed" do
|
|
|
|
token =
|
|
|
|
UserAuthToken.generate!(user_id: user.id, user_agent: "some user agent", client_ip: "1.1.2.3")
|
|
|
|
|
|
|
|
UserAuthToken.lookup(token.unhashed_auth_token, seen: true)
|
|
|
|
|
|
|
|
freeze_time(10.minutes.from_now)
|
|
|
|
|
|
|
|
prev_token = token.unhashed_auth_token
|
|
|
|
|
|
|
|
token.rotate!(user_agent: "firefox", client_ip: "1.1.1.1")
|
|
|
|
|
|
|
|
freeze_time(10.minutes.from_now)
|
|
|
|
|
|
|
|
expect(UserAuthToken.lookup(token.unhashed_auth_token, seen: true)).not_to eq(nil)
|
|
|
|
expect(UserAuthToken.lookup(prev_token, seen: true)).not_to eq(nil)
|
2017-01-31 17:21:37 -05:00
|
|
|
end
|
|
|
|
|
2017-02-13 14:01:01 -05:00
|
|
|
it "can correctly log auth tokens" do
|
|
|
|
SiteSetting.verbose_auth_token_logging = true
|
|
|
|
|
|
|
|
token =
|
|
|
|
UserAuthToken.generate!(user_id: user.id, user_agent: "some user agent", client_ip: "1.1.2.3")
|
2023-01-09 06:18:21 -05:00
|
|
|
|
2017-02-13 14:01:01 -05:00
|
|
|
expect(
|
|
|
|
UserAuthTokenLog.where(
|
|
|
|
action: "generate",
|
|
|
|
user_id: user.id,
|
|
|
|
user_agent: "some user agent",
|
|
|
|
client_ip: "1.1.2.3",
|
|
|
|
user_auth_token_id: token.id,
|
|
|
|
).count,
|
|
|
|
).to eq(1)
|
2023-01-09 06:18:21 -05:00
|
|
|
|
2017-02-13 14:01:01 -05:00
|
|
|
UserAuthToken.lookup(
|
|
|
|
token.unhashed_auth_token,
|
|
|
|
seen: true,
|
|
|
|
user_agent: "something diff",
|
|
|
|
client_ip: "1.2.3.3",
|
|
|
|
)
|
|
|
|
|
|
|
|
UserAuthToken.lookup(
|
|
|
|
token.unhashed_auth_token,
|
|
|
|
seen: true,
|
|
|
|
user_agent: "something diff2",
|
|
|
|
client_ip: "1.2.3.3",
|
|
|
|
)
|
|
|
|
|
|
|
|
expect(
|
|
|
|
UserAuthTokenLog.where(
|
|
|
|
action: "seen token",
|
|
|
|
user_id: user.id,
|
|
|
|
auth_token: token.auth_token,
|
|
|
|
client_ip: "1.2.3.3",
|
|
|
|
user_auth_token_id: token.id,
|
|
|
|
).count,
|
|
|
|
).to eq(1)
|
|
|
|
|
|
|
|
fake_token = SecureRandom.hex
|
2017-03-07 13:27:34 -05:00
|
|
|
UserAuthToken.lookup(
|
|
|
|
fake_token,
|
|
|
|
seen: true,
|
2017-02-13 14:01:01 -05:00
|
|
|
user_agent: "bob",
|
2017-03-07 13:27:34 -05:00
|
|
|
client_ip: "127.0.0.1",
|
|
|
|
path: "/path",
|
2023-01-09 06:18:21 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
expect(
|
2017-02-13 14:01:01 -05:00
|
|
|
UserAuthTokenLog.where(
|
|
|
|
action: "miss token",
|
|
|
|
auth_token: UserAuthToken.hash_token(fake_token),
|
|
|
|
user_agent: "bob",
|
2017-03-07 13:27:34 -05:00
|
|
|
client_ip: "127.0.0.1",
|
|
|
|
path: "/path",
|
2017-02-13 14:01:01 -05:00
|
|
|
).count,
|
|
|
|
).to eq(1)
|
|
|
|
|
|
|
|
freeze_time(UserAuthToken::ROTATE_TIME.from_now)
|
|
|
|
|
|
|
|
token.rotate!(user_agent: "firefox", client_ip: "1.1.1.1")
|
|
|
|
|
|
|
|
expect(
|
|
|
|
UserAuthTokenLog.where(
|
|
|
|
action: "rotate",
|
|
|
|
auth_token: token.auth_token,
|
|
|
|
user_agent: "firefox",
|
|
|
|
client_ip: "1.1.1.1",
|
|
|
|
user_auth_token_id: token.id,
|
|
|
|
).count,
|
|
|
|
).to eq(1)
|
|
|
|
end
|
|
|
|
|
2018-09-02 03:24:54 -04:00
|
|
|
it "calls before_destroy" do
|
|
|
|
SiteSetting.verbose_auth_token_logging = true
|
|
|
|
|
|
|
|
token =
|
|
|
|
UserAuthToken.generate!(user_id: user.id, user_agent: "some user agent", client_ip: "1.1.2.3")
|
|
|
|
|
|
|
|
expect(user.user_auth_token_logs.count).to eq(1)
|
|
|
|
|
|
|
|
token.destroy
|
|
|
|
|
|
|
|
expect(user.user_auth_token_logs.count).to eq(2)
|
|
|
|
expect(user.user_auth_token_logs.last.action).to eq("destroy")
|
|
|
|
expect(user.user_auth_token_logs.last.user_agent).to eq("some user agent")
|
|
|
|
expect(user.user_auth_token_logs.last.client_ip).to eq("1.1.2.3")
|
|
|
|
end
|
|
|
|
|
2017-02-28 10:38:22 -05:00
|
|
|
it "will not mark token unseen when prev and current are the same" do
|
|
|
|
token =
|
|
|
|
UserAuthToken.generate!(user_id: user.id, user_agent: "some user agent", client_ip: "1.1.2.3")
|
|
|
|
|
|
|
|
lookup = UserAuthToken.lookup(token.unhashed_auth_token, seen: true)
|
|
|
|
lookup = UserAuthToken.lookup(token.unhashed_auth_token, seen: true)
|
|
|
|
lookup.reload
|
|
|
|
expect(lookup.auth_token_seen).to eq(true)
|
|
|
|
end
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context "with suspicious login" do
|
2019-05-06 23:12:20 -04:00
|
|
|
fab!(:admin) { Fabricate(:admin) }
|
2018-11-12 09:34:12 -05:00
|
|
|
|
|
|
|
it "is not checked when generated for non-staff" do
|
|
|
|
UserAuthToken.generate!(user_id: user.id, staff: user.staff?)
|
|
|
|
|
|
|
|
expect(Jobs::SuspiciousLogin.jobs.size).to eq(0)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "is checked when generated for staff" do
|
|
|
|
UserAuthToken.generate!(user_id: admin.id, staff: admin.staff?)
|
|
|
|
|
|
|
|
expect(Jobs::SuspiciousLogin.jobs.size).to eq(1)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "is not checked when generated by impersonate" do
|
|
|
|
UserAuthToken.generate!(user_id: admin.id, staff: admin.staff?, impersonate: true)
|
|
|
|
|
|
|
|
expect(Jobs::SuspiciousLogin.jobs.size).to eq(0)
|
|
|
|
end
|
|
|
|
end
|
2017-01-31 17:21:37 -05:00
|
|
|
end
|