diff --git a/package-lock.json b/package-lock.json index ad8d96f0b6..915751b418 100644 --- a/package-lock.json +++ b/package-lock.json @@ -65,6 +65,11 @@ "resolved": "https://registry.npmjs.org/commander/-/commander-2.15.1.tgz", "integrity": "sha512-VlfT9F3V0v+jr4yxPc5gg9s62/fIVWsd2Bk2iD435um1NlGMYdVCq+MjcXnhYq2icNOizHr1kK+5TI6H0Hy0ag==" }, + "deepmerge": { + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/deepmerge/-/deepmerge-2.1.0.tgz", + "integrity": "sha512-Q89Z26KAfA3lpPGhbF6XMfYAm3jIV3avViy6KOJ2JLzFbeWHOvPQUu5aSJIWXap3gDZC2y1eF5HXEPI2wGqgvw==" + }, "diff": { "version": "3.5.0", "resolved": "https://registry.npmjs.org/diff/-/diff-3.5.0.tgz", @@ -75,11 +80,21 @@ "resolved": "https://registry.npmjs.org/escape-string-regexp/-/escape-string-regexp-1.0.5.tgz", "integrity": "sha1-G2HAViGQqN/2rjuyzwIAyhMLhtQ=" }, + "graceful-fs": { + "version": "4.1.11", + "resolved": "https://registry.npmjs.org/graceful-fs/-/graceful-fs-4.1.11.tgz", + "integrity": "sha1-Dovf5NHduIVNZOBOp8AOKgJuVlg=" + }, "has-flag": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/has-flag/-/has-flag-3.0.0.tgz", "integrity": "sha1-tdRU3CGZriJWmfNGfloH87lVuv0=" }, + "imurmurhash": { + "version": "0.1.4", + "resolved": "https://registry.npmjs.org/imurmurhash/-/imurmurhash-0.1.4.tgz", + "integrity": "sha1-khi5srkoojixPcT7a21XbyMUU+o=" + }, "make-error": { "version": "1.3.4", "resolved": "https://registry.npmjs.org/make-error/-/make-error-1.3.4.tgz", @@ -105,6 +120,24 @@ } } }, + "node-fetch": { + "version": "2.1.2", + "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.1.2.tgz", + "integrity": "sha1-q4hOjn5X44qUR1POxwb3iNF2i7U=" + }, + "node-localstorage": { + "version": "1.3.1", + "resolved": "https://registry.npmjs.org/node-localstorage/-/node-localstorage-1.3.1.tgz", + "integrity": "sha512-NMWCSWWc6JbHT5PyWlNT2i8r7PgGYXVntmKawY83k/M0UJScZ5jirb61TLnqKwd815DfBQu+lR3sRw08SPzIaQ==", + "requires": { + "write-file-atomic": "1.3.4" + } + }, + "slide": { + "version": "1.1.6", + "resolved": "https://registry.npmjs.org/slide/-/slide-1.1.6.tgz", + "integrity": "sha1-VusCfWW00tzmyy4tMsTUr8nh1wc=" + }, "source-map": { "version": "0.6.1", "resolved": "https://registry.npmjs.org/source-map/-/source-map-0.6.1.tgz", @@ -119,6 +152,16 @@ "source-map": "0.6.1" } }, + "strip-bom": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/strip-bom/-/strip-bom-3.0.0.tgz", + "integrity": "sha1-IzTBjpx1n3vdVv3vfprj1YjmjtM=" + }, + "strip-json-comments": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/strip-json-comments/-/strip-json-comments-2.0.1.tgz", + "integrity": "sha1-PFMZQukIwml8DsNEhYwobHygpgo=" + }, "supports-color": { "version": "5.4.0", "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-5.4.0.tgz", @@ -142,11 +185,32 @@ "yn": "2.0.0" } }, + "tsconfig-paths": { + "version": "3.3.2", + "resolved": "https://registry.npmjs.org/tsconfig-paths/-/tsconfig-paths-3.3.2.tgz", + "integrity": "sha512-21VLRJIXGZDaZ0YVml12IXwSyEcluxvBZjS9xY5ZiKle0iF9NmNg7Hatb77FDg5Ahkas3dfvST8IDldMYpCH0g==", + "requires": { + "deepmerge": "2.1.0", + "minimist": "1.2.0", + "strip-bom": "3.0.0", + "strip-json-comments": "2.0.1" + } + }, "typescript": { "version": "2.8.3", "resolved": "https://registry.npmjs.org/typescript/-/typescript-2.8.3.tgz", "integrity": "sha512-K7g15Bb6Ra4lKf7Iq2l/I5/En+hLIHmxWZGq3D4DIRNFxMNV6j2SHSvDOqs2tGd4UvD/fJvrwopzQXjLrT7Itw==" }, + "write-file-atomic": { + "version": "1.3.4", + "resolved": "https://registry.npmjs.org/write-file-atomic/-/write-file-atomic-1.3.4.tgz", + "integrity": "sha1-+Aek8LHZ6ROuekgRLmzDrxmRtF8=", + "requires": { + "graceful-fs": "4.1.11", + "imurmurhash": "0.1.4", + "slide": "1.1.6" + } + }, "yn": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/yn/-/yn-2.0.0.tgz", diff --git a/package.json b/package.json index d10d369dd0..e21c91ec04 100644 --- a/package.json +++ b/package.json @@ -25,6 +25,9 @@ }, "dependencies": { "commander": "^2.15.1", + "node-fetch": "2.1.2", + "node-localstorage": "1.3.1", + "tsconfig-paths": "3.3.2", "ts-node": "6.0.3", "typescript": "^2.7.1" } diff --git a/src/bw.js b/src/bw.js index 77928a43ac..9652fe4813 100644 --- a/src/bw.js +++ b/src/bw.js @@ -1,4 +1,5 @@ #!/usr/bin/env node +require('tsconfig-paths').register(); require('ts-node').register(); require('./main.ts'); diff --git a/src/commands/login.command.ts b/src/commands/login.command.ts index 8bcd399d27..0107c9a088 100644 --- a/src/commands/login.command.ts +++ b/src/commands/login.command.ts @@ -9,7 +9,12 @@ export class LoginCommand { } - run(email: string, password: string, cmd: program.Command) { - + async run(email: string, password: string, cmd: program.Command) { + try { + const result = await this.authService.logIn(email, password); + console.log(result); + } catch (e) { + console.log(e); + } } } diff --git a/src/main.ts b/src/main.ts index fed6b82df4..de3890b0c3 100644 --- a/src/main.ts +++ b/src/main.ts @@ -2,8 +2,41 @@ import * as program from 'commander'; import { AuthService } from 'jslib/services/auth.service'; + import { LoginCommand } from './commands/login.command'; +import { CryptoService } from 'jslib/services/crypto.service'; +import { NodeCryptoFunctionService } from 'jslib/services/nodeCryptoFunction.service'; +import { NodeStorageService } from './services/nodeStorage.service'; +import { ApiService } from 'jslib/services/api.service'; +import { NodePlatformUtilsService } from './services/nodePlatformUtils.service'; +import { AppIdService } from 'jslib/services/appId.service'; +import { TokenService } from 'jslib/services/token.service'; +import { EnvironmentService } from 'jslib/services/environment.service'; +import { UserService } from 'jslib/services/user.service'; +import { ContainerService } from 'jslib/services/container.service'; +import { NodeMessagingService } from './services/nodeMessaging.service'; + +const platformUtilsService = new NodePlatformUtilsService(); +const cryptoFunctionService = new NodeCryptoFunctionService(); +const storageService = new NodeStorageService('./scratch'); +const cryptoService = new CryptoService(storageService, storageService, cryptoFunctionService); +const appIdService = new AppIdService(storageService); +const tokenService = new TokenService(storageService); +const messagingService = new NodeMessagingService(); +const apiService = new ApiService(tokenService, platformUtilsService, + (expired: boolean) => { }); +const environmentService = new EnvironmentService(apiService, storageService); +const userService = new UserService(tokenService, storageService); +const containerService = new ContainerService(cryptoService, platformUtilsService); +const authService = new AuthService(cryptoService, apiService, userService, tokenService, appIdService, + null, platformUtilsService, messagingService, true); + +containerService.attachToWindow(global); +environmentService.setUrlsFromStorage().then(() => { + // Do nothing +}); + program .version('1.0.0', '-v, --version'); @@ -12,9 +45,9 @@ program .description('Log into a Bitwarden user account.') .option('-m, --method ', '2FA method.') .option('-c, --code ', '2FA code.') - .action((email: string, password: string, cmd: program.Command) => { - const command = new LoginCommand(null); - command.run(email, password, cmd); + .action(async (email: string, password: string, cmd: program.Command) => { + const command = new LoginCommand(authService); + await command.run(email, password, cmd); }); program diff --git a/src/services/nodeMessaging.service.ts b/src/services/nodeMessaging.service.ts new file mode 100644 index 0000000000..5eacfd9143 --- /dev/null +++ b/src/services/nodeMessaging.service.ts @@ -0,0 +1,7 @@ +import { MessagingService } from 'jslib/abstractions/messaging.service'; + +export class NodeMessagingService implements MessagingService { + send(subscriber: string, arg: any = {}) { + // TODO + } +} diff --git a/src/services/nodePlatformUtils.service.ts b/src/services/nodePlatformUtils.service.ts index c7b031308e..fcd56ab85c 100644 --- a/src/services/nodePlatformUtils.service.ts +++ b/src/services/nodePlatformUtils.service.ts @@ -10,8 +10,8 @@ export class NodePlatformUtilsService implements PlatformUtilsService { private deviceCache: DeviceType = null; - constructor(private i18nService: I18nService, private isDesktopApp: boolean) { - this.identityClientId = 'cli'; + constructor() { + this.identityClientId = 'desktop'; // TODO: cli } getDevice(): DeviceType { diff --git a/src/services/nodeStorage.service.ts b/src/services/nodeStorage.service.ts index d9165f4447..28143ee402 100644 --- a/src/services/nodeStorage.service.ts +++ b/src/services/nodeStorage.service.ts @@ -1,15 +1,42 @@ import { StorageService } from 'jslib/abstractions/storage.service'; +import { Utils } from 'jslib/misc/utils'; + +let localStorage = Utils.isBrowser ? window.localStorage : null; export class NodeStorageService implements StorageService { + constructor(dirLocation: string) { + if (Utils.isNode && localStorage == null) { + const LocalStorage = require('node-localstorage').LocalStorage; + localStorage = new LocalStorage(dirLocation); + } + } + get(key: string): Promise { + const val = localStorage.getItem(key); + if (val != null) { + try { + const obj = JSON.parse(val); + if (obj != null && obj[key] != null) { + return Promise.resolve(obj[key] as T); + } + } catch{ } + } return Promise.resolve(null); } save(key: string, obj: any): Promise { + let val: string = null; + if (obj != null) { + const o: any = {}; + o[key] = obj; + val = JSON.stringify(o); + } + localStorage.setItem(key, val); return Promise.resolve(); } remove(key: string): Promise { + localStorage.removeItem(key); return Promise.resolve(); } } diff --git a/tsconfig.json b/tsconfig.json index e115538c82..b9e7524485 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -12,7 +12,10 @@ "jslib/*": [ "jslib/src/*" ] - } + }, + "typeRoots": [ + "node_modules/@types" + ] }, "exclude": [ "node_modules",