npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@zhaoanke/authz-node

v0.0.8

Published

一个轻量级的授权(Authorization)客户端库

Readme

Node.js Authz Client

一个轻量级的授权(Authorization)客户端库。

npm version License: MIT

安装

npm install @zhaoanke/authz-node
# 或
yarn add @zhaoanke/authz-node

环境变量配置

创建 .env 文件并配置以下环境变量:

# Authz Configuration
AUTHZ_APPID=authz
AUTHZ_LOGIN_TYPE=password
AUTHZ_GRANT_TYPE=authorization_code

# API Configuration
AUTHZ_API_URL=http://0.0.0.0:8080

# Secrets
AUTHZ_APP_SECRET=your-app-secret            # 用于认证的 APP 秘钥
AUTHZ_ENCRYPTION_SECRET=your-encrypt-secret  # 用于加密的加密秘钥

使用方法

import { AuthzClient } from '@zhaoanke/authz-node';

const client = new AuthzClient({
  baseUrl: 'https://your-authz-server.com',
  apiKey: 'your-api-key'
});

// 检查权限
const hasAccess = await client.check({
  subject: 'user:123',
  action: 'read',
  resource: 'document:456'
});

// 批量检查权限
const results = await client.batchCheck([
  {
    subject: 'user:123',
    action: 'read',
    resource: 'document:456'
  },
  {
    subject: 'user:123',
    action: 'write',
    resource: 'document:789'
  }
]);

API 文档

创建客户端

const client = new AuthzClient(options: AuthzClientOptions)

选项

  • baseUrl: 授权服务器的基础URL
  • apiKey: API密钥
  • timeout: 请求超时时间(毫秒),默认5000ms

OAuth2 认证

getToken(request: OAuth2TokenRequest): Promise

获取访问令牌:

// 使用授权码获取令牌
const tokenResponse = await client.getToken({
  grant_type: 'authorization_code',
  code: 'authorization_code',
  redirect_uri: 'https://your-app.com/callback'
});

// 使用刷新令牌
const newTokenResponse = await client.getToken({
  grant_type: 'refresh_token',
  refresh_token: 'refresh_token'
});

authorize(request: OAuth2AuthorizeRequest): Promise

请求授权:

const redirectUrl = await client.authorize({
  response_type: 'code',
  client_id: 'your_client_id',
  redirect_uri: 'https://your-app.com/callback',
  scope: 'read write',
  state: 'random_state'
});

authenticate(token: string): Promise

验证身份:

const authResponse = await client.authenticate('access_token');

权限检查

check(query: CheckQuery): Promise

检查单个权限

batchCheck(queries: CheckQuery[]): Promise<boolean[]>

批量检查权限

错误处理

库会抛出类型化的错误,可以通过 AuthzError 来处理:

try {
  await client.check(query);
} catch (error) {
  if (error instanceof AuthzError) {
    console.error('授权错误:', error.message);
  }
}