-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: extract origin config fetching logic
- Loading branch information
Showing
11 changed files
with
151 additions
and
95 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,14 @@ | ||
import {ApiHandler} from './handlers/apiHandler.js'; | ||
import {Fetcher} from './utils/fetcher.js'; | ||
|
||
/** | ||
* Creates default Monokle Fetcher instance. | ||
* | ||
* @deprecated Use createMonokleFetcherFromOrigin or createMonokleFetcherFromConfig instead which does not rely on hardcoded config. | ||
* | ||
* @param apiHandler | ||
* @returns Fetcher instance | ||
*/ | ||
export function createDefaultMonokleFetcher(apiHandler: ApiHandler = new ApiHandler()) { | ||
return new Fetcher(apiHandler); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 35 additions & 23 deletions
58
packages/synchronizer/src/createMonokleAuthenticatorFromOrigin.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,48 @@ | ||
import {ApiHandler} from './handlers/apiHandler.js'; | ||
import {DeviceFlowHandler} from './handlers/deviceFlowHandler.js'; | ||
import {StorageHandlerAuth} from './handlers/storageHandlerAuth.js'; | ||
import {Fetcher} from './utils/fetcher.js'; | ||
import {Authenticator} from './utils/authenticator.js'; | ||
import {DEFAULT_DEVICE_FLOW_ALG, DEFAULT_DEVICE_FLOW_CLIENT_SECRET, DEFAULT_ORIGIN} from './constants.js'; | ||
import {OriginConfig, fetchOriginConfig} from './handlers/configHandler.js'; | ||
|
||
export async function createMonokleAuthenticatorFromOrigin(authClientId: string, origin: string = DEFAULT_ORIGIN) { | ||
export async function createMonokleAuthenticatorFromOrigin( | ||
authClientId: string, | ||
origin: string = DEFAULT_ORIGIN, | ||
storageHandler: StorageHandlerAuth = new StorageHandlerAuth() | ||
) { | ||
try { | ||
const originConfig = await Fetcher.getOriginConfig(origin); | ||
const originConfig = await fetchOriginConfig(origin); | ||
|
||
if (!authClientId) { | ||
throw new Error(`No auth clientId provided.`); | ||
} | ||
return createMonokleAuthenticatorFromConfig(authClientId, originConfig, storageHandler); | ||
} catch (err: any) { | ||
throw err; | ||
} | ||
} | ||
|
||
if (!originConfig?.apiOrigin) { | ||
throw new Error(`No api origin found in origin config from ${origin}.`); | ||
} | ||
export function createMonokleAuthenticatorFromConfig( | ||
authClientId: string, | ||
config: OriginConfig, | ||
storageHandler: StorageHandlerAuth = new StorageHandlerAuth() | ||
) { | ||
if (!authClientId) { | ||
throw new Error(`No auth clientId provided.`); | ||
} | ||
|
||
if (!originConfig?.authOrigin) { | ||
throw new Error(`No auth origin found in origin config from ${origin}.`); | ||
} | ||
if (!config?.apiOrigin) { | ||
throw new Error(`No api origin found in origin config from ${origin}.`); | ||
} | ||
|
||
return new Authenticator( | ||
new StorageHandlerAuth(), | ||
new ApiHandler(originConfig.apiOrigin), | ||
new DeviceFlowHandler(originConfig.authOrigin, { | ||
client_id: authClientId, | ||
client_secret: DEFAULT_DEVICE_FLOW_CLIENT_SECRET, | ||
id_token_signed_response_alg: DEFAULT_DEVICE_FLOW_ALG, | ||
}) | ||
); | ||
} catch (err: any) { | ||
throw err; | ||
if (!config?.authOrigin) { | ||
throw new Error(`No auth origin found in origin config from ${origin}.`); | ||
} | ||
|
||
return new Authenticator( | ||
storageHandler, | ||
new ApiHandler(config), | ||
new DeviceFlowHandler(config.authOrigin, { | ||
client_id: authClientId, | ||
client_secret: DEFAULT_DEVICE_FLOW_CLIENT_SECRET, | ||
id_token_signed_response_alg: DEFAULT_DEVICE_FLOW_ALG, | ||
}) | ||
); | ||
} |
22 changes: 22 additions & 0 deletions
22
packages/synchronizer/src/createMonokleFetcherFromOrigin.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import {DEFAULT_ORIGIN} from './constants.js'; | ||
import {ApiHandler} from './handlers/apiHandler.js'; | ||
import {OriginConfig, fetchOriginConfig} from './handlers/configHandler.js'; | ||
import {Fetcher} from './utils/fetcher.js'; | ||
|
||
export async function createMonokleFetcherFromOrigin(origin: string = DEFAULT_ORIGIN) { | ||
try { | ||
const originConfig = await fetchOriginConfig(origin); | ||
|
||
return createMonokleFetcherFromConfig(originConfig); | ||
} catch (err: any) { | ||
throw err; | ||
} | ||
} | ||
|
||
export function createMonokleFetcherFromConfig(config: OriginConfig) { | ||
if (!config?.apiOrigin) { | ||
throw new Error(`No api origin found in origin config from ${origin}.`); | ||
} | ||
|
||
return new Fetcher(new ApiHandler(config)); | ||
} |
28 changes: 20 additions & 8 deletions
28
packages/synchronizer/src/createMonokleSynchronizerFromOrigin.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,32 @@ | ||
import {DEFAULT_ORIGIN} from './constants.js'; | ||
import {ApiHandler} from './handlers/apiHandler.js'; | ||
import {OriginConfig, fetchOriginConfig} from './handlers/configHandler.js'; | ||
import {GitHandler} from './handlers/gitHandler.js'; | ||
import {StorageHandlerPolicy} from './handlers/storageHandlerPolicy.js'; | ||
import {Fetcher} from './utils/fetcher.js'; | ||
import {Synchronizer} from './utils/synchronizer.js'; | ||
|
||
export async function createMonokleSynchronizerFromOrigin(origin: string = DEFAULT_ORIGIN) { | ||
export async function createMonokleSynchronizerFromOrigin( | ||
origin: string = DEFAULT_ORIGIN, | ||
storageHandler: StorageHandlerPolicy = new StorageHandlerPolicy(), | ||
gitHandler: GitHandler = new GitHandler() | ||
) { | ||
try { | ||
const originConfig = await Fetcher.getOriginConfig(origin); | ||
const originConfig = await fetchOriginConfig(origin); | ||
|
||
if (!originConfig?.apiOrigin) { | ||
throw new Error(`No api origin found in origin config from ${origin}.`); | ||
} | ||
|
||
return new Synchronizer(new StorageHandlerPolicy(), new ApiHandler(originConfig.apiOrigin), new GitHandler()); | ||
return createMonokleSynchronizerFromConfig(originConfig, storageHandler, gitHandler); | ||
} catch (err: any) { | ||
throw err; | ||
} | ||
} | ||
|
||
export function createMonokleSynchronizerFromConfig( | ||
config: OriginConfig, | ||
storageHandler: StorageHandlerPolicy = new StorageHandlerPolicy(), | ||
gitHandler: GitHandler = new GitHandler() | ||
) { | ||
if (!config?.apiOrigin) { | ||
throw new Error(`No api origin found in origin config from ${origin}.`); | ||
} | ||
|
||
return new Synchronizer(storageHandler, new ApiHandler(config), gitHandler); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import normalizeUrl from 'normalize-url'; | ||
import fetch from 'node-fetch'; | ||
|
||
export type OriginConfig = { | ||
origin: string; | ||
apiOrigin: string; | ||
authOrigin: string; | ||
[key: string]: string; | ||
}; | ||
|
||
export type CachedOriginConfig = { | ||
config: OriginConfig; | ||
downloadedAt: number; | ||
origin: string; | ||
}; | ||
|
||
let originConfigCache: CachedOriginConfig | undefined = undefined; | ||
|
||
export async function fetchOriginConfig(origin: string) { | ||
if (originConfigCache) { | ||
// Use recently fetched config if from same origin and it's less than 5 minutes old. | ||
if (origin === originConfigCache.origin && Date.now() - originConfigCache.downloadedAt < 1000 * 60 * 5) { | ||
return originConfigCache.config; | ||
} | ||
} | ||
|
||
try { | ||
const configUrl = normalizeUrl(`${origin}/config.js`); | ||
const response = await fetch(configUrl); | ||
const responseText = await response.text(); | ||
|
||
const values = Array.from(responseText.matchAll(/([A-Z_]+)\s*:\s*"(.*?)"/gm)).reduce( | ||
(acc: Record<string, string>, match) => { | ||
if (match[1] && match[2]) { | ||
acc[match[1]] = match[2]; | ||
} | ||
return acc; | ||
}, | ||
{} | ||
); | ||
|
||
if (values) { | ||
values.origin = normalizeUrl(origin); | ||
values.apiOrigin = values.API_ORIGIN; | ||
values.authOrigin = values.OIDC_DISCOVERY_URL; | ||
} | ||
|
||
originConfigCache = { | ||
config: values as OriginConfig, | ||
downloadedAt: Date.now(), | ||
origin, | ||
}; | ||
|
||
return values as OriginConfig; | ||
} catch (error: any) { | ||
// Rethrow error so integrations can catch it and propagate/react. | ||
throw error; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters