pymaskinporten

The pymaskinporten package.

Provides a set of functions to fetch a token from Maskinporten

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"]

Example usage

This assumes that:

  1. You have created the integration on digidir.no and,
  2. You have set the necessary environment variables for your API credentials.
# set environment variables for your API    
import os
os.environ["KID"] = "your_kid"
os.environ["PRIVATE_KEY"] = "your_private_key"
os.environ["MASKINPORTEN_CLIENT_ID"] = "your_client_id"
os.environ["SCOPE"] = "your_scope"

# get the access token
from pymaskinporten.request_token import request_maskinporten_token
access_token, expires_in = request_maskinporten_token("my_api", "test")
print(f"Access Token: {access_token}")
print(f"Expires In: {expires_in} seconds")

Docker container

If you want to test the library through a web browser the image opens a flask app:

cat > .env <<EOF
>PRIVATE_KEY="-----BEGIN PRIVATE KEY-----\n...\n-----END PRIVATE KEY-----\n"
>MASKINPORTEN_CLIENT_ID="your-client-id"
>KID="your-kid"
>SCOPE="your-scope"
>STRICT_KEY_VALIDATION=false
>EOF

docker run -p 5000:5000 --env-file .env ghcr.io/norwegianveterinaryinstitute/pymaskinporten:main
 1"""
 2The pymaskinporten package.
 3
 4Provides a set of functions to fetch a token from Maskinporten
 5
 6.. include:: ../../README.md
 7   :start-line: 15
 8"""
 9
10
11def main() -> str:
12    return "Hello from pymaskinporten!"
13
14
15if __name__ == "__main__":
16    print(main())
def main() -> str:
12def main() -> str:
13    return "Hello from pymaskinporten!"