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

[SM-1425] Update Napi SDK #1045

Merged
merged 3 commits into from
Sep 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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));
}
}
Loading