Sha256: ad25bd6d99363854d08357f9edf07a6f69d0d3472dbb1725920783989ea0ec10

Contents?: true

Size: 813 Bytes

Versions: 2

Compression:

Stored size: 813 Bytes

Contents

require 'redis'

module XSignature
  class RedisNonceValidator < NonceValidator

    attr_accessor :redis_connection, :keys_prefix

    def initialize(redis_connection: nil, keys_prefix: 'XSignature:LastNonce:')
      @redis_connection = redis_connection || Redis.current
      @keys_prefix      = keys_prefix
    end

    def valid?(data)
      key = "#{keys_prefix}#{data.client}"
      loop do
        redis_connection.watch key do
          last_nonce = redis_connection.get(key).to_i
          if last_nonce < data.nonce
            result = redis_connection.multi do |multi|
              multi.set key, data.nonce
            end
            return true if result[0] == 'OK'
          else
            redis_connection.unwatch
            return false
          end
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
x-signature-0.1.1 lib/x-signature/redis_nonce_validator.rb
x-signature-0.1.0 lib/x-signature/redis_nonce_validator.rb