Class: Macaroons::RawMacaroon

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key: nil, identifier: nil, location: nil) ⇒ RawMacaroon

Returns a new instance of RawMacaroon.



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/macaroons/raw_macaroon.rb', line 13

def initialize(key: nil, identifier: nil, location: nil)
  if key.nil? || identifier.nil? || location.nil?
    raise ArgumentError, 'Must provide all three: (key, id, location)'
  end

  @key = key
  @identifier = identifier
  @location = location
  @signature = create_initial_macaroon_signature(key, identifier)
  @caveats = []
end

Instance Attribute Details

#caveatsObject

Returns the value of attribute caveats.



36
37
38
# File 'lib/macaroons/raw_macaroon.rb', line 36

def caveats
  @caveats
end

#identifierObject (readonly)

Returns the value of attribute identifier.



33
34
35
# File 'lib/macaroons/raw_macaroon.rb', line 33

def identifier
  @identifier
end

#keyObject (readonly)

Returns the value of attribute key.



34
35
36
# File 'lib/macaroons/raw_macaroon.rb', line 34

def key
  @key
end

#locationObject (readonly)

Returns the value of attribute location.



35
36
37
# File 'lib/macaroons/raw_macaroon.rb', line 35

def location
  @location
end

#signatureObject

Returns the value of attribute signature.



37
38
39
# File 'lib/macaroons/raw_macaroon.rb', line 37

def signature
  @signature
end

Class Method Details

.from_binary(serialized: nil) ⇒ Object



25
26
27
# File 'lib/macaroons/raw_macaroon.rb', line 25

def self.from_binary(serialized: nil)
  Macaroons::BinarySerializer.new().deserialize(serialized)
end

.from_json(serialized: nil) ⇒ Object



29
30
31
# File 'lib/macaroons/raw_macaroon.rb', line 29

def self.from_json(serialized: nil)
  Macaroons::JsonSerializer.new().deserialize(serialized)
end

Instance Method Details

#add_first_party_caveat(predicate) ⇒ Object



43
44
45
46
47
# File 'lib/macaroons/raw_macaroon.rb', line 43

def add_first_party_caveat(predicate)
  caveat = Caveat.new(predicate)
  @caveats << caveat
  @signature = Utils.sign_first_party_caveat(@signature, predicate)
end

#add_third_party_caveat(caveat_key, caveat_id, caveat_location) ⇒ Object



49
50
51
52
53
54
55
56
57
58
# File 'lib/macaroons/raw_macaroon.rb', line 49

def add_third_party_caveat(caveat_key, caveat_id, caveat_location)
  derived_caveat_key = Utils.truncate_or_pad(Utils.hmac('macaroons-key-generator', caveat_key))
  truncated_or_padded_signature = Utils.truncate_or_pad(@signature)
  box = RbNaCl::SimpleBox.from_secret_key(truncated_or_padded_signature)
  ciphertext = box.encrypt(derived_caveat_key)
  verification_id = ciphertext
  caveat = Caveat.new(caveat_id, verification_id, caveat_location)
  @caveats << caveat
  @signature = Utils.sign_third_party_caveat(@signature, verification_id, caveat_id)
end

#bind_signature(signature) ⇒ Object



75
76
77
78
79
80
# File 'lib/macaroons/raw_macaroon.rb', line 75

def bind_signature(signature)
  key = Utils.truncate_or_pad("\0")
  hash1 = Utils.hmac(key, Utils.unhexlify(self.signature))
  hash2 = Utils.hmac(key, Utils.unhexlify(signature))
  Utils.hmac(key, hash1 + hash2)
end

#prepare_for_request(macaroon) ⇒ Object



68
69
70
71
72
73
# File 'lib/macaroons/raw_macaroon.rb', line 68

def prepare_for_request(macaroon)
  bound_macaroon = Marshal.load( Marshal.dump( macaroon ) )
  raw = bound_macaroon.instance_variable_get(:@raw_macaroon)
  raw.signature = bind_signature(macaroon.signature)
  bound_macaroon
end

#serializeObject



60
61
62
# File 'lib/macaroons/raw_macaroon.rb', line 60

def serialize
  Macaroons::BinarySerializer.new().serialize(self)
end

#serialize_jsonObject



64
65
66
# File 'lib/macaroons/raw_macaroon.rb', line 64

def serialize_json
  Macaroons::JsonSerializer.new().serialize(self)
end