-
Notifications
You must be signed in to change notification settings - Fork 187
Expand file tree
/
Copy pathmodule.ts
More file actions
563 lines (542 loc) · 15.3 KB
/
Copy pathmodule.ts
File metadata and controls
563 lines (542 loc) · 15.3 KB
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
import type { ScryptConfig } from '@adonisjs/hash/types'
import {
addComponentsDir,
addImports,
addPlugin,
addServerHandler,
addServerImportsDir,
addServerPlugin,
createResolver,
defineNuxtModule,
logger,
} from '@nuxt/kit'
import { defu } from 'defu'
import type { SessionConfig } from 'h3'
import { readFile, writeFile } from 'node:fs/promises'
import { join } from 'pathe'
import { randomUUID } from 'uncrypto'
import type { AtprotoProviderClientMetadata } from './runtime/types/atproto'
import { atprotoProviderDefaultClientMetadata, atprotoProviders, getClientMetadataFilename } from './runtime/utils/atproto'
// Module options TypeScript interface definition
export interface ModuleOptions {
/**
* Enable WebAuthn (Passkeys)
* @default false
*/
webAuthn?: boolean
/**
* Enable atproto OAuth (Bluesky, etc.)
* @default false
*/
atproto?: boolean
/**
* Hash options used for password hashing
*/
hash?: {
/**
* scrypt options used for password hashing
*/
scrypt?: ScryptConfig
}
/**
* Session load strategy
* @default 'server-first'
*/
loadStrategy?: 'server-first' | 'client-only' | 'none'
}
declare module 'nuxt/schema' {
interface RuntimeConfig {
hash: {
scrypt: ScryptConfig
}
/**
* Session configuration
*/
session: SessionConfig
}
interface PublicRuntimeConfig {
auth: {
loadStrategy: 'server-first' | 'client-only' | 'none'
}
}
}
export default defineNuxtModule<ModuleOptions>({
meta: {
name: 'auth-utils',
configKey: 'auth',
},
// Default configuration options of the Nuxt module
defaults: {
webAuthn: false,
atproto: false,
hash: {
scrypt: {},
},
loadStrategy: 'server-first',
},
async setup(options, nuxt) {
const resolver = createResolver(import.meta.url)
nuxt.options.alias['#auth-utils'] = resolver.resolve(
'./runtime/types/index',
)
const composables = [
{ name: 'useUserSession', from: resolver.resolve('./runtime/app/composables/session') },
]
if (options.webAuthn) {
composables.push({ name: 'useWebAuthn', from: resolver.resolve('./runtime/app/composables/webauthn') })
}
// App
addComponentsDir({ path: resolver.resolve('./runtime/app/components') })
addImports(composables)
if (options.loadStrategy !== 'none') {
addPlugin(resolver.resolve('./runtime/app/plugins/session.server'))
addPlugin(resolver.resolve('./runtime/app/plugins/session.client'))
}
// Server
addServerPlugin(resolver.resolve('./runtime/server/plugins/oauth'))
addServerImportsDir(resolver.resolve('./runtime/server/lib/oauth'))
if (nuxt.options.nitro?.experimental?.websocket) {
addServerPlugin(resolver.resolve('./runtime/server/plugins/ws'))
}
// WebAuthn enabled
if (options.webAuthn) {
// Check if dependencies are installed
const missingDeps: string[] = []
const peerDeps = ['@simplewebauthn/server', '@simplewebauthn/browser']
for (const pkg of peerDeps) {
await import(pkg).catch(() => {
missingDeps.push(pkg)
})
}
if (missingDeps.length > 0) {
logger.withTag('nuxt-auth-utils').error(`Missing dependencies for \`WebAuthn\`, please install with:\n\n\`npx nypm i ${missingDeps.join(' ')}\``)
process.exit(1)
}
addServerImportsDir(resolver.resolve('./runtime/server/lib/webauthn'))
}
addServerImportsDir(resolver.resolve('./runtime/server/utils'))
addServerHandler({
handler: resolver.resolve('./runtime/server/api/session.delete'),
route: '/api/_auth/session',
method: 'delete',
})
addServerHandler({
handler: resolver.resolve('./runtime/server/api/session.get'),
route: '/api/_auth/session',
method: 'get',
})
// Set node:crypto as unenv external
nuxt.options.nitro.unenv ||= {}
// @ts-expect-error we can use external as array
nuxt.options.nitro.unenv.external ||= []
// @ts-expect-error see comment above
if (!nuxt.options.nitro.unenv.external.includes('node:crypto')) {
// @ts-expect-error see comment above
nuxt.options.nitro.unenv.external.push('node:crypto')
}
// Runtime Config
const runtimeConfig = nuxt.options.runtimeConfig
const envSessionPassword = `${
runtimeConfig.nitro?.envPrefix || 'NUXT_'
}SESSION_PASSWORD`
runtimeConfig.session = defu(runtimeConfig.session, {
name: 'nuxt-session',
password: '',
cookie: {
sameSite: 'lax',
},
}) as SessionConfig
runtimeConfig.hash = defu(runtimeConfig.hash, {
scrypt: options.hash?.scrypt,
})
// Generate the session password
if (nuxt.options.dev && !process.env[envSessionPassword]) {
// If the password is set in the runtime config, use it
if (nuxt.options.runtimeConfig.session.password) {
process.env[envSessionPassword] = nuxt.options.runtimeConfig.session.password
}
else {
const password = process.env[envSessionPassword] = randomUUID().replace(/-/g, '')
// Add it to .env
const envPath = join(nuxt.options.rootDir, '.env')
const envContent = await readFile(envPath, 'utf-8').catch(() => '')
if (!envContent.includes(envSessionPassword)) {
await writeFile(
envPath,
`${
envContent ? envContent + '\n' : envContent
}${envSessionPassword}=${password}`,
'utf-8',
)
}
}
}
// Load strategy
runtimeConfig.public.auth = defu(runtimeConfig.public.auth, {
loadStrategy: options.loadStrategy ?? 'server-first',
})
// WebAuthn settings
runtimeConfig.webauthn = defu(runtimeConfig.webauthn, {
register: {},
authenticate: {},
})
// OAuth settings
runtimeConfig.oauth = defu(runtimeConfig.oauth, {})
// Gitea OAuth
runtimeConfig.oauth.gitea = defu(runtimeConfig.oauth.gitea, {
clientId: '',
clientSecret: '',
redirectURL: '',
baseURL: '',
})
// Box OAuth
runtimeConfig.oauth.box = defu(runtimeConfig.oauth.box, {
clientId: '',
clientSecret: '',
redirectURL: '',
scope: [],
})
// GitHub OAuth
runtimeConfig.oauth.github = defu(runtimeConfig.oauth.github, {
clientId: '',
clientSecret: '',
redirectURL: '',
})
// GitLab OAuth
runtimeConfig.oauth.gitlab = defu(runtimeConfig.oauth.gitlab, {
clientId: '',
clientSecret: '',
redirectURL: '',
baseURL: 'https://gitlab.com',
})
// Spotify OAuth
runtimeConfig.oauth.spotify = defu(runtimeConfig.oauth.spotify, {
clientId: '',
clientSecret: '',
redirectURL: '',
})
// Google OAuth
runtimeConfig.oauth.google = defu(runtimeConfig.oauth.google, {
clientId: '',
clientSecret: '',
redirectURL: '',
})
// Twitch OAuth
runtimeConfig.oauth.twitch = defu(runtimeConfig.oauth.twitch, {
clientId: '',
clientSecret: '',
redirectURL: '',
})
// Auth0 OAuth
runtimeConfig.oauth.auth0 = defu(runtimeConfig.oauth.auth0, {
clientId: '',
clientSecret: '',
domain: '',
audience: '',
redirectURL: '',
})
// WorkOS OAuth
runtimeConfig.oauth.workos = defu(runtimeConfig.oauth.workos, {
clientId: '',
clientSecret: '',
connectionId: '',
screenHint: '',
redirectURL: '',
})
// Microsoft OAuth
runtimeConfig.oauth.microsoft = defu(runtimeConfig.oauth.microsoft, {
clientId: '',
clientSecret: '',
tenant: '',
scope: [],
authorizationURL: '',
tokenURL: '',
userURL: '',
redirectURL: '',
})
// Azure OAuth
runtimeConfig.oauth.azureb2c = defu(runtimeConfig.oauth.azureb2c, {
clientId: '',
policy: '',
tenant: '',
scope: [],
authorizationURL: '',
tokenURL: '',
userURL: '',
redirectURL: '',
})
// Discord OAuth
runtimeConfig.oauth.discord = defu(runtimeConfig.oauth.discord, {
clientId: '',
clientSecret: '',
redirectURL: '',
})
// Battle.net OAuth
runtimeConfig.oauth.battledotnet = defu(runtimeConfig.oauth.battledotnet, {
clientId: '',
clientSecret: '',
redirectURL: '',
})
// Atproto OAuth
for (const provider of atprotoProviders) {
runtimeConfig.oauth[provider] = defu(runtimeConfig.oauth[provider], atprotoProviderDefaultClientMetadata)
}
if (options.atproto) {
const missingDeps: string[] = []
const peerDeps = ['@atproto/oauth-client-node', '@atproto/api']
for (const pkg of peerDeps) {
await import(pkg).catch(() => {
missingDeps.push(pkg)
})
}
if (missingDeps.length > 0) {
logger.withTag('nuxt-auth-utils').error(`Missing dependencies for \`atproto\`, please install with:\n\n\`npx nypm i ${missingDeps.join(' ')}\``)
process.exit(1)
}
for (const provider of atprotoProviders) {
addServerHandler({
handler: resolver.resolve('./runtime/server/routes/atproto/client-metadata.json.get'),
route: '/' + getClientMetadataFilename(provider, runtimeConfig.oauth[provider] as AtprotoProviderClientMetadata),
method: 'get',
})
}
addServerImportsDir(resolver.resolve('./runtime/server/lib/atproto'))
}
// Keycloak OAuth
runtimeConfig.oauth.keycloak = defu(runtimeConfig.oauth.keycloak, {
clientId: '',
clientSecret: '',
serverUrl: '',
serverUrlInternal: '',
realm: '',
redirectURL: '',
})
// Linear OAuth
runtimeConfig.oauth.linear = defu(runtimeConfig.oauth.linear, {
clientId: '',
clientSecret: '',
redirectURL: '',
})
// LinkedIn OAuth
runtimeConfig.oauth.linkedin = defu(runtimeConfig.oauth.linkedin, {
clientId: '',
clientSecret: '',
redirectURL: '',
})
// Cognito OAuth
runtimeConfig.oauth.cognito = defu(runtimeConfig.oauth.cognito, {
clientId: '',
clientSecret: '',
region: '',
userPoolId: '',
redirectURL: '',
})
// Facebook OAuth
runtimeConfig.oauth.facebook = defu(runtimeConfig.oauth.facebook, {
clientId: '',
clientSecret: '',
redirectURL: '',
})
// Instagram OAuth
runtimeConfig.oauth.instagram = defu(runtimeConfig.oauth.instagram, {
clientId: '',
clientSecret: '',
redirectURL: '',
})
// PayPal OAuth
runtimeConfig.oauth.paypal = defu(runtimeConfig.oauth.paypal, {
clientId: '',
clientSecret: '',
redirectURL: '',
})
// Steam OAuth
runtimeConfig.oauth.steam = defu(runtimeConfig.oauth.steam, {
apiKey: '',
redirectURL: '',
})
// X OAuth
runtimeConfig.oauth.x = defu(runtimeConfig.oauth.x, {
clientId: '',
clientSecret: '',
redirectURL: '',
})
// XSUAA OAuth
runtimeConfig.oauth.xsuaa = defu(runtimeConfig.oauth.xsuaa, {
clientId: '',
clientSecret: '',
domain: '',
redirectURL: '',
})
// VK OAuth
runtimeConfig.oauth.vk = defu(runtimeConfig.oauth.vk, {
clientId: '',
clientSecret: '',
redirectURL: '',
})
// Yandex OAuth
runtimeConfig.oauth.yandex = defu(runtimeConfig.oauth.yandex, {
clientId: '',
clientSecret: '',
redirectURL: '',
})
// TikTok OAuth
runtimeConfig.oauth.tiktok = defu(runtimeConfig.oauth.tiktok, {
clientKey: '',
clientSecret: '',
redirectURL: '',
})
// Dropbox OAuth
runtimeConfig.oauth.dropbox = defu(runtimeConfig.oauth.dropbox, {
clientId: '',
clientSecret: '',
redirectURL: '',
})
// Polar OAuth
runtimeConfig.oauth.polar = defu(runtimeConfig.oauth.polar, {
clientId: '',
clientSecret: '',
redirectURL: '',
})
// Zitadel OAuth
runtimeConfig.oauth.zitadel = defu(runtimeConfig.oauth.zitadel, {
clientId: '',
clientSecret: '',
domain: '',
redirectURL: '',
})
// Authentik OAuth
runtimeConfig.oauth.authentik = defu(runtimeConfig.oauth.authentik, {
clientId: '',
clientSecret: '',
domain: '',
redirectURL: '',
})
// Seznam OAuth
runtimeConfig.oauth.seznam = defu(runtimeConfig.oauth.seznam, {
clientId: '',
clientSecret: '',
redirectURL: '',
})
// Strava OAuth
runtimeConfig.oauth.strava = defu(runtimeConfig.oauth.strava, {
clientId: '',
clientSecret: '',
redirectURL: '',
})
// Hubspot OAuth
runtimeConfig.oauth.hubspot = defu(runtimeConfig.oauth.hubspot, {
clientId: '',
clientSecret: '',
redirectURL: '',
})
// Line OAuth
runtimeConfig.oauth.line = defu(runtimeConfig.oauth.line, {
clientId: '',
clientSecret: '',
redirectURL: '',
})
// Atlassian OAuth
runtimeConfig.oauth.atlassian = defu(runtimeConfig.oauth.atlassian, {
clientId: '',
clientSecret: '',
redirectURL: '',
})
// Apple OAuth
runtimeConfig.oauth.apple = defu(runtimeConfig.oauth.apple, {
teamId: '',
keyId: '',
privateKey: '',
redirectURL: '',
clientId: '',
})
// Kick OAuth
runtimeConfig.oauth.kick = defu(runtimeConfig.oauth.kick, {
clientId: '',
clientSecret: '',
redirectURL: '',
})
// LiveChat OAuth
runtimeConfig.oauth.livechat = defu(runtimeConfig.oauth.livechat, {
clientId: '',
clientSecret: '',
})
// Salesforce OAuth
runtimeConfig.oauth.salesforce = defu(runtimeConfig.oauth.salesforce, {
clientId: '',
clientSecret: '',
redirectURL: '',
baseURL: '',
scope: '',
})
// Slack OAuth
runtimeConfig.oauth.slack = defu(runtimeConfig.oauth.slack, {
clientId: '',
clientSecret: '',
redirectURL: '',
scope: '',
})
// Heroku OAuth
runtimeConfig.oauth.heroku = defu(runtimeConfig.oauth.heroku, {
clientId: '',
clientSecret: '',
redirectURL: '',
scope: '',
})
// Roblox OAuth
runtimeConfig.oauth.roblox = defu(runtimeConfig.oauth.roblox, {
clientId: '',
clientSecret: '',
redirectURL: '',
scope: '',
})
// Okta OAuth
runtimeConfig.oauth.okta = defu(runtimeConfig.oauth.okta, {
clientId: '',
clientSecret: '',
domain: '',
audience: '',
scope: [],
redirectURL: '',
})
// Ory OAuth
runtimeConfig.oauth.ory = defu(runtimeConfig.oauth.ory, {
clientId: '',
clientSecret: '',
sdkURL: '',
redirectURL: '',
scope: [],
authorizationURL: '',
tokenURL: '',
userURL: '',
})
// Shopify Customer
runtimeConfig.oauth.shopifyCustomer = defu(runtimeConfig.oauth.shopifyCustomer, {
shopDomain: '',
clientId: '',
redirectURL: '',
scope: [],
})
// OIDC OAuth
runtimeConfig.oauth.oidc = defu(runtimeConfig.oauth.oidc, {
clientId: '',
clientSecret: '',
openidConfig: '',
redirectURL: '',
scope: [],
})
// osu! OAuth
runtimeConfig.oauth.osu = defu(runtimeConfig.oauth.osu, {
clientId: '',
clientSecret: '',
redirectURL: '',
scope: [],
})
// RiotGames OAuth
runtimeConfig.oauth.riotgames = defu(runtimeConfig.oauth.riotgames, {
clientId: '',
clientSecret: '',
redirectURL: '',
scope: [],
})
},
})