Skip to main content

This client library unlocks Pecan IAM for server applications.

Project description

PecanHQ

To get started, make sure you have signed up at https://www.pecanhq.com/, released an artifact, and created an application user with access to the app role.

Follow the steps below to embed the client library into your application.

Step 1: Install the pip package.

python3 -m pip install pecanhq

The PecanHQ pip package can be used to embed Pecan into your python application.

Step 2: Set up the client

# Create the authorization service
import pecanhq, requests
pecan = pecanhq.create(
    requests.session(),
    "<app-key>",
    "<app-secret>",
    "<artifact>",
    <version>)

Create a single instance of the service on application startup. The service requires application credentials and a released schema, and will try load that schema from the Pecan API.

Step 2: Load a user profile

# Look up a security principal by an identity claim
assertion = pecan.find("<claim>", "<value>", tenant=None)
if assertion is None:
    # In this case, user profiles are being lazily created
    profile = pecan.resource.as_assign_identity_uri().post(
        "<claim>",
        "<value>",
        [ "<scope>" ])

    # Construct a claims principal from the newly-created profile
    principal = {
        f"{x['issuer']}{x.['key']}": x['value'] for x in profile['assertions']
    }
else:
    # Load all claims associated with the account.
    result = pecan.load(assertion['account_id'])

    if result:
        # Construct a claims principal from the result
        principal = result['claims'];
    else:
        principal = {}

After authentication, the client library locates user profiles using unique asserted claims. Once located, the full profile can be be loaded using the identifier. The data in that profile can then be added to the security principal.

Step 3: Use an authorization session

# Create an authorization session
session = pecanhq.Session(pecan, principal)

# Assert access to a specific resource
if session.has_permissions("<service>", "<resource>", "<permissions>"):
    # Do guarded action
    pass

# Assume system-level access for the user
session = session.escalate_privileges();

# Fetch a typed assertion for attribute-based access control
value = session.as_decimal("<claim>")

Before each request, a new authentication session is created for the current principal. This session can be used to ensure access to a specific resource, escalate permissions, or extract data to use in attribute-based access control.

Advanced Usage

A key design goal of Pecan was to minimise communication with the API during normal operation. To achieve that, services and profiles can be locally cached, and enough data exists in each profile to make all authorization decisions locally.

Caching the service

# All schema details can be exported to JSON and cached.
cached = pecan.dump()

# Cached state can be used to reconstruct the service locally.
pecan = pecanhq.create(
    schema,
    "<app-key>",
    "<app-secret>",
    "<artifact>",
    <version>,
    cached=cached)

The client library loads the schema from the API, and uses it to make authorization decisions. To improve startup times, the service can be dumped to JSON and reloaded from this local copy on startup.

Caching profiles

# The claim profiles can be exported to JSON and cached.
data = pecan.as_json(result)

# Cached claims can be used to reconstruct profiles locally
result = pecan.from_json(data)

You don't control third party services. If they go down, or are too slow, they can affect your SLAs. So limit your exposure to that risk, and speed up your code, by caching user profiles locally.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pecanhq-0.0.4-py3-none-any.whl (22.3 kB view details)

Uploaded Python 3

File details

Details for the file pecanhq-0.0.4-py3-none-any.whl.

File metadata

  • Download URL: pecanhq-0.0.4-py3-none-any.whl
  • Upload date:
  • Size: 22.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.25.1 setuptools/45.2.0 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.8.10

File hashes

Hashes for pecanhq-0.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 78a3e3370532b2d7504c4b2dfce3ccb461bc6a51915a0fc7dc9618d649cb66f1
MD5 17ae591b06756021220c2baa71d66e6c
BLAKE2b-256 d6f446e274b4ede0aa66a9a951fa4b48ac96bc30a2f6931e32bc1b4c567c8f90

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page