Some checks failed
App Prod-Parity E2E Dispatch / dispatch (push) Failing after 10s
CD Storybook UI / build (push) Failing after 3m25s
CD Storybook UI / dispatch (push) Has been skipped
CD deploy main / website-changed-files (push) Successful in 18m8s
CI Agent Skills / changed-files-check (push) Successful in 18m9s
CI Create App E2E minimal / changed-files-check (push) Successful in 24s
CI Create App / changed-files-check (push) Successful in 26s
CI Docs / changed-files-check (push) Successful in 26s
CI Emails / changed-files-check (push) Successful in 28s
CI Example App Hello World / changed-files-check (push) Successful in 23s
CI Front / changed-files-check (push) Has been skipped
CI Example App Postcard / changed-files-check (push) Successful in 27s
CI UI / changed-files-check (push) Has been skipped
CI Utils Test / changed-files-check (push) Successful in 45s
CI Twenty Apps / discover (push) Successful in 1m3s
CD deploy main / deploy-main (push) Failing after 3s
CI Agent Skills / agent-skills-validate (push) Has been skipped
CI Create App / create-app-test (lint) (push) Has been skipped
CI Emails / emails-test (push) Has been skipped
CI Front / front-task (lingui:compile) (push) Has been skipped
CI Front / front-task (lint) (push) Has been skipped
CI Example App Postcard / example-app-postcard (push) Has been skipped
CI UI / ui-task (lint) (push) Has been skipped
CI UI / ui-task (pack-size) (push) Has been skipped
CI UI / ui-task (size) (push) Has been skipped
CI UI / ui-task (test) (push) Has been skipped
CI UI / ui-task (typecheck) (push) Has been skipped
CI Utils Test / utils-test (push) Has been skipped
CI Twenty Apps / ${{ matrix.app.name }} (push) Has been skipped
CI Emails / ci-emails-status-check (push) Successful in 2s
CI Example App Hello World / ci-example-app-hello-world-status-check (push) Successful in 2s
CI Example App Postcard / ci-example-app-postcard-status-check (push) Successful in 3s
CI UI / ui-sb-test (push) Has been skipped
CI Utils Test / ci-utils-test-status-check (push) Successful in 2s
CI Twenty Apps / ci-twenty-apps-status-check (push) Successful in 2s
CI UI / ci-ui-status-check (push) Successful in 2s
CI Desktop / desktop (push) Failing after 1m8s
Push docs to Crowdin / Push documentation to Crowdin (push) Failing after 55s
Push translations to Crowdin / Extract and upload translations (push) Failing after 5s
Push website translations to Crowdin / Extract and upload website translations (push) Failing after 5s
CI Create App / create-app-test (test) (push) Has been skipped
CI Create App / create-app-test (typecheck) (push) Has been skipped
CI Docs / docs-lint (push) Has been skipped
CI Example App Hello World / example-app-hello-world (push) Has been skipped
CI Front / front-task (lingui:extract) (push) Has been skipped
CI Front / front-task (test) (push) Has been skipped
CI Front / front-task (typecheck) (push) Has been skipped
CI UI / ui-sb-build (push) Failing after 1m32s
CI Twenty Apps / ${{ matrix.app.name }} (${{ matrix.server-source }}) (push) Has been skipped
CI Agent Skills / agent-skills-publish (push) Has been skipped
CI Create App / ci-create-app-status-check (push) Successful in 2s
CI Agent Skills / agent-skills-remote-source (push) Has been skipped
CI Create App E2E minimal / create-app-e2e-minimal (push) Has been cancelled
CI E2E Main / e2e-test (push) Has been cancelled
CI Front / front-sb-build (push) Has been cancelled
CI Front / front-build (push) Has been cancelled
CI Create App E2E minimal / ci-create-app-e2e-minimal-status-check (push) Has been cancelled
CI E2E Main / ci-e2e-main-status-check (push) Has been cancelled
CI E2E Main / notify-main-ci-failure (push) Has been cancelled
CI E2E Main / qa-scout-comment (push) Has been cancelled
CI Front / front-sb-test (push) Has been cancelled
CI Front / front-sb-screenshots (push) Has been cancelled
CI Front / ci-front-status-check (push) Has been cancelled
DPA Sub-Processor Sync / sync-subprocessors (push) Failing after 3s
AI Catalog Sync / sync-catalog (push) Failing after 3s
172 lines
5.2 KiB
TypeScript
172 lines
5.2 KiB
TypeScript
import crypto from 'crypto';
|
|
|
|
import { type DataSource } from 'typeorm';
|
|
import { getAuthTokensFromSsoExchangeToken } from 'test/integration/graphql/utils/get-auth-tokens-from-sso-exchange-token.util';
|
|
|
|
import { AppTokenType } from 'src/engine/core-modules/app-token/app-token.entity';
|
|
import { AuthExceptionCode } from 'src/engine/core-modules/auth/auth.exception';
|
|
import { USER_DATA_SEED_IDS } from 'src/engine/workspace-manager/dev-seeder/core/utils/seed-users.util';
|
|
import { AuthProviderEnum } from 'src/engine/core-modules/workspace/types/workspace.type';
|
|
|
|
const hashToken = (token: string) =>
|
|
crypto.createHash('sha256').update(token).digest('hex');
|
|
|
|
const expectUniformInvalidTokenError = (errors: { message: string }[]) => {
|
|
expect(errors).toHaveLength(1);
|
|
expect(errors[0]).toMatchObject({
|
|
message: 'Invalid SSO exchange token',
|
|
extensions: { subCode: AuthExceptionCode.INVALID_INPUT },
|
|
});
|
|
};
|
|
|
|
describe('SSO exchange token redemption (integration)', () => {
|
|
let dataSource: DataSource;
|
|
|
|
const seedSsoExchangeToken = async ({
|
|
expiresAt = new Date(Date.now() + 5 * 60 * 1000),
|
|
type = AppTokenType.SsoExchangeToken,
|
|
revokedAt = null,
|
|
}: {
|
|
expiresAt?: Date;
|
|
type?: AppTokenType;
|
|
revokedAt?: Date | null;
|
|
} = {}): Promise<string> => {
|
|
const plainToken = crypto.randomBytes(32).toString('hex');
|
|
|
|
await dataSource.query(
|
|
`INSERT INTO core."appToken" ("userId", "type", "value", "expiresAt", "revokedAt", "context")
|
|
VALUES ($1, $2, $3, $4, $5, $6)`,
|
|
[
|
|
USER_DATA_SEED_IDS.JANE,
|
|
type,
|
|
hashToken(plainToken),
|
|
expiresAt,
|
|
revokedAt,
|
|
JSON.stringify({ authProvider: AuthProviderEnum.Google }),
|
|
],
|
|
);
|
|
|
|
return plainToken;
|
|
};
|
|
|
|
const countRemainingRows = async (
|
|
plainToken: string,
|
|
type: AppTokenType = AppTokenType.SsoExchangeToken,
|
|
): Promise<number> => {
|
|
const rows = await dataSource.query(
|
|
`SELECT 1 FROM core."appToken" WHERE "value" = $1 AND "type" = $2`,
|
|
[hashToken(plainToken), type],
|
|
);
|
|
|
|
return rows.length;
|
|
};
|
|
|
|
beforeAll(() => {
|
|
dataSource = global.testDataSource;
|
|
});
|
|
|
|
it('should exchange a valid token for a token pair and consume it', async () => {
|
|
const plainToken = await seedSsoExchangeToken();
|
|
|
|
const { data } = await getAuthTokensFromSsoExchangeToken({
|
|
ssoExchangeToken: plainToken,
|
|
expectToFail: false,
|
|
});
|
|
|
|
expect(
|
|
data.getAuthTokensFromSSOExchangeToken.tokens
|
|
.accessOrWorkspaceAgnosticToken.token,
|
|
).toBeDefined();
|
|
expect(
|
|
data.getAuthTokensFromSSOExchangeToken.tokens.refreshToken.token,
|
|
).toBeDefined();
|
|
expect(await countRemainingRows(plainToken)).toBe(0);
|
|
});
|
|
|
|
it('should reject a second redemption of the same token', async () => {
|
|
const plainToken = await seedSsoExchangeToken();
|
|
|
|
await getAuthTokensFromSsoExchangeToken({
|
|
ssoExchangeToken: plainToken,
|
|
expectToFail: false,
|
|
});
|
|
|
|
const { errors } = await getAuthTokensFromSsoExchangeToken({
|
|
ssoExchangeToken: plainToken,
|
|
expectToFail: true,
|
|
});
|
|
|
|
expectUniformInvalidTokenError(errors);
|
|
});
|
|
|
|
it('should reject an expired token', async () => {
|
|
const plainToken = await seedSsoExchangeToken({
|
|
expiresAt: new Date(Date.now() - 1000),
|
|
});
|
|
|
|
const { errors } = await getAuthTokensFromSsoExchangeToken({
|
|
ssoExchangeToken: plainToken,
|
|
expectToFail: true,
|
|
});
|
|
|
|
expectUniformInvalidTokenError(errors);
|
|
expect(await countRemainingRows(plainToken)).toBe(0);
|
|
});
|
|
|
|
it('should reject a revoked token without consuming it', async () => {
|
|
const plainToken = await seedSsoExchangeToken({
|
|
revokedAt: new Date(),
|
|
});
|
|
|
|
const { errors } = await getAuthTokensFromSsoExchangeToken({
|
|
ssoExchangeToken: plainToken,
|
|
expectToFail: true,
|
|
});
|
|
|
|
expectUniformInvalidTokenError(errors);
|
|
expect(await countRemainingRows(plainToken)).toBe(1);
|
|
});
|
|
|
|
it('should not redeem an app token of another type sharing the same value', async () => {
|
|
const plainToken = await seedSsoExchangeToken({
|
|
type: AppTokenType.EmailVerificationToken,
|
|
});
|
|
|
|
const { errors } = await getAuthTokensFromSsoExchangeToken({
|
|
ssoExchangeToken: plainToken,
|
|
expectToFail: true,
|
|
});
|
|
|
|
expectUniformInvalidTokenError(errors);
|
|
expect(
|
|
await countRemainingRows(plainToken, AppTokenType.EmailVerificationToken),
|
|
).toBe(1);
|
|
});
|
|
|
|
it('should reject an unknown token', async () => {
|
|
const { errors } = await getAuthTokensFromSsoExchangeToken({
|
|
ssoExchangeToken: crypto.randomBytes(32).toString('hex'),
|
|
expectToFail: true,
|
|
});
|
|
|
|
expectUniformInvalidTokenError(errors);
|
|
});
|
|
|
|
it('should let exactly one of several concurrent redemptions succeed', async () => {
|
|
const plainToken = await seedSsoExchangeToken();
|
|
|
|
const responses = await Promise.all(
|
|
Array.from({ length: 5 }, () =>
|
|
getAuthTokensFromSsoExchangeToken({ ssoExchangeToken: plainToken }),
|
|
),
|
|
);
|
|
|
|
const succeeded = responses.filter(
|
|
(response) => response.data?.getAuthTokensFromSSOExchangeToken,
|
|
);
|
|
|
|
expect(succeeded).toHaveLength(1);
|
|
expect(await countRemainingRows(plainToken)).toBe(0);
|
|
});
|
|
});
|