@leaguebrain/provenance 0.0.0-stage → 0.14.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +7 -0
- package/README.md +135 -2
- package/dist/context-C2cW461e.d.ts +84 -0
- package/dist/helpers/index.d.ts +35 -0
- package/dist/helpers/index.js +113 -0
- package/dist/helpers/index.js.map +1 -0
- package/dist/index-CPpCO-Oc.d.ts +103 -0
- package/dist/index.d.ts +210 -0
- package/dist/index.js +551 -0
- package/dist/index.js.map +1 -0
- package/dist/initiatives/index.d.ts +19 -0
- package/dist/initiatives/index.js +74 -0
- package/dist/initiatives/index.js.map +1 -0
- package/dist/providers/index.d.ts +2 -0
- package/dist/providers/index.js +377 -0
- package/dist/providers/index.js.map +1 -0
- package/dist/vite/index.d.ts +87 -0
- package/dist/vite/index.js +171 -0
- package/dist/vite/index.js.map +1 -0
- package/package.json +85 -3
- package/src/vite/sveltekit/runtime/js.d.ts +62 -0
- package/src/vite/sveltekit/runtime/js.js +412 -0
- package/src/vite/sveltekit/runtime/ts.ts +432 -0
|
@@ -0,0 +1,412 @@
|
|
|
1
|
+
/* eslint-disable */
|
|
2
|
+
/**
|
|
3
|
+
* This file was generated by 'vite-plugin-provenance'
|
|
4
|
+
*
|
|
5
|
+
* >> DO NOT EDIT THIS FILE MANUALLY <<
|
|
6
|
+
*/
|
|
7
|
+
import { redirect } from '@sveltejs/kit';
|
|
8
|
+
import { sequence } from '@sveltejs/kit/hooks';
|
|
9
|
+
|
|
10
|
+
import { o, s, c, r } from '@leaguebrain/provenance';
|
|
11
|
+
|
|
12
|
+
import { logStarter } from '@leaguebrain/provenance/helpers';
|
|
13
|
+
|
|
14
|
+
import { dev } from '$app/environment';
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* @template ProviderSession
|
|
18
|
+
* @template {ProviderSession} AppSession
|
|
19
|
+
*
|
|
20
|
+
* @param {import('@sveltejs/kit').RequestEvent} event
|
|
21
|
+
* @param {{
|
|
22
|
+
oauth: import('@leaguebrain/provenance').OAuthModule;
|
|
23
|
+
session: import('@leaguebrain/provenance').SessionModule<ProviderSession>;
|
|
24
|
+
routes: import('@leaguebrain/provenance').RoutesModule;
|
|
25
|
+
checks: import('@leaguebrain/provenance').ChecksModule<{ referrer?: string }>;
|
|
26
|
+
}} modules
|
|
27
|
+
* @param {{
|
|
28
|
+
logging: boolean;
|
|
29
|
+
getDomain?: (event: import('@sveltejs/kit').RequestEvent) => string | undefined;
|
|
30
|
+
}} config
|
|
31
|
+
* @returns {import('@leaguebrain/provenance').Context<ProviderSession, AppSession>} an auth object with handle to be used in \`hooks.server.ts\` and \`protectRoute\` to redirect to login from \`+page.server.ts\` load functions if user is not authenticated
|
|
32
|
+
*/
|
|
33
|
+
function createContext(event, modules, config) {
|
|
34
|
+
const isRoute = (/** @type {string} */ searchPathname) => {
|
|
35
|
+
const pathName = event.url.pathname;
|
|
36
|
+
|
|
37
|
+
return pathName.startsWith(searchPathname);
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
return {
|
|
41
|
+
oauth: {
|
|
42
|
+
processAuthResponse: (expectedState) => {
|
|
43
|
+
const url = event.url;
|
|
44
|
+
|
|
45
|
+
if (config.logging) {
|
|
46
|
+
logStarter('oauth:', 'processAuthResponse');
|
|
47
|
+
console.log('url:', url.href);
|
|
48
|
+
console.log('expectedState:', expectedState);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const authResponse = modules.oauth.processAuthResponse(url, expectedState);
|
|
52
|
+
|
|
53
|
+
if (config.logging) {
|
|
54
|
+
console.log('authResponse:', authResponse);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
return authResponse;
|
|
58
|
+
},
|
|
59
|
+
requestToken: async (codeVerifier, authorizationCode) => {
|
|
60
|
+
/**
|
|
61
|
+
* @param {URL} url
|
|
62
|
+
* @param {{
|
|
63
|
+
clientId: string;
|
|
64
|
+
clientSecret: string;
|
|
65
|
+
redirectUri: string;
|
|
66
|
+
}} params
|
|
67
|
+
* @returns {Promise<Response>}
|
|
68
|
+
*/
|
|
69
|
+
const fetchRequestToken = async (url, params) => {
|
|
70
|
+
const client_id = params.clientId;
|
|
71
|
+
const client_secret = params.clientSecret;
|
|
72
|
+
const redirect_uri = new URL(params.redirectUri, event.url.origin).toString();
|
|
73
|
+
const grant_type = 'authorization_code';
|
|
74
|
+
const code = authorizationCode;
|
|
75
|
+
const code_verifier = codeVerifier;
|
|
76
|
+
|
|
77
|
+
if (config.logging) {
|
|
78
|
+
console.log('client_id:', client_id);
|
|
79
|
+
console.log('client_secret:', client_secret);
|
|
80
|
+
console.log('redirect_uri:', redirect_uri);
|
|
81
|
+
console.log('grant_type:', grant_type);
|
|
82
|
+
console.log('code:', code);
|
|
83
|
+
console.log('code_verifier:', code_verifier);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
return await event.fetch(url, {
|
|
87
|
+
method: 'POST',
|
|
88
|
+
headers: {
|
|
89
|
+
'Content-Type': 'application/x-www-form-urlencoded',
|
|
90
|
+
Accept: 'application/json'
|
|
91
|
+
},
|
|
92
|
+
body: new URLSearchParams({
|
|
93
|
+
client_id: client_id,
|
|
94
|
+
client_secret: client_secret,
|
|
95
|
+
redirect_uri: redirect_uri,
|
|
96
|
+
grant_type: grant_type,
|
|
97
|
+
code: authorizationCode,
|
|
98
|
+
code_verifier: codeVerifier
|
|
99
|
+
})
|
|
100
|
+
});
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
if (config.logging) {
|
|
104
|
+
logStarter('oauth:', 'requestToken');
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
const tokenEndpointResponse = await modules.oauth.requestToken(fetchRequestToken);
|
|
108
|
+
|
|
109
|
+
if (config.logging) {
|
|
110
|
+
console.log('tokenEndpointResponse:', tokenEndpointResponse);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
return tokenEndpointResponse;
|
|
114
|
+
},
|
|
115
|
+
refresh: async (refreshToken) => {
|
|
116
|
+
/**
|
|
117
|
+
* @param {URL} url
|
|
118
|
+
* @param {{
|
|
119
|
+
client_id: string;
|
|
120
|
+
client_secret: string;
|
|
121
|
+
grant_type: 'refresh_token';
|
|
122
|
+
refresh_token: string;
|
|
123
|
+
}} body
|
|
124
|
+
* @returns {Promise<Response>}
|
|
125
|
+
*/
|
|
126
|
+
const fetchRefreshedToken = async (url, body) => {
|
|
127
|
+
if (config.logging) {
|
|
128
|
+
console.log('client_id:', body.client_id);
|
|
129
|
+
console.log('client_secret:', body.client_secret);
|
|
130
|
+
console.log('grant_type:', body.grant_type);
|
|
131
|
+
console.log('refresh_token:', body.refresh_token);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
return await event.fetch(url, {
|
|
135
|
+
method: 'POST',
|
|
136
|
+
headers: {
|
|
137
|
+
'Content-Type': 'application/x-www-form-urlencoded'
|
|
138
|
+
},
|
|
139
|
+
body: new URLSearchParams(body)
|
|
140
|
+
});
|
|
141
|
+
};
|
|
142
|
+
|
|
143
|
+
if (config.logging) {
|
|
144
|
+
logStarter('oauth:', 'refresh');
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
const tokenEndpointResponse = await modules.oauth.refresh(
|
|
148
|
+
fetchRefreshedToken,
|
|
149
|
+
refreshToken
|
|
150
|
+
);
|
|
151
|
+
|
|
152
|
+
if (config.logging) {
|
|
153
|
+
console.log('tokenEndpointResponse:', tokenEndpointResponse);
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
return tokenEndpointResponse;
|
|
157
|
+
},
|
|
158
|
+
referrer: event.url.searchParams.get('referrer'),
|
|
159
|
+
redirectLogin: async (referrer) => {
|
|
160
|
+
const origin = event.url.origin;
|
|
161
|
+
|
|
162
|
+
if (config.logging) {
|
|
163
|
+
logStarter('oauth:', 'redirectLogin');
|
|
164
|
+
console.log('origin:', origin);
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
return redirect(302, await modules.oauth.login(origin, referrer, event.cookies.set));
|
|
168
|
+
},
|
|
169
|
+
redirectSignup: async (referrer) => {
|
|
170
|
+
const origin = event.url.origin;
|
|
171
|
+
|
|
172
|
+
if (config.logging) {
|
|
173
|
+
logStarter('oauth:', 'redirectSignup');
|
|
174
|
+
console.log('origin:', origin);
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
return redirect(302, await modules.oauth.signup(origin, referrer, event.cookies.set));
|
|
178
|
+
},
|
|
179
|
+
preLogout: async (session) => {
|
|
180
|
+
/**
|
|
181
|
+
* @param {URL} url
|
|
182
|
+
* @param {URLSearchParams=} body
|
|
183
|
+
* @returns {Promise<Response>}
|
|
184
|
+
*/
|
|
185
|
+
const fetch = async (url, body) => {
|
|
186
|
+
if (config.logging) {
|
|
187
|
+
console.log('url:', url);
|
|
188
|
+
console.log('body:', body);
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
return await event.fetch(url, {
|
|
192
|
+
method: 'POST',
|
|
193
|
+
headers: {
|
|
194
|
+
'Content-Type': 'application/x-www-form-urlencoded'
|
|
195
|
+
},
|
|
196
|
+
body: body
|
|
197
|
+
});
|
|
198
|
+
};
|
|
199
|
+
|
|
200
|
+
if (config.logging) {
|
|
201
|
+
logStarter('oauth:', 'preLogout');
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
return await modules.oauth.logout(fetch, session);
|
|
205
|
+
}
|
|
206
|
+
},
|
|
207
|
+
checks: {
|
|
208
|
+
state: {
|
|
209
|
+
use: () => {
|
|
210
|
+
const state = modules.checks.state.use(event.cookies);
|
|
211
|
+
|
|
212
|
+
if (config.logging) {
|
|
213
|
+
logStarter('checks:', 'state:', 'use');
|
|
214
|
+
console.log('state:', state);
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
return state;
|
|
218
|
+
}
|
|
219
|
+
},
|
|
220
|
+
pkce: {
|
|
221
|
+
use: () => {
|
|
222
|
+
const pkce = modules.checks.pkce.use(event.cookies);
|
|
223
|
+
|
|
224
|
+
if (config.logging) {
|
|
225
|
+
logStarter('checks:', 'pkce:', 'use');
|
|
226
|
+
console.log('pkce:', pkce);
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
return pkce;
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
},
|
|
233
|
+
session: {
|
|
234
|
+
create: (tokens) => {
|
|
235
|
+
if (config.logging) {
|
|
236
|
+
logStarter('session:', 'create');
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
const session = modules.session.create(tokens);
|
|
240
|
+
|
|
241
|
+
if (config.logging) {
|
|
242
|
+
console.log('session:', session);
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
return session;
|
|
246
|
+
},
|
|
247
|
+
getCookie: () => {
|
|
248
|
+
if (config.logging) {
|
|
249
|
+
logStarter('session:', 'getCookie');
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
const cookie = modules.session.getCookie(event.cookies.getAll);
|
|
253
|
+
|
|
254
|
+
if (config.logging) {
|
|
255
|
+
console.log('cookie:', cookie);
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
return cookie;
|
|
259
|
+
},
|
|
260
|
+
setCookie: (session) => {
|
|
261
|
+
if (config.logging) {
|
|
262
|
+
logStarter('session:', 'setCookie');
|
|
263
|
+
console.log('session:', session);
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
const domain = config.getDomain?.(event);
|
|
267
|
+
|
|
268
|
+
modules.session.setCookie((name, value, maxAge) => {
|
|
269
|
+
if (config.logging) {
|
|
270
|
+
console.log('name:', name);
|
|
271
|
+
console.log('value:', value);
|
|
272
|
+
console.log('maxAge:', maxAge);
|
|
273
|
+
console.log('domain:', domain);
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
return event.cookies.set(name, value, {
|
|
277
|
+
path: '/',
|
|
278
|
+
maxAge,
|
|
279
|
+
domain
|
|
280
|
+
});
|
|
281
|
+
}, session);
|
|
282
|
+
},
|
|
283
|
+
deleteCookie: () => {
|
|
284
|
+
if (config.logging) {
|
|
285
|
+
logStarter('session:', 'deleteCookie');
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
const domain = config.getDomain?.(event);
|
|
289
|
+
|
|
290
|
+
modules.session.deleteCookie(event.cookies.getAll, (name) => {
|
|
291
|
+
if (config.logging) {
|
|
292
|
+
console.log('name:', name);
|
|
293
|
+
console.log('domain:', domain);
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
event.cookies.delete(name, {
|
|
297
|
+
path: '/',
|
|
298
|
+
domain
|
|
299
|
+
});
|
|
300
|
+
});
|
|
301
|
+
}
|
|
302
|
+
},
|
|
303
|
+
locals: {
|
|
304
|
+
// @ts-ignore
|
|
305
|
+
get session() {
|
|
306
|
+
return event.locals.session;
|
|
307
|
+
},
|
|
308
|
+
// @ts-ignore
|
|
309
|
+
set session(value) {
|
|
310
|
+
event.locals.session = value;
|
|
311
|
+
}
|
|
312
|
+
},
|
|
313
|
+
routes: {
|
|
314
|
+
redirect: (location) => {
|
|
315
|
+
return redirect(302, location);
|
|
316
|
+
},
|
|
317
|
+
redirectUri: {
|
|
318
|
+
is: isRoute(modules.routes.redirectUri.pathname)
|
|
319
|
+
},
|
|
320
|
+
login: {
|
|
321
|
+
redirect: () => {
|
|
322
|
+
const loginPathname = modules.routes.login.pathname;
|
|
323
|
+
|
|
324
|
+
const loginPath = `${loginPathname}?${new URLSearchParams({ referrer: event.url.pathname + event.url.search })}`;
|
|
325
|
+
|
|
326
|
+
return redirect(302, loginPath);
|
|
327
|
+
},
|
|
328
|
+
is: isRoute(modules.routes.login.pathname)
|
|
329
|
+
},
|
|
330
|
+
signup: {
|
|
331
|
+
redirect: () => {
|
|
332
|
+
const signupPathname = modules.routes.signup.pathname;
|
|
333
|
+
|
|
334
|
+
const signupPath = `${signupPathname}?${new URLSearchParams({ referrer: event.url.pathname + event.url.search })}`;
|
|
335
|
+
|
|
336
|
+
return redirect(302, signupPath);
|
|
337
|
+
},
|
|
338
|
+
is: isRoute(modules.routes.signup.pathname)
|
|
339
|
+
},
|
|
340
|
+
logout: {
|
|
341
|
+
redirect: () => {
|
|
342
|
+
const logoutPathname = modules.routes.logout.pathname;
|
|
343
|
+
|
|
344
|
+
const logoutPath = `${logoutPathname}?${new URLSearchParams({ referrer: event.url.pathname + event.url.search })}`;
|
|
345
|
+
|
|
346
|
+
return redirect(302, logoutPath);
|
|
347
|
+
},
|
|
348
|
+
is: isRoute(modules.routes.logout.pathname)
|
|
349
|
+
},
|
|
350
|
+
home: {
|
|
351
|
+
redirect: () => {
|
|
352
|
+
const homePath = modules.routes.home.pathname;
|
|
353
|
+
|
|
354
|
+
if (config.logging) {
|
|
355
|
+
logStarter('routes:', 'home:', 'redirect');
|
|
356
|
+
console.log('homePath:', homePath);
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
return redirect(302, homePath);
|
|
360
|
+
},
|
|
361
|
+
is: isRoute(modules.routes.home.pathname)
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
};
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
/**
|
|
368
|
+
* @template ProviderSession
|
|
369
|
+
*
|
|
370
|
+
* @param {import('@leaguebrain/provenance').Provider<ProviderSession>} provider configuration for your OAuth provider ([see](@leaguebrain/provenance/providers/index.ts))
|
|
371
|
+
* @param {import('./js').ProvenanceConfig=} config optional extra configuration options for provenance behaviour
|
|
372
|
+
* @returns an auth object with handle to be used in \`hooks.server.ts\` and \`protectRoute\` to redirect to login from \`+page.server.ts\` load functions if user is not authenticated
|
|
373
|
+
*/
|
|
374
|
+
export const provenance = (provider, config) => {
|
|
375
|
+
const defaultedOptions = {
|
|
376
|
+
redirectUriPathname: '/auth',
|
|
377
|
+
sessionCookieName: 'session',
|
|
378
|
+
loginPathname: '/login',
|
|
379
|
+
signupPathname: '/signup',
|
|
380
|
+
logoutPathname: '/logout',
|
|
381
|
+
refreshPathname: '/refresh',
|
|
382
|
+
homePathname: '/',
|
|
383
|
+
...config?.options
|
|
384
|
+
};
|
|
385
|
+
|
|
386
|
+
const defaultedConfig = {
|
|
387
|
+
logging: dev,
|
|
388
|
+
...config
|
|
389
|
+
};
|
|
390
|
+
|
|
391
|
+
const checks = c();
|
|
392
|
+
const routes = r(defaultedOptions);
|
|
393
|
+
const oauth = o({ checks }, provider, defaultedOptions);
|
|
394
|
+
const session = s(provider, defaultedOptions);
|
|
395
|
+
|
|
396
|
+
const handles = provider.resolvers.map((resolver) => {
|
|
397
|
+
/** @type import('@sveltejs/kit').Handle */
|
|
398
|
+
const handle = async ({ event, resolve }) => {
|
|
399
|
+
const context = createContext(event, { checks, oauth, session, routes }, defaultedConfig);
|
|
400
|
+
|
|
401
|
+
return await resolver(context, async () => await resolve(event), defaultedConfig.logging);
|
|
402
|
+
};
|
|
403
|
+
return handle;
|
|
404
|
+
});
|
|
405
|
+
|
|
406
|
+
return {
|
|
407
|
+
handle: sequence(...handles),
|
|
408
|
+
options: defaultedOptions,
|
|
409
|
+
createContext: (/** @type {import("@sveltejs/kit").RequestEvent} */ event) =>
|
|
410
|
+
createContext(event, { checks, oauth, session, routes }, defaultedConfig)
|
|
411
|
+
};
|
|
412
|
+
};
|