Macaroon Bakery
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
- gotcha The library's PyPI status is '2 - Pre-Alpha', indicating that its API may not be stable and could change without adhering to strict semantic versioning. Users should expect potential breaking changes in minor or patch releases.
- gotcha macaroonbakery relies heavily on `pymacaroons` for its core macaroon functionality. The `pymacaroons` library appears to be less actively maintained (last release Feb 2018), which could limit new features or delay critical updates for macaroonbakery.
- gotcha While `setup.py` indicates compatibility with Python 2.7, modern Python development primarily targets Python 3. Using `macaroonbakery` with Python 2.7 might lead to compatibility issues with other libraries or a lack of future support.
Install
-
pip install macaroonbakery
Imports
- httpbakery
from macaroonbakery import httpbakery
- BakeryAuth
from macaroonbakery.httpbakery import BakeryAuth
Quickstart
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}")