summaryrefslogtreecommitdiff
path: root/macaroonbakery/json_serializer.py
blob: 2faea002fd74958e333f9d9dbe43e1cba404bc40 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# Copyright 2017 Canonical Ltd.
# Licensed under the LGPLv3, see LICENCE file for details.

import base64
import json

from pymacaroons.macaroon import Macaroon
from pymacaroons.caveat import Caveat


class JsonSerializer(object):
    '''Serializer used to produce JSON macaroon format v1.
    '''
    def serialize(self, macaroon):
        '''Serialize the macaroon in JSON format v1.

        @param macaroon the macaroon to serialize.
        @return JSON macaroon.
        '''
        serialized = {
            'identifier': macaroon.identifier,
            'signature': macaroon.signature
        }
        if macaroon.location:
            serialized['location'] = macaroon.location
        if macaroon.caveats:
            serialized['caveats'] = [
                caveat_v1_to_dict(caveat) for caveat in macaroon.caveats
            ]
        return json.dumps(serialized)

    def deserialize(self, serialized):
        '''Deserialize a JSON macaroon v1.

        @param serialized the macaroon in JSON format v1.
        @return the macaroon object.
        '''
        from macaroonbakery import utils
        caveats = []
        deserialized = json.loads(serialized)

        for c in deserialized['caveats']:
            caveat = Caveat(
                caveat_id=c['cid'],
                verification_key_id=(
                    utils.raw_urlsafe_b64decode(c['vid']) if c.get('vid')
                    else None
                ),
                location=(
                    c['cl'] if c.get('cl') else None
                )
            )
            caveats.append(caveat)

        return Macaroon(
            location=deserialized['location'],
            identifier=deserialized['identifier'],
            caveats=caveats,
            signature=deserialized['signature']
        )


def caveat_v1_to_dict(c):
    ''' Return a caveat as a dictionary for export as the JSON
    macaroon v1 format
    '''
    serialized = {}
    if len(c.caveat_id) > 0:
        serialized['cid'] = c.caveat_id
    if c.verification_key_id:
        serialized['vid'] = base64.urlsafe_b64encode(
            c.verification_key_id).decode('ascii')
    if c.location:
        serialized['cl'] = c.location
    return serialized