update dependencies
[websub-hub] / bin / authUserAdd.js
1 #!/usr/bin/env node
2 /* eslint-disable node/shebang */
3 'use strict';
4
5 const readline = require('readline');
6 const stream = require('stream');
7 const { Authenticator } = require('@squeep/authentication-module');
8 const DB = require('../src/db');
9 const Logger = require('../src/logger');
10 const Config = require('../config');
11 const config = new Config(process.env.NODE_ENV);
12
13 const logger = new Logger(config);
14 const db = new DB(logger, config);
15 const authenticator = new Authenticator(logger, db, config);
16
17 const identifier = process.argv[2];
18
19 if (!identifier) {
20 console.log('missing user to add');
21 throw new Error('missing argument');
22 }
23
24 async function readPassword(prompt) {
25 const input = process.stdin;
26 const output = new stream.Writable({
27 write: function (chunk, encoding, callback) {
28 if (!this.muted) {
29 process.stdout.write(chunk, encoding);
30 }
31 callback();
32 },
33 });
34 const rl = readline.createInterface({ input, output, terminal: !!process.stdin.isTTY });
35 rl.setPrompt(prompt);
36 rl.prompt();
37 output.muted = true;
38
39 return new Promise((resolve) => {
40 rl.question('', (answer) => {
41 output.muted = false;
42 rl.close();
43 output.write('\n');
44 resolve(answer);
45 });
46 });
47 }
48
49 (async () => {
50 await db.initialize();
51 const password = await readPassword('password: ');
52 const credential = await authenticator.authn.argon2.hash(password, { type: authenticator.authn.argon2.argon2id });
53 console.log(`\t${identifier}:${credential}`);
54 await db.context(async (dbCtx) => {
55 const result = await db.authenticationUpsert(dbCtx, identifier, credential);
56 console.log(result);
57 });
58 console.log('done');
59 await db._closeConnection();
60 })();