6d75b9a1bf
* feat: add BankID authentication via TIC Identity API Integrate BankID as a login/signup method using the TIC Identity API. Users can authenticate with BankID QR codes (desktop) or deep links (mobile), link BankID to existing accounts, and skip TOTP MFA when BankID is linked. Removes Step 0 (role choice) from onboarding for all users. Adds enrichment data support for pre-filling company details from Bolagsverket during signup. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * fix: address PR review — server-side rate limit, unlink clears MFA bypass - Add per-IP rate limit (5s cooldown) on /bankid/start to prevent unbounded billable TIC sessions from unauthenticated callers - Add /bankid/unlink endpoint that deletes bankid_identities AND clears app_metadata.bankid_linked so MFA enforcement resumes after unlink - Update BankIdSettings to call server-side unlink instead of client-side delete Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * fix: move rate limiter to module scope, add BankID logo and year-end skill Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
78 lines
2.8 KiB
TypeScript
78 lines
2.8 KiB
TypeScript
/**
|
|
* BankID authentication helpers.
|
|
*
|
|
* BankID is only available on the hosted deployment (requires TIC Identity API).
|
|
* Self-hosted deployments never show the BankID option.
|
|
*/
|
|
|
|
import crypto from 'crypto'
|
|
|
|
const ALGORITHM = 'aes-256-gcm'
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Feature flag
|
|
// ---------------------------------------------------------------------------
|
|
|
|
export function isBankIdEnabled(): boolean {
|
|
if (process.env.NEXT_PUBLIC_SELF_HOSTED === 'true') return false
|
|
return process.env.NEXT_PUBLIC_BANKID_ENABLED === 'true'
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Personnummer hashing (for lookup)
|
|
// ---------------------------------------------------------------------------
|
|
|
|
/** SHA-256 hash of a personnummer for fast DB lookup. */
|
|
export function hashPersonalNumber(personalNumber: string): string {
|
|
return crypto.createHash('sha256').update(personalNumber).digest('hex')
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Personnummer encryption (for display in settings)
|
|
// ---------------------------------------------------------------------------
|
|
|
|
function getEncryptionKey(): Buffer {
|
|
const key = process.env.BANKID_ENCRYPTION_KEY
|
|
if (!key) throw new Error('BANKID_ENCRYPTION_KEY is required for BankID operations')
|
|
return Buffer.from(key, 'hex')
|
|
}
|
|
|
|
/** AES-256-GCM encrypt a personnummer for storage. */
|
|
export function encryptPersonalNumber(personalNumber: string): Buffer {
|
|
const key = getEncryptionKey()
|
|
const iv = crypto.randomBytes(12)
|
|
const cipher = crypto.createCipheriv(ALGORITHM, key, iv)
|
|
|
|
const encrypted = Buffer.concat([cipher.update(personalNumber, 'utf8'), cipher.final()])
|
|
const tag = cipher.getAuthTag()
|
|
|
|
// Format: iv (12) + tag (16) + ciphertext
|
|
return Buffer.concat([iv, tag, encrypted])
|
|
}
|
|
|
|
/** AES-256-GCM decrypt a stored personnummer. */
|
|
export function decryptPersonalNumber(data: Buffer): string {
|
|
const key = getEncryptionKey()
|
|
|
|
const iv = data.subarray(0, 12)
|
|
const tag = data.subarray(12, 28)
|
|
const encrypted = data.subarray(28)
|
|
|
|
const decipher = crypto.createDecipheriv(ALGORITHM, key, iv)
|
|
decipher.setAuthTag(tag)
|
|
|
|
return Buffer.concat([decipher.update(encrypted), decipher.final()]).toString('utf8')
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Display helpers
|
|
// ---------------------------------------------------------------------------
|
|
|
|
/** Mask a personnummer for display: "XXXXXXXX-1234" */
|
|
export function maskPersonalNumber(personalNumber: string): string {
|
|
if (personalNumber.length < 4) return '****'
|
|
const last4 = personalNumber.slice(-4)
|
|
const masked = personalNumber.length === 12 ? 'XXXXXXXX' : 'XXXXXX'
|
|
return `${masked}-${last4}`
|
|
}
|