From cc5c5f4a2ee58207bbe326067c4c3eb344715b70 Mon Sep 17 00:00:00 2001 From: Nicola Molinari Date: Mon, 20 Dec 2021 09:08:21 +0100 Subject: [PATCH] refactor(cypress): expose oidc helper from appshell/ssr bundle (#2437) --- .changeset/giant-glasses-wash.md | 6 +++ .../application-shell/redirect-to-login.tsx | 2 +- .../has-cached-authentication-state.ts | 2 +- packages/application-shell/src/ssr/index.ts | 1 + .../helpers.ts => utils/oidc.ts} | 2 +- packages/cypress/package.json | 1 + packages/cypress/src/add-commands/index.ts | 2 +- packages/cypress/src/build-oidc-scope.ts | 38 ------------------- packages/cypress/src/constants.ts | 7 ---- yarn.lock | 1 + 10 files changed, 13 insertions(+), 49 deletions(-) create mode 100644 .changeset/giant-glasses-wash.md rename packages/application-shell/src/{components/authenticated/helpers.ts => utils/oidc.ts} (95%) delete mode 100644 packages/cypress/src/build-oidc-scope.ts diff --git a/.changeset/giant-glasses-wash.md b/.changeset/giant-glasses-wash.md new file mode 100644 index 0000000000..12bfb39467 --- /dev/null +++ b/.changeset/giant-glasses-wash.md @@ -0,0 +1,6 @@ +--- +'@commercetools-frontend/application-shell': patch +'@commercetools-frontend/cypress': patch +--- + +Expose helper function from `@commercetools-frontend/application-shell/ssr`, clean up duplicate in `@commercetools-frontend/cypress`. diff --git a/packages/application-shell/src/components/application-shell/redirect-to-login.tsx b/packages/application-shell/src/components/application-shell/redirect-to-login.tsx index ea0324f4c9..da45367be4 100644 --- a/packages/application-shell/src/components/application-shell/redirect-to-login.tsx +++ b/packages/application-shell/src/components/application-shell/redirect-to-login.tsx @@ -8,9 +8,9 @@ import { trimLeadingAndTrailingSlashes, } from '@commercetools-frontend/url-utils'; import { LOGOUT_REASONS } from '@commercetools-frontend/constants'; -import { buildOidcScope } from '../authenticated/helpers'; import useIsServedByProxy from '../../hooks/use-is-served-by-proxy'; import { OIDC_RESPONSE_TYPES } from '../../constants'; +import { buildOidcScope } from '../../utils/oidc'; import * as oidcStorage from '../../utils/oidc-storage'; import Redirector from '../redirector'; import { getMcOrigin } from './helpers'; diff --git a/packages/application-shell/src/components/authenticated/has-cached-authentication-state.ts b/packages/application-shell/src/components/authenticated/has-cached-authentication-state.ts index 175e4a3564..8d0e095c79 100644 --- a/packages/application-shell/src/components/authenticated/has-cached-authentication-state.ts +++ b/packages/application-shell/src/components/authenticated/has-cached-authentication-state.ts @@ -1,8 +1,8 @@ import type { ApplicationWindow } from '@commercetools-frontend/constants'; import { STORAGE_KEYS, OIDC_CLAIMS } from '../../constants'; +import { buildOidcScope } from '../../utils/oidc'; import * as oidcStorage from '../../utils/oidc-storage'; -import { buildOidcScope } from './helpers'; declare let window: ApplicationWindow; diff --git a/packages/application-shell/src/ssr/index.ts b/packages/application-shell/src/ssr/index.ts index dbbfe19ec3..fc97063556 100644 --- a/packages/application-shell/src/ssr/index.ts +++ b/packages/application-shell/src/ssr/index.ts @@ -1 +1,2 @@ export * from '../utils/formatters'; +export { buildOidcScope } from '../utils/oidc'; diff --git a/packages/application-shell/src/components/authenticated/helpers.ts b/packages/application-shell/src/utils/oidc.ts similarity index 95% rename from packages/application-shell/src/components/authenticated/helpers.ts rename to packages/application-shell/src/utils/oidc.ts index ad620784ba..027330522f 100644 --- a/packages/application-shell/src/components/authenticated/helpers.ts +++ b/packages/application-shell/src/utils/oidc.ts @@ -1,6 +1,6 @@ import type { ApplicationOidcForDevelopmentConfig } from '@commercetools-frontend/constants'; -import { OIDC_CLAIMS } from '../../constants'; +import { OIDC_CLAIMS } from '../constants'; type BuilOidcScopeOptions = { projectKey?: ApplicationOidcForDevelopmentConfig['initialProjectKey']; diff --git a/packages/cypress/package.json b/packages/cypress/package.json index 59aeaf7a49..5446adccb8 100644 --- a/packages/cypress/package.json +++ b/packages/cypress/package.json @@ -38,6 +38,7 @@ "@babel/runtime": "7.16.5", "@babel/runtime-corejs3": "7.16.5", "@commercetools-frontend/application-config": "20.12.1", + "@commercetools-frontend/application-shell": "20.12.2", "@manypkg/get-packages": "1.1.3", "uuid": "8.3.2" }, diff --git a/packages/cypress/src/add-commands/index.ts b/packages/cypress/src/add-commands/index.ts index 07ef217536..fff90e76cc 100644 --- a/packages/cypress/src/add-commands/index.ts +++ b/packages/cypress/src/add-commands/index.ts @@ -2,7 +2,7 @@ import type { ApplicationRuntimeConfig } from '@commercetools-frontend/applicati import { v4 as uuidv4 } from 'uuid'; import { STORAGE_KEYS, OIDC_RESPONSE_TYPES } from '../constants'; -import buildOidcScope from '../build-oidc-scope'; +import { buildOidcScope } from '@commercetools-frontend/application-shell/ssr'; // eslint-disable-next-line @typescript-eslint/no-explicit-any declare const Cypress: any; diff --git a/packages/cypress/src/build-oidc-scope.ts b/packages/cypress/src/build-oidc-scope.ts deleted file mode 100644 index 9185f2851c..0000000000 --- a/packages/cypress/src/build-oidc-scope.ts +++ /dev/null @@ -1,38 +0,0 @@ -import type { ApplicationOidcForDevelopmentConfig } from '@commercetools-frontend/constants'; - -import { OIDC_CLAIMS } from './constants'; - -type BuilOidcScopeOptions = { - projectKey?: ApplicationOidcForDevelopmentConfig['initialProjectKey']; - oAuthScopes?: ApplicationOidcForDevelopmentConfig['oAuthScopes']; - teamId?: ApplicationOidcForDevelopmentConfig['teamId']; -}; - -const buildOidcScope = (options: BuilOidcScopeOptions): string => { - const claims = []; - if (options.projectKey) { - claims.push(`${OIDC_CLAIMS.PROJECT_KEY}:${options.projectKey}`); - claims.push( - ...(options.oAuthScopes?.view ?? []).map( - (scope) => `${OIDC_CLAIMS.VIEW}:${scope}` - ) - ); - claims.push( - ...(options.oAuthScopes?.manage ?? []).map( - (scope) => `${OIDC_CLAIMS.MANAGE}:${scope}` - ) - ); - } - const teamId = options.teamId; - if (teamId) { - claims.push(`${OIDC_CLAIMS.TEAM_ID}:${teamId}`); - } - return [ - // This is required as per OIDC spec. - OIDC_CLAIMS.OPEN_ID, - // Custom claims - ...claims, - ].join(' '); -}; - -export default buildOidcScope; diff --git a/packages/cypress/src/constants.ts b/packages/cypress/src/constants.ts index b88446f61d..e9d2a01b13 100644 --- a/packages/cypress/src/constants.ts +++ b/packages/cypress/src/constants.ts @@ -5,10 +5,3 @@ export const STORAGE_KEYS = { } as const; export const OIDC_RESPONSE_TYPES = { ID_TOKEN: 'id_token' }; -export const OIDC_CLAIMS = { - OPEN_ID: 'openid', - PROJECT_KEY: 'project_key', - TEAM_ID: 'team_id', - VIEW: 'view', - MANAGE: 'manage', -}; diff --git a/yarn.lock b/yarn.lock index e0c7fb774b..e2bb1c7372 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2893,6 +2893,7 @@ __metadata: "@babel/runtime": 7.16.5 "@babel/runtime-corejs3": 7.16.5 "@commercetools-frontend/application-config": 20.12.1 + "@commercetools-frontend/application-shell": 20.12.2 "@manypkg/get-packages": 1.1.3 cypress: 8.7.0 uuid: 8.3.2