Class: WP::HMAC::KeyCabinet

Inherits:
Object
  • Object
show all
Defined in:
lib/wp/hmac/key_cabinet.rb

Overview

Key Cabinet

Stores the secret keys used in the hash function.

Defined Under Namespace

Classes: KeyNotFound

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.keysObject



16
17
18
# File 'lib/wp/hmac/key_cabinet.rb', line 16

def keys
  @keys ||= {}
end

.lookup_block=(value) ⇒ Object (writeonly)

Sets the attribute lookup_block

Parameters:

  • value

    the value to set the attribute lookup_block to.



10
11
12
# File 'lib/wp/hmac/key_cabinet.rb', line 10

def lookup_block=(value)
  @lookup_block = value
end

Class Method Details

.add_key(id:, auth_key:) ⇒ Object



12
13
14
# File 'lib/wp/hmac/key_cabinet.rb', line 12

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

.find_by_auth_id(id) ⇒ Object

This method will be called by EY::ApiHMAC. It must return an object that responds to id and auth_key



22
23
24
25
26
27
# File 'lib/wp/hmac/key_cabinet.rb', line 22

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

.lookup(id) ⇒ Object



29
30
31
32
33
# File 'lib/wp/hmac/key_cabinet.rb', line 29

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