Sha256: 7f343a67e4638b209b2cc444104ca8837684cff79ca5d73cc5ae01ed22b64e4b

Contents?: true

Size: 941 Bytes

Versions: 2

Compression:

Stored size: 941 Bytes

Contents

module WP
  module HMAC
    # = Key Cabinet
    #
    # Stores the secret keys used in the hash function.
    class KeyCabinet
      class KeyNotFound < Exception; end

      class << self
        attr_writer :lookup_block, :keys

        def add_key(id:, auth_key:)
          keys[id] = { id: id, auth_key: auth_key }
        end

        def keys
          @keys ||= {}
        end

        # This method will be called by EY::ApiHMAC. It must return
        # an object that responds to +id+ and +auth_key+
        def find_by_auth_id(id)
          hash = lookup(id) || keys[id]
          msg = 'Ensure secret keys are loaded with `HMAC::KeyCabinet.add_key`'
          fail KeyNotFound, msg if hash.nil?
          OpenStruct.new(hash)
        end

        def lookup(id)
          return unless @lookup_block
          key = @lookup_block.call(id)
          return { id: id, auth_key: key } if key
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
wp-hmac-0.2.5 lib/wp/hmac/key_cabinet.rb
wp-hmac-0.2.4 lib/wp/hmac/key_cabinet.rb