예제 #1
0
    def test_extract_macaroons_from_request(self):
        def encode_macaroon(m):
            macaroons = '[' + utils.macaroon_to_json_string(m) + ']'
            return base64.urlsafe_b64encode(
                utils.to_bytes(macaroons)).decode('ascii')

        req = Request('http://example.com')
        m1 = pymacaroons.Macaroon(version=pymacaroons.MACAROON_V2,
                                  identifier='one')
        req.add_header('Macaroons', encode_macaroon(m1))
        m2 = pymacaroons.Macaroon(version=pymacaroons.MACAROON_V2,
                                  identifier='two')
        jar = requests.cookies.RequestsCookieJar()
        jar.set_cookie(
            utils.cookie(
                name='macaroon-auth',
                value=encode_macaroon(m2),
                url='http://example.com',
            ))
        jar.set_cookie(
            utils.cookie(
                name='macaroon-empty',
                value='',
                url='http://example.com',
            ))
        jar.add_cookie_header(req)

        macaroons = httpbakery.extract_macaroons(req)
        self.assertEquals(len(macaroons), 2)
        macaroons.sort(key=lambda ms: ms[0].identifier)
        self.assertEquals(macaroons[0][0].identifier, m1.identifier)
        self.assertEquals(macaroons[1][0].identifier, m2.identifier)
    def test_extract_macaroons_from_request(self):
        def encode_macaroon(m):
            macaroons = '[' + utils.macaroon_to_json_string(m) + ']'
            return base64.urlsafe_b64encode(utils.to_bytes(macaroons)).decode('ascii')

        req = Request('http://example.com')
        m1 = pymacaroons.Macaroon(version=pymacaroons.MACAROON_V2, identifier='one')
        req.add_header('Macaroons', encode_macaroon(m1))
        m2 = pymacaroons.Macaroon(version=pymacaroons.MACAROON_V2, identifier='two')
        jar = requests.cookies.RequestsCookieJar()
        jar.set_cookie(utils.cookie(
            name='macaroon-auth',
            value=encode_macaroon(m2),
            url='http://example.com',
        ))
        jar.set_cookie(utils.cookie(
            name='macaroon-empty',
            value='',
            url='http://example.com',
        ))
        jar.add_cookie_header(req)

        macaroons = httpbakery.extract_macaroons(req)
        self.assertEquals(len(macaroons), 2)
        macaroons.sort(key=lambda ms: ms[0].identifier)
        self.assertEquals(macaroons[0][0].identifier, m1.identifier)
        self.assertEquals(macaroons[1][0].identifier, m2.identifier)
예제 #3
0
    def handle_error(self, error, url):
        '''Try to resolve the given error, which should be a response
        to the given URL, by discharging any macaroon contained in
        it. That is, if error.code is ERR_DISCHARGE_REQUIRED
        then it will try to discharge err.info.macaroon. If the discharge
        succeeds, the discharged macaroon will be saved to the client's cookie
        jar, otherwise an exception will be raised.
        '''
        if error.info is None or error.info.macaroon is None:
            raise BakeryException('unable to read info in discharge error '
                                  'response')

        discharges = bakery.discharge_all(
            error.info.macaroon,
            self.acquire_discharge,
            self.key,
        )
        macaroons = '[' + ','.join(
            map(utils.macaroon_to_json_string, discharges)) + ']'
        all_macaroons = base64.urlsafe_b64encode(utils.to_bytes(macaroons))

        full_path = relative_url(url, error.info.macaroon_path)
        if error.info.cookie_name_suffix is not None:
            name = 'macaroon-' + error.info.cookie_name_suffix
        else:
            name = 'macaroon-auth'
        expires = checkers.macaroons_expiry_time(checkers.Namespace(),
                                                 discharges)
        self.cookies.set_cookie(
            utils.cookie(
                name=name,
                value=all_macaroons.decode('ascii'),
                url=full_path,
                expires=expires,
            ))
    def handle_error(self, error, url):
        '''Try to resolve the given error, which should be a response
        to the given URL, by discharging any macaroon contained in
        it. That is, if error.code is ERR_DISCHARGE_REQUIRED
        then it will try to discharge err.info.macaroon. If the discharge
        succeeds, the discharged macaroon will be saved to the client's cookie
        jar, otherwise an exception will be raised.
        '''
        if error.info is None or error.info.macaroon is None:
            raise BakeryException('unable to read info in discharge error '
                                  'response')

        discharges = bakery.discharge_all(
            error.info.macaroon,
            self.acquire_discharge,
            self.key,
        )
        macaroons = '[' + ','.join(map(utils.macaroon_to_json_string,
                                       discharges)) + ']'
        all_macaroons = base64.urlsafe_b64encode(utils.to_bytes(macaroons))

        full_path = urljoin(url, error.info.macaroon_path)
        if error.info.cookie_name_suffix is not None:
            name = 'macaroon-' + error.info.cookie_name_suffix
        else:
            name = 'macaroon-auth'
        expires = checkers.macaroons_expiry_time(checkers.Namespace(), discharges)
        self.cookies.set_cookie(utils.cookie(
            name=name,
            value=all_macaroons.decode('ascii'),
            url=full_path,
            expires=expires,
        ))
예제 #5
0
 def test_cookie_expires_naive(self):
     timestamp = datetime.utcnow()
     c = cookie('http://example.com', 'test', 'value', expires=timestamp)
     self.assertEqual(
         c.expires, int((timestamp - datetime(1970, 1, 1)).total_seconds()))
예제 #6
0
 def test_cookie_with_hostname_not_ascii(self):
     c = cookie('http://κουλουράκι', 'test', 'value')
     self.assertEqual(c.domain, 'κουλουράκι.local')
예제 #7
0
 def test_cookie_with_hostname_like_ipv4(self):
     c = cookie('http://1.2.3.4.com', 'test', 'value')
     self.assertEqual(c.domain, '1.2.3.4.com')
예제 #8
0
 def test_cookie_with_hostname_ipv6(self):
     c = cookie('http://[dead::beef]', 'test', 'value')
     self.assertEqual(c.domain, 'dead::beef')
예제 #9
0
 def test_cookie_with_hostname_not_fqdn(self):
     c = cookie('http://myhost', 'test', 'value')
     self.assertEqual(c.domain, 'myhost.local')
 def test_cookie_expires_naive(self):
     timestamp = datetime.utcnow()
     c = cookie('http://example.com', 'test', 'value', expires=timestamp)
     self.assertEqual(
         c.expires, int((timestamp - datetime(1970, 1, 1)).total_seconds()))