Class: Macaroons::JsonSerializer

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

Instance Method Summary collapse

Instance Method Details

#deserialize(serialized) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/macaroons/serializers/json.rb', line 27

def deserialize(serialized)
  deserialized = MultiJson.load(serialized)
  macaroon = Macaroons::RawMacaroon.new(key: 'no_key', identifier: deserialized['identifier'], location: deserialized['location'])
  deserialized['caveats'].each do |c|
    if c['vid']
      caveat = Macaroons::Caveat.new(c['cid'], Base64.strict_decode64(c['vid']), c['cl'])
    else
      caveat = Macaroons::Caveat.new(c['cid'], c['vid'], c['cl'])
    end
    macaroon.caveats << caveat
  end
  macaroon.signature = Utils.unhexlify(deserialized['signature'])
  macaroon
end

#serialize(macaroon) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/macaroons/serializers/json.rb', line 6

def serialize(macaroon)
  caveats = macaroon.caveats.map! do |c|
    if c.first_party?
      c
    else
      Macaroons::Caveat.new(
        c.caveat_id,
        verification_id=Base64.strict_encode64(c.verification_id),
        caveat_location=c.caveat_location
      )
    end
  end
  serialized = {
    location: macaroon.location,
    identifier: macaroon.identifier,
    caveats: caveats.map(&:to_h),
    signature: macaroon.signature
  }
  MultiJson.dump(serialized)
end