* feat(skatteverket): ombudsregister grant verification, honest session expiry, daily ombud sync
Users reported the Skatteverket connection "just disappearing" with no
banner, needing BankID again every time. Two causes, both fixed here:
1. SKV's per-flow refresh token lives 65 minutes. /status and the
skv_disconnected notice called any stored refresh token "refreshable",
so a days-dead session reported healthy and the reconnect banner never
fired until a submission failed live. lib/skatteverket/session-lifetime
now decides refreshability (expires_at + 5 min, refresh cap) for both
surfaces; the settings panel states the one-hour session lifetime.
2. The durable fix is the ombud (system certificate) path, dormant since
July behind SKATTEVERKET_SYSTEM_AUTH_MODE. Skatteverket added scope
`obr` (Ombudshantering v2) to our application id on 2026-09-01, so grant
verification can now ask the ombudsregister instead of classifying 403s
from the read services:
- lib/ombud-client.ts: GET /ombud/autentisieratOmbud, GET /roller,
POST .../djuplank/utseombud, on the system identity, per the public
tjanstebeskrivning v2.0 (mirrored in dev_docs/skatteverket/ombudshantering).
Role codes are env-pinned (SKATTEVERKET_OMBUD_ROLL_LASOMBUD/_MOMS) or
matched on rollbeskrivning text; a deep link never mints with a
guessed code.
- grant-probe.ts: register first, read-service probes only as fallback.
- New daily cron /api/extensions/skatteverket/ombud/sync/cron (30 3 * * *):
one register call discovers every company that granted us, creates or
downgrades connection rows by org number, runs from shadow mode on,
and never mass-revokes on an empty register.
- POST /system-connection/deeplink + "Utse {app} som ombud" button:
the company lands in SKV's e-service with roles pre-selected.
- Default system scopes include `obr`; skvRequestWithAuth gains an
`accept` option (Ombudshantering requires the Accept header).
Still inert in prod until the org certificate and avtal land; the cron and
verify routes no-op while system auth is off or unconfigured.
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01HkLnhWnxt5wWB9j3vfMmxu
* fix(skatteverket): skeptic round on ombud sync, register 404 fallback, opt-in-only rows, mass-downgrade guard
Cron touches only existing connection rows (a tenant's own Verifiera or
deep-link opt-in; the deeplink route now records a pending row), so an
org-number twin never gets auto-verified, and rows the tenant revoked
locally stay revoked. A register 404 throws by default (spec: wrong URI)
and is empty only for the cron, which guards it. Decisions are planned
before any upsert; a run that would fully deny >= 3 rows and > 50% of the
granted ones applies no downgrade. Grants that classify as neither
behörighet are 'error', not 'denied'. Literal select in listConnections for
the phantom-column scanner. window.open without 'noopener' so the
pre-opened tab exists; opener nulled by hand. Deeplink route test added.
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01HkLnhWnxt5wWB9j3vfMmxu
* fix(skatteverket): CodeRabbit round: exact role labels, paginate connections, deny never-listed rows, fail deeplink without opt-in row
Role descriptions match the whole label so 'Momsdeklaration,
deklarationsombud' is never read as the narrow moms role. listConnections
pages through fetchAllRows on (created_at, id). A pending row the register
never lists is written once as denied instead of staying 'Inte verifierad'.
The deeplink route returns 500 when the opt-in row cannot be stored, and the
panel navigates in-tab when the pre-opened tab was blocked.
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01HkLnhWnxt5wWB9j3vfMmxu
* fix(skatteverket): fence ombud grants on contested org numbers; cron honours unrecognised role codes
An org number claimed by more than one live company is contested: verify
and deep link answer 409 ORG_NUMBER_CONTESTED and the nightly sync changes
nothing on it, so a tenant that typed a victim's public org number cannot
inherit the victim's grant. The sync also skips huvudmän whose register
roles classify as neither behörighet (pinning problem, never a denial),
mirroring probeViaOmbudsregister.
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01HkLnhWnxt5wWB9j3vfMmxu
* fix(skatteverket): validate the ombud deep link host; withdraw grants on contested org numbers
The register's djuplank must be an https skatteverket.se URL before it is
returned or navigated to (the settings page follows it). The nightly sync
now withdraws a grant already recorded on an org number that more than one
live company claims, instead of only refusing new ones.
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01HkLnhWnxt5wWB9j3vfMmxu
---------
Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com>
262 lines
10 KiB
TypeScript
262 lines
10 KiB
TypeScript
/**
|
|
* Grant verification: the ombudsregister (Ombudshantering v2) decides when it
|
|
* answers; the read-service probes (200 / felkod 3 / OMBUD_GRANT_MISSING /
|
|
* 404 / transient) decide only when the register cannot be consulted. The
|
|
* transient-error-never-downgrades rule lives in connection-store's
|
|
* recordProbeResult and is asserted through the recorded input here.
|
|
*/
|
|
import { describe, it, expect, vi, beforeEach } from 'vitest'
|
|
|
|
const mockSkvRequestWithAuth = vi.fn()
|
|
vi.mock('../lib/api-client', async (importOriginal) => {
|
|
const actual = (await importOriginal()) as Record<string, unknown>
|
|
return {
|
|
...actual,
|
|
skvRequestWithAuth: (...a: unknown[]) => mockSkvRequestWithAuth(...a),
|
|
}
|
|
})
|
|
|
|
const mockRecordProbeResult = vi.fn()
|
|
vi.mock('../lib/connection-store', async (importOriginal) => {
|
|
const actual = (await importOriginal()) as Record<string, unknown>
|
|
return {
|
|
...actual,
|
|
recordProbeResult: (...a: unknown[]) => mockRecordProbeResult(...a),
|
|
}
|
|
})
|
|
|
|
import { probeCompanyGrants, probeViaOmbudsregister } from '../lib/grant-probe'
|
|
import { SkatteverketAuthError } from '../lib/api-client'
|
|
|
|
const ORG = '165560000000'
|
|
const TODAY = '2026-09-01'
|
|
|
|
/** The register call is always first; make it fail so the service probes decide. */
|
|
function registryUnavailable() {
|
|
mockSkvRequestWithAuth.mockResolvedValueOnce({ ok: false, status: 503, text: async () => 'down' })
|
|
}
|
|
|
|
function registryAnswers(posts: unknown[]) {
|
|
mockSkvRequestWithAuth.mockResolvedValueOnce({ ok: true, status: 200, json: async () => posts })
|
|
}
|
|
|
|
beforeEach(() => {
|
|
vi.clearAllMocks()
|
|
mockRecordProbeResult.mockResolvedValue({ id: 'conn-1', status: 'verified' })
|
|
vi.spyOn(console, 'info').mockImplementation(() => {})
|
|
vi.spyOn(console, 'warn').mockImplementation(() => {})
|
|
})
|
|
|
|
describe('probeCompanyGrants via the ombudsregister', () => {
|
|
it('both roles active today -> both granted, source registry, no service probes', async () => {
|
|
registryAnswers([
|
|
{ huvudman: ORG, roll: 'JLO', rollbeskrivning: 'Juridiskt läsombud', ombud: '165590000000', giltigFrom: '2026-07-19' },
|
|
{ huvudman: ORG, roll: 'MOMS', rollbeskrivning: 'Momsdeklaration, ombud', ombud: '165590000000', giltigFrom: '2026-07-19', giltigTom: '' },
|
|
])
|
|
|
|
const result = await probeCompanyGrants('company-1', ORG)
|
|
|
|
expect(result.source).toBe('registry')
|
|
expect(result.lasombud.status).toBe('granted')
|
|
expect(result.momsOmbud.status).toBe('granted')
|
|
expect(mockSkvRequestWithAuth).toHaveBeenCalledTimes(1)
|
|
// System identity, ombud base URL, Accept header, huvudman filter.
|
|
const [auth, method, path, , options] = mockSkvRequestWithAuth.mock.calls[0]
|
|
expect(auth).toEqual({ mode: 'system' })
|
|
expect(method).toBe('GET')
|
|
expect(path).toBe(`/ombud/autentisieratOmbud?huvudman=${ORG}`)
|
|
expect(options).toMatchObject({
|
|
baseUrl: 'https://api.test.skatteverket.se/behorighet/ombudshantering/v2',
|
|
accept: 'application/json',
|
|
})
|
|
})
|
|
|
|
it('only läsombud granted -> moms denied with the roles listed in the detail', async () => {
|
|
registryAnswers([
|
|
{ huvudman: ORG, roll: 'JLO', rollbeskrivning: 'Juridiskt läsombud', giltigFrom: '2026-07-19' },
|
|
{ huvudman: ORG, roll: 'DEKL', rollbeskrivning: 'Deklarationsombud', giltigFrom: '2026-07-19' },
|
|
])
|
|
|
|
const result = await probeCompanyGrants('company-1', ORG)
|
|
|
|
expect(result.lasombud.status).toBe('granted')
|
|
expect(result.momsOmbud.status).toBe('denied')
|
|
expect(result.momsOmbud.detail).toContain('JLO, DEKL')
|
|
expect(mockRecordProbeResult).toHaveBeenCalledWith(
|
|
expect.objectContaining({
|
|
lasombud: expect.objectContaining({ status: 'granted' }),
|
|
momsOmbud: expect.objectContaining({ status: 'denied' }),
|
|
error: null,
|
|
})
|
|
)
|
|
})
|
|
|
|
it('a 200 with no posts for the huvudman -> both denied (the company never granted)', async () => {
|
|
registryAnswers([])
|
|
|
|
const result = await probeCompanyGrants('company-1', ORG)
|
|
|
|
expect(result.source).toBe('registry')
|
|
expect(result.lasombud.status).toBe('denied')
|
|
expect(result.momsOmbud.status).toBe('denied')
|
|
})
|
|
|
|
it('a future-dated or expired grant does not count', async () => {
|
|
const future = await probeViaOmbudsregisterWith([
|
|
{ huvudman: ORG, roll: 'JLO', rollbeskrivning: 'Juridiskt läsombud', giltigFrom: '2026-12-01' },
|
|
])
|
|
expect(future.result?.lasombud.status).toBe('denied')
|
|
|
|
const expired = await probeViaOmbudsregisterWith([
|
|
{ huvudman: ORG, roll: 'JLO', rollbeskrivning: 'Juridiskt läsombud', giltigFrom: '2025-01-01', giltigTom: '2026-08-31' },
|
|
])
|
|
expect(expired.result?.lasombud.status).toBe('denied')
|
|
|
|
const active = await probeViaOmbudsregisterWith([
|
|
{ huvudman: ORG, roll: 'JLO', rollbeskrivning: 'Juridiskt läsombud', giltigFrom: '2025-01-01', giltigTom: '2026-09-01' },
|
|
])
|
|
expect(active.result?.lasombud.status).toBe('granted')
|
|
})
|
|
|
|
it('a register 404 is a transport fault: falls back to the service probes instead of denying', async () => {
|
|
mockSkvRequestWithAuth.mockResolvedValueOnce({ ok: false, status: 404, text: async () => '{"message":"Not found"}' })
|
|
mockSkvRequestWithAuth
|
|
.mockResolvedValueOnce({ ok: true, status: 200 })
|
|
.mockResolvedValueOnce({ ok: true, status: 200 })
|
|
|
|
const result = await probeCompanyGrants('company-1', ORG)
|
|
|
|
expect(result.source).toBe('service')
|
|
expect(result.lasombud.status).toBe('granted')
|
|
expect(result.lasombud.detail).toContain('OBR_HTTP_ERROR')
|
|
})
|
|
|
|
it('grants that classify as neither behörighet are an error (unrecognised role codes), never a denial', async () => {
|
|
registryAnswers([
|
|
{ huvudman: ORG, roll: 'ZZ1', rollbeskrivning: 'Läsombud, juridisk person', giltigFrom: '2026-07-19' },
|
|
{ huvudman: ORG, roll: 'ZZ2', rollbeskrivning: 'Ombud för momsdeklaration', giltigFrom: '2026-07-19' },
|
|
])
|
|
|
|
const result = await probeCompanyGrants('company-1', ORG)
|
|
|
|
expect(result.source).toBe('registry')
|
|
expect(result.lasombud.status).toBe('error')
|
|
expect(result.momsOmbud.status).toBe('error')
|
|
expect(result.lasombud.detail).toContain('ZZ1, ZZ2')
|
|
expect(mockRecordProbeResult.mock.calls[0][0].error).toBeTruthy()
|
|
})
|
|
|
|
it('a register 403 (scope/avtal missing) is an error, never a company denial', async () => {
|
|
// api-client maps a system-mode 403 to OMBUD_GRANT_MISSING; on the register that is run-level.
|
|
mockSkvRequestWithAuth.mockRejectedValueOnce(new SkatteverketAuthError('nope', 'OMBUD_GRANT_MISSING'))
|
|
// Fallback service probes both succeed.
|
|
mockSkvRequestWithAuth
|
|
.mockResolvedValueOnce({ ok: true, status: 200 })
|
|
.mockResolvedValueOnce({ ok: true, status: 200 })
|
|
|
|
const result = await probeCompanyGrants('company-1', ORG)
|
|
|
|
expect(result.source).toBe('service')
|
|
expect(result.lasombud.status).toBe('granted')
|
|
expect(result.lasombud.detail).toContain('OBR_FORBIDDEN')
|
|
expect(mockSkvRequestWithAuth).toHaveBeenCalledTimes(3)
|
|
})
|
|
})
|
|
|
|
async function probeViaOmbudsregisterWith(posts: unknown[]) {
|
|
registryAnswers(posts)
|
|
return probeViaOmbudsregister(ORG, TODAY)
|
|
}
|
|
|
|
describe('probeCompanyGrants service-probe fallback', () => {
|
|
it('both probes 200 -> both granted', async () => {
|
|
registryUnavailable()
|
|
mockSkvRequestWithAuth
|
|
.mockResolvedValueOnce({ ok: true, status: 200 }) // saldo
|
|
.mockResolvedValueOnce({ ok: true, status: 200 }) // utkast
|
|
|
|
const result = await probeCompanyGrants('company-1', ORG)
|
|
|
|
expect(result.source).toBe('service')
|
|
expect(result.lasombud.status).toBe('granted')
|
|
expect(result.momsOmbud.status).toBe('granted')
|
|
// All calls ran on SYSTEM credentials.
|
|
for (const call of mockSkvRequestWithAuth.mock.calls) expect(call[0]).toEqual({ mode: 'system' })
|
|
// The fallback reason travels in the detail for the settings panel/probe row.
|
|
expect(result.lasombud.detail).toContain('ombudsregister otillgängligt')
|
|
})
|
|
|
|
it('records the actual 2xx status as detail, not a hardcoded 200', async () => {
|
|
registryUnavailable()
|
|
mockSkvRequestWithAuth
|
|
.mockResolvedValueOnce({ ok: true, status: 204 }) // saldo
|
|
.mockResolvedValueOnce({ ok: true, status: 200 }) // utkast
|
|
|
|
const result = await probeCompanyGrants('company-1', ORG)
|
|
|
|
expect(result.lasombud.status).toBe('granted')
|
|
expect(result.lasombud.detail.startsWith('204')).toBe(true)
|
|
expect(result.momsOmbud.detail.startsWith('200')).toBe(true)
|
|
})
|
|
|
|
it('felkod 3 (no skattekonto) still proves the lasombud authorization', async () => {
|
|
registryUnavailable()
|
|
mockSkvRequestWithAuth
|
|
.mockResolvedValueOnce({ ok: false, status: 400, json: async () => ({ felkod: 3 }) })
|
|
.mockResolvedValueOnce({ ok: false, status: 404 })
|
|
|
|
const result = await probeCompanyGrants('company-1', ORG)
|
|
|
|
expect(result.lasombud.status).toBe('granted')
|
|
// 404 on /utkast = no draft, but the gateway authorized us.
|
|
expect(result.momsOmbud.status).toBe('granted')
|
|
})
|
|
|
|
it('OMBUD_GRANT_MISSING on the read services classifies as denied', async () => {
|
|
registryUnavailable()
|
|
mockSkvRequestWithAuth
|
|
.mockRejectedValueOnce(new SkatteverketAuthError('saknas', 'OMBUD_GRANT_MISSING'))
|
|
.mockRejectedValueOnce(new SkatteverketAuthError('saknas', 'OMBUD_GRANT_MISSING'))
|
|
|
|
const result = await probeCompanyGrants('company-1', ORG)
|
|
|
|
expect(result.lasombud.status).toBe('denied')
|
|
expect(result.momsOmbud.status).toBe('denied')
|
|
})
|
|
|
|
it('transient failures classify as error, never denied', async () => {
|
|
registryUnavailable()
|
|
mockSkvRequestWithAuth
|
|
.mockRejectedValueOnce(new SkatteverketAuthError('overload', 'RATE_LIMITED'))
|
|
.mockRejectedValueOnce(new Error('fetch failed'))
|
|
|
|
const result = await probeCompanyGrants('company-1', ORG)
|
|
|
|
expect(result.lasombud.status).toBe('error')
|
|
expect(result.momsOmbud.status).toBe('error')
|
|
|
|
const recorded = mockRecordProbeResult.mock.calls[0][0]
|
|
expect(recorded.error).toBeTruthy()
|
|
})
|
|
|
|
it('persists the probe outcome via recordProbeResult', async () => {
|
|
registryUnavailable()
|
|
mockSkvRequestWithAuth
|
|
.mockResolvedValueOnce({ ok: true, status: 200 })
|
|
.mockResolvedValueOnce({ ok: true, status: 200 })
|
|
|
|
await probeCompanyGrants('company-1', ORG, 'user-1')
|
|
|
|
expect(mockRecordProbeResult).toHaveBeenCalledWith(
|
|
expect.objectContaining({
|
|
companyId: 'company-1',
|
|
orgNumber: ORG,
|
|
createdBy: 'user-1',
|
|
lasombud: expect.objectContaining({ status: 'granted' }),
|
|
momsOmbud: expect.objectContaining({ status: 'granted' }),
|
|
error: null,
|
|
})
|
|
)
|
|
})
|
|
})
|