SSB Guardian Client
Project description
SSB Guardian Client
Features
- Maskinporten Integration: Seamless integration with Maskinporten for secure API authentication
- Guardian Token Management: Automatic retrieval and management of Maskinporten access tokens via Guardian
- Multi-Environment Support: Works across DEV, TEST, and PROD environments in the Dapla platform
- Simple API: Easy-to-use client interface for calling external APIs with Maskinporten authentication
- Automatic Token Handling: Fetches personal tokens automatically using dapla-auth-client
Requirements
- Python 3.10 or higher
- Access to Statistics Norway's Dapla platform
- Valid Maskinporten client credentials
DAPLA_ENVIRONMENTenvironment variable set to one of:DEV,TEST, orPROD
Installation
You can install SSB Guardian Client via pip from PyPI:
pip install ssb-guardian-client
Usage
Basic Usage
The simplest way to call an external API with Maskinporten authentication:
from ssb_guardian_client import GuardianClient
# Create a GuardianClient instance
client = GuardianClient()
# Call an external API with Maskinporten authentication
response = client.call_api(
api_endpoint_url="https://api.example.com/data",
maskinporten_client_id="your-maskinporten-client-id",
scopes="scope1 scope2", # Space-separated string
)
# The response contains the JSON data from the API
print(response)
The call_api method automatically:
- Fetches your personal Keycloak token using
dapla-auth-client - Requests a Maskinporten access token from Guardian
- Calls the target API with the Maskinporten token
- Returns the JSON response
Getting Maskinporten Tokens Directly
If you need more control or only want to retrieve the Maskinporten token:
from ssb_guardian_client import GuardianClient
# Create a GuardianClient instance
client = GuardianClient()
# Get a Maskinporten token for custom use
# Scopes can be passed as either a list or a space-separated string
maskinporten_token = client.get_maskinporten_token(
maskinporten_client_id="your-client-id",
scopes=["scope1", "scope2"] # List of scopes
)
# Or with a space-separated string:
maskinporten_token = client.get_maskinporten_token(
maskinporten_client_id="your-client-id",
scopes="scope1 scope2" # Space-separated string
)
# Use the token for custom API calls
import requests
response = requests.get(
"https://api.example.com/data",
headers={"Authorization": f"Bearer {maskinporten_token}"}
)
The get_maskinporten_token method automatically fetches your personal Keycloak token and retrieves the appropriate Guardian URL based on your environment.
📝 Note: Scopes Format
The scopes parameter accepts both formats:
- ✅ List:
["scope1", "scope2"]or["elhub:publicstakeholder"] - ✅ String:
"scope1 scope2"or"elhub:publicstakeholder"
Environment Configuration
The client automatically detects the Dapla environment from the DAPLA_ENVIRONMENT environment variable and uses the appropriate Guardian URL:
- DEV/TEST:
https://guardian.intern.test.ssb.no/maskinporten/access-token - PROD:
https://guardian.intern.ssb.no/maskinporten/access-token
Make sure the DAPLA_ENVIRONMENT variable is set to one of: DEV, TEST, or PROD.
Complete Example
Here's a complete example showing typical usage:
import os
from ssb_guardian_client import GuardianClient
# Ensure environment is set (usually already set in Dapla notebooks)
# os.environ["DAPLA_ENVIRONMENT"] = "TEST"
# Create a GuardianClient instance
client = GuardianClient()
# Call an external API
try:
response = client.call_api(
api_endpoint_url="https://api.external-service.no/v1/data",
maskinporten_client_id="my-maskinporten-client",
scopes="nav:some/scope nav:another/scope", # Space-separated string
)
# Process the response
for item in response.get("data", []):
print(item)
except RuntimeError as e:
print(f"API call failed: {e}")
Please see the Reference Guide for details.
Contributing
Contributions are very welcome. To learn more, see the Contributor Guide.
Development
Setting Up a Development Environment
-
Clone the repository:
git clone https://github.com/statisticsnorway/ssb-guardian-client.git cd ssb-guardian-client
-
Install Poetry if you haven't already.
-
Install dependencies:
poetry install -
Set up pre-commit hooks:
poetry run pre-commit install
Running Tests
Run tests with pytest:
poetry run pytest
Run tests with coverage:
poetry run coverage run -m pytest
poetry run coverage report
Code Quality
This project uses several tools to maintain code quality:
- Black: Code formatting
- Ruff: Linting and code quality checks
- MyPy: Static type checking
- pre-commit: Git hooks for automated checks
Run all checks:
poetry run pre-commit run --all-files
License
Distributed under the terms of the MIT license, SSB Guardian Client is free and open source software.
Troubleshooting
Common Issues
ValueError: Unknown environment
- Ensure the
DAPLA_ENVIRONMENTenvironment variable is set to one of:DEV,TEST, orPROD
RuntimeError: Error getting maskinporten access-token from guardian
- Verify your Maskinporten client ID and scopes are correct
- Ensure you have the necessary permissions to use the specified Maskinporten client
- Check that your Keycloak token is valid
RuntimeError: Error calling target API
- Verify the API endpoint URL is correct
- Ensure the Maskinporten scopes match what the API requires
- Check that the API is accessible from your environment
Issues
If you encounter any problems, please file an issue along with a detailed description.
Credits
This project was generated from Statistics Norway's SSB PyPI Template.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file ssb_guardian_client-0.0.11.tar.gz.
File metadata
- Download URL: ssb_guardian_client-0.0.11.tar.gz
- Upload date:
- Size: 7.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
21ee10ab826f81561d13e7b2d441f87dd962ee2cfae129fae70bdf652cb489dd
|
|
| MD5 |
a4e29417a86e4585d1aebffd3f976182
|
|
| BLAKE2b-256 |
70ac42e74301a0c43ec4009674ef5ac301f7d5a7804f4d9c9750e3526e655f23
|
Provenance
The following attestation bundles were made for ssb_guardian_client-0.0.11.tar.gz:
Publisher:
release.yml on statisticsnorway/ssb-guardian-client
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
ssb_guardian_client-0.0.11.tar.gz -
Subject digest:
21ee10ab826f81561d13e7b2d441f87dd962ee2cfae129fae70bdf652cb489dd - Sigstore transparency entry: 725532595
- Sigstore integration time:
-
Permalink:
statisticsnorway/ssb-guardian-client@a2b335ca23fa9a8602a5c65f3099b6c6b9517103 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/statisticsnorway
-
Access:
internal
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@a2b335ca23fa9a8602a5c65f3099b6c6b9517103 -
Trigger Event:
push
-
Statement type:
File details
Details for the file ssb_guardian_client-0.0.11-py3-none-any.whl.
File metadata
- Download URL: ssb_guardian_client-0.0.11-py3-none-any.whl
- Upload date:
- Size: 8.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1289c3fdf76d4c3aee71d952de305d3b7a812f3c84f55601219e1d67dfd79ae0
|
|
| MD5 |
0fc388e5b1b5a15cf948534d3cc1e742
|
|
| BLAKE2b-256 |
e7111fc1984c223ae632b96c959851b24e81037dd853900e4e235922de49f581
|
Provenance
The following attestation bundles were made for ssb_guardian_client-0.0.11-py3-none-any.whl:
Publisher:
release.yml on statisticsnorway/ssb-guardian-client
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
ssb_guardian_client-0.0.11-py3-none-any.whl -
Subject digest:
1289c3fdf76d4c3aee71d952de305d3b7a812f3c84f55601219e1d67dfd79ae0 - Sigstore transparency entry: 725532617
- Sigstore integration time:
-
Permalink:
statisticsnorway/ssb-guardian-client@a2b335ca23fa9a8602a5c65f3099b6c6b9517103 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/statisticsnorway
-
Access:
internal
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@a2b335ca23fa9a8602a5c65f3099b6c6b9517103 -
Trigger Event:
push
-
Statement type: