2017-01-31 17:21:37 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
require 'digest/sha1'
|
|
|
|
|
|
|
|
class UserAuthToken < ActiveRecord::Base
|
|
|
|
belongs_to :user
|
|
|
|
|
2018-05-03 20:11:58 -04:00
|
|
|
# TODO 2019: remove this line
|
|
|
|
self.ignored_columns = ["legacy"]
|
|
|
|
|
2017-01-31 17:21:37 -05:00
|
|
|
ROTATE_TIME = 10.minutes
|
|
|
|
# used when token did not arrive at client
|
|
|
|
URGENT_ROTATE_TIME = 1.minute
|
|
|
|
|
2018-08-31 04:18:06 -04:00
|
|
|
USER_ACTIONS = ['generate']
|
|
|
|
|
2017-01-31 17:21:37 -05:00
|
|
|
attr_accessor :unhashed_auth_token
|
|
|
|
|
2018-08-31 04:18:06 -04:00
|
|
|
before_destroy do
|
|
|
|
UserAuthToken.log(action: 'destroy',
|
|
|
|
user_auth_token_id: self.id,
|
|
|
|
user_id: self.user_id,
|
|
|
|
user_agent: self.user_agent,
|
|
|
|
client_ip: self.client_ip,
|
|
|
|
auth_token: self.auth_token)
|
|
|
|
end
|
|
|
|
|
2017-03-07 10:57:48 -05:00
|
|
|
def self.log(info)
|
|
|
|
if SiteSetting.verbose_auth_token_logging
|
|
|
|
UserAuthTokenLog.create!(info)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-01-31 17:21:37 -05:00
|
|
|
def self.generate!(info)
|
|
|
|
token = SecureRandom.hex(16)
|
|
|
|
hashed_token = hash_token(token)
|
|
|
|
user_auth_token = UserAuthToken.create!(
|
|
|
|
user_id: info[:user_id],
|
|
|
|
user_agent: info[:user_agent],
|
|
|
|
client_ip: info[:client_ip],
|
|
|
|
auth_token: hashed_token,
|
|
|
|
prev_auth_token: hashed_token,
|
|
|
|
rotated_at: Time.zone.now
|
|
|
|
)
|
|
|
|
user_auth_token.unhashed_auth_token = token
|
2017-02-13 14:01:01 -05:00
|
|
|
|
2017-03-07 10:57:48 -05:00
|
|
|
log(action: 'generate',
|
2017-02-13 14:01:01 -05:00
|
|
|
user_auth_token_id: user_auth_token.id,
|
|
|
|
user_id: info[:user_id],
|
|
|
|
user_agent: info[:user_agent],
|
|
|
|
client_ip: info[:client_ip],
|
2017-03-07 13:27:34 -05:00
|
|
|
path: info[:path],
|
2017-03-07 10:57:48 -05:00
|
|
|
auth_token: hashed_token)
|
2017-02-13 14:01:01 -05:00
|
|
|
|
2017-01-31 17:21:37 -05:00
|
|
|
user_auth_token
|
|
|
|
end
|
|
|
|
|
2017-07-27 21:20:09 -04:00
|
|
|
def self.lookup(unhashed_token, opts = nil)
|
2017-01-31 17:21:37 -05:00
|
|
|
|
|
|
|
mark_seen = opts && opts[:seen]
|
|
|
|
|
|
|
|
token = hash_token(unhashed_token)
|
|
|
|
expire_before = SiteSetting.maximum_session_age.hours.ago
|
|
|
|
|
|
|
|
user_token = find_by("(auth_token = :token OR
|
2018-05-03 20:11:58 -04:00
|
|
|
prev_auth_token = :token) AND rotated_at > :expire_before",
|
|
|
|
token: token, expire_before: expire_before)
|
2017-01-31 17:21:37 -05:00
|
|
|
|
2017-02-26 17:09:57 -05:00
|
|
|
if !user_token
|
2017-02-13 14:01:01 -05:00
|
|
|
|
2017-03-07 10:57:48 -05:00
|
|
|
log(action: "miss token",
|
2017-02-13 14:01:01 -05:00
|
|
|
user_id: user_token&.user_id,
|
|
|
|
auth_token: token,
|
|
|
|
user_agent: opts && opts[:user_agent],
|
2017-03-07 13:27:34 -05:00
|
|
|
path: opts && opts[:path],
|
2017-03-07 10:57:48 -05:00
|
|
|
client_ip: opts && opts[:client_ip])
|
2017-02-13 14:01:01 -05:00
|
|
|
|
2017-01-31 17:21:37 -05:00
|
|
|
return nil
|
|
|
|
end
|
2017-02-26 17:09:57 -05:00
|
|
|
|
2017-02-28 10:38:22 -05:00
|
|
|
if user_token.auth_token != token && user_token.prev_auth_token == token && user_token.auth_token_seen
|
2017-02-26 17:09:57 -05:00
|
|
|
changed_rows = UserAuthToken
|
2017-03-07 13:27:34 -05:00
|
|
|
.where("rotated_at < ?", 1.minute.ago)
|
2017-02-26 17:09:57 -05:00
|
|
|
.where(id: user_token.id, prev_auth_token: token)
|
|
|
|
.update_all(auth_token_seen: false)
|
|
|
|
|
|
|
|
# not updating AR model cause we want to give it one more req
|
|
|
|
# with wrong cookie
|
2017-03-07 10:57:48 -05:00
|
|
|
UserAuthToken.log(
|
2017-02-26 17:09:57 -05:00
|
|
|
action: changed_rows == 0 ? "prev seen token unchanged" : "prev seen token",
|
|
|
|
user_auth_token_id: user_token.id,
|
|
|
|
user_id: user_token.user_id,
|
|
|
|
auth_token: user_token.auth_token,
|
|
|
|
user_agent: opts && opts[:user_agent],
|
2017-03-07 13:27:34 -05:00
|
|
|
path: opts && opts[:path],
|
2017-02-26 17:09:57 -05:00
|
|
|
client_ip: opts && opts[:client_ip]
|
|
|
|
)
|
|
|
|
end
|
2017-01-31 17:21:37 -05:00
|
|
|
|
|
|
|
if mark_seen && user_token && !user_token.auth_token_seen && user_token.auth_token == token
|
2017-02-14 09:34:09 -05:00
|
|
|
# we must protect against concurrency issues here
|
2017-02-15 10:58:18 -05:00
|
|
|
changed_rows = UserAuthToken
|
|
|
|
.where(id: user_token.id, auth_token: token)
|
|
|
|
.update_all(auth_token_seen: true, seen_at: Time.zone.now)
|
|
|
|
|
2017-02-14 09:34:39 -05:00
|
|
|
if changed_rows == 1
|
2017-02-14 09:34:09 -05:00
|
|
|
# not doing a reload so we don't risk loading a rotated token
|
|
|
|
user_token.auth_token_seen = true
|
2017-02-15 10:58:18 -05:00
|
|
|
user_token.seen_at = Time.zone.now
|
2017-02-14 09:34:09 -05:00
|
|
|
end
|
2017-02-13 14:01:01 -05:00
|
|
|
|
2017-03-07 10:57:48 -05:00
|
|
|
log(action: changed_rows == 0 ? "seen wrong token" : "seen token",
|
2017-02-13 14:01:01 -05:00
|
|
|
user_auth_token_id: user_token.id,
|
|
|
|
user_id: user_token.user_id,
|
|
|
|
auth_token: user_token.auth_token,
|
|
|
|
user_agent: opts && opts[:user_agent],
|
2017-03-07 13:27:34 -05:00
|
|
|
path: opts && opts[:path],
|
2017-03-07 10:57:48 -05:00
|
|
|
client_ip: opts && opts[:client_ip])
|
2017-01-31 17:21:37 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
user_token
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.hash_token(token)
|
|
|
|
Digest::SHA1.base64digest("#{token}#{GlobalSetting.safe_secret_key_base}")
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.cleanup!
|
2017-02-13 14:01:01 -05:00
|
|
|
|
|
|
|
if SiteSetting.verbose_auth_token_logging
|
|
|
|
UserAuthTokenLog.where('created_at < :time',
|
|
|
|
time: SiteSetting.maximum_session_age.hours.ago - ROTATE_TIME).delete_all
|
|
|
|
end
|
|
|
|
|
2017-01-31 17:21:37 -05:00
|
|
|
where('rotated_at < :time',
|
|
|
|
time: SiteSetting.maximum_session_age.hours.ago - ROTATE_TIME).delete_all
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2017-07-27 21:20:09 -04:00
|
|
|
def rotate!(info = nil)
|
2017-01-31 17:21:37 -05:00
|
|
|
user_agent = (info && info[:user_agent] || self.user_agent)
|
|
|
|
client_ip = (info && info[:client_ip] || self.client_ip)
|
|
|
|
|
|
|
|
token = SecureRandom.hex(16)
|
|
|
|
|
2018-06-19 02:13:14 -04:00
|
|
|
result = DB.exec("
|
2017-01-31 17:21:37 -05:00
|
|
|
UPDATE user_auth_tokens
|
|
|
|
SET
|
|
|
|
auth_token_seen = false,
|
2017-02-15 10:58:18 -05:00
|
|
|
seen_at = null,
|
2017-01-31 17:21:37 -05:00
|
|
|
user_agent = :user_agent,
|
|
|
|
client_ip = :client_ip,
|
|
|
|
prev_auth_token = case when auth_token_seen then auth_token else prev_auth_token end,
|
|
|
|
auth_token = :new_token,
|
|
|
|
rotated_at = :now
|
|
|
|
WHERE id = :id AND (auth_token_seen or rotated_at < :safeguard_time)
|
|
|
|
", id: self.id,
|
|
|
|
user_agent: user_agent,
|
|
|
|
client_ip: client_ip&.to_s,
|
|
|
|
now: Time.zone.now,
|
|
|
|
new_token: UserAuthToken.hash_token(token),
|
|
|
|
safeguard_time: 30.seconds.ago
|
|
|
|
)
|
|
|
|
|
2018-06-19 02:13:14 -04:00
|
|
|
if result > 0
|
2017-01-31 17:21:37 -05:00
|
|
|
reload
|
|
|
|
self.unhashed_auth_token = token
|
2017-02-13 14:01:01 -05:00
|
|
|
|
2017-03-07 10:57:48 -05:00
|
|
|
UserAuthToken.log(
|
|
|
|
action: "rotate",
|
|
|
|
user_auth_token_id: id,
|
|
|
|
user_id: user_id,
|
|
|
|
auth_token: auth_token,
|
|
|
|
user_agent: user_agent,
|
2017-03-07 13:27:34 -05:00
|
|
|
client_ip: client_ip,
|
|
|
|
path: info && info[:path]
|
2017-03-07 10:57:48 -05:00
|
|
|
)
|
2017-02-13 14:01:01 -05:00
|
|
|
|
2017-01-31 17:21:37 -05:00
|
|
|
true
|
|
|
|
else
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: user_auth_tokens
|
|
|
|
#
|
|
|
|
# id :integer not null, primary key
|
|
|
|
# user_id :integer not null
|
|
|
|
# auth_token :string not null
|
2017-03-22 02:26:53 -04:00
|
|
|
# prev_auth_token :string not null
|
2017-01-31 17:21:37 -05:00
|
|
|
# user_agent :string
|
|
|
|
# auth_token_seen :boolean default(FALSE), not null
|
|
|
|
# client_ip :inet
|
2017-03-22 02:26:53 -04:00
|
|
|
# rotated_at :datetime not null
|
2018-02-20 01:28:58 -05:00
|
|
|
# created_at :datetime not null
|
|
|
|
# updated_at :datetime not null
|
2017-03-22 02:26:53 -04:00
|
|
|
# seen_at :datetime
|
|
|
|
#
|
|
|
|
# Indexes
|
|
|
|
#
|
|
|
|
# index_user_auth_tokens_on_auth_token (auth_token) UNIQUE
|
|
|
|
# index_user_auth_tokens_on_prev_auth_token (prev_auth_token) UNIQUE
|
2017-01-31 17:21:37 -05:00
|
|
|
#
|