0
Fork 0
mirror of https://github.com/logto-io/logto.git synced 2024-12-30 20:33:54 -05:00

fix(core): fix session not found redirect hostname (#4018)

This commit is contained in:
wangsijie 2023-06-19 11:31:10 +09:00 committed by GitHub
parent 019cb6cadd
commit 160f861800
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 4 deletions

View file

@ -1,7 +1,8 @@
import { createMockUtils } from '@logto/shared/esm';
import Provider from 'oidc-provider';
import Sinon from 'sinon';
import { UserApps } from '#src/env-set/index.js';
import { EnvSet, UserApps } from '#src/env-set/index.js';
import { MockQueries } from '#src/test-utils/tenant.js';
import { createContextWithRouteParameters } from '#src/utils/test-utils.js';
@ -100,4 +101,18 @@ describe('koaSpaSessionGuard', () => {
await koaSpaSessionGuard(provider, queries)(ctx, next);
expect(ctx.redirect).toBeCalledWith('https://foo.bar');
});
it(`should redirect to current hostname if isDomainBasedMultiTenancy`, async () => {
const stub = Sinon.stub(EnvSet, 'values').value({
...EnvSet.values,
isDomainBasedMultiTenancy: true,
});
interactionDetails.mockRejectedValue(new Error('session not found'));
const ctx = createContextWithRouteParameters({
url: '/sign-in/foo',
});
await koaSpaSessionGuard(provider, queries)(ctx, next);
expect(ctx.redirect).toBeCalledWith('https://test.com/unknown-session');
stub.restore();
});
});

View file

@ -55,9 +55,15 @@ export default function koaSpaSessionGuard<
throw new RequestError({ code: 'session.not_found', status: 404 });
}
ctx.redirect(
appendPath(getTenantEndpoint(tenantId, EnvSet.values), sessionNotFoundPath).href
);
const tenantEndpoint = getTenantEndpoint(tenantId, EnvSet.values);
if (EnvSet.values.isDomainBasedMultiTenancy) {
// Replace to current hostname (if custom domain is used)
// eslint-disable-next-line @silverhand/fp/no-mutation
tenantEndpoint.hostname = ctx.request.hostname;
}
ctx.redirect(appendPath(tenantEndpoint, sessionNotFoundPath).href);
return;
}