def visit(url, request):
            if request.headers.get('Accept') == 'application/json':
                return {'status_code': 200, 'content': {'agent': request.url}}
            cs = SimpleCookie()
            cookies = request.headers.get('Cookie')
            if cookies is not None:
                cs.load(str(cookies))
            public_key = None
            for c in cs:
                if c == 'agent-login':
                    json_cookie = json.loads(
                        base64.b64decode(cs[c].value).decode('utf-8'))
                    public_key = bakery.PublicKey.deserialize(
                        json_cookie.get('public_key'))
            ms = httpbakery.extract_macaroons(request.headers)
            if len(ms) == 0:
                b = bakery.Bakery(key=discharge_key)
                m = b.oven.macaroon(
                    version=bakery.LATEST_VERSION,
                    expiry=datetime.utcnow() + timedelta(days=1),
                    caveats=[
                        bakery.local_third_party_caveat(
                            public_key,
                            version=httpbakery.request_version(
                                request.headers))
                    ],
                    ops=[bakery.Op(entity='agent', action='login')])
                content, headers = httpbakery.discharge_required_response(
                    m, '/', 'test', 'message')
                resp = response(status_code=401,
                                content=content,
                                headers=headers)
                return request.hooks['response'][0](resp)

            return {'status_code': 200, 'content': {'agent-login': True}}
 def test_discharge_all_local_discharge_version1(self):
     oc = common.new_bakery('ts', None)
     client_key = bakery.generate_key()
     m = oc.oven.macaroon(bakery.VERSION_1, common.ages, [
         bakery.local_third_party_caveat(client_key.public_key,
                                         bakery.VERSION_1)
     ], [bakery.LOGIN_OP])
     ms = bakery.discharge_all(m, no_discharge(self), client_key)
     oc.checker.auth([ms]).allow(common.test_context, [bakery.LOGIN_OP])
 def test_discharge_all_local_discharge_version1(self):
     oc = common.new_bakery('ts', None)
     client_key = bakery.generate_key()
     m = oc.oven.macaroon(bakery.VERSION_1, common.ages, [
         bakery.local_third_party_caveat(
             client_key.public_key, bakery.VERSION_1)
     ], [bakery.LOGIN_OP])
     ms = bakery.discharge_all(m, no_discharge(self), client_key)
     oc.checker.auth([ms]).allow(common.test_context,
                                 [bakery.LOGIN_OP])
 def login(url, request):
     b = bakery.Bakery(key=discharge_key)
     m = b.oven.macaroon(
         version=bakery.LATEST_VERSION,
         expiry=datetime.utcnow() + timedelta(days=1),
         caveats=[
             bakery.local_third_party_caveat(
                 key.public_key,
                 version=httpbakery.request_version(request.headers))
         ],
         ops=[bakery.Op(entity='agent', action='login')])
     return {'status_code': 200, 'content': {'macaroon': m.to_dict()}}
 def test_discharge_all_local_discharge(self):
     oc = common.new_bakery('ts', None)
     client_key = macaroonbakery.generate_key()
     m = oc.oven.macaroon(macaroonbakery.LATEST_BAKERY_VERSION, common.ages,
                          [
                              macaroonbakery.local_third_party_caveat(
                                  client_key.public_key,
                                  macaroonbakery.LATEST_BAKERY_VERSION)
                          ], [macaroonbakery.LOGIN_OP])
     ms = macaroonbakery.discharge_all(common.test_context, m,
                                       no_discharge(self), client_key)
     oc.checker.auth([ms]).allow(common.test_context,
                                 [macaroonbakery.LOGIN_OP])