mirror of
https://github.com/bitwarden/browser
synced 2025-12-19 17:53:39 +00:00
setup storage service and log in
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
require('tsconfig-paths').register();
|
||||
require('ts-node').register();
|
||||
require('./main.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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
39
src/main.ts
39
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 <method>', '2FA method.')
|
||||
.option('-c, --code <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
|
||||
|
||||
7
src/services/nodeMessaging.service.ts
Normal file
7
src/services/nodeMessaging.service.ts
Normal file
@@ -0,0 +1,7 @@
|
||||
import { MessagingService } from 'jslib/abstractions/messaging.service';
|
||||
|
||||
export class NodeMessagingService implements MessagingService {
|
||||
send(subscriber: string, arg: any = {}) {
|
||||
// TODO
|
||||
}
|
||||
}
|
||||
@@ -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 {
|
||||
|
||||
@@ -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<T>(key: string): Promise<T> {
|
||||
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<any> {
|
||||
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<any> {
|
||||
localStorage.removeItem(key);
|
||||
return Promise.resolve();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user