Wenche.Systembruker (wenche v0.3.0)

Copy Markdown View Source

System user flow for Altinn 3.

Ported from wenche/systembruker.py in the original Python Wenche project.

Altinn 3 requires that end-user systems register themselves in the system register and create a system user for each organization they will act on behalf of.

Required options

All public functions require a :name option — a short, lowercase identifier for the system (e.g. "kontira"). This is used to build the system ID (<vendor_orgnr>_<name>) and as the display name in Altinn.

registrer_system/4 additionally requires a :description option — a map with "nb", "nn", and "en" keys describing the system.

Configurable scopes

By default, the system requests rights for årsregnskap and aksjonærregisteroppgaven. Additional scopes can be enabled via the :features option:

# Default — årsregnskap + aksjonærregister only
Wenche.Systembruker.rights()

# Include skattemelding scope
Wenche.Systembruker.rights([:skattemelding])

Setup (run once)

  1. registrer_system/4 — registers the system in Altinn's system register
  2. opprett_forespoersel/4 — sends request to org for approval
  3. User approves via confirmUrl in browser

For submission, use Wenche.Maskinporten.get_systemuser_token/2 to get a token.

Summary

Functions

Gets all approved system users for the system.

Creates a system user request for the organization.

Registers or updates the system in Altinn's system register.

Returns the list of resource IDs that the system requests access to.

Returns the raw rights structure used in Altinn API payloads.

Deletes (marks as deleted) a system from Altinn's system register.

Returns the system ID in the format <vendor_orgnr>_<name>.

Functions

hent_forespoersel_status(maskinporten_token, request_id, opts \\ [])

Gets the status of a system user request.

Returns {:ok, response_map} or {:error, reason}.

Optional options

  • :env"test" or "prod" (default: "prod")

hent_systembrukere(maskinporten_token, vendor_orgnr, opts \\ [])

Gets all approved system users for the system.

Returns {:ok, [system_user_map]} or {:error, reason}.

Required options

  • :name — short lowercase system identifier (e.g. "kontira")

Optional options

  • :env"test" or "prod" (default: "prod")

opprett_forespoersel(maskinporten_token, vendor_orgnr, org_nummer, opts \\ [])

Creates a system user request for the organization.

Returns {:ok, %{id: uuid, status: "New", confirmUrl: url}} or {:error, reason}.

The user must go to confirmUrl and approve in the browser.

Required options

  • :name — short lowercase system identifier (e.g. "kontira")

Optional options

  • :env"test" or "prod" (default: "prod")
  • :features — list of feature atoms to enable additional scopes (default: [])

registrer_system(maskinporten_token, vendor_orgnr, client_id, opts \\ [])

Registers or updates the system in Altinn's system register.

Tries POST first. If the system already exists, uses PUT to update.

Required options

  • :name — short lowercase system identifier (e.g. "kontira")
  • :description — map with "nb", "nn", "en" keys

Optional options

  • :env"test" or "prod" (default: "prod")
  • :features — list of feature atoms to enable additional scopes (default: [])

Returns {:ok, response_map} or {:error, reason}.

resource_ids(features \\ [])

Returns the list of resource IDs that the system requests access to.

Accepts an optional list of feature atoms to enable additional scopes.

Examples

Wenche.Systembruker.resource_ids()
#=> ["app_brg_aarsregnskap-vanlig-202406", "ske-innrapportering-aksjonaerregisteroppgave"]

Wenche.Systembruker.resource_ids([:skattemelding])
#=> ["app_brg_aarsregnskap-vanlig-202406", "ske-innrapportering-aksjonaerregisteroppgave",
#    "app_skd_formueinntekt-skattemelding-v2"]

rights(features \\ [])

Returns the raw rights structure used in Altinn API payloads.

Accepts an optional list of feature atoms to enable additional scopes. By default, only årsregnskap and aksjonærregisteroppgaven rights are included.

Supported features

  • :skattemelding — adds the skattemelding scope (app_skd_formueinntekt-skattemelding-v2). Lets the systemuser authenticate against Skatteetaten's /valider endpoint via Maskinporten. Submission still requires ID-porten — a system user cannot submit the skattemelding. See Wenche.Skattemelding.

MVA-melding is intentionally not listed: Skatteetaten only supports ID-porten authentication for MVA (validation and submission), so it cannot be requested via a system user.

Examples

Wenche.Systembruker.rights()
#=> [%{"resource" => [...]}, %{"resource" => [...]}]

Wenche.Systembruker.rights([:skattemelding])
#=> [%{"resource" => [...]}, %{"resource" => [...]}, %{"resource" => [...]}]

slett_system(maskinporten_token, vendor_orgnr, opts \\ [])

Deletes (marks as deleted) a system from Altinn's system register.

Returns :ok or {:error, reason}.

Required options

  • :name — short lowercase system identifier (e.g. "kontira")

Optional options

  • :env"test" or "prod" (default: "prod")

system_id(vendor_orgnr, name)

Returns the system ID in the format <vendor_orgnr>_<name>.