2 /* eslint-disable node/shebang */
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
);
13 const logger
= new Logger(config
);
14 const db
= new DB(logger
, config
);
15 const authenticator
= new Authenticator(logger
, db
, config
);
17 const identifier
= process
.argv
[2];
20 console
.log('missing user to add');
21 throw new Error('missing argument');
24 async
function readPassword(prompt
) {
25 const input
= process
.stdin
;
26 const output
= new stream
.Writable({
27 write: function (chunk
, encoding
, callback
) {
29 process
.stdout
.write(chunk
, encoding
);
34 const rl
= readline
.createInterface({ input
, output
, terminal: !!process
.stdin
.isTTY
});
39 return new Promise((resolve
) => {
40 rl
.question('', (answer
) => {
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
);
59 await db
._closeConnection();