JavaScript/ TypeScript 的 AWS Bedrock 托肯生成器
一个轻量级库,用于为AWS Bedrock API认证生成短期无记名符号.
特征
- ✅ 简单 API: 生成无记名符号的 Async 函数
- ✅ 安全: 使用 AWS SigV4 签名,可配置标志过期(最多12小时)
- ✅ AWS SDK 整合: 与 AWS 证书提供者无缝合作
- ✅ 轻巧: 最小依赖,有重点的功能
- ✅ 测试良好:具有多种情景的单位综合测试
- ✅ 类型脚本: 完整类型定义,以便更好的IDE体验
安装
npm install @aws/bedrock-token-generator
快速启动
使用带有默认证书和区域提供者的托肯提供者
import { getTokenProvider } from "@aws/bedrock-token-generator";
// Create a token provider that uses default credentials and region providers.
// You can configure it to use other credential providers.
const provideToken = getTokenProvider();
async function example() {
const token = await provideToken();
// Use the token for API calls. The token has a default expiration of 12 hour.
// If the expiresInSeconds parameter is specified during token creation, the
// expiration can be configured up to a maximum of 12 hours. However, the actual
// token validity period will always be the minimum of the requested expiration
// time and the AWS credentials' expiry time
console.log(`Bearer Token: ${token}`);
}
使用带有证书提供者和地区的托肯提供者
您可以找到支持的证书提供者 这里。 。 。 .
import { getTokenProvider } from "@aws/bedrock-token-generator";
import { fromTemporaryCredentials } from "@aws-sdk/credential-providers";
const provideToken = getTokenProvider({
credentials: fromTemporaryCredentials({
params: {
RoleArn: "arn:aws:iam::123456789012:role/BedrockRole",
},
}),
region: "us-east-1",
});
async function example() {
const token = await provideToken();
// Use the token for API calls. The token has a default expiration of 12 hour.
// If the expiresInSeconds parameter is specified during token creation, the
// expiration can be configured up to a maximum of 12 hours. However, the actual
// token validity period will always be the minimum of the requested expiration
// time and the AWS credentials' expiry time
console.log(`Bearer Token: ${token}`);
}
使用具有特定证书、区域和过期的托肯供应商
import { getTokenProvider } from "@aws/bedrock-token-generator";
const credentials = {
accessKeyId: "YOUR_ACCESS_KEY_ID",
secretAccessKey: "YOUR_SECRET_ACCESS_KEY",
sessionToken: "YOUR_SESSION_TOKEN",
};
const provideToken = getTokenProvider({
credentials,
region: "us-east-1",
expiresInSeconds: 7200,
});
async function example() {
const token = await provideToken();
// Use the token for API calls. The token has an expiration of 2 hour. However, the actual token validity period
// will always be the minimum of the requested expiration time and the AWS credentials' expiry time
console.log(`Bearer Token: ${token}`);
}
使用具有特定证书、区域和过期的无国籍函数
import { getToken } from "@aws/bedrock-token-generator";
async function example() {
const credentials = {
accessKeyId: "YOUR_ACCESS_KEY_ID",
secretAccessKey: "YOUR_SECRET_ACCESS_KEY",
sessionToken: "YOUR_SESSION_TOKEN",
};
const token = await getToken({
credentials,
region: "us-east-1",
expiresInSeconds: 7200,
});
// Use the token for API calls. The token has an expiration of 2 hour. However, the actual token validity period
// will always be the minimum of the requested expiration time and the AWS credentials' expiry time
console.log(`Bearer Token: ${token}`);
}
API 参考
- API 参考 - 详细的API文档
调试格式
生成的符号遵循此格式 :
bedrock-api-key-<base64-encoded-presigned-url>&Version=1
- 前缀数字 :
bedrock-api-key-识别符号类型 - 有效载荷: 具有嵌入式证书的 Base64 编码前的 URL
- 版本数字 :
&Version=1未来兼容性 - 过期: 该令牌的默认到期时间为12小时. 如果在符号创建期间指定了过期的InSeconds参数,则过期的配置最多可达12小时. 但是,实际有效期限将永远是 成为所要求的到期时间和AWS证书到期时间的最小值.
安全考虑
- 托肯过期: 该令牌的默认到期时间为12小时. 如果在符号创建期间指定了过期的InSeconds参数,则过期的配置最多可达12小时. 但是,实际有效期限将永远是 成为所要求的到期时间和AWS证书到期时间的最小值。 信使一旦过期必须再次生成, 因为它不能被刷新或延长.
- 安全存储: 安全地存储令牌并避免记录它们
- 证书管理:尽可能使用IAM的作用和临时证书
- 网络安全: 发送令牌时始终使用 HTTPS
- 最低特权原则:确保基本证书具备最低要求的许可
所需资源
- 节点.js: 16.0.0 或较晚
- 类型脚本: 4.7.0或更晚(用于TypeScript用户)
发展
建立发展环境
# Clone the repository
git clone https://github.com/aws/aws-bedrock-token-generator-js.git
cd aws-bedrock-token-generator-js
# Install dependencies
npm install
# Build the project
npm run build
# Run tests
npm test
运行测试
# Run all tests
npm test
# Run tests in watch mode
npm run test:watch
# Run linter
npm run lint
# Format code
npm run format
捐款
我们欢迎捐款! 请看 捐款额 详细情况.
发展 工作流量
- 叉子存储器
- 创建特性分支数字 :
git checkout -b feature-name - 更改并添加测试
- 运行测试数字 :
npm test - 格式代码数字 :
npm run format - 提交拉动请求
支助
- 文档数字 : AWS 贝德洛克文档
- 问题数字 : GitHub 问题
- AWS 支持数字 : AWS 支助中心
许可证
这个项目根据Apache许可证2.0获得许可——见 自由主义 详细文件.
相关项目
更改日志
见 变动情况 用于列出更改和版本历史.