Esempio n. 1
0
    def test_decode_caveat_v1_from_go(self):
        tp_key = macaroonbakery.PrivateKey(
            nacl.public.PrivateKey(
                base64.b64decode(
                    'TSpvLpQkRj+T3JXnsW2n43n5zP/0X4zn0RvDiWC3IJ0=')))
        fp_key = macaroonbakery.PrivateKey(
            nacl.public.PrivateKey(
                base64.b64decode(
                    'KXpsoJ9ujZYi/O2Cca6kaWh65MSawzy79LWkrjOfzcs=')))
        root_key = base64.b64decode('vDxEmWZEkgiNEFlJ+8ruXe3qDSLf1H+o')
        # This caveat has been generated from the go code
        # to check the compatibilty

        encrypted_cav = six.b(
            'eyJUaGlyZFBhcnR5UHVibGljS2V5IjoiOFA3R1ZZc3BlWlN4c'
            '3hFdmJsSVFFSTFqdTBTSWl0WlIrRFdhWE40cmxocz0iLCJGaX'
            'JzdFBhcnR5UHVibGljS2V5IjoiSDlqSFJqSUxidXppa1VKd2o'
            '5VGtDWk9qeW5oVmtTdHVsaUFRT2d6Y0NoZz0iLCJOb25jZSI6'
            'Ii9lWTRTTWR6TGFxbDlsRFc3bHUyZTZuSzJnVG9veVl0IiwiS'
            'WQiOiJra0ZuOGJEaEt4RUxtUjd0NkJxTU0vdHhMMFVqaEZjR1'
            'BORldUUExGdjVla1dWUjA4Uk1sbGJhc3c4VGdFbkhzM0laeVo'
            '0V2lEOHhRUWdjU3ljOHY4eUt4dEhxejVEczJOYmh1ZDJhUFdt'
            'UTVMcVlNWitmZ2FNaTAxdE9DIn0=')
        cav = macaroonbakery.decode_caveat(tp_key, encrypted_cav)
        self.assertEquals(
            cav,
            macaroonbakery.ThirdPartyCaveatInfo(
                condition='caveat condition',
                first_party_public_key=fp_key.public_key,
                third_party_key_pair=tp_key,
                root_key=root_key,
                caveat=encrypted_cav,
                version=macaroonbakery.BAKERY_V1,
                namespace=macaroonbakery.legacy_namespace()))
Esempio n. 2
0
def _decode_caveat_v2_v3(version, key, caveat):
    '''Decodes a version 2 or version 3 caveat.
    '''
    if (len(caveat) < 1 + _PUBLIC_KEY_PREFIX_LEN + _KEY_LEN +
            nacl.public.Box.NONCE_SIZE + 16):
        raise macaroonbakery.VerificationError('caveat id too short')
    original_caveat = caveat
    caveat = caveat[1:]  # skip version (already checked)

    pk_prefix = caveat[:_PUBLIC_KEY_PREFIX_LEN]
    caveat = caveat[_PUBLIC_KEY_PREFIX_LEN:]
    if key.public_key.encode(raw=True)[:_PUBLIC_KEY_PREFIX_LEN] != pk_prefix:
        raise macaroonbakery.VerificationError('public key mismatch')

    first_party_pub = caveat[:_KEY_LEN]
    caveat = caveat[_KEY_LEN:]
    nonce = caveat[:nacl.public.Box.NONCE_SIZE]
    caveat = caveat[nacl.public.Box.NONCE_SIZE:]
    fp_public_key = nacl.public.PublicKey(first_party_pub)
    box = nacl.public.Box(key.key, fp_public_key)
    data = box.decrypt(caveat, nonce)
    root_key, condition, ns = _decode_secret_part_v2_v3(version, data)
    return macaroonbakery.ThirdPartyCaveatInfo(
        condition=condition.decode('utf-8'),
        first_party_public_key=macaroonbakery.PublicKey(fp_public_key),
        third_party_key_pair=key,
        root_key=root_key,
        caveat=original_caveat,
        version=version,
        namespace=ns)
Esempio n. 3
0
 def test_decode_caveat_v3_from_go(self):
     tp_key = macaroonbakery.PrivateKey(
         nacl.public.PrivateKey(
             base64.b64decode(
                 'TSpvLpQkRj+T3JXnsW2n43n5zP/0X4zn0RvDiWC3IJ0=')))
     fp_key = macaroonbakery.PrivateKey(
         nacl.public.PrivateKey(
             base64.b64decode(
                 'KXpsoJ9ujZYi/O2Cca6kaWh65MSawzy79LWkrjOfzcs=')))
     root_key = base64.b64decode(b'oqOXI3/Mz/pKjCuFOt2eYxb7ndLq66GY')
     # This caveat has been generated from the go code
     # to check the compatibilty
     encrypted_cav = base64.urlsafe_b64decode(
         utils.add_base64_padding(
             six.
             b('A_D-xlUf2MdGMgtu7OKRQnCP1OQJk6PKeFWRK26WIBA6DNwKGNLeFSkD2M-8A'
               'EYvmgVH95GWu7T7caKxKhhOQFcEKgnXKJvYXxz1zin4cZc4Q6C7gVqA-J4_j3'
               '1LX4VKxymqG62UGPo78wOv0_fKjr3OI6PPJOYOQgBMclemlRF2')))
     cav = macaroonbakery.decode_caveat(tp_key, encrypted_cav)
     self.assertEquals(
         cav,
         macaroonbakery.ThirdPartyCaveatInfo(
             condition='third party condition',
             first_party_public_key=fp_key.public_key,
             third_party_key_pair=tp_key,
             root_key=root_key,
             caveat=encrypted_cav,
             version=macaroonbakery.BAKERY_V3,
             namespace=macaroonbakery.legacy_namespace()))
Esempio n. 4
0
 def test_decode_caveat_v2_from_go(self):
     tp_key = macaroonbakery.PrivateKey(
         nacl.public.PrivateKey(
             base64.b64decode(
                 'TSpvLpQkRj+T3JXnsW2n43n5zP/0X4zn0RvDiWC3IJ0=')))
     fp_key = macaroonbakery.PrivateKey(
         nacl.public.PrivateKey(
             base64.b64decode(
                 'KXpsoJ9ujZYi/O2Cca6kaWh65MSawzy79LWkrjOfzcs=')))
     root_key = base64.b64decode('wh0HSM65wWHOIxoGjgJJOFvQKn2jJFhC')
     # This caveat has been generated from the go code
     # to check the compatibilty
     encrypted_cav = base64.urlsafe_b64decode(
         utils.add_base64_padding(
             six.
             b('AvD-xlUf2MdGMgtu7OKRQnCP1OQJk6PKeFWRK26WIBA6DNwKGIHq9xGcHS9IZ'
               'Lh0cL6D9qpeKI0mXmCPfnwRQDuVYC8y5gVWd-oCGZaj5TGtk3byp2Vnw6ojmt'
               'sULDhY59YA_J_Y0ATkERO5T9ajoRWBxU2OXBoX6bImXA')))
     cav = macaroonbakery.decode_caveat(tp_key, encrypted_cav)
     self.assertEqual(
         cav,
         macaroonbakery.ThirdPartyCaveatInfo(
             condition='third party condition',
             first_party_public_key=fp_key.public_key,
             third_party_key_pair=tp_key,
             root_key=root_key,
             caveat=encrypted_cav,
             version=macaroonbakery.BAKERY_V2,
             namespace=macaroonbakery.legacy_namespace()))
Esempio n. 5
0
 def test_v2_round_trip(self):
     tp_info = bakery.ThirdPartyInfo(version=bakery.VERSION_2,
                                     public_key=self.tp_key.public_key)
     cid = bakery.encode_caveat('is-authenticated-user', b'a random string',
                                tp_info, self.fp_key, None)
     res = bakery.decode_caveat(self.tp_key, cid)
     self.assertEquals(
         res,
         bakery.ThirdPartyCaveatInfo(
             first_party_public_key=self.fp_key.public_key,
             root_key=b'a random string',
             condition='is-authenticated-user',
             caveat=cid,
             third_party_key_pair=self.tp_key,
             version=bakery.VERSION_2,
             id=None,
             namespace=bakery.legacy_namespace()))
Esempio n. 6
0
 def test_v3_round_trip(self):
     tp_info = bakery.ThirdPartyInfo(version=bakery.VERSION_3,
                                     public_key=self.tp_key.public_key)
     ns = checkers.Namespace()
     ns.register('testns', 'x')
     cid = bakery.encode_caveat('is-authenticated-user', b'a random string',
                                tp_info, self.fp_key, ns)
     res = bakery.decode_caveat(self.tp_key, cid)
     self.assertEquals(
         res,
         bakery.ThirdPartyCaveatInfo(
             first_party_public_key=self.fp_key.public_key,
             root_key=b'a random string',
             condition='is-authenticated-user',
             caveat=cid,
             third_party_key_pair=self.tp_key,
             version=bakery.VERSION_3,
             id=None,
             namespace=ns))
Esempio n. 7
0
def _decode_caveat_v1(key, caveat):
    '''Decode a base64 encoded JSON id.

    @param key the nacl private key to decode.
    @param caveat a base64 encoded JSON string.
    '''

    data = base64.b64decode(caveat).decode('utf-8')
    wrapper = json.loads(data)
    tp_public_key = nacl.public.PublicKey(
        base64.b64decode(wrapper['ThirdPartyPublicKey']))
    if key.public_key.key != tp_public_key:
        raise Exception('public key mismatch')  # TODO

    if wrapper.get('FirstPartyPublicKey', None) is None:
        raise Exception('target service public key not specified')

    # The encrypted string is base64 encoded in the JSON representation.
    secret = base64.b64decode(wrapper.get('Id'))
    nonce = base64.b64decode(wrapper.get('Nonce'))

    fp_public_key = nacl.public.PublicKey(
        base64.b64decode(wrapper.get('FirstPartyPublicKey')))

    box = nacl.public.Box(key.key, fp_public_key)
    c = box.decrypt(secret, nonce)
    record = json.loads(c.decode('utf-8'))
    fp_key = nacl.public.PublicKey(
        base64.b64decode(wrapper.get('FirstPartyPublicKey')))
    return bakery.ThirdPartyCaveatInfo(
        condition=record.get('Condition'),
        first_party_public_key=bakery.PublicKey(fp_key),
        third_party_key_pair=key,
        root_key=base64.b64decode(record.get('RootKey')),
        caveat=caveat,
        id=None,
        version=bakery.VERSION_1,
        namespace=bakery.legacy_namespace())
Esempio n. 8
0
def discharge(ctx, id, caveat, key, checker, locator):
    ''' Creates a macaroon to discharge a third party caveat.

    The given parameters specify the caveat and how it should be checked.
    The condition implicit in the caveat is checked for validity using checker.
    If it is valid, a new macaroon is returned which discharges the caveat.
    The macaroon is created with a version derived from the version that was
    used to encode the id.

    :param id: (bytes) holds the id to give to the discharge macaroon.
    If Caveat is empty, then the id also holds the encrypted third party
    caveat.
    :param caveat: (bytes) holds the encrypted third party caveat.
    If this is None, id will be used.
    :param key: holds the key to use to decrypt the third party caveat
    information and to encrypt any additional third party caveats returned by
    the caveat checker.
    :param checker: used to check the third party caveat, and may also return
    further caveats to be added to the discharge macaroon.
    :param locator: used to information on third parties referred to by third
    party caveats returned by the Checker.
    '''
    caveat_id_prefix = []
    if caveat is None:
        # The caveat information is encoded in the id itself.
        caveat = id
    else:
        # We've been given an explicit id, so when extra third party
        # caveats are added, use that id as the prefix
        # for any more ids.
        caveat_id_prefix = id
    cav_info = bakery.decode_caveat(key, caveat)
    cav_info = bakery.ThirdPartyCaveatInfo(
        condition=cav_info.condition,
        first_party_public_key=cav_info.first_party_public_key,
        third_party_key_pair=cav_info.third_party_key_pair,
        root_key=cav_info.root_key,
        caveat=cav_info.caveat,
        version=cav_info.version,
        id=id,
        namespace=cav_info.namespace
    )
    # Note that we don't check the error - we allow the
    # third party checker to see even caveats that we can't
    # understand.
    try:
        cond, arg = checkers.parse_caveat(cav_info.condition)
    except ValueError as exc:
        raise bakery.VerificationError(exc.args[0])

    if cond == checkers.COND_NEED_DECLARED:
        cav_info = cav_info._replace(condition=arg.encode('utf-8'))
        caveats = _check_need_declared(ctx, cav_info, checker)
    else:
        caveats = checker.check_third_party_caveat(ctx, cav_info)

    # Note that the discharge macaroon does not need to
    # be stored persistently. Indeed, it would be a problem if
    # we did, because then the macaroon could potentially be used
    # for normal authorization with the third party.
    m = bakery.Macaroon(
        cav_info.root_key,
        id,
        '',
        cav_info.version,
        cav_info.namespace,
    )
    m._caveat_id_prefix = caveat_id_prefix
    if caveats is not None:
        for cav in caveats:
            m.add_caveat(cav, key, locator)
    return m