-
Notifications
You must be signed in to change notification settings - Fork 5
/
bl-login.js
executable file
·48 lines (41 loc) · 1.59 KB
/
bl-login.js
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
#!/usr/bin/env node
const fs = require('fs');
const commander = require('commander');
const request = require('request');
const colors = require('colors/safe');
const jwt = require('jsonwebtoken');
const timediff = require('datetime-difference');
const config = require('./config');
const util = require('./util');
const readlineSync = require('readline-sync');
commander
.option('ldap', 'login using ldap')
.option('--username <username>', 'your brainlife username (optional)')
.option('--password <password>', 'your brainlife password (optional)')
.option('--ttl <time to live>', 'set the amount of days before your token expires (default: 7)', 7)
.parse(process.argv);
async function login() {
let username = commander.username;
if(!username) username = readlineSync.question("username: ");
let password = commander.password;
if(!password) password = readlineSync.question("password: ", {hideEchoBack: true});
try {
const _jwt = await util.login({
ldap: commander.ldap,
ttl: commander.ttl,
username,
password,
});
let token = jwt.decode(_jwt);
let ttl = timediff(new Date(token.exp*1000), new Date());
let formattedTime = Object.keys(ttl).map(units => {
let time = ttl[units];
if (time == 0 || units == 'milliseconds') return '';
return time + " " + units;
}).filter(t => t.trim().length > 0).join(", ");
console.log("Successfully logged in for " + formattedTime);
} catch (err) {
console.error(err.toString());
}
}
login();