Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SG-536] Implement bw-generate-password #3370

Merged
merged 8 commits into from
Aug 29, 2022
Merged
3 changes: 2 additions & 1 deletion apps/desktop/native-messaging-test-runner/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"status": "tsc && node dist/apps/desktop/native-messaging-test-runner/src/bw-status.js",
"retrieve": "tsc && node dist/apps/desktop/native-messaging-test-runner/src/bw-credential-retrieval.js",
"create": "tsc && node dist/apps/desktop/native-messaging-test-runner/src/bw-credential-create.js",
"update": "tsc && node dist/apps/desktop/native-messaging-test-runner/src/bw-credential-update.js"
"update": "tsc && node dist/apps/desktop/native-messaging-test-runner/src/bw-credential-update.js",
"generate": "tsc && node dist/apps/desktop/native-messaging-test-runner/src/bw-generate-password.js"
},
"author": "Bitwarden Inc. <hello@bitwarden.com> (https://bitwarden.com)",
"license": "GPL-3.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import "module-alias/register";
import yargs from "yargs";
import { hideBin } from "yargs/helpers";

import { NativeMessagingVersion } from "@bitwarden/common/enums/nativeMessagingVersion";

import { CredentialCreatePayload } from "../../src/models/nativeMessaging/credentialCreatePayload";

import { LogUtils } from "./logUtils";
Expand All @@ -19,9 +21,9 @@ const argv: any = yargs(hideBin(process.argv)).option("name", {
const { name } = argv;

(async () => {
const nativeMessageService = new NativeMessageService(1.0);
const nativeMessageService = new NativeMessageService(NativeMessagingVersion.One);
// Handshake
LogUtils.logWarning("Sending Handshake");
LogUtils.logInfo("Sending Handshake");
const handshakeResponse = await nativeMessageService.sendHandshake(config.testRsaPublicKey);

if (handshakeResponse.status !== "success") {
Expand All @@ -37,7 +39,7 @@ const { name } = argv;
if (activeUser === undefined) {
LogUtils.logError("No active or unlocked user");
}
LogUtils.logWarning("Active userId: " + activeUser.id);
LogUtils.logInfo("Active userId: " + activeUser.id);

LogUtils.logSuccess("Handshake success response");
const response = await nativeMessageService.credentialCreation(handshakeResponse.sharedKey, {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
/* eslint-disable no-console */

import "module-alias/register";

import yargs from "yargs";
import { hideBin } from "yargs/helpers";

import { NativeMessagingVersion } from "@bitwarden/common/enums/nativeMessagingVersion";

import { LogUtils } from "./logUtils";
import NativeMessageService from "./nativeMessageService";
import * as config from "./variables";
Expand All @@ -19,9 +19,9 @@ const argv: any = yargs(hideBin(process.argv)).option("uri", {
const { uri } = argv;

(async () => {
const nativeMessageService = new NativeMessageService(1.0);
const nativeMessageService = new NativeMessageService(NativeMessagingVersion.One);
// Handshake
LogUtils.logWarning("Sending Handshake");
LogUtils.logInfo("Sending Handshake");
const handshakeResponse = await nativeMessageService.sendHandshake(config.testRsaPublicKey);

if (handshakeResponse.status !== "success") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import "module-alias/register";
import yargs from "yargs";
import { hideBin } from "yargs/helpers";

import { NativeMessagingVersion } from "@bitwarden/common/enums/nativeMessagingVersion";

import { CredentialUpdatePayload } from "../../src/models/nativeMessaging/credentialUpdatePayload";

import { LogUtils } from "./logUtils";
Expand Down Expand Up @@ -38,9 +40,9 @@ const argv: any = yargs(hideBin(process.argv))
const { name, username, password, uri } = argv;

(async () => {
const nativeMessageService = new NativeMessageService(1.0);
const nativeMessageService = new NativeMessageService(NativeMessagingVersion.One);
// Handshake
LogUtils.logWarning("Sending Handshake");
LogUtils.logInfo("Sending Handshake");
const handshakeResponse = await nativeMessageService.sendHandshake(config.testRsaPublicKey);

if (handshakeResponse.status !== "success") {
Expand All @@ -57,7 +59,7 @@ const { name, username, password, uri } = argv;
if (activeUser === undefined) {
LogUtils.logError("No active or unlocked user");
}
LogUtils.logWarning("Active userId: " + activeUser.id);
LogUtils.logInfo("Active userId: " + activeUser.id);

const response = await nativeMessageService.credentialUpdate(handshakeResponse.sharedKey, {
name: name,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import "module-alias/register";

import yargs from "yargs";
import { hideBin } from "yargs/helpers";

import { NativeMessagingVersion } from "@bitwarden/common/enums/nativeMessagingVersion";

import { LogUtils } from "./logUtils";
import NativeMessageService from "./nativeMessageService";
import * as config from "./variables";

const argv: any = yargs(hideBin(process.argv)).option("userId", {
alias: "u",
demand: true,
describe: "UserId to generate password for",
type: "string",
}).argv;

const { userId } = argv;

(async () => {
const nativeMessageService = new NativeMessageService(NativeMessagingVersion.One);
// Handshake
LogUtils.logInfo("Sending Handshake");
const handshakeResponse = await nativeMessageService.sendHandshake(config.testRsaPublicKey);

if (handshakeResponse.status !== "success") {
LogUtils.logError("Handshake failed. Status was:", handshakeResponse.status);
nativeMessageService.disconnect();
return;
}

LogUtils.logSuccess("Handshake success response");
const response = await nativeMessageService.generatePassword(handshakeResponse.sharedKey, userId);

if (response.payload.error != null) {
LogUtils.logError("Error response returned: ", response.payload.error);
} else {
LogUtils.logSuccess("Response: ", response);
}

nativeMessageService.disconnect();
})();
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import "module-alias/register";

import { NativeMessagingVersion } from "@bitwarden/common/enums/nativeMessagingVersion";

import { LogUtils } from "./logUtils";
import NativeMessageService from "./nativeMessageService";
import * as config from "./variables";

(async () => {
const nativeMessageService = new NativeMessageService(1.0);
const nativeMessageService = new NativeMessageService(NativeMessagingVersion.One);

const response = await nativeMessageService.sendHandshake(config.testRsaPublicKey);
LogUtils.logSuccess("Received response to handshake request");
Expand Down
7 changes: 5 additions & 2 deletions apps/desktop/native-messaging-test-runner/src/bw-status.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import "module-alias/register";

import { NativeMessagingVersion } from "@bitwarden/common/enums/nativeMessagingVersion";

import { LogUtils } from "./logUtils";
import NativeMessageService from "./nativeMessageService";
import * as config from "./variables";

(async () => {
const nativeMessageService = new NativeMessageService(1.0);
const nativeMessageService = new NativeMessageService(NativeMessagingVersion.One);

LogUtils.logWarning("Sending Handshake");
LogUtils.logInfo("Sending Handshake");
const handshakeResponse = await nativeMessageService.sendHandshake(config.testRsaPublicKey);
LogUtils.logSuccess("Received response to handshake request");

Expand Down
27 changes: 14 additions & 13 deletions apps/desktop/native-messaging-test-runner/src/logUtils.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,29 @@
/* eslint-disable no-console */

// Class for logging messages with colors for ease of readin important info
// Class for logging messages with colors for ease of reading important info
// Reference: https://stackoverflow.com/a/41407246
export class LogUtils {
static logSuccess(message: string, payload?: any): void {
if (payload) {
console.log(`\x1b[32m ${message} \x1b[0m`, payload);
} else {
console.log(`\x1b[32m ${message} \x1b[0m`);
}
this.logFormat(message, "32", payload);
}

static logWarning(message: string, payload?: any): void {
if (payload) {
console.log(`\x1b[33m ${message} \x1b[0m`, payload);
} else {
console.log(`\x1b[33m ${message} \x1b[0m`);
}
this.logFormat(message, "33", payload);
}

static logError(message: string, payload?: any): void {
this.logFormat(message, "31", payload);
}

static logInfo(message: string, payload?: any): void {
this.logFormat(message, "36", payload);
}

private static logFormat(message: string, color: string, payload?: any) {
if (payload) {
console.log(`\x1b[31m ${message} \x1b[0m`, payload);
console.log(`\x1b[${color}m ${message} \x1b[0m`, payload);
} else {
console.log(`\x1b[31m ${message} \x1b[0m`);
console.log(`\x1b[${color}m ${message} \x1b[0m`);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import IPCService from "./ipcService";
import * as config from "./variables";

type HandshakePayload = {
status: "success" | "cancelled";
status: "success" | "canceled";
sharedKey?: string;
};

Expand Down Expand Up @@ -125,6 +125,23 @@ export default class NativeMessageService {
return this.decryptResponsePayload(response.encryptedPayload, key);
}

async generatePassword(key: string, userId: string): Promise<DecryptedCommandData> {
const encryptedCommand = await this.encryptCommandData(
{
command: "bw-generate-password",
payload: {
userId: userId,
},
},
key
);
const response = await this.sendEncryptedMessage({
encryptedCommand,
});

return this.decryptResponsePayload(response.encryptedPayload, key);
}

// Private message sending

private async sendEncryptedMessage(
Expand Down
2 changes: 2 additions & 0 deletions apps/desktop/src/app/services/services.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
LogService as LogServiceAbstraction,
} from "@bitwarden/common/abstractions/log.service";
import { MessagingService as MessagingServiceAbstraction } from "@bitwarden/common/abstractions/messaging.service";
import { PasswordGenerationService } from "@bitwarden/common/abstractions/passwordGeneration.service";
import { PasswordRepromptService as PasswordRepromptServiceAbstraction } from "@bitwarden/common/abstractions/passwordReprompt.service";
import { PlatformUtilsService as PlatformUtilsServiceAbstraction } from "@bitwarden/common/abstractions/platformUtils.service";
import { PolicyService as PolicyServiceAbstraction } from "@bitwarden/common/abstractions/policy/policy.service.abstraction";
Expand Down Expand Up @@ -161,6 +162,7 @@ const RELOAD_CALLBACK = new InjectionToken<() => any>("RELOAD_CALLBACK");
CipherServiceAbstraction,
PolicyServiceAbstraction,
MessagingServiceAbstraction,
PasswordGenerationService,
],
},
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export type UnencryptedMessageResponse = MessageCommon &
(
| {
payload: {
status: "cancelled";
status: "canceled";
};
}
| {
Expand All @@ -15,7 +15,7 @@ export type UnencryptedMessageResponse = MessageCommon &
}
| {
payload: {
error: "locked" | "cannot-decrypt";
error: "locked" | "cannot-decrypt" | "version-discrepancy";
};
}
);
Loading