@access-tokens/core

中文文档
1.1.0发布于 2025/12/31ISCTypeScriptCommonJS构建来源已验证近 30 天下载 1.5万

Personal access token library for secure token generation, verification, and lifecycle management

原始使用文档
文档目录17 节
  1. Features
  2. Installation
  3. Quick Start
  4. API Reference
  5. Constructor
  6. Token Operations
  7. Token Generation Utilities
  8. Token Format
  9. Security
  10. DynamoDB Schema
  11. Table
  12. TTL Configuration
  13. Error Handling
  14. Requirements
  15. Related Packages
  16. License
  17. Links

@access-tokens/core

npm License: ISC

Core library for managing Personal Access Tokens (PATs) with secure scrypt-based hashing. Supports DynamoDB backend.

Features

  • Secure Token Generation: Cryptographically secure random token generation
  • Scrypt Hashing: Industry-standard password hashing with PHC format
  • Token Lifecycle Management: Issue, verify, update, revoke, and restore tokens
  • Role-Based Access Control: Attach arbitrary roles to tokens with atomic add/remove operations
  • DynamoDB Integration: Low-cost, scalable storage with TTL expiration
  • TypeScript: Full type safety with comprehensive API types

Installation

npm install @access-tokens/core

Quick Start

import { DynamoDBPat } from "@access-tokens/core";
import { DynamoDBDocumentClient } from "@aws-sdk/lib-dynamodb";
import { DynamoDBClient } from "@aws-sdk/client-dynamodb";

// Initialize AWS clients
const dynamoClient = new DynamoDBClient({ region: "us-east-1" });
const docClient = DynamoDBDocumentClient.from(dynamoClient);

// Create DynamoDBPat instance
const pat = new DynamoDBPat({
  tableName: "my-tokens",
  docClient,
});

// Issue a new token
const { token, record } = await pat.issue({
  owner: "[email protected]",
  isAdmin: false,
  expiresAt: Math.floor(Date.now() / 1000) + 365 * 24 * 60 * 60, // 1 year
});

console.log("Token:", token); // pat_abc123...xyz
console.log("Token ID:", record.tokenId);

// Verify a token
const result = await pat.verify(token);
if (result.valid) {
  console.log("Token is valid!");
  console.log("Owner:", result.record.owner);
  console.log("Is Admin:", result.record.isAdmin);
}

API Reference

Constructor

new DynamoDBPat(options)

Creates a new DynamoDBPat instance.

Options:

{
  tableName: string;                    // DynamoDB table name (required)
  ddbClient?: DynamoDBClient;           // AWS DynamoDB Client (optional)
  docClient?: DynamoDBDocumentClient;   // AWS DynamoDB Document Client (optional)
  tokenPrefix?: string;                 // Token prefix (default: "pat_")
  keyLength?: number;                   // Scrypt key length in bytes (default: 64)
  saltLength?: number;                  // Scrypt salt length in bytes (default: 16)
  scryptOptions?: {                     // Scrypt configuration (optional)
    cost?: number;                      // CPU/memory cost (default: 16384)
    blockSize?: number;                 // Block size (default: 8)
    parallelization?: number;           // Parallelization (default: 1)
    maxmem?: number;                    // Maximum memory (optional)
  };
  bootstrapPhc?: string;                // Bootstrap PHC for key derivation (optional)
}

Note: You only need to provide one of ddbClient or docClient, not both. If neither is provided, a default DynamoDB client will be created automatically. If only ddbClient is provided, a document client will be created from it. If docClient is provided, it will be used directly.

Token Operations

issue(params): Promise<{ token: string; record: PatRecord }>

Issues a new token.

Parameters:

{
  owner: string;           // Token owner (e.g., email address)
  isAdmin: boolean;        // Whether token has admin privileges
  roles?: string[];        // Array of role strings (optional, max 50 roles, 100 chars each)
  expiresAt?: number;      // Unix timestamp for expiration (optional)
  tokenId?: string;        // Pre-generated token ID (optional)
}

Returns: Object containing the full token string and the database record.

verify(token: string): Promise<VerifyResult>

Verifies a token and returns its record if valid.

Returns:

{
  valid: boolean;
  record?: PatRecord;      // Only present if valid=true
  reason?: string;         // Only present if valid=false
}

Reasons for invalid tokens:

  • "invalid_prefix" - Token prefix doesn't match expected prefix
  • "invalid_format" - Token format is incorrect (malformed)
  • "not_found" - Token ID not found in database
  • "invalid_phc" - Stored PHC hash format is invalid
  • "unsupported_algorithm" - Hash algorithm in PHC is not supported
  • "invalid_parameters" - Scrypt parameters in PHC are invalid
  • "invalid_secret" - Secret doesn't match stored hash
  • "revoked" - Token has been revoked
  • "expired" - Token has expired

register(params): Promise<TokenRecord>

Registers a token with a pre-generated ID and secret hash.

Parameters:

{
  tokenId: string;         // Token ID
  secretPhc: string;       // PHC-formatted secret hash
  owner: string;           // Token owner
  isAdmin: boolean;        // Admin status
  roles?: string[];        // Array of role strings (optional)
  expiresAt?: number;      // Expiration timestamp (optional)
}

update(tokenId: string, updates): Promise<void>

Updates an existing token's properties. Supports atomic role add/remove operations.

Parameters:

  • tokenId: string - Token ID to update
  • updates: object - Properties to update:
{
  owner?: string;          // New owner (optional)
  isAdmin?: boolean;       // New admin status (optional)
  roles?: RolesUpdate;     // Roles update (optional, see below)
  secretPhc?: string;      // New secret hash (optional)
  expiresAt?: number | null; // New expiration or null to remove (optional)
}

// RolesUpdate can be:
type RolesUpdate =
  | string[]               // Replace all roles
  | { add: string[] }      // Atomic add (cannot combine with remove)
  | { remove: string[] }   // Atomic remove (cannot combine with add)

Examples:

// Update basic properties
await pat.update("34NwRzvnBbgI3uedkrQ3Q", { owner: "[email protected]" });

// Replace all roles
await pat.update("34NwRzvnBbgI3uedkrQ3Q", { roles: ["reader", "writer"] });

// Add roles atomically (idempotent)
await pat.update("34NwRzvnBbgI3uedkrQ3Q", { roles: { add: ["admin"] } });

// Remove roles atomically (idempotent)
await pat.update("34NwRzvnBbgI3uedkrQ3Q", { roles: { remove: ["guest"] } });

// Clear all roles
await pat.update("34NwRzvnBbgI3uedkrQ3Q", { roles: [] });

// Update multiple properties at once
await pat.update("34NwRzvnBbgI3uedkrQ3Q", {
  owner: "[email protected]",
  roles: { add: ["admin"] },
});

revoke(tokenId: string, options?: { expiresAt?: number }): Promise<void>

Revokes a token. Optionally sets an expiration for automatic cleanup.

Example:

// Revoke immediately
await pat.revoke("34NwRzvnBbgI3uedkrQ3Q");

// Revoke with cleanup in 30 days
await pat.revoke("34NwRzvnBbgI3uedkrQ3Q", {
  expiresAt: Math.floor(Date.now() / 1000) + 30 * 24 * 60 * 60,
});

restore(tokenId: string): Promise<void>

Restores a previously revoked token.

Example:

await pat.restore("34NwRzvnBbgI3uedkrQ3Q");

list(options?): AsyncGenerator<PublicTokenRecord>

Lists all tokens with optional filtering. Returns an async generator that yields tokens.

Options:

{
  afterTokenId?: string;         // Start after this token ID (pagination)
  limit?: number;                // Maximum tokens to return
  includeSecretPhc?: boolean;    // Include secret hashes (default: false)
  hasRole?: string;              // Filter tokens that have this role (optional)
}

Example:

// Iterate through all tokens
for await (const token of pat.list()) {
  console.log(token.owner);
}

// With pagination
for await (const token of pat.list({
  limit: 100,
  afterTokenId: "34NwRzvnBbgI3uedkrQ3Q",
})) {
  console.log(token);
}

Note: Filtering for revoked and expired tokens should be done by the caller after retrieving records.

Token Generation Utilities

generate(config?: { tokenId?: string }): Promise<{ token: string; tokenId: string; secretPhc: string }>

Generates a token without storing it in the database. Useful for pre-generating tokens.

Example:

// Generate new token
const { token, tokenId, secretPhc } = await pat.generate();

// Generate with specific token ID
const result = await pat.generate({ tokenId: "34NwRzvnBbgI3uedkrQ3Q" });

Token Format

Tokens follow the format: {prefix}{tokenId}.{secret}

Example: pat_34NwRzvnBbgI3uedkrQ3Q.a8b9c0d1e2f3g4h5i6j7k8l9m0n1o2p3q4r5s6t7u8v9w0x1y2z3

  • Prefix: Configurable (default: pat_)
  • Token ID: 21-character Base62-encoded unique identifier
  • Secret: Base64-encoded random bytes (default: 32 bytes = 43 Base64 chars)

Security

  • Scrypt Hashing: Secrets are hashed using scrypt with secure default parameters
  • PHC Format: Hashes stored in Argon2-compatible PHC string format for algorithm agility
  • Timing-Safe Comparison: Constant-time comparison prevents timing attacks
  • Secure Random Generation: Uses Node.js crypto.randomBytes() for token generation

DynamoDB Schema

Table

Primary Key:

  • Partition Key: tokenId (String)

Attributes:

  • tokenId: Unique token identifier
  • secretPhc: PHC-formatted secret hash
  • owner: Token owner identifier
  • isAdmin: Boolean admin flag
  • roles: String Set of role names (optional, stored as DynamoDB SS type)
  • isRevoked: Boolean revoked status
  • expiresAt: Unix timestamp (optional, enables TTL)
  • createdAt: Unix timestamp
  • updatedAt: Unix timestamp

Global Secondary Index (recommended):

  • Index Name: owner-index
  • Partition Key: owner (String)
  • Enables efficient listByOwner() queries

TTL Configuration

Configure DynamoDB TTL on the expiresAt attribute for automatic cleanup of expired tokens.

Error Handling

All methods may throw errors. Use try-catch for error handling:

try {
  const result = await pat.verify(token);
  if (!result.valid) {
    console.log("Invalid token:", result.reason);
  }
} catch (error) {
  console.error("Error verifying token:", error);
}

Requirements

  • Node.js 20+
  • AWS SDK v3
  • DynamoDB table with appropriate permissions

License

ISC © 2025 Loan Crate, Inc.

版本与发布记录

@access-tokens/core 共发布 2 个版本,首次发布于 2025/11/07;过去 12 个月发布了 1 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
1.1.0latest2025/12/3110 个月前
1.0.02025/11/071 年前

关于 @access-tokens/core 的常见问题

@access-tokens/core 是什么?

Personal access token library for secure token generation, verification, and lifecycle management

如何安装 @access-tokens/core?

在项目目录运行 npm install @access-tokens/core,或使用 pnpm add / yarn add / bun add。

@access-tokens/core 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@access-tokens/core 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@access-tokens/core 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=20.0.0。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@access-tokens/core 可以商用吗?

@access-tokens/core 使用 ISC 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@access-tokens/core 的下载量有多少?

根据 npm 官方下载统计,@access-tokens/core 在 2026/10/05 – 2026/11/03 这 30 天内被下载 14,654 次,日均约 488 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@access-tokens/core 还在维护吗?

最新版本 1.1.0 发布于 2025/12/31(10 个月前);至今共发布 2 个版本,其中过去 12 个月发布了 1 个;首次发布于 2025/11/07。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。