@1password/connect 中文文档教程

发布于 3年前 浏览 26 项目主页 更新于 3年前

1Password Connect Node SDK

codecov

1Password Connect Node SDK 提供您的 JavaScript & TypeScript 应用程序访问托管在您的基础设施上的 1Password Connect API,并利用 1Password Secrets Automation 的强大功能

该库可由 NodeJS 应用程序、工具使用,以及其他用于访问和管理 1Password Vaults 中项目的自动化。

Installation

您可以使用 npm

npm install @1password/connect

或使用 Yarn

yarn add @1password/connect

Usage

Creating an API client

import { OnePasswordConnect, ItemBuilder } from "@1password/connect";

// Create new connector with HTTP Pooling
const op = OnePasswordConnect({
    serverURL: "http://localhost:8080",
    token: "my-token",
    keepAlive: true,
});

Retrieving Vaults

// Get all vaults
let allVaults = await op.listVaults();

// Get a specific vault
let vault = await op.getVault({vault_id});

Interacting with Items

const myVaultId = {vault_uuid};

// Create an Item
const newItem = new ItemBuilder()
    .setCategory("LOGIN")
    .addField({
        label: "Example",
        value: "MySecret",
        sectionName: "Demo Section",
    })
    .build();

const createdItem = await op.createItem(myVaultId, newItem);

// Get an Item
const item = await op.getItem(myVaultId, {item_uuid});

// Get Item by name
const namedItem = await op.getItemByTitle(myVaultId, "Example Title");

// Update an Item
item.title = "New Title";
const updatedItem = await op.updateItem(myVaultId, myItem);

// Delete an Item
await op.deleteItem(myVaultId, updatedItem.id);

Custom HTTPClient

安装 SDK您可以提供自定义 HTTPClient 类来自定义库向服务器发送请求的方式。

HTTPClient 必须实现 IRequestClient 接口:

import { ClientRequestOptions } from "./client";
interface IRequestClient {
    defaultTimeout: number;

    request(
        method: HTTPMethod,
        url: string,
        opts: ClientRequestOptions,
    ): Promise<Response>;
}

您可以使用自定义客户端:

  • handle proxy network access
  • add additional logging
  • use your own node HTTP request library

Defining ClientRequestOptions

HTTPClient.request(method, url, opts) 方法需要一个选项参数。 下表描述了每个选项:

OptionDescriptionRequired
authTokenThe token used to authenticate the client to a 1Password Connect API.Yes
paramsObject with string key-value pairs to be used as querystring parametersNo
dataA string or object made up of key-value pairs. Defines the request body.No
headersObject with string key-value pairs. Merged with default headers.No
timeoutSets timeout value for the HTTP request.No

Logging with debug

1Password Connect JS 客户端使用 debug 库来记录运行时信息。

所有日志消息都在 opconnect 命名空间下定义。 要打印日志语句,请在定义 DEBUG 环境变量时包含 opconnect 命名空间:

DEBUG=opconnect:*

Development

Running Tests

来自存储库根目录:

```shell 脚本 制作测试

### Building

shell脚本 建造 ```


About 1Password

1Password 是世界上最受欢迎的密码管理器。 通过结合行业领先的安全性和屡获殊荣的设计,该公司为全球企业和消费者提供私密、安全和用户友好的密码管理。 超过 60,000 家企业客户信任 1Password 作为他们的企业密码管理器。

Security

1Password 要求您在发现漏洞时进行负责任的披露。 请通过 BugCrowd 提交发现。

有关安全实践的信息,请访问我们的安全主页

1Password Connect Node SDK

codecov

The 1Password Connect Node SDK provides your JavaScript & TypeScript applications access to the 1Password Connect API hosted on your infrastructure and leverage the power of 1Password Secrets Automation

The library can be used by NodeJS applications, tools, and other automations to access and manage items in 1Password Vaults.

Installation

You can install the SDK using npm

npm install @1password/connect

or using Yarn

yarn add @1password/connect

Usage

Creating an API client

import { OnePasswordConnect, ItemBuilder } from "@1password/connect";

// Create new connector with HTTP Pooling
const op = OnePasswordConnect({
    serverURL: "http://localhost:8080",
    token: "my-token",
    keepAlive: true,
});

Retrieving Vaults

// Get all vaults
let allVaults = await op.listVaults();

// Get a specific vault
let vault = await op.getVault({vault_id});

Interacting with Items

const myVaultId = {vault_uuid};

// Create an Item
const newItem = new ItemBuilder()
    .setCategory("LOGIN")
    .addField({
        label: "Example",
        value: "MySecret",
        sectionName: "Demo Section",
    })
    .build();

const createdItem = await op.createItem(myVaultId, newItem);

// Get an Item
const item = await op.getItem(myVaultId, {item_uuid});

// Get Item by name
const namedItem = await op.getItemByTitle(myVaultId, "Example Title");

// Update an Item
item.title = "New Title";
const updatedItem = await op.updateItem(myVaultId, myItem);

// Delete an Item
await op.deleteItem(myVaultId, updatedItem.id);

Custom HTTPClient

You may provide a custom HTTPClient class to customize how the library sends requests to the server.

The HTTPClient must implement the IRequestClient interface:

import { ClientRequestOptions } from "./client";
interface IRequestClient {
    defaultTimeout: number;

    request(
        method: HTTPMethod,
        url: string,
        opts: ClientRequestOptions,
    ): Promise<Response>;
}

You can use a custom client to:

  • handle proxy network access
  • add additional logging
  • use your own node HTTP request library

Defining ClientRequestOptions

The HTTPClient.request(method, url, opts) method requires an options argument. The following table describes each option:

OptionDescriptionRequired
authTokenThe token used to authenticate the client to a 1Password Connect API.Yes
paramsObject with string key-value pairs to be used as querystring parametersNo
dataA string or object made up of key-value pairs. Defines the request body.No
headersObject with string key-value pairs. Merged with default headers.No
timeoutSets timeout value for the HTTP request.No

Logging with debug

The 1Password Connect JS client uses the debug library to log runtime information.

All log messages are defined under the opconnect namespace. To print log statements, include the opconnect namespace when defining the DEBUG environment variable:

DEBUG=opconnect:*

Development

Running Tests

From repository root:

```shell script make test

### Building

shell script make build ```


About 1Password

1Password is the world’s most-loved password manager. By combining industry-leading security and award-winning design, the company provides private, secure, and user-friendly password management to businesses and consumers globally. More than 60,000 business customers trust 1Password as their enterprise password manager.

Security

1Password requests you practice responsible disclosure if you discover a vulnerability. Please submit discoveries via BugCrowd.

For information about security practices, please visit our Security homepage.

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文