From f9347fc2ba5c6a4466f5b4a6fd91bd5cc7e2776f Mon Sep 17 00:00:00 2001 From: Colton Hurst Date: Fri, 13 Sep 2024 16:10:15 -0400 Subject: [PATCH 1/2] SM-1425: Update api for napi to reflect consistency across other bindings --- crates/bitwarden-napi/README.md | 3 +- .../src-ts/bitwarden_client/index.ts | 46 ++++++++++++------- 2 files changed, 31 insertions(+), 18 deletions(-) diff --git a/crates/bitwarden-napi/README.md b/crates/bitwarden-napi/README.md index e9c3d0a71..a69c476d6 100644 --- a/crates/bitwarden-napi/README.md +++ b/crates/bitwarden-napi/README.md @@ -17,11 +17,12 @@ const settings: ClientSettings = { }; const accessToken = "-- REDACTED --"; +const stateFile = "some/path/to/state/file"; const client = new BitwardenClient(settings, LogLevel.Info); // Authenticating using a machine account access token -await client.accessTokenLogin(accessToken); +await client.loginAccessToken(accessToken, stateFile); // List secrets const secrets = await client.secrets().list(); diff --git a/crates/bitwarden-napi/src-ts/bitwarden_client/index.ts b/crates/bitwarden-napi/src-ts/bitwarden_client/index.ts index 52a53ef4f..3a3765a12 100644 --- a/crates/bitwarden-napi/src-ts/bitwarden_client/index.ts +++ b/crates/bitwarden-napi/src-ts/bitwarden_client/index.ts @@ -37,19 +37,6 @@ export class BitwardenClient { this.client = new rust.BitwardenClient(settingsJson, loggingLevel ?? LogLevel.Info); } - async accessTokenLogin(accessToken: string, stateFile?: string): Promise { - const response = await this.client.runCommand( - Convert.commandToJson({ - accessTokenLogin: { - accessToken, - stateFile, - }, - }), - ); - - handleResponse(Convert.toResponseForAccessTokenLoginResponse(response)); - } - secrets(): SecretsClient { return new SecretsClient(this.client); } @@ -57,6 +44,10 @@ export class BitwardenClient { projects(): ProjectsClient { return new ProjectsClient(this.client); } + + auth(): AuthClient { + return new AuthClient(this.client); + } } export class SecretsClient { @@ -91,11 +82,11 @@ export class SecretsClient { } async create( + organizationId: string, key: string, value: string, note: string, projectIds: string[], - organizationId: string, ): Promise { const response = await this.client.runCommand( Convert.commandToJson({ @@ -121,12 +112,12 @@ export class SecretsClient { } async update( + organizationId: string, id: string, key: string, value: string, note: string, projectIds: string[], - organizationId: string, ): Promise { const response = await this.client.runCommand( Convert.commandToJson({ @@ -183,7 +174,7 @@ export class ProjectsClient { return handleResponse(Convert.toResponseForProjectResponse(response)); } - async create(name: string, organizationId: string): Promise { + async create(organizationId: string, name: string): Promise { const response = await this.client.runCommand( Convert.commandToJson({ projects: { @@ -207,7 +198,7 @@ export class ProjectsClient { return handleResponse(Convert.toResponseForProjectsResponse(response)); } - async update(id: string, name: string, organizationId: string): Promise { + async update(organizationId: string, id: string, name: string): Promise { const response = await this.client.runCommand( Convert.commandToJson({ projects: { @@ -231,3 +222,24 @@ export class ProjectsClient { return handleResponse(Convert.toResponseForProjectsDeleteResponse(response)); } } + +export class AuthClient { + client: rust.BitwardenClient; + + constructor(client: rust.BitwardenClient) { + this.client = client; + } + + async loginAccessToken(accessToken: string, stateFile?: string): Promise { + const response = await this.client.runCommand( + Convert.commandToJson({ + loginAccessToken: { + accessToken, + stateFile, + }, + }), + ); + + handleResponse(Convert.toResponseForAccessTokenLoginResponse(response)); + } +} From 200112edcb9fcfa2754760914edc908937a86223 Mon Sep 17 00:00:00 2001 From: Colton Hurst Date: Fri, 13 Sep 2024 16:12:32 -0400 Subject: [PATCH 2/2] SM-1425: Add auth() to example in readme --- crates/bitwarden-napi/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/bitwarden-napi/README.md b/crates/bitwarden-napi/README.md index a69c476d6..6fce8d069 100644 --- a/crates/bitwarden-napi/README.md +++ b/crates/bitwarden-napi/README.md @@ -22,7 +22,7 @@ const stateFile = "some/path/to/state/file"; const client = new BitwardenClient(settings, LogLevel.Info); // Authenticating using a machine account access token -await client.loginAccessToken(accessToken, stateFile); +await client.auth().loginAccessToken(accessToken, stateFile); // List secrets const secrets = await client.secrets().list();