AWS 外部 Anthropic 的托肯生成器( JavaScript/ TypeScript)
一个轻量级库,用于为AWS外部Anthropic API认证生成短期无记名符号.
安装
npm install @aws/token-generator-for-aws-external-anthropic
快速启动
托肯时间可定制(1秒至12小时). 实际代号寿命为:
min(specified duration, credentials expiry, 12 hours)默认为12小时.
用法1 — 使用默认的证书和区域
import { getTokenProvider } from "@aws/token-generator-for-aws-external-anthropic";
// Create a token provider that uses default credentials and region 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}`);
}
用法 2 — 使用自定义配置
这个例子使用STS Assume角色. 你可以使用任意 支持的证书提供者。 。 。 .
import { getTokenProvider } from "@aws/token-generator-for-aws-external-anthropic";
import { fromTemporaryCredentials } from "@aws-sdk/credential-providers";
const provideToken = getTokenProvider({
credentials: fromTemporaryCredentials({
params: {
RoleArn: "arn:aws:iam::123456789012:role/MyRole",
},
}),
region: "us-east-1",
expiresInSeconds: 3600,
});
async function example() {
const token = await provideToken();
console.log(`Bearer Token: ${token}`);
}
用法 3 — 使用静态一发
直接通过全权证书、区域及期满。 无需实例.
import { getToken } from "@aws/token-generator-for-aws-external-anthropic";
async function example() {
const token = await getToken({
credentials: {
accessKeyId: "YOUR_ACCESS_KEY_ID",
secretAccessKey: "YOUR_SECRET_ACCESS_KEY",
sessionToken: "YOUR_SESSION_TOKEN",
},
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文档
调试格式
生成的令牌有格式 :
aws-external-anthropic-api-key-<base64-encoded-payload>
该有效载荷为Base64-encode SigV4预发式URL,范围为 aws-external-anthropic 服务。 令牌可以被解码为调试目的,但应该作为生产中的不透明字符串处理.
所需资源
- 节点.js: 16.0.0 或较晚
- 类型脚本: 4.7.0或更晚(用于TypeScript用户)
安全考虑
- 托肯过期:托肯斯寿命短,寿命最长为12小时. 实际期满是:
min(specified duration, credentials expiry, 12 hours)。使用最短的实际使用时间. - 安全存储: 不要在纯文本中日志或存储符号。 把他们当作敏感证书.
- 无嵌入式证书:代号中不嵌入长期证书. 符号包含一个SigV4预先签名的URL,而不是签名密钥本身.
- 证书管理:尽可能使用IAM角色或临时证书来代替长期访问密钥.
- 网络安全: 总是通过 HTTPS 传输符号.
- 最小特权: 范围 IAM 权限到您使用时所需的最小值 .
- 区域范围:托肯斯被范围限定为特定AWS区域,不能跨区域使用.
发展
# Install dependencies
npm install
# Build the project
npm run build
# Run tests
npm test
捐款
见 捐款 更多信息.
许可证
这个项目根据Apache许可证2.0版本获得许可. 见 自由主义 详细文件.
更改日志
见 变化 为释放历史.