Skip to main content

mitid-client

Log in to Danish services with MitID, from Python. No browser, no Selenium.


Every MitID-protected site works the same way. An identity broker hands the browser an aux blob, the browser feeds that to MitID's JavaScript core client, and the core client returns an authorisation code the broker swaps for a session. This package is a Python version of that core client, plus a broker for the public sector and two ways to put a login on screen.

import requests
from mitid.brokers import nemlogin

session = nemlogin.new_session()
final = nemlogin.log_in(session, "https://www.tinglysning.dk/...", "MyMitIDUserID")
# `session` now carries the service's own login cookies.

What is in it

module what it does
mitid.authenticate an aux blob and a user ID in, an authorisation code out
mitid.core the core client: SRP, the app channel, the QR frames, polling
mitid.srp SRP-6a and the AES-GCM bits the authenticators need
mitid.brokers.nemlogin NemLog-in, which fronts the Danish public sector
mitid.store keeps a login's cookies between runs, 0600, in $XDG_CONFIG_HOME
mitid.ui.console status lines, a scannable QR and a code box, on stderr
mitid.ui.tui the same login as a Textual screen

The protocol draws nothing itself. It reports progress through callbacks (on_status, on_qr, on_otp, ask_token_code, choose_identity), which is what lets the same login be a few lines on stderr in one program and a screen in another.

Logging in

Two methods. APP sends a request to the MitID app and shows either a QR to scan or a six-digit code to type, whichever the app asks for. TOKEN takes six digits from a code token, followed by the account password.

from mitid.ui.console import LoginConsole

screen = LoginConsole()
final = nemlogin.log_in(
    session,
    START_URL,
    user_id,
    on_status=screen.status,  # progress, and which service is asking
    on_qr=screen.qr,  # a QR matrix, redrawn in place each second
    on_otp=screen.otp,  # a code to type into the app
    ask_token_code=screen.ask,  # only with method=mitid.TOKEN
    choose_identity=screen.choose,  # when one MitID unlocks several identities
)

Textual is a framework for building terminal UIs. If your app uses it, the same login is a screen instead:

from functools import partial
from mitid.ui.tui import MitIDLoginScreen

result = await self.push_screen_wait(
    MitIDLoginScreen(partial(nemlogin.log_in, session, START_URL))
)

MitIDLoginScreen calls what you give it with the five callbacks above and dismisses with whatever it returns, or None if the user gave up. It renders the QR, the code, the token prompt and the identity chooser, and runs the login on a worker thread so the UI stays responsive. Textual is an optional dependency, so install mitid-client[textual] if you want it.

Keeping the session

A login costs a tap on a phone, so it must not happen once per request. What it produces is a set of cookies, and CookieStore keeps those between runs:

from mitid.store import CookieStore

store = CookieStore(
    "yourapp", "service-session.json", session_factory=nemlogin.new_session
)
store.save(session, user_id=user_id)

restored = store.restore()  # (session, saved) or None
if restored:
    session, saved = restored
    idle = store.idle_for(saved["saved_at"])

Whether the service still honours those cookies is up to the service, so ask it. Most end a session that has sat idle for half an hour.

Which services it works with

Any service that authenticates through MitID, in principle. What differs between them is the broker: the part that starts the session and hands over the aux blob. Once you have that blob, mitid.authenticate does the rest, and that half is the same everywhere.

mitid.brokers.nemlogin covers NemLog-in, which fronts the Danish public sector. Point it at any NemLog-in-protected URL and it should work as it stands. Other brokers need a short module of their own to fetch the aux blob; nemlogin.py is the worked example to copy from.

Tried so far against tinglysning.dk (NemLog-in) and nordnet.dk (Signicat).

Installing

uv add mitid-client              # or: pip install mitid-client
uv add "mitid-client[textual]"   # for the Textual screen

What you are taking on

Use it for your own accounts and your own data.

Tests

uv run pytest

Covers the cookie store's round trip and file mode, both QR renderers' shape and polarity, and the parsing of MitID's error responses. The protocol itself is not covered. It can only be run against the real thing.

Credits and licence

The protocol here was worked out by Hundter/MitID-BrowserClient, MIT-licensed, © 2024 Hundter. mitid/core.py and mitid/srp.py started as that code and have been changed since.

MIT. See LICENSE.

Metadata

Release files for mitid-client 0.1.2

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

Source distribution (sdist)

Source distribution for mitid-client 0.1.2
File Size Uploaded Provenance
mitid_client-0.1.2.tar.gz 25.8 kB With provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for mitid-client 0.1.2
File Interpreter ABI Platform
mitid_client-0.1.2-py3-none-any.whl Python 3 none any Details

Total release size: 55.4 kB

Release files / mitid_client-0.1.2.tar.gz

Download URL mitid_client-0.1.2.tar.gz
Size 25.8 kB
Tags Source
SHA-256 checksum
How to use checksums
bc1b520b452b2d4280276a3883ff9f4257743e72c56a62a3fe301266a91a0883
BLAKE2b-256 checksum
How to use checksums
13174de9db9acfb9fe4d6c94dd8873bab93310f62308494c3da806867d2eeb98
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 2, 2026.

Transparency log

Release files / mitid_client-0.1.2-py3-none-any.whl

Download URL mitid_client-0.1.2-py3-none-any.whl
Size 29.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
d23e6d0686e4f69dc8ed74907a88acad3e9bd9b13a4e8b706711dbdcb620bf13
BLAKE2b-256 checksum
How to use checksums
7af7af02bf8c1d4ef57d525828327cfec2274ecece553bedea43a5a22eab4780
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 2, 2026.

Transparency log

Security for mitid-client 0.1.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

All 2 files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for mitid_client-0.1.2.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 2, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

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