Skip to content

Commit

Permalink
[SM-1425] Update Napi SDK (#1045)
Browse files Browse the repository at this point in the history
## 🎟️ Tracking

https://bitwarden.atlassian.net/browse/SM-1425

## 📔 Objective

Update the Napi SDK to include the SM updates.

## ⏰ Reminders before review

- Contributor guidelines followed
- All formatters and local linters executed and passed
- Written new unit and / or integration tests where applicable
- Protected functional changes with optionality (feature flags)
- Used internationalization (i18n) for all UI strings
- CI builds passed
- Communicated to DevOps any deployment requirements
- Updated any necessary documentation (Confluence, contributing docs) or
informed the documentation
  team

## 🦮 Reviewer guidelines

<!-- Suggested interactions but feel free to use (or not) as you desire!
-->

- 👍 (`:+1:`) or similar for great changes
- 📝 (`:memo:`) or ℹ️ (`:information_source:`) for notes or general info
- ❓ (`:question:`) for questions
- 🤔 (`:thinking:`) or 💭 (`:thought_balloon:`) for more open inquiry
that's not quite a confirmed
  issue and could potentially benefit from discussion
- 🎨 (`:art:`) for suggestions / improvements
- ❌ (`:x:`) or ⚠️ (`:warning:`) for more significant problems or
concerns needing attention
- 🌱 (`:seedling:`) or ♻️ (`:recycle:`) for future improvements or
indications of technical debt
- ⛏ (`:pick:`) for minor or nitpick changes
  • Loading branch information
coltonhurst authored Sep 19, 2024
1 parent 3ab4eef commit 2e506f7
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 18 deletions.
3 changes: 2 additions & 1 deletion crates/bitwarden-napi/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.auth().loginAccessToken(accessToken, stateFile);

// List secrets
const secrets = await client.secrets().list();
Expand Down
46 changes: 29 additions & 17 deletions crates/bitwarden-napi/src-ts/bitwarden_client/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,26 +37,17 @@ export class BitwardenClient {
this.client = new rust.BitwardenClient(settingsJson, loggingLevel ?? LogLevel.Info);
}

async accessTokenLogin(accessToken: string, stateFile?: string): Promise<void> {
const response = await this.client.runCommand(
Convert.commandToJson({
accessTokenLogin: {
accessToken,
stateFile,
},
}),
);

handleResponse(Convert.toResponseForAccessTokenLoginResponse(response));
}

secrets(): SecretsClient {
return new SecretsClient(this.client);
}

projects(): ProjectsClient {
return new ProjectsClient(this.client);
}

auth(): AuthClient {
return new AuthClient(this.client);
}
}

export class SecretsClient {
Expand Down Expand Up @@ -91,11 +82,11 @@ export class SecretsClient {
}

async create(
organizationId: string,
key: string,
value: string,
note: string,
projectIds: string[],
organizationId: string,
): Promise<SecretResponse> {
const response = await this.client.runCommand(
Convert.commandToJson({
Expand All @@ -121,12 +112,12 @@ export class SecretsClient {
}

async update(
organizationId: string,
id: string,
key: string,
value: string,
note: string,
projectIds: string[],
organizationId: string,
): Promise<SecretResponse> {
const response = await this.client.runCommand(
Convert.commandToJson({
Expand Down Expand Up @@ -183,7 +174,7 @@ export class ProjectsClient {
return handleResponse(Convert.toResponseForProjectResponse(response));
}

async create(name: string, organizationId: string): Promise<ProjectResponse> {
async create(organizationId: string, name: string): Promise<ProjectResponse> {
const response = await this.client.runCommand(
Convert.commandToJson({
projects: {
Expand All @@ -207,7 +198,7 @@ export class ProjectsClient {
return handleResponse(Convert.toResponseForProjectsResponse(response));
}

async update(id: string, name: string, organizationId: string): Promise<ProjectResponse> {
async update(organizationId: string, id: string, name: string): Promise<ProjectResponse> {
const response = await this.client.runCommand(
Convert.commandToJson({
projects: {
Expand All @@ -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<void> {
const response = await this.client.runCommand(
Convert.commandToJson({
loginAccessToken: {
accessToken,
stateFile,
},
}),
);

handleResponse(Convert.toResponseForAccessTokenLoginResponse(response));
}
}

0 comments on commit 2e506f7

Please sign in to comment.