|
| 1 | +import winston from 'winston'; |
1 | 2 | import { connectAndPrepare, reset } from '../modules/database';
|
2 | 3 | import { deactivateDevices, listDevices, ListDevicesOutput } from '../endpoints';
|
3 | 4 | import { askConfirmReset, epochTimestampToIso } from '../utils';
|
| 5 | +import { registerDevice } from '../modules/auth'; |
| 6 | +import { get2FAStatusUnauthenticated } from '../endpoints/get2FAStatusUnauthenticated'; |
4 | 7 |
|
5 | 8 | type OutputDevice = ListDevicesOutput['devices'][number] & {
|
6 | 9 | isCurrentDevice: boolean;
|
7 | 10 | };
|
8 | 11 |
|
9 | 12 | export async function listAllDevices(options: { json: boolean }) {
|
10 |
| - const { secrets, deviceConfiguration } = await connectAndPrepare({ autoSync: false }); |
| 13 | + const { secrets, deviceConfiguration, db } = await connectAndPrepare({ autoSync: false }); |
11 | 14 | if (!deviceConfiguration) {
|
12 | 15 | throw new Error('Require to be connected');
|
13 | 16 | }
|
14 | 17 | const listDevicesResponse = await listDevices({ secrets, login: deviceConfiguration.login });
|
| 18 | + db.close(); |
| 19 | + |
15 | 20 | const result: OutputDevice[] = listDevicesResponse.devices.map(
|
16 | 21 | (device) => <OutputDevice>{ ...device, isCurrentDevice: device.deviceId === secrets.accessKey }
|
17 | 22 | );
|
@@ -89,3 +94,41 @@ export async function removeAllDevices(devices: string[] | null, options: { all:
|
89 | 94 | }
|
90 | 95 | db.close();
|
91 | 96 | }
|
| 97 | + |
| 98 | +export const registerNonInteractiveDevice = async (deviceName: string, options: { json: boolean }) => { |
| 99 | + const { |
| 100 | + secrets: { login }, |
| 101 | + db, |
| 102 | + } = await connectAndPrepare({ autoSync: false }); |
| 103 | + |
| 104 | + const { type } = await get2FAStatusUnauthenticated({ login }); |
| 105 | + |
| 106 | + if (type === 'totp_login') { |
| 107 | + throw new Error("You can't register a non-interactive device when you have OTP at each login enabled."); |
| 108 | + } |
| 109 | + |
| 110 | + if (type === 'sso') { |
| 111 | + throw new Error("You can't register a non-interactive device when you are using SSO."); |
| 112 | + } |
| 113 | + |
| 114 | + const { deviceAccessKey, deviceSecretKey } = await registerDevice({ |
| 115 | + login, |
| 116 | + deviceName: `Non-Interactive - ${deviceName}`, |
| 117 | + }); |
| 118 | + |
| 119 | + if (options.json) { |
| 120 | + console.log( |
| 121 | + JSON.stringify({ |
| 122 | + DASHLANE_DEVICE_ACCESS_KEY: deviceAccessKey, |
| 123 | + DASHLANE_DEVICE_SECRET_KEY: deviceSecretKey, |
| 124 | + }) |
| 125 | + ); |
| 126 | + } else { |
| 127 | + winston.info('The device credentials have been generated, save and run the following commands to export them:'); |
| 128 | + console.log(`export DASHLANE_DEVICE_ACCESS_KEY=${deviceAccessKey}`); |
| 129 | + console.log(`export DASHLANE_DEVICE_SECRET_KEY=${deviceSecretKey}`); |
| 130 | + console.log(`export DASHLANE_MASTER_PASSWORD=<insert your master password here>`); |
| 131 | + } |
| 132 | + |
| 133 | + db.close(); |
| 134 | +}; |
0 commit comments