Skip to content

Commit

Permalink
Merge pull request #29 from storyblok/EXT-2130-app-extension-auth
Browse files Browse the repository at this point in the history
fix: use region-helper instead of hardcoded region functions
  • Loading branch information
BibiSebi authored Jan 31, 2024
2 parents 02c3a2b + b5b82ab commit bad891f
Show file tree
Hide file tree
Showing 11 changed files with 21 additions and 58 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
},
"packageManager": "[email protected]",
"dependencies": {
"@storyblok/region-helper": "0.1.0",
"jsonwebtoken": "^9.0.0",
"openid-client": "^5.4.2"
},
Expand Down
2 changes: 1 addition & 1 deletion src/session/types/AppSession.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down
18 changes: 0 additions & 18 deletions src/session/types/Region.test.ts

This file was deleted.

5 changes: 0 additions & 5 deletions src/session/types/Region.ts

This file was deleted.

1 change: 0 additions & 1 deletion src/session/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,3 @@ export * from './AppSessionCookieStoreFactory'
export * from './AppSessionKeys'
export * from './AppSession'
export * from './AppSessionStore'
export * from './Region'
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { numberFromString } from '../../../utils'
import { Region } from '../../../session'
import { URL } from 'url'
import { getRegion, Region } from '@storyblok/region-helper'

const spaceIdFromUrl = (url: string): number | undefined => {
const isRelativeUrl = !url.startsWith('http')
Expand All @@ -15,19 +15,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)
}
14 changes: 0 additions & 14 deletions src/storyblok-auth-api/handle-requests/oauthApiBaseUrl.ts

This file was deleted.

11 changes: 5 additions & 6 deletions src/storyblok-auth-api/handle-requests/openidClient.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { BaseClient, Issuer } from 'openid-client'
import { redirectUri } from './redirectUri'
import { AuthHandlerParams } from '../AuthHandlerParams'
import { oauthApiBaseUrl } from './oauthApiBaseUrl'
import { Region } from '../../session'
import { getRegionUrl, Region } from '@storyblok/region-helper'

export type CreateOpenIdClient = (
params: Pick<
Expand All @@ -16,15 +15,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`
? `${getRegionUrl(region)}/oauth/token`
: undefined,
userinfo_endpoint:
typeof region !== 'undefined'
? `${oauthApiBaseUrl(region)}/user_info`
? `${getRegionUrl(region)}/oauth/user_info`
: undefined,
})
return new Client({
Expand Down
2 changes: 1 addition & 1 deletion src/storyblok-auth-api/refreshToken.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
8 changes: 8 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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.1.0
"@types/cookie": ^0.5.1
"@types/cookies": ^0.7.7
"@types/jest": ^27.4.1
Expand Down Expand Up @@ -1042,6 +1043,13 @@ __metadata:
languageName: unknown
linkType: soft

"@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

"@tootallnate/once@npm:1":
version: 1.1.2
resolution: "@tootallnate/once@npm:1.1.2"
Expand Down

0 comments on commit bad891f

Please sign in to comment.