Macaroon Bakery

1.3.4 · active · verified Wed Apr 15

macaroonbakery is a Python library that provides higher-level operations for working with macaroons, building upon the core `pymacaroons` library. It implements the httpbakery conventions, allowing for automatic gathering of discharge macaroons for third-party caveats. The library is currently at version 1.3.4 and, while listed as '2 - Pre-Alpha' on PyPI, shows recent activity on its GitHub repository, suggesting active maintenance.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use `macaroonbakery.httpbakery.BakeryAuth` to interact with a protected URL. It automatically manages the acquisition and storage of macaroons using a `RequestsCookieJar`. The `PROTECTED_URL` should be replaced with an actual endpoint that uses macaroon-based authentication. The first request might trigger a redirection to an authentication provider, which `BakeryAuth` is designed to handle.

import requests
from macaroonbakery import httpbakery
import os

# A placeholder for a URL protected by a macaroon bakery service.
# In a real application, this would be a URL to your service.
PROTECTED_URL = os.environ.get('MACAROON_PROTECTED_URL', 'http://localhost:8080/protected')

# Initialize a RequestsCookieJar to store macaroons
jar = requests.cookies.RequestsCookieJar()

try:
    # Make a request using BakeryAuth. It will handle macaroon acquisition.
    # For the first request, it might redirect to an authentication service.
    resp = requests.get(
        PROTECTED_URL,
        cookies=jar,
        auth=httpbakery.BakeryAuth(cookies=jar)
    )
    resp.raise_for_status() # Raise an exception for HTTP errors

    print(f"Successfully accessed {PROTECTED_URL}. Response: {resp.text[:100]}...")
    # Subsequent requests will reuse the macaroons in the jar

except requests.exceptions.HTTPError as e:
    print(f"HTTP Error: {e.response.status_code} - {e.response.text}")
except requests.exceptions.RequestException as e:
    print(f"Request failed: {e}")

view raw JSON →