|
| 1 | +import Database from 'better-sqlite3'; |
| 2 | +import { Entry } from '@napi-rs/keyring'; |
| 3 | +import fs from 'fs'; |
| 4 | + |
| 5 | +import { connect, getDatabasePath } from '../modules/database/connect.js'; |
| 6 | +import { DeviceConfiguration } from '../types.js'; |
| 7 | +import { logger } from '../logger.js'; |
| 8 | + |
| 9 | +const SERVICE = 'dashlane-cli'; |
| 10 | + |
| 11 | +const isVaultLocked = (deviceConfiguration: DeviceConfiguration): boolean => { |
| 12 | + // If the master password should not be saved, the vault is always "locked" |
| 13 | + if (deviceConfiguration.shouldNotSaveMasterPassword) { |
| 14 | + return true; |
| 15 | + } |
| 16 | + |
| 17 | + // If no encrypted master password is stored in the DB, the vault is locked |
| 18 | + if (!deviceConfiguration.masterPasswordEncrypted) { |
| 19 | + return true; |
| 20 | + } |
| 21 | + |
| 22 | + // Try to retrieve the local key from the OS keychain |
| 23 | + try { |
| 24 | + const entry = new Entry(SERVICE, deviceConfiguration.login); |
| 25 | + const localKeyEncoded = entry.getPassword(); |
| 26 | + if (localKeyEncoded) { |
| 27 | + return false; |
| 28 | + } |
| 29 | + } catch { |
| 30 | + logger.error('Unable to access the keychain to determine vault lock status'); |
| 31 | + return true; |
| 32 | + } |
| 33 | + |
| 34 | + return true; |
| 35 | +}; |
| 36 | + |
| 37 | +export const runStatus = (): void => { |
| 38 | + const dbPath = getDatabasePath(); |
| 39 | + if (!fs.existsSync(dbPath)) { |
| 40 | + logger.content('Logged in: no'); |
| 41 | + return; |
| 42 | + } |
| 43 | + |
| 44 | + let db: Database.Database; |
| 45 | + try { |
| 46 | + db = connect(); |
| 47 | + } catch { |
| 48 | + logger.error('Unable to access the database to determine login status'); |
| 49 | + return; |
| 50 | + } |
| 51 | + |
| 52 | + try { |
| 53 | + const deviceConfiguration = db.prepare('SELECT * FROM device LIMIT 1').get() as DeviceConfiguration | undefined; |
| 54 | + |
| 55 | + if (!deviceConfiguration) { |
| 56 | + logger.content('Logged in: no'); |
| 57 | + return; |
| 58 | + } |
| 59 | + |
| 60 | + const locked = isVaultLocked(deviceConfiguration); |
| 61 | + |
| 62 | + logger.content(`Logged in: yes`); |
| 63 | + logger.content(`Login: ${deviceConfiguration.login}`); |
| 64 | + logger.content(`Locked: ${locked ? 'yes' : 'no'}`); |
| 65 | + } finally { |
| 66 | + db.close(); |
| 67 | + } |
| 68 | +}; |
0 commit comments