forked from adonisjs/auth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
instructions.ts
463 lines (409 loc) · 11.5 KB
/
instructions.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
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
/*
* @adonisjs/auth
*
* (c) Harminder Virk <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
import { join } from 'path'
import * as sinkStatic from '@adonisjs/sink'
import { string } from '@poppinss/utils/build/helpers'
import { ApplicationContract } from '@ioc:Adonis/Core/Application'
type InstructionsState = {
modelName?: string
modelReference?: string
modelNamespace?: string
usersTableName: string
usersSchemaName: string
tokensTableName: string
tokensSchemaName: string
provider: 'lucid' | 'database'
tokensProvider: 'database' | 'redis'
guards: ('web' | 'api' | 'basic')[]
hasGuard: {
web: boolean
api: boolean
basic: boolean
}
}
// const USER_MIGRATION_TIME_PREFIX = '1587988332388'
// const TOKENS_MIGRATION_TIME_PREFIX = '1592489784670'
/**
* Base path to contract stub partials
*/
const CONTRACTS_PARTIALS_BASE = './contract/partials'
/**
* Base path to config stub partials
*/
const CONFIG_PARTIALS_BASE = './config/partials'
/**
* Prompt choices for the provider selection
*/
const PROVIDER_PROMPT_CHOICES = [
{
name: 'lucid' as const,
message: 'Lucid',
hint: ' (Uses Data Models)',
},
{
name: 'database' as const,
message: 'Database',
hint: ' (Uses Database QueryBuilder)',
},
]
/**
* Prompt choices for the guard selection
*/
const GUARD_PROMPT_CHOICES = [
{
name: 'web' as const,
message: 'Web',
hint: ' (Uses sessions for managing auth state)',
},
{
name: 'api' as const,
message: 'API tokens',
hint: ' (Uses database backed opaque tokens)',
},
{
name: 'basic' as const,
message: 'Basic Auth',
hint: ' (Uses HTTP Basic auth for authenticating requests)',
},
]
/**
* Prompt choices for the tokens provider selection
*/
const TOKENS_PROVIDER_PROMPT_CHOICES = [
{
name: 'database' as const,
message: 'Database',
hint: ' (Uses SQL table for storing API tokens)',
},
{
name: 'redis' as const,
message: 'Redis',
hint: ' (Uses Redis for storing API tokens)',
},
]
/**
* Returns absolute path to the stub relative from the templates
* directory
*/
function getStub(...relativePaths: string[]) {
return join(__dirname, 'templates', ...relativePaths)
}
/**
* Creates the model file
*/
function makeModel(
projectRoot: string,
app: ApplicationContract,
sink: typeof sinkStatic,
state: InstructionsState
) {
const modelsDirectory = app.resolveNamespaceDirectory('models') || 'app/Models'
const modelPath = join(modelsDirectory, `${state.modelName}.ts`)
const template = new sink.files.MustacheFile(projectRoot, modelPath, getStub('model.txt'))
if (template.exists()) {
sink.logger.action('create').skipped(`${modelPath} file already exists`)
return
}
template.apply(state).commit()
sink.logger.action('create').succeeded(modelPath)
}
/**
* Create the migration file
*/
function makeUsersMigration(
projectRoot: string,
app: ApplicationContract,
sink: typeof sinkStatic,
state: InstructionsState
) {
const migrationsDirectory = app.directoriesMap.get('migrations') || 'database'
const migrationPath = join(migrationsDirectory, `${Date.now()}_${state.usersTableName}.ts`)
const template = new sink.files.MustacheFile(
projectRoot,
migrationPath,
getStub('migrations/auth.txt')
)
if (template.exists()) {
sink.logger.action('create').skipped(`${migrationPath} file already exists`)
return
}
template.apply(state).commit()
sink.logger.action('create').succeeded(migrationPath)
}
/**
* Create the migration file
*/
function makeTokensMigration(
projectRoot: string,
app: ApplicationContract,
sink: typeof sinkStatic,
state: InstructionsState
) {
const migrationsDirectory = app.directoriesMap.get('migrations') || 'database'
const migrationPath = join(migrationsDirectory, `${Date.now()}_${state.tokensTableName}.ts`)
const template = new sink.files.MustacheFile(
projectRoot,
migrationPath,
getStub('migrations/api_tokens.txt')
)
if (template.exists()) {
sink.logger.action('create').skipped(`${migrationPath} file already exists`)
return
}
template.apply(state).commit()
sink.logger.action('create').succeeded(migrationPath)
}
/**
* Create the middleware(s)
*/
function makeMiddleware(
projectRoot: string,
app: ApplicationContract,
sink: typeof sinkStatic,
state: InstructionsState
) {
const middlewareDirectory = app.resolveNamespaceDirectory('middleware') || 'app/Middleware'
/**
* Auth middleware
*/
const authPath = join(middlewareDirectory, 'Auth.ts')
const authTemplate = new sink.files.MustacheFile(
projectRoot,
authPath,
getStub('middleware/Auth.txt')
)
if (authTemplate.exists()) {
sink.logger.action('create').skipped(`${authPath} file already exists`)
} else {
authTemplate.apply(state).commit()
sink.logger.action('create').succeeded(authPath)
}
/**
* Silent auth middleware
*/
const silentAuthPath = join(middlewareDirectory, 'SilentAuth.ts')
const silentAuthTemplate = new sink.files.MustacheFile(
projectRoot,
silentAuthPath,
getStub('middleware/SilentAuth.txt')
)
if (silentAuthTemplate.exists()) {
sink.logger.action('create').skipped(`${silentAuthPath} file already exists`)
} else {
silentAuthTemplate.apply(state).commit()
sink.logger.action('create').succeeded(silentAuthPath)
}
}
/**
* Creates the contract file
*/
function makeContract(
projectRoot: string,
app: ApplicationContract,
sink: typeof sinkStatic,
state: InstructionsState
) {
const contractsDirectory = app.directoriesMap.get('contracts') || 'contracts'
const contractPath = join(contractsDirectory, 'auth.ts')
const template = new sink.files.MustacheFile(
projectRoot,
contractPath,
getStub('contract/auth.txt')
)
template.overwrite = true
const partials: any = {
provider: getStub(CONTRACTS_PARTIALS_BASE, `user-provider-${state.provider}.txt`),
}
state.guards.forEach((guard) => {
partials[`${guard}_guard`] = getStub(CONTRACTS_PARTIALS_BASE, `${guard}-guard.txt`)
})
template.apply(state).partials(partials).commit()
sink.logger.action('create').succeeded(contractPath)
}
/**
* Makes the auth config file
*/
function makeConfig(
projectRoot: string,
app: ApplicationContract,
sink: typeof sinkStatic,
state: InstructionsState
) {
const configDirectory = app.directoriesMap.get('config') || 'config'
const configPath = join(configDirectory, 'auth.ts')
const template = new sink.files.MustacheFile(projectRoot, configPath, getStub('config/auth.txt'))
template.overwrite = true
const partials: any = {
provider: getStub(CONFIG_PARTIALS_BASE, `user-provider-${state.provider}.txt`),
token_provider: getStub(CONFIG_PARTIALS_BASE, `tokens-provider-${state.tokensProvider}.txt`),
}
state.guards.forEach((guard) => {
partials[`${guard}_guard`] = getStub(CONFIG_PARTIALS_BASE, `${guard}-guard.txt`)
})
template.apply(state).partials(partials).commit()
sink.logger.action('create').succeeded(configPath)
}
/**
* Prompts user to select the provider
*/
async function getProvider(sink: typeof sinkStatic) {
return sink.getPrompt().choice('Select provider for finding users', PROVIDER_PROMPT_CHOICES, {
validate(choice) {
return choice && choice.length ? true : 'Select the provider for finding users'
},
})
}
/**
* Prompts user to select the tokens provider
*/
async function getTokensProvider(sink: typeof sinkStatic) {
return sink
.getPrompt()
.choice('Select the provider for storing API tokens', TOKENS_PROVIDER_PROMPT_CHOICES, {
validate(choice) {
return choice && choice.length ? true : 'Select the provider for storing API tokens'
},
})
}
/**
* Prompts user to select one or more guards
*/
async function getGuard(sink: typeof sinkStatic) {
return sink
.getPrompt()
.multiple(
'Select which guard you need for authentication (select using space)',
GUARD_PROMPT_CHOICES,
{
validate(choices) {
return choices && choices.length
? true
: 'Select one or more guards for authenticating users'
},
}
)
}
/**
* Prompts user for the model name
*/
async function getModelName(sink: typeof sinkStatic): Promise<string> {
return sink.getPrompt().ask('Enter model name to be used for authentication', {
validate(value) {
return !!value.trim().length
},
})
}
/**
* Prompts user for the table name
*/
async function getTableName(sink: typeof sinkStatic): Promise<string> {
return sink.getPrompt().ask('Enter the database table name to look up users', {
validate(value) {
return !!value.trim().length
},
})
}
/**
* Prompts user for the table name
*/
async function getMigrationConsent(sink: typeof sinkStatic, tableName: string): Promise<boolean> {
return sink
.getPrompt()
.confirm(`Create migration for the ${sink.logger.colors.underline(tableName)} table?`)
}
/**
* Instructions to be executed when setting up the package.
*/
export default async function instructions(
projectRoot: string,
app: ApplicationContract,
sink: typeof sinkStatic
) {
const state: InstructionsState = {
usersTableName: '',
tokensTableName: 'api_tokens',
tokensSchemaName: 'ApiTokens',
usersSchemaName: '',
provider: 'lucid',
tokensProvider: 'database',
guards: [],
hasGuard: {
web: false,
api: false,
basic: false,
},
}
state.provider = await getProvider(sink)
state.guards = await getGuard(sink)
/**
* Need booleans for mustache templates
*/
state.guards.forEach((guard) => (state.hasGuard[guard] = true))
/**
* Make model when provider is lucid otherwise prompt for the database
* table name
*/
if (state.provider === 'lucid') {
const modelName = await getModelName(sink)
state.modelName = string.pascalCase(string.singularize(modelName.replace(/(\.ts|\.js)$/, '')))
state.usersTableName = string.pluralize(string.snakeCase(state.modelName))
state.modelReference = string.camelCase(string.singularize(state.modelName))
state.modelNamespace = `${app.namespacesMap.get('models') || 'App/Models'}/${state.modelName}`
} else {
state.usersTableName = await getTableName(sink)
}
const usersMigrationConsent = await getMigrationConsent(sink, state.usersTableName)
let tokensMigrationConsent = false
/**
* Only ask for the consent when using the api guard
*/
if (state.hasGuard.api) {
state.tokensProvider = await getTokensProvider(sink)
if (state.tokensProvider === 'database') {
tokensMigrationConsent = await getMigrationConsent(sink, state.tokensTableName)
}
}
/**
* Pascal case
*/
const camelCaseSchemaName = string.camelCase(`${state.usersTableName}_schema`)
state.usersSchemaName = `${camelCaseSchemaName
.charAt(0)
.toUpperCase()}${camelCaseSchemaName.slice(1)}`
/**
* Make model when prompted for it
*/
if (state.modelName) {
makeModel(projectRoot, app, sink, state)
}
/**
* Make users migration file
*/
if (usersMigrationConsent) {
makeUsersMigration(projectRoot, app, sink, state)
}
/**
* Make tokens migration file
*/
if (tokensMigrationConsent) {
makeTokensMigration(projectRoot, app, sink, state)
}
/**
* Make contract file
*/
makeContract(projectRoot, app, sink, state)
/**
* Make config file
*/
makeConfig(projectRoot, app, sink, state)
/**
* Make middleware
*/
makeMiddleware(projectRoot, app, sink, state)
}