From e43447370ab9457b6bac21dc6dca470e9a213d25 Mon Sep 17 00:00:00 2001 From: Bibiana Sebestianova Date: Mon, 29 Jan 2024 17:12:42 +0100 Subject: [PATCH 1/3] fix: use region-helper instead of hardcoded region functions --- package.json | 1 + src/session/types/Region.ts | 8 ++++++-- .../handleCallback/spaceIdFromUrl.ts | 13 +++---------- .../handle-requests/oauthApiBaseUrl.ts | 14 -------------- .../handle-requests/openidClient.ts | 10 +++++----- 5 files changed, 15 insertions(+), 31 deletions(-) delete mode 100644 src/storyblok-auth-api/handle-requests/oauthApiBaseUrl.ts diff --git a/package.json b/package.json index 34ff9b2..089ead8 100644 --- a/package.json +++ b/package.json @@ -30,6 +30,7 @@ }, "packageManager": "yarn@3.2.4", "dependencies": { + "@storyblok/region-helper": "0.0.1", "jsonwebtoken": "^9.0.0", "openid-client": "^5.4.2" }, diff --git a/src/session/types/Region.ts b/src/session/types/Region.ts index 94c719f..bb37757 100644 --- a/src/session/types/Region.ts +++ b/src/session/types/Region.ts @@ -1,5 +1,9 @@ // As in https://github.com/storyblok/storyblok-js-client/blob/main/src/sbHelpers.ts -export type Region = 'eu' | 'us' +export type Region = 'eu' | 'us' | 'ca' | 'cn' | 'ap' export const isRegion = (data: unknown): data is Region => - data === 'eu' || data === 'us' + data === 'eu' || + data === 'us' || + data === 'cn' || + data === 'ca' || + data === 'ap' diff --git a/src/storyblok-auth-api/handle-requests/handleCallback/spaceIdFromUrl.ts b/src/storyblok-auth-api/handle-requests/handleCallback/spaceIdFromUrl.ts index e54df8f..1a18d7b 100644 --- a/src/storyblok-auth-api/handle-requests/handleCallback/spaceIdFromUrl.ts +++ b/src/storyblok-auth-api/handle-requests/handleCallback/spaceIdFromUrl.ts @@ -1,6 +1,7 @@ import { numberFromString } from '../../../utils' import { Region } from '../../../session' import { URL } from 'url' +import { getRegion } from '@storyblok/region-helper' const spaceIdFromUrl = (url: string): number | undefined => { const isRelativeUrl = !url.startsWith('http') @@ -15,19 +16,11 @@ const spaceIdFromUrl = (url: string): number | undefined => { return numberFromString(spaceStr) } -const isEuSpace = (spaceId: number) => spaceId >= 0 && spaceId < 1000000 -const isUsSpace = (spaceId: number) => spaceId >= 1000000 && spaceId < 2000000 - export const regionFromUrl = (url: string): Region | undefined => { const spaceId = spaceIdFromUrl(url) if (typeof spaceId === 'undefined') { return undefined } - if (isEuSpace(spaceId)) { - return 'eu' - } - if (isUsSpace(spaceId)) { - return 'us' - } - return undefined + + return getRegion(spaceId) } diff --git a/src/storyblok-auth-api/handle-requests/oauthApiBaseUrl.ts b/src/storyblok-auth-api/handle-requests/oauthApiBaseUrl.ts deleted file mode 100644 index ca7404e..0000000 --- a/src/storyblok-auth-api/handle-requests/oauthApiBaseUrl.ts +++ /dev/null @@ -1,14 +0,0 @@ -import { Region } from '../../session' - -/** - * Given a region, returns the API base url for authenticating with oauth - * @param region - */ -export const oauthApiBaseUrl = (region: Region) => { - switch (region) { - case 'eu': - return 'https://app.storyblok.com/oauth' - case 'us': - return 'https://app.storyblok.com/v1_us/oauth' - } -} diff --git a/src/storyblok-auth-api/handle-requests/openidClient.ts b/src/storyblok-auth-api/handle-requests/openidClient.ts index b7d5863..8b462cb 100644 --- a/src/storyblok-auth-api/handle-requests/openidClient.ts +++ b/src/storyblok-auth-api/handle-requests/openidClient.ts @@ -1,8 +1,8 @@ import { BaseClient, Issuer } from 'openid-client' import { redirectUri } from './redirectUri' import { AuthHandlerParams } from '../AuthHandlerParams' -import { oauthApiBaseUrl } from './oauthApiBaseUrl' import { Region } from '../../session' +import { getRegionUrl } from '@storyblok/region-helper' export type CreateOpenIdClient = ( params: Pick< @@ -16,15 +16,15 @@ export const openidClient: CreateOpenIdClient = (params, region) => { const { clientId, clientSecret } = params const { Client } = new Issuer({ issuer: 'storyblok', - // This is always the eu endpoint, even for other regions - authorization_endpoint: `${oauthApiBaseUrl('eu')}/authorize`, + // TODO: at this point there is no region && the subdomains do not have the /oauth/authorize endpoint working at the moment that is why this endpoint is initially requested + authorization_endpoint: `https://app.storyblok.com/oauth/authorize`, token_endpoint: typeof region !== 'undefined' - ? `${oauthApiBaseUrl(region)}/token` + ? `https://${getRegionUrl(region)}/oauth/token` : undefined, userinfo_endpoint: typeof region !== 'undefined' - ? `${oauthApiBaseUrl(region)}/user_info` + ? `https://${getRegionUrl(region)}/oauth/user_info` : undefined, }) return new Client({ From baeb6e43abed21a69e0d14b9e532944800054679 Mon Sep 17 00:00:00 2001 From: Bibiana Sebestianova Date: Mon, 29 Jan 2024 17:29:22 +0100 Subject: [PATCH 2/3] fix: yarn lock --- yarn.lock | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/yarn.lock b/yarn.lock index e2d6520..1c7ac2d 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1011,6 +1011,7 @@ __metadata: "@rollup/plugin-commonjs": 21.0.1 "@rollup/plugin-json": 4.1.0 "@rollup/plugin-node-resolve": 13.1.3 + "@storyblok/region-helper": 0.0.1 "@types/cookie": ^0.5.1 "@types/cookies": ^0.7.7 "@types/jest": ^27.4.1 @@ -1042,6 +1043,13 @@ __metadata: languageName: unknown linkType: soft +"@storyblok/region-helper@npm:0.0.1": + version: 0.0.1 + resolution: "@storyblok/region-helper@npm:0.0.1" + checksum: 073fc6332290c5786e1d6332521e9a3640d048d45374b8cbbfb1ea4d370d3da093a74114ec1babcf9ab69562397b4334df85a2a417130259ba3c656c54518f6c + languageName: node + linkType: hard + "@tootallnate/once@npm:1": version: 1.1.2 resolution: "@tootallnate/once@npm:1.1.2" From b5b82abde416e02eaeff1c675de99740c4154680 Mon Sep 17 00:00:00 2001 From: Bibiana Sebestianova Date: Tue, 30 Jan 2024 16:25:07 +0100 Subject: [PATCH 3/3] fix: updated to latest region helper version --- package.json | 2 +- src/session/types/AppSession.ts | 2 +- src/session/types/Region.test.ts | 18 ------------------ src/session/types/Region.ts | 9 --------- src/session/types/index.ts | 1 - .../handleCallback/fetchAppSession.ts | 3 ++- .../handleCallback/spaceIdFromUrl.ts | 3 +-- .../handle-requests/openidClient.ts | 7 +++---- src/storyblok-auth-api/refreshToken.ts | 2 +- yarn.lock | 10 +++++----- 10 files changed, 14 insertions(+), 43 deletions(-) delete mode 100644 src/session/types/Region.test.ts delete mode 100644 src/session/types/Region.ts diff --git a/package.json b/package.json index 089ead8..9ba55d8 100644 --- a/package.json +++ b/package.json @@ -30,7 +30,7 @@ }, "packageManager": "yarn@3.2.4", "dependencies": { - "@storyblok/region-helper": "0.0.1", + "@storyblok/region-helper": "0.1.0", "jsonwebtoken": "^9.0.0", "openid-client": "^5.4.2" }, diff --git a/src/session/types/AppSession.ts b/src/session/types/AppSession.ts index caf21b0..06c0151 100644 --- a/src/session/types/AppSession.ts +++ b/src/session/types/AppSession.ts @@ -1,5 +1,5 @@ -import { isRegion, Region } from './Region' import { hasKey } from '../../utils' +import { isRegion, Region } from '@storyblok/region-helper' export type AppSession = { spaceId: number // primary key diff --git a/src/session/types/Region.test.ts b/src/session/types/Region.test.ts deleted file mode 100644 index c73437e..0000000 --- a/src/session/types/Region.test.ts +++ /dev/null @@ -1,18 +0,0 @@ -import { isRegion } from './Region' - -describe('Region', () => { - describe('validation', () => { - it('can be "eu"', () => { - expect(isRegion('eu')).toEqual(true) - }) - it('can be "us"', () => { - expect(isRegion('eu')).toEqual(true) - }) - it('cannot be anything else', () => { - expect(isRegion('de')).toEqual(false) - expect(isRegion('abc')).toEqual(false) - expect(isRegion(1)).toEqual(false) - expect(isRegion([])).toEqual(false) - }) - }) -}) diff --git a/src/session/types/Region.ts b/src/session/types/Region.ts deleted file mode 100644 index bb37757..0000000 --- a/src/session/types/Region.ts +++ /dev/null @@ -1,9 +0,0 @@ -// As in https://github.com/storyblok/storyblok-js-client/blob/main/src/sbHelpers.ts -export type Region = 'eu' | 'us' | 'ca' | 'cn' | 'ap' - -export const isRegion = (data: unknown): data is Region => - data === 'eu' || - data === 'us' || - data === 'cn' || - data === 'ca' || - data === 'ap' diff --git a/src/session/types/index.ts b/src/session/types/index.ts index 5e9c483..aa8b49a 100644 --- a/src/session/types/index.ts +++ b/src/session/types/index.ts @@ -2,4 +2,3 @@ export * from './AppSessionCookieStoreFactory' export * from './AppSessionKeys' export * from './AppSession' export * from './AppSessionStore' -export * from './Region' diff --git a/src/storyblok-auth-api/handle-requests/handleCallback/fetchAppSession.ts b/src/storyblok-auth-api/handle-requests/handleCallback/fetchAppSession.ts index c635be6..0e0c201 100644 --- a/src/storyblok-auth-api/handle-requests/handleCallback/fetchAppSession.ts +++ b/src/storyblok-auth-api/handle-requests/handleCallback/fetchAppSession.ts @@ -1,9 +1,10 @@ import { AuthHandlerParams } from '../../AuthHandlerParams' -import { AppSession, Region } from '../../../session' +import { AppSession } from '../../../session' import { openidClient } from '../openidClient' import { redirectUri } from '../redirectUri' import { isTokenSet } from './isTokenSet' import { isStoryblokRole, isUserInfo, Role } from '../../user-info' +import { Region } from '@storyblok/region-helper' export const fetchAppSession = async ( params: AuthHandlerParams, diff --git a/src/storyblok-auth-api/handle-requests/handleCallback/spaceIdFromUrl.ts b/src/storyblok-auth-api/handle-requests/handleCallback/spaceIdFromUrl.ts index 1a18d7b..7a025d7 100644 --- a/src/storyblok-auth-api/handle-requests/handleCallback/spaceIdFromUrl.ts +++ b/src/storyblok-auth-api/handle-requests/handleCallback/spaceIdFromUrl.ts @@ -1,7 +1,6 @@ import { numberFromString } from '../../../utils' -import { Region } from '../../../session' import { URL } from 'url' -import { getRegion } from '@storyblok/region-helper' +import { getRegion, Region } from '@storyblok/region-helper' const spaceIdFromUrl = (url: string): number | undefined => { const isRelativeUrl = !url.startsWith('http') diff --git a/src/storyblok-auth-api/handle-requests/openidClient.ts b/src/storyblok-auth-api/handle-requests/openidClient.ts index 8b462cb..c638a55 100644 --- a/src/storyblok-auth-api/handle-requests/openidClient.ts +++ b/src/storyblok-auth-api/handle-requests/openidClient.ts @@ -1,8 +1,7 @@ import { BaseClient, Issuer } from 'openid-client' import { redirectUri } from './redirectUri' import { AuthHandlerParams } from '../AuthHandlerParams' -import { Region } from '../../session' -import { getRegionUrl } from '@storyblok/region-helper' +import { getRegionUrl, Region } from '@storyblok/region-helper' export type CreateOpenIdClient = ( params: Pick< @@ -20,11 +19,11 @@ export const openidClient: CreateOpenIdClient = (params, region) => { authorization_endpoint: `https://app.storyblok.com/oauth/authorize`, token_endpoint: typeof region !== 'undefined' - ? `https://${getRegionUrl(region)}/oauth/token` + ? `${getRegionUrl(region)}/oauth/token` : undefined, userinfo_endpoint: typeof region !== 'undefined' - ? `https://${getRegionUrl(region)}/oauth/user_info` + ? `${getRegionUrl(region)}/oauth/user_info` : undefined, }) return new Client({ diff --git a/src/storyblok-auth-api/refreshToken.ts b/src/storyblok-auth-api/refreshToken.ts index 0a78207..404b335 100644 --- a/src/storyblok-auth-api/refreshToken.ts +++ b/src/storyblok-auth-api/refreshToken.ts @@ -1,7 +1,7 @@ import { hasKey } from '../utils' import { AuthHandlerParams } from './AuthHandlerParams' import { openidClient } from './handle-requests/openidClient' -import { Region } from '../session' +import { Region } from '@storyblok/region-helper' export type RefreshTokenWithFetchParams = Pick< AuthHandlerParams, diff --git a/yarn.lock b/yarn.lock index 1c7ac2d..728e6d1 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1011,7 +1011,7 @@ __metadata: "@rollup/plugin-commonjs": 21.0.1 "@rollup/plugin-json": 4.1.0 "@rollup/plugin-node-resolve": 13.1.3 - "@storyblok/region-helper": 0.0.1 + "@storyblok/region-helper": 0.1.0 "@types/cookie": ^0.5.1 "@types/cookies": ^0.7.7 "@types/jest": ^27.4.1 @@ -1043,10 +1043,10 @@ __metadata: languageName: unknown linkType: soft -"@storyblok/region-helper@npm:0.0.1": - version: 0.0.1 - resolution: "@storyblok/region-helper@npm:0.0.1" - checksum: 073fc6332290c5786e1d6332521e9a3640d048d45374b8cbbfb1ea4d370d3da093a74114ec1babcf9ab69562397b4334df85a2a417130259ba3c656c54518f6c +"@storyblok/region-helper@npm:0.1.0": + version: 0.1.0 + resolution: "@storyblok/region-helper@npm:0.1.0" + checksum: 602eb93f1081ed6e746751c1de4b24ff3c7d4b6aeb208449f5474c0bbf780e8257c46e892919b09374364597ab93a3084f903d6c8cda6f9a07e0d3df02e08f8b languageName: node linkType: hard