2018-10-22 03:00:50 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-11-13 14:35:47 -05:00
|
|
|
require 'json'
|
|
|
|
|
|
|
|
module Gitlab
|
|
|
|
class ChatNameToken
|
|
|
|
attr_reader :token
|
|
|
|
|
|
|
|
TOKEN_LENGTH = 50
|
2016-11-16 17:10:27 -05:00
|
|
|
EXPIRY_TIME = 10.minutes
|
2016-11-13 14:35:47 -05:00
|
|
|
|
|
|
|
def initialize(token = new_token)
|
|
|
|
@token = token
|
|
|
|
end
|
|
|
|
|
|
|
|
def get
|
2017-07-10 23:35:47 -04:00
|
|
|
Gitlab::Redis::SharedState.with do |redis|
|
|
|
|
data = redis.get(redis_shared_state_key)
|
2022-08-12 23:09:28 -04:00
|
|
|
params = Gitlab::Json.parse(data, symbolize_names: true) if data
|
|
|
|
params[:integration_id] ||= params.delete(:service_id) if params && params[:service_id]
|
|
|
|
params
|
2016-11-13 14:35:47 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def store!(params)
|
2017-07-10 23:35:47 -04:00
|
|
|
Gitlab::Redis::SharedState.with do |redis|
|
2016-11-13 14:35:47 -05:00
|
|
|
params = params.to_json
|
2017-07-10 23:35:47 -04:00
|
|
|
redis.set(redis_shared_state_key, params, ex: EXPIRY_TIME)
|
2016-11-13 14:35:47 -05:00
|
|
|
token
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def delete
|
2017-07-10 23:35:47 -04:00
|
|
|
Gitlab::Redis::SharedState.with do |redis|
|
|
|
|
redis.del(redis_shared_state_key)
|
2016-11-13 14:35:47 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def new_token
|
|
|
|
Devise.friendly_token(TOKEN_LENGTH)
|
|
|
|
end
|
|
|
|
|
2017-07-10 23:35:47 -04:00
|
|
|
def redis_shared_state_key
|
2016-11-13 14:35:47 -05:00
|
|
|
"gitlab:chat_names:#{token}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|