BigW Consortium Gitlab

chat_name_token.rb 876 Bytes
Newer Older
1 2 3 4 5 6 7
require 'json'

module Gitlab
  class ChatNameToken
    attr_reader :token

    TOKEN_LENGTH = 50
8
    EXPIRY_TIME = 10.minutes
9 10 11 12 13 14

    def initialize(token = new_token)
      @token = token
    end

    def get
15 16
      Gitlab::Redis::SharedState.with do |redis|
        data = redis.get(redis_shared_state_key)
17 18 19 20 21
        JSON.parse(data, symbolize_names: true) if data
      end
    end

    def store!(params)
22
      Gitlab::Redis::SharedState.with do |redis|
23
        params = params.to_json
24
        redis.set(redis_shared_state_key, params, ex: EXPIRY_TIME)
25 26 27 28 29
        token
      end
    end

    def delete
30 31
      Gitlab::Redis::SharedState.with do |redis|
        redis.del(redis_shared_state_key)
32 33 34 35 36 37 38 39 40
      end
    end

    private

    def new_token
      Devise.friendly_token(TOKEN_LENGTH)
    end

41
    def redis_shared_state_key
42 43 44 45
      "gitlab:chat_names:#{token}"
    end
  end
end