-
Notifications
You must be signed in to change notification settings - Fork 74
/
Copy pathconfig.ts
275 lines (257 loc) · 7.93 KB
/
config.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/**
* Copyright Amazon.com, Inc. and its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"). You
* may not use this file except in compliance with the License. A copy of
* the License is located at
*
* http://aws.amazon.com/apache2.0/
*
* or in the "license" file accompanying this file. This file is
* distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
* ANY KIND, either express or implied. See the License for the specific
* language governing permissions and limitations under the License.
*/
interface Headers {
[key: string]: string;
}
export interface Config {
/**
* The Amazon Cognito IDP endpoint.
* Either provide just the region, e.g. "eu-west-1",
* or provide a full URL (e.g. if you are using a proxy API)
*/
cognitoIdpEndpoint: string;
/** The Amazon Cognito Client ID */
clientId: string;
/** The Amazon Cognito Client Secret (optional: don't use this in Web clients, use when running server side) */
clientSecret?: string;
/** The Amazon Cognito User Pool ID */
userPoolId?: string;
/** FIDO2 (WebAuthn) configuration */
fido2?: {
/** The base URL (i.e. the URL with path "/") of your FIDO2 API */
baseUrl: string;
/**
* FIDO2 authenticator selection criteria:
*
* - authenticatorAttachment: platform, or cross-platform
* - residentKey (aka Passkey, discoverable credential): discouraged, preferred, or required
* - userVerification: discouraged, preferred, or required
*/
authenticatorSelection?: AuthenticatorSelectionCriteria;
/** Configuration of the Relying Party */
rp?: {
name?: string;
id?: string;
};
/** FIDO2 Attestation Conveyance Preference you want to use */
attestation?: AttestationConveyancePreference;
/** FIDO2 extensions you want to use */
extensions?: AuthenticationExtensionsClientInputs;
/**
* FIDO2 timeout. This sets the timeout for native FIDO dialogs,
* i.e. when creating a new credential and when signing in
*/
timeout?: number;
};
/**
* Function that will be called with debug information,
* e.g. you can use `console.debug` here.
*/
debug?: (...args: unknown[]) => unknown;
/** The storage object to use. E.g. `localStorage` */
storage?: CustomStorage;
/**
* If you use a custom proxy in front of Amazon Cognito,
* you may want to pass additional HTTP headers.
*/
proxyApiHeaders?: Headers;
/**
* Overriding fetch implementation. Default: globalThis.fetch
*/
fetch?: MinimalFetch;
/**
* Overriding crypto implementation. Default: globalThis.crypto
*/
crypto?: MinimalCrypto;
/**
* Overriding location implementation. Default: globalThis.location
*/
location?: MinimalLocation;
/**
* Overriding history implementation. Default: globalThis.history
*/
history?: MinimalHistory;
}
export type ConfigWithDefaults = Config &
Required<
Pick<Config, "storage" | "crypto" | "fetch" | "location" | "history">
>;
type ConfigInput = Omit<Config, "cognitoIdpEndpoint"> &
Partial<Pick<Config, "cognitoIdpEndpoint">>;
let config_: ConfigWithDefaults | undefined = undefined;
export function configure(config?: ConfigInput) {
if (config) {
const cognitoIdpEndpoint =
config.cognitoIdpEndpoint || config.userPoolId?.split("_")[0];
if (!cognitoIdpEndpoint) {
throw new Error(
"Invalid configuration provided: either cognitoIdpEndpoint or userPoolId must be provided"
);
}
config_ = {
...config,
cognitoIdpEndpoint,
crypto: config.crypto ?? Defaults.crypto,
storage: config.storage ?? Defaults.storage,
fetch: config.fetch ?? Defaults.fetch,
location: config.location ?? Defaults.location,
history: config.history ?? Defaults.history,
};
config_.debug?.("Configuration loaded:", config);
} else {
if (!config_) {
throw new Error("Call configure(config) first");
}
}
return config_;
}
type Maybe<T> = T | undefined | null;
export interface CustomStorage {
getItem: (key: string) => Maybe<string> | Promise<Maybe<string>>;
setItem: (key: string, value: string) => void | Promise<void>;
removeItem: (key: string) => void | Promise<void>;
}
export function configureFromAmplify(
amplifyConfig: AmplifyAuthConfig | AmplifyConfig
) {
const { region, userPoolId, userPoolWebClientId } = isAmplifyConfig(
amplifyConfig
)
? amplifyConfig.Auth
: amplifyConfig;
if (typeof region !== "string") {
throw new Error(
"Invalid Amplify configuration provided: invalid or missing region"
);
}
if (typeof userPoolId !== "string") {
throw new Error(
"Invalid Amplify configuration provided: invalid or missing userPoolId"
);
}
if (typeof userPoolWebClientId !== "string") {
throw new Error(
"Invalid Amplify configuration provided: invalid or missing userPoolWebClientId"
);
}
configure({
cognitoIdpEndpoint: region,
userPoolId,
clientId: userPoolWebClientId,
});
return {
with: (
config: Omit<Config, "cognitoIdpEndpoint" | "userPoolId" | "clientId">
) => {
return configure({
cognitoIdpEndpoint: region,
userPoolId,
clientId: userPoolWebClientId,
...config,
});
},
};
}
interface AmplifyAuthConfig {
region?: unknown;
userPoolId?: unknown;
userPoolWebClientId?: unknown;
}
interface AmplifyConfig {
Auth: AmplifyAuthConfig;
}
function isAmplifyConfig(c: unknown): c is AmplifyConfig {
return !!c && typeof c === "object" && "Auth" in c;
}
class MemoryStorage {
private memory: Map<string, string>;
constructor() {
this.memory = new Map();
}
getItem(key: string) {
return this.memory.get(key);
}
setItem(key: string, value: string) {
this.memory.set(key, value);
}
removeItem(key: string) {
this.memory.delete(key);
}
}
export class UndefinedGlobalVariableError extends Error {}
class Defaults {
static getFailingProxy(expected: string) {
const message = `"${expected}" is not available as a global variable in your JavaScript runtime, so you must configure it explicitly with Passwordless.configure()`;
return new Proxy((() => undefined) as object, {
apply() {
throw new UndefinedGlobalVariableError(message);
},
get() {
throw new UndefinedGlobalVariableError(message);
},
});
}
static get storage() {
return typeof globalThis.localStorage !== "undefined"
? globalThis.localStorage
: new MemoryStorage();
}
static get crypto(): MinimalCrypto {
if (typeof globalThis.crypto !== "undefined") return globalThis.crypto;
return Defaults.getFailingProxy("crypto") as MinimalCrypto;
}
static get fetch(): MinimalFetch {
if (typeof globalThis.fetch !== "undefined") return globalThis.fetch;
return Defaults.getFailingProxy("fetch") as MinimalFetch;
}
static get location(): MinimalLocation {
if (typeof globalThis.location !== "undefined") return globalThis.location;
return Defaults.getFailingProxy("location") as MinimalLocation;
}
static get history(): MinimalHistory {
if (typeof globalThis.history !== "undefined") return globalThis.history;
return Defaults.getFailingProxy("history") as MinimalHistory;
}
}
export interface MinimalResponse {
ok: boolean;
json: () => Promise<unknown>;
}
export interface MinimalLocation {
href: string;
hostname: string;
}
export type MinimalFetch = (
input: string | URL,
init?:
| {
signal?: AbortSignal;
headers?: Record<string, string>;
method?: string;
body?: string;
}
| undefined
) => Promise<MinimalResponse>;
export interface MinimalHistory {
pushState(data: unknown, unused: string, url?: string | URL | null): void;
}
export interface MinimalCrypto {
getRandomValues: Crypto["getRandomValues"];
subtle: {
digest: Crypto["subtle"]["digest"];
importKey: Crypto["subtle"]["importKey"];
sign: Crypto["subtle"]["sign"];
};
}