bitwarden-estensione-browser/apps/cli/src/commands/status.command.ts

55 lines
1.7 KiB
TypeScript
Raw Normal View History

import { AuthService } from "jslib-common/abstractions/auth.service";
2021-12-20 18:04:00 +01:00
import { EnvironmentService } from "jslib-common/abstractions/environment.service";
import { StateService } from "jslib-common/abstractions/state.service";
2021-12-20 18:04:00 +01:00
import { SyncService } from "jslib-common/abstractions/sync.service";
import { AuthenticationStatus } from "jslib-common/enums/authenticationStatus";
2021-12-20 18:04:00 +01:00
import { Response } from "jslib-node/cli/models/response";
2021-12-20 18:04:00 +01:00
import { TemplateResponse } from "../models/response/templateResponse";
export class StatusCommand {
2021-12-20 18:04:00 +01:00
constructor(
private envService: EnvironmentService,
private syncService: SyncService,
private stateService: StateService,
private authService: AuthService
2021-12-20 18:04:00 +01:00
) {}
async run(): Promise<Response> {
try {
const baseUrl = this.baseUrl();
const status = await this.status();
const lastSync = await this.syncService.getLastSync();
const userId = await this.stateService.getUserId();
const email = await this.stateService.getEmail();
2021-12-20 18:04:00 +01:00
return Response.success(
new TemplateResponse({
serverUrl: baseUrl,
lastSync: lastSync,
userEmail: email,
userId: userId,
status: status,
})
);
} catch (e) {
return Response.error(e);
}
2021-12-20 18:04:00 +01:00
}
2021-12-20 18:04:00 +01:00
private baseUrl(): string {
return this.envService.getUrls().base;
}
private async status(): Promise<"unauthenticated" | "locked" | "unlocked"> {
const authStatus = await this.authService.getAuthStatus();
if (authStatus === AuthenticationStatus.Unlocked) {
return "unlocked";
} else if (authStatus === AuthenticationStatus.Locked) {
return "locked";
} else {
2021-12-20 18:04:00 +01:00
return "unauthenticated";
}
2021-12-20 18:04:00 +01:00
}
}