fix: Prevent error when switching accounts

This commit is contained in:
Joachim Van Herwegen 2024-02-14 07:41:17 +01:00
parent 137027e421
commit 68975e6627
2 changed files with 25 additions and 0 deletions

View File

@ -86,4 +86,12 @@ export async function forgetWebId(provider: Provider, oidcInteraction: Interacti
delete session.accountId;
await session.persist();
}
// If a client previously successfully completed an interaction a grant will have been created.
// If the same session gets reused to authenticate with a different WebID,
// we need to first delete the previous grant as the oidc-provider will try to reuse it.
if (oidcInteraction.grantId) {
const grant = await provider.Grant.find(oidcInteraction.grantId);
await grant?.destroy();
}
}

View File

@ -85,14 +85,31 @@ describe('InteractionUtil', (): void => {
persist: jest.fn(),
}),
},
Grant: {
find: jest.fn().mockResolvedValue({
destroy: jest.fn(),
}),
},
} as any;
});
it('removes the accountId from the session.', async(): Promise<void> => {
await expect(forgetWebId(provider, oidcInteraction)).resolves.toBeUndefined();
expect(provider.Session.find).toHaveBeenCalledTimes(1);
expect(provider.Session.find).toHaveBeenLastCalledWith('cookie');
const session = await (provider.Session.find as jest.Mock).mock.results[0].value;
expect(session.accountId).toBeUndefined();
expect(session.persist).toHaveBeenCalledTimes(1);
});
it('deletes the grant if there is one associated to the session.', async(): Promise<void> => {
delete oidcInteraction.session;
oidcInteraction.grantId = 'grantId';
await expect(forgetWebId(provider, oidcInteraction)).resolves.toBeUndefined();
expect(provider.Grant.find).toHaveBeenCalledTimes(1);
expect(provider.Grant.find).toHaveBeenLastCalledWith('grantId');
const grant = await (provider.Grant.find as jest.Mock).mock.results[0].value;
expect(grant.destroy).toHaveBeenCalledTimes(1);
});
});
});