2018-11-18 11:00:15 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-10-15 14:42:47 +05:30
|
|
|
class ActiveSession
|
|
|
|
include ActiveModel::Model
|
|
|
|
|
2019-09-30 21:07:59 +05:30
|
|
|
SESSION_BATCH_SIZE = 200
|
2020-01-01 13:55:28 +05:30
|
|
|
ALLOWED_NUMBER_OF_ACTIVE_SESSIONS = 100
|
2019-09-30 21:07:59 +05:30
|
|
|
|
2020-03-09 13:42:32 +05:30
|
|
|
attr_writer :session_id
|
|
|
|
|
2018-10-15 14:42:47 +05:30
|
|
|
attr_accessor :created_at, :updated_at,
|
2020-03-09 13:42:32 +05:30
|
|
|
:ip_address, :browser, :os,
|
|
|
|
:device_name, :device_type,
|
2019-03-13 22:55:13 +05:30
|
|
|
:is_impersonated
|
2018-10-15 14:42:47 +05:30
|
|
|
|
|
|
|
def current?(session)
|
|
|
|
return false if session_id.nil? || session.id.nil?
|
|
|
|
|
|
|
|
session_id == session.id
|
|
|
|
end
|
|
|
|
|
|
|
|
def human_device_type
|
|
|
|
device_type&.titleize
|
|
|
|
end
|
|
|
|
|
2020-03-09 13:42:32 +05:30
|
|
|
def public_id
|
|
|
|
encrypted_id = Gitlab::CryptoHelper.aes256_gcm_encrypt(session_id)
|
|
|
|
CGI.escape(encrypted_id)
|
|
|
|
end
|
|
|
|
|
2018-10-15 14:42:47 +05:30
|
|
|
def self.set(user, request)
|
|
|
|
Gitlab::Redis::SharedState.with do |redis|
|
|
|
|
session_id = request.session.id
|
|
|
|
client = DeviceDetector.new(request.user_agent)
|
|
|
|
timestamp = Time.current
|
|
|
|
|
|
|
|
active_user_session = new(
|
|
|
|
ip_address: request.ip,
|
|
|
|
browser: client.name,
|
|
|
|
os: client.os_name,
|
|
|
|
device_name: client.device_name,
|
|
|
|
device_type: client.device_type,
|
|
|
|
created_at: user.current_sign_in_at || timestamp,
|
|
|
|
updated_at: timestamp,
|
2019-03-13 22:55:13 +05:30
|
|
|
session_id: session_id,
|
|
|
|
is_impersonated: request.session[:impersonator_id].present?
|
2018-10-15 14:42:47 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
redis.pipelined do
|
|
|
|
redis.setex(
|
|
|
|
key_name(user.id, session_id),
|
|
|
|
Settings.gitlab['session_expire_delay'] * 60,
|
|
|
|
Marshal.dump(active_user_session)
|
|
|
|
)
|
|
|
|
|
|
|
|
redis.sadd(
|
|
|
|
lookup_key_name(user.id),
|
|
|
|
session_id
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.list(user)
|
|
|
|
Gitlab::Redis::SharedState.with do |redis|
|
2019-09-04 21:01:54 +05:30
|
|
|
cleaned_up_lookup_entries(redis, user).map do |entry|
|
2018-10-15 14:42:47 +05:30
|
|
|
# rubocop:disable Security/MarshalLoad
|
|
|
|
Marshal.load(entry)
|
|
|
|
# rubocop:enable Security/MarshalLoad
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.destroy(user, session_id)
|
|
|
|
Gitlab::Redis::SharedState.with do |redis|
|
2020-01-01 13:55:28 +05:30
|
|
|
destroy_sessions(redis, user, [session_id])
|
|
|
|
end
|
|
|
|
end
|
2018-10-15 14:42:47 +05:30
|
|
|
|
2020-03-09 13:42:32 +05:30
|
|
|
def self.destroy_with_public_id(user, public_id)
|
|
|
|
session_id = decrypt_public_id(public_id)
|
|
|
|
destroy(user, session_id) unless session_id.nil?
|
|
|
|
end
|
|
|
|
|
2020-01-01 13:55:28 +05:30
|
|
|
def self.destroy_sessions(redis, user, session_ids)
|
|
|
|
key_names = session_ids.map {|session_id| key_name(user.id, session_id) }
|
|
|
|
session_names = session_ids.map {|session_id| "#{Gitlab::Redis::SharedState::SESSION_NAMESPACE}:#{session_id}" }
|
2018-10-15 14:42:47 +05:30
|
|
|
|
2020-01-01 13:55:28 +05:30
|
|
|
redis.srem(lookup_key_name(user.id), session_ids)
|
|
|
|
redis.del(key_names)
|
|
|
|
redis.del(session_names)
|
2018-10-15 14:42:47 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def self.cleanup(user)
|
|
|
|
Gitlab::Redis::SharedState.with do |redis|
|
2020-01-01 13:55:28 +05:30
|
|
|
clean_up_old_sessions(redis, user)
|
2019-09-04 21:01:54 +05:30
|
|
|
cleaned_up_lookup_entries(redis, user)
|
2018-10-15 14:42:47 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.key_name(user_id, session_id = '*')
|
|
|
|
"#{Gitlab::Redis::SharedState::USER_SESSIONS_NAMESPACE}:#{user_id}:#{session_id}"
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.lookup_key_name(user_id)
|
|
|
|
"#{Gitlab::Redis::SharedState::USER_SESSIONS_LOOKUP_NAMESPACE}:#{user_id}"
|
|
|
|
end
|
|
|
|
|
2019-09-04 21:01:54 +05:30
|
|
|
def self.list_sessions(user)
|
2019-10-12 21:52:04 +05:30
|
|
|
sessions_from_ids(session_ids_for_user(user.id))
|
2019-09-04 21:01:54 +05:30
|
|
|
end
|
2018-10-15 14:42:47 +05:30
|
|
|
|
2019-10-12 21:52:04 +05:30
|
|
|
def self.session_ids_for_user(user_id)
|
2019-09-04 21:01:54 +05:30
|
|
|
Gitlab::Redis::SharedState.with do |redis|
|
2019-10-12 21:52:04 +05:30
|
|
|
redis.smembers(lookup_key_name(user_id))
|
2019-09-04 21:01:54 +05:30
|
|
|
end
|
|
|
|
end
|
2018-10-15 14:42:47 +05:30
|
|
|
|
2019-09-04 21:01:54 +05:30
|
|
|
def self.sessions_from_ids(session_ids)
|
|
|
|
return [] if session_ids.empty?
|
2018-10-15 14:42:47 +05:30
|
|
|
|
2019-09-04 21:01:54 +05:30
|
|
|
Gitlab::Redis::SharedState.with do |redis|
|
|
|
|
session_keys = session_ids.map { |session_id| "#{Gitlab::Redis::SharedState::SESSION_NAMESPACE}:#{session_id}" }
|
2018-10-15 14:42:47 +05:30
|
|
|
|
2019-09-30 21:07:59 +05:30
|
|
|
session_keys.each_slice(SESSION_BATCH_SIZE).flat_map do |session_keys_batch|
|
|
|
|
redis.mget(session_keys_batch).compact.map do |raw_session|
|
|
|
|
# rubocop:disable Security/MarshalLoad
|
|
|
|
Marshal.load(raw_session)
|
|
|
|
# rubocop:enable Security/MarshalLoad
|
|
|
|
end
|
2019-09-04 21:01:54 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-01-01 13:55:28 +05:30
|
|
|
def self.raw_active_session_entries(redis, session_ids, user_id)
|
2019-09-04 21:01:54 +05:30
|
|
|
return [] if session_ids.empty?
|
|
|
|
|
2020-01-01 13:55:28 +05:30
|
|
|
entry_keys = session_ids.map { |session_id| key_name(user_id, session_id) }
|
|
|
|
|
|
|
|
redis.mget(entry_keys)
|
|
|
|
end
|
2019-09-04 21:01:54 +05:30
|
|
|
|
2020-01-01 13:55:28 +05:30
|
|
|
def self.active_session_entries(session_ids, user_id, redis)
|
|
|
|
return [] if session_ids.empty?
|
|
|
|
|
|
|
|
entry_keys = raw_active_session_entries(redis, session_ids, user_id)
|
|
|
|
|
|
|
|
entry_keys.compact.map do |raw_session|
|
|
|
|
Marshal.load(raw_session) # rubocop:disable Security/MarshalLoad
|
2019-09-04 21:01:54 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-01-01 13:55:28 +05:30
|
|
|
def self.clean_up_old_sessions(redis, user)
|
|
|
|
session_ids = session_ids_for_user(user.id)
|
|
|
|
|
|
|
|
return if session_ids.count <= ALLOWED_NUMBER_OF_ACTIVE_SESSIONS
|
|
|
|
|
|
|
|
# remove sessions if there are more than ALLOWED_NUMBER_OF_ACTIVE_SESSIONS.
|
|
|
|
sessions = active_session_entries(session_ids, user.id, redis)
|
|
|
|
sessions.sort_by! {|session| session.updated_at }.reverse!
|
2020-03-09 13:42:32 +05:30
|
|
|
destroyable_sessions = sessions.drop(ALLOWED_NUMBER_OF_ACTIVE_SESSIONS)
|
|
|
|
destroyable_session_ids = destroyable_sessions.map { |session| session.send :session_id } # rubocop:disable GitlabSecurity/PublicSend
|
|
|
|
destroy_sessions(redis, user, destroyable_session_ids) if destroyable_session_ids.any?
|
2020-01-01 13:55:28 +05:30
|
|
|
end
|
|
|
|
|
2019-09-04 21:01:54 +05:30
|
|
|
def self.cleaned_up_lookup_entries(redis, user)
|
2019-10-12 21:52:04 +05:30
|
|
|
session_ids = session_ids_for_user(user.id)
|
2020-01-01 13:55:28 +05:30
|
|
|
entries = raw_active_session_entries(redis, session_ids, user.id)
|
2018-10-15 14:42:47 +05:30
|
|
|
|
|
|
|
# remove expired keys.
|
|
|
|
# only the single key entries are automatically expired by redis, the
|
|
|
|
# lookup entries in the set need to be removed manually.
|
2019-09-04 21:01:54 +05:30
|
|
|
session_ids_and_entries = session_ids.zip(entries)
|
2019-10-12 21:52:04 +05:30
|
|
|
redis.pipelined do
|
|
|
|
session_ids_and_entries.reject { |_session_id, entry| entry }.each do |session_id, _entry|
|
|
|
|
redis.srem(lookup_key_name(user.id), session_id)
|
|
|
|
end
|
2018-10-15 14:42:47 +05:30
|
|
|
end
|
|
|
|
|
2019-09-04 21:01:54 +05:30
|
|
|
entries.compact
|
2018-10-15 14:42:47 +05:30
|
|
|
end
|
2020-03-09 13:42:32 +05:30
|
|
|
|
|
|
|
private_class_method def self.decrypt_public_id(public_id)
|
|
|
|
decoded_id = CGI.unescape(public_id)
|
|
|
|
Gitlab::CryptoHelper.aes256_gcm_decrypt(decoded_id)
|
|
|
|
rescue
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
attr_reader :session_id
|
2018-10-15 14:42:47 +05:30
|
|
|
end
|