A local-first personal finance app
[AI] fix(enablebanking): fix HypoVereinsbank auth and data access (#8355)
cakallie821caa264304
diff --git a/packages/loot-core/src/server/accounts/sync.ts b/packages/loot-core/src/server/accounts/sync.tsindex 6adca336a6..c15278cd9e 100644--- a/packages/loot-core/src/server/accounts/sync.ts+++ b/packages/loot-core/src/server/accounts/sync.ts@@ -358,6 +358,7 @@ async function downloadAkahuTransactions(async function downloadEnableBankingTransactions(acctId: string,since: string,+ aspspName?: string,) {const userToken = await asyncStorage.getItem('user-token');if (!userToken) return;@@ -369,6 +370,7 @@ async function downloadEnableBankingTransactions({accountId: acctId,startDate: since,+ aspspName,},{'X-ACTUAL-TOKEN': userToken,@@ -1266,7 +1268,12 @@ export async function syncAccount(newAccount,);} else if (acctRow.account_sync_source === 'enableBanking') {- download = await downloadEnableBankingTransactions(acctId, syncStartDate);+ const bankRow = await db.select('banks', acctRow.bank);+ download = await downloadEnableBankingTransactions(+ acctId,+ syncStartDate,+ bankRow?.name,+ );} else {throw new Error(`Unrecognized bank-sync provider: ${acctRow.account_sync_source}`,diff --git a/packages/sync-server/src/app-enablebanking/app-enablebanking.ts b/packages/sync-server/src/app-enablebanking/app-enablebanking.tsindex bcf01efffd..3ae791f42c 100644--- a/packages/sync-server/src/app-enablebanking/app-enablebanking.ts+++ b/packages/sync-server/src/app-enablebanking/app-enablebanking.ts@@ -58,10 +58,25 @@ function extractPsuHeaders(req: Request): PsuHeaders {return headers;}+// Some ASPSPs reject PSU headers on balance/transaction data-access calls even+// though they require them during the authorization step. List their names here.+const ASPSPS_WITHOUT_DATA_ACCESS_PSU_HEADERS = new Set(['HypoVereinsbank']);++function dataAccessPsuHeaders(+ aspspName: string | undefined,+ psuHeaders: PsuHeaders | undefined,+): PsuHeaders | undefined {+ if (aspspName && ASPSPS_WITHOUT_DATA_ACCESS_PSU_HEADERS.has(aspspName)) {+ return undefined;+ }+ return psuHeaders;+}+async function buildSessionResult(session: EnableBankingSession,psuHeaders?: PsuHeaders,) {+ const accessHeaders = dataAccessPsuHeaders(session.aspsp?.name, psuHeaders);const accountsWithBalances = await Promise.all(session.accounts.map(async account => {const normalized = normalizeAccount(account, session.aspsp);@@ -70,7 +85,7 @@ async function buildSessionResult(try {const balanceResult = await enableBankingService.getBalances(account.uid,- psuHeaders,+ accessHeaders,);balances = balanceResult.balances.map(normalizeBalance);} catch (err) {@@ -309,6 +324,7 @@ app.post(state,typeof maxConsentValidity === 'number' ? maxConsentValidity : undefined,psuType === 'business' ? 'business' : 'personal',+ extractPsuHeaders(req),);res.send({@@ -491,7 +507,7 @@ app.post(app.post('/transactions',handleError(async (req: Request, res: Response) => {- const { accountId, startDate } = req.body || {};+ const { accountId, startDate, aspspName } = req.body || {};if (!accountId || !startDate) {res.send({@@ -504,8 +520,6 @@ app.post(return;}- const psuHeaders = extractPsuHeaders(req);-try {const dateTo = new Date().toISOString().split('T')[0];const dateFrom =@@ -513,6 +527,11 @@ app.post(? startDate: new Date(startDate).toISOString().split('T')[0];+ const psuHeaders = dataAccessPsuHeaders(+ aspspName,+ extractPsuHeaders(req),+ );+// Fetch balancesconst balanceResult = await enableBankingService.getBalances(accountId,diff --git a/packages/sync-server/src/app-enablebanking/services/enablebanking-service.ts b/packages/sync-server/src/app-enablebanking/services/enablebanking-service.tsindex bfa69413b9..85c7353f17 100644--- a/packages/sync-server/src/app-enablebanking/services/enablebanking-service.ts+++ b/packages/sync-server/src/app-enablebanking/services/enablebanking-service.ts@@ -350,6 +350,7 @@ export const enableBankingService = {state: string,maxConsentValidity?: number,psuType: PsuType = 'personal',+ psuHeaders?: PsuHeaders,): Promise<EnableBankingAuthResponse> {const DEFAULT_CONSENT_DAYS = 90;const defaultMs = DEFAULT_CONSENT_DAYS * 24 * 60 * 60 * 1000;@@ -363,15 +364,23 @@ export const enableBankingService = {const validUntil = new Date(Date.now() + consentMs);- return request<EnableBankingAuthResponse>('POST', '/auth', {- aspsp: { name: aspsp.name, country: aspsp.country },- redirect_url: redirectUrl,- state,- access: {- valid_until: validUntil.toISOString(),+ return request<EnableBankingAuthResponse>(+ 'POST',+ '/auth',+ {+ aspsp: { name: aspsp.name, country: aspsp.country },+ redirect_url: redirectUrl,+ state,+ access: {+ valid_until: validUntil.toISOString(),+ balances: true,+ transactions: true,+ },+ psu_type: psuType,},- psu_type: psuType,- });+ undefined,+ psuHeaders,+ );},async createSession(code: string): Promise<EnableBankingSession> {diff --git a/upcoming-release-notes/8355.md b/upcoming-release-notes/8355.mdnew file mode 100644index 0000000000..53874da605--- /dev/null+++ b/upcoming-release-notes/8355.md@@ -0,0 +1,6 @@+---+category: Bugfix+authors: [cakallie]+---++Fix Enable Banking HypoVereinsbank authentication routing to personal banking portal and resolve data access errors