Sha256: 63f994fa905b50d2e8bb2b9aeeb75f6e389996add45c0bf8e61f2ccee277d9a8

Contents?: true

Size: 1.57 KB

Versions: 4

Compression:

Stored size: 1.57 KB

Contents

module JWTKeeper
  module Datastore
    class << self
      # @!visibility private
      def rotate(jti, seconds)
        set_with_expiry(jti, seconds, :soft)
      end

      # @!visibility private
      def revoke(jti, seconds)
        set_with_expiry(jti, seconds, :hard)
      end

      # @!visibility private
      def pending?(jti)
        value = get(jti)
        value.present? && value.to_sym == :soft
      end

      # @!visibility private
      def revoked?(jti)
        value = get(jti)
        value.present? && value.to_sym == :hard
      end

      private

      # @!visibility private
      def set_with_expiry(jti, seconds, type)
        with_redis do |redis|
          if redis.respond_to?(:call) # For RedisClient
            redis.call('SETEX', jti, seconds, type)
          elsif redis.respond_to?(:setex) # For Redis
            redis.setex(jti, seconds, type)
          else
            throw 'Bad Redis Connection'
          end
        end
      end

      # @!visibility private
      def get(jti)
        with_redis do |redis|
          if redis.respond_to?(:call) # For RedisClient
            redis.call('GET', jti)
          elsif redis.respond_to?(:get) # For Redis
            redis.get(jti)
          else
            throw 'Bad Redis Connection'
          end
        end
      end

      # @!visibility private
      def with_redis
        redis = JWTKeeper.configuration.redis_connection

        if redis.respond_to?(:with)
          redis.with do |conn|
            yield conn
          end
        else
          yield(redis)
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
jwt_keeper-6.1.3 lib/jwt_keeper/datastore.rb
jwt_keeper-6.1.2 lib/jwt_keeper/datastore.rb
jwt_keeper-6.1.1 lib/jwt_keeper/datastore.rb
jwt_keeper-6.1.0 lib/jwt_keeper/datastore.rb