Class: Macaroons::Verifier

Inherits:
Object
  • Object
show all
Defined in:
lib/macaroons/verifier.rb

Direct Known Subclasses

Macaroon::Verifier

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeVerifier

Returns a new instance of Verifier.



10
11
12
13
14
# File 'lib/macaroons/verifier.rb', line 10

def initialize
  @predicates = []
  @callbacks = []
  @calculated_signature = nil
end

Instance Attribute Details

#callbacksObject

Returns the value of attribute callbacks.



8
9
10
# File 'lib/macaroons/verifier.rb', line 8

def callbacks
  @callbacks
end

#predicatesObject

Returns the value of attribute predicates.



7
8
9
# File 'lib/macaroons/verifier.rb', line 7

def predicates
  @predicates
end

Instance Method Details

#satisfy_exact(predicate) ⇒ Object

Raises:

  • (ArgumentError)


16
17
18
19
# File 'lib/macaroons/verifier.rb', line 16

def satisfy_exact(predicate)
  raise ArgumentError, 'Must provide predicate' unless predicate
  @predicates << predicate
end

#satisfy_general(callback = nil, &block) ⇒ Object

Raises:

  • (ArgumentError)


21
22
23
24
25
# File 'lib/macaroons/verifier.rb', line 21

def satisfy_general(callback = nil, &block)
  raise ArgumentError, 'Must provide callback or block' unless callback || block_given?
  callback = block if block_given?
  @callbacks << callback
end

#verify(macaroon: nil, key: nil, discharge_macaroons: nil) ⇒ Object

Raises:

  • (ArgumentError)


27
28
29
30
31
# File 'lib/macaroons/verifier.rb', line 27

def verify(macaroon: nil, key: nil, discharge_macaroons: nil)
  raise ArgumentError, 'Macaroon and Key required' if macaroon.nil? || key.nil?
  key = Utils.generate_derived_key(key)
  verify_discharge(root: macaroon, macaroon: macaroon, key: key, discharge_macaroons:discharge_macaroons)
end

#verify_discharge(root: nil, macaroon: nil, key: nil, discharge_macaroons: []) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/macaroons/verifier.rb', line 33

def verify_discharge(root: nil, macaroon: nil, key: nil, discharge_macaroons: [])
  @calculated_signature = Utils.hmac(key, macaroon.identifier)

  verify_caveats(macaroon, discharge_macaroons)

  if root != macaroon
    raw = root.instance_variable_get(:@raw_macaroon)
    @calculated_signature = raw.bind_signature(Utils.hexlify(@calculated_signature).downcase)
  end

  raise SignatureMismatchError, 'Signatures do not match.' unless signatures_match(Utils.unhexlify(macaroon.signature), @calculated_signature)

  return true
end