Skip to main content

pymaskinporten

Python Version from PEP 621 TOML PyPI version CI release-please Docs (pdoc to GitHub Pages) PyPI Dependabot uv Ruff Conventional Commits License

This is a python package to request a token from Maskinporten - the Norwegian national access control solution for businesses that exchange data.

graph LR
  Consumer["Application"] -- request token ---> Maskinporten
  Maskinporten -- issue new token ---> Consumer
  Consumer -- use token ---> Service["External Service"]

Installation

pip install pymaskinporten

Optional extras add the secret-store SDKs and the demo app:

Extra Adds
azure Azure Key Vault loader
databricks Databricks secrets loader
aws AWS Secrets Manager loader
gcp Google Secret Manager loader
web The pymaskinporten-web demo app

For example: pip install "pymaskinporten[azure,web]".

Example usage

This assumes you have created the integration at Digdir and have its private key (PEM), key ID, client ID and scope.

By default the credentials are read from the environment variables PRIVATE_KEY, MASKINPORTEN_CLIENT_ID, KID and SCOPE:

from pymaskinporten import request_maskinporten_token

access_token, expires_in = request_maskinporten_token("test")  # or "prod"

api_env must be "test" or "prod"; anything else raises ValueError. A failed token request raises MaskinportenTokenError.

Secret stores

load_config() can also read the credentials from a secret store. Pass the result with config=:

from pymaskinporten import load_config, request_maskinporten_token

config = load_config("azure", key_vault_uri="https://my-vault.vault.azure.net/")
access_token, expires_in = request_maskinporten_token("prod", config=config)
Store Call Install
Azure Key Vault load_config("azure", key_vault_uri="https://...") pip install "pymaskinporten[azure]"
Databricks secrets load_config("databricks", secret_scope="maskinporten") pip install "pymaskinporten[databricks]"
AWS Secrets Manager load_config("aws", region="eu-north-1") pip install "pymaskinporten[aws]"
Google Secret Manager load_config("gcp", project_id="my-project") pip install "pymaskinporten[gcp]"

Each store signs in with its SDK's default credentials: DefaultAzureCredential for Azure, the Databricks runtime for Databricks, boto3's credential chain for AWS (region falls back to boto3's configured region), and Application Default Credentials for Google (the latest secret version is read).

Secret stores use dashes in the names, because Key Vault doesn't allow underscores:

Credential Environment variable Secret name
Private key (PEM) PRIVATE_KEY PRIVATE-KEY
Client ID MASKINPORTEN_CLIENT_ID MASKINPORTEN-CLIENT-ID
Key ID KID KID
Scope SCOPE SCOPE

Name prefixes

prefix is put in front of every environment variable or secret name, so test and prod credentials can live side by side:

# Reads PROD_PRIVATE_KEY, PROD_KID, ...
load_config(prefix="PROD_")

# Reads maskinporten/prod/PRIVATE-KEY, ...
load_config("aws", prefix="maskinporten/prod/")

The prefix is used as-is, so use only characters the source allows (besides letters and digits):

  • Environment variables: _
  • Azure Key Vault: -
  • Google Secret Manager: - _
  • Databricks: - _ .
  • AWS Secrets Manager: - _ / + = . @

Passing credentials directly

To use credentials from anywhere else, build them yourself and pass them with config=:

from pymaskinporten import MaskinportenSecrets, request_maskinporten_token

secrets = MaskinportenSecrets(
    PRIVATE_KEY=open("private_key.pem").read(),
    MASKINPORTEN_CLIENT_ID="your-client-id",
    KID="your-kid",
    SCOPE="your-scope",
)
access_token, expires_in = request_maskinporten_token("prod", config=secrets)

Using a .env file

By default, load_config() reads environment variables only. To keep the credentials in a .env file, install python-dotenv (pip install python-dotenv) and load it before requesting a token:

from dotenv import load_dotenv

load_dotenv()

The demo app below does this for you.

Testing against Maskinporten

To check a change against the real test environment, create a Maskinporten integration for a synthetic organisation (syntetisk organisasjon) in Digdir's test self-service, then from the repo root:

uv run --extra web python scripts/make_test_key.py   # writes a new .env with a key pair
# paste PUBLIC_KEY from .env into the integration, fill in KID, MASKINPORTEN_CLIENT_ID, SCOPE
uv run --extra web python scripts/real_token_test.py

Docker container

To test the library in a web browser, use the pymaskinporten-web demo app (a flask app). Run it locally with pip install "pymaskinporten[web]" and pymaskinporten-web, or use the Docker image:

cat > .env <<EOF
MASKINPORTEN_CLIENT_ID=your-client-id
KID=your-kid
SCOPE=your-scope
EOF

docker run -p 127.0.0.1:5000:5000 --env-file .env \
  -e PRIVATE_KEY="$(cat private_key.pem)" \
  ghcr.io/norwegianveterinaryinstitute/pymaskinporten:main

--env-file keeps quotes as part of the value and doesn't support multi-line values, so leave the values unquoted and pass the PEM key with -e. In PowerShell, use -e PRIVATE_KEY="$(Get-Content -Raw private_key.pem)".

Then open http://127.0.0.1:5000.

Metadata

Release files for pymaskinporten 2.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pymaskinporten 2.0.1
File Size Uploaded
pymaskinporten-2.0.1.tar.gz 10.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pymaskinporten 2.0.1
File Interpreter ABI Platform
pymaskinporten-2.0.1-py3-none-any.whl Python 3 none any Details

Total release size: 23.1 kB

Release files / pymaskinporten-2.0.1.tar.gz

Download URL pymaskinporten-2.0.1.tar.gz
Size 10.7 kB
Tags Source
SHA-256 checksum
How to use checksums
362b4f18002d828ab601d9084dd344837740a914912ea7660a785985fd840ef6
BLAKE2b-256 checksum
How to use checksums
254ecce33174b0419a475574c2ebbc69b982c2825216b599dde0f7ac874fedf8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.13.1 {"installer":{"name":"uv","version":"0.13.1","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / pymaskinporten-2.0.1-py3-none-any.whl

Download URL pymaskinporten-2.0.1-py3-none-any.whl
Size 12.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
32dcae30c5590aec543101352d20130a596da43663cacf65774d55bc227c238b
BLAKE2b-256 checksum
How to use checksums
8c728ba2df95aef0a26d89824edaa7e035c734d07d042d2090c93192437a0e49
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.13.1 {"installer":{"name":"uv","version":"0.13.1","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release history Release notifications | RSS feed

This release

2.0.1 This release

2 release files

1.1.4

2 release files

1.1.3

2 release files

1.1.2

2 release files

1.1.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page