debian-mirror-gitlab/lib/gitlab/chat_name_token.rb

48 lines
915 B
Ruby
Raw Normal View History

2018-12-13 13:39:08 +05:30
# frozen_string_literal: true
2017-08-17 22:00:37 +05:30
require 'json'
module Gitlab
class ChatNameToken
attr_reader :token
TOKEN_LENGTH = 50
EXPIRY_TIME = 10.minutes
def initialize(token = new_token)
@token = token
end
def get
2017-09-10 17:25:29 +05:30
Gitlab::Redis::SharedState.with do |redis|
data = redis.get(redis_shared_state_key)
2020-05-24 23:13:21 +05:30
Gitlab::Json.parse(data, symbolize_names: true) if data
2017-08-17 22:00:37 +05:30
end
end
def store!(params)
2017-09-10 17:25:29 +05:30
Gitlab::Redis::SharedState.with do |redis|
2017-08-17 22:00:37 +05:30
params = params.to_json
2017-09-10 17:25:29 +05:30
redis.set(redis_shared_state_key, params, ex: EXPIRY_TIME)
2017-08-17 22:00:37 +05:30
token
end
end
def delete
2017-09-10 17:25:29 +05:30
Gitlab::Redis::SharedState.with do |redis|
redis.del(redis_shared_state_key)
2017-08-17 22:00:37 +05:30
end
end
private
def new_token
Devise.friendly_token(TOKEN_LENGTH)
end
2017-09-10 17:25:29 +05:30
def redis_shared_state_key
2017-08-17 22:00:37 +05:30
"gitlab:chat_names:#{token}"
end
end
end