GitHub

Low-level Authentication

Raw authentication endpoint methods, token and XAdES sequence contracts, and SDK token binding.

On this page

Use low-level authentication when another system owns signing, token encryption, or polling policy. Raw authentication uses schema-native response models until you explicitly map tokens back to the public SDK model.

Endpoint methods

MethodTransportReturnsUse for
client.raw.auth.challenge()unauthenticatedspec.AuthenticationChallengeResponseCreate the challenge used by token or XAdES authentication.
client.raw.auth.token_auth(body)unauthenticatedspec.AuthenticationInitResponseStart KSeF-token authentication with InitTokenAuthenticationRequest.
client.raw.auth.xades_auth(signed_xml, verify_chain=False)unauthenticatedspec.AuthenticationInitResponseSubmit externally signed XAdES XML.
client.raw.auth.auth_status(bearer_token, reference_number)temporary auth bearerspec.AuthenticationOperationStatusResponsePoll authentication operation status.
client.raw.auth.redeem_token(bearer_token)temporary auth bearerspec.AuthenticationTokensResponseExchange the temporary authentication token for access and refresh tokens.
client.raw.auth.refresh_token(bearer_token)refresh bearerspec.AuthenticationTokenRefreshResponseRefresh an access token.
auth.raw.auth.list_sessions(...)authenticatedspec.AuthenticationListResponseList authentication sessions.
auth.raw.auth.terminate_current_session()authenticatedNoneTerminate the current authentication session.
auth.raw.auth.terminate_auth_session(reference_number)authenticatedNoneTerminate another authentication session by reference.

Async clients expose the same method names and await network calls.

Token authentication sequence

StepCall or objectKeep
Create challengeclient.raw.auth.challenge()challenge.challenge, challenge.timestampMs
Fetch token-encryption certificateclient.raw.encryption.fetch_public_certificates()certificate where usage contains KsefTokenEncryption
Encrypt token payloadencrypt_token(ksef_token, str(challenge.timestampMs), cert.certificate)encrypted token payload
Build requestspec.InitTokenAuthenticationRequest(...)challenge, contextIdentifier, encryptedToken, publicKeyId
Start operationclient.raw.auth.token_auth(request)init.authenticationToken.token, init.referenceNumber
Pollclient.raw.auth.auth_status(...)final status code
Redeemclient.raw.auth.redeem_token(init.authenticationToken.token)schema-native access and refresh token response
Bind to SDKclient.authentication.resume(AuthenticationResumeState.from_tokens(auth_mapper.from_spec(raw_tokens)))AuthenticatedClient

XAdES external signing sequence

StepCall or objectKeep
Create challengeclient.raw.auth.challenge()challenge payload for the signing system
Sign outside SDKHSM, gateway, or external signersigned XML bytes
Start operationclient.raw.auth.xades_auth(signed_xml, verify_chain=True)init.authenticationToken.token, init.referenceNumber
Pollclient.raw.auth.auth_status(...)final status code
Redeemclient.raw.auth.redeem_token(init.authenticationToken.token)schema-native access and refresh token response
Bind to SDKclient.authentication.resume(AuthenticationResumeState.from_tokens(auth_mapper.from_spec(raw_tokens)))AuthenticatedClient

Binding raw tokens

from ksef2.models import AuthenticationResumeState
from ksef2.raw.mappers import auth as auth_mapper

raw_tokens = client.raw.auth.redeem_token(init.authenticationToken.token)
auth_tokens = auth_mapper.from_spec(raw_tokens)
auth = client.authentication.resume(AuthenticationResumeState.from_tokens(auth_tokens))
python

After binding, you can use workflow branches such as auth.invoices or authenticated raw branches such as auth.raw.invoices.

Polling status contract

auth_status() uses the temporary authentication token as a bearer token, not the final access token. Poll until KSeF returns a terminal authentication status, then call redeem_token() with the same temporary token.

Status conditionCaller action
Success statusRedeem the temporary authentication token.
Failure statusStop and surface the status code/description.
Local timeoutPersist init.referenceNumber and decide whether another worker should continue polling.
  • Authentication methods: Understand token, XAdES, TEST certificate, and profile authentication before dropping to raw endpoints.
  • Low-level API: Review low-level ownership, model namespaces, and utility exports.
  • Endpoint map: Find all raw endpoint branches.