你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

@azure/postgresql-auth package

接口

ConfigureEntraAuthenticationOptions

使用 Sequelize 配置 Entra ID 认证的选项。

EntraTokenProviderOptions

entraTokenProvider的选项。

SequelizeBeforeConnectHook

一个结构类型,表示类似Sequelize的实例,支持 beforeConnect 生命周期钩子。 这样可以避免对封装的硬依赖 sequelize ,同时还能提供类型安全。

函数

configureEntraAuthentication(SequelizeBeforeConnectHook, TokenCredential, ConfigureEntraAuthenticationOptions)

配置一个 Sequelize 实例以使用 Entra ID 认证。

该函数在 Sequelize 实例上注册一个 beforeConnect 钩子,在每次新的数据库连接前自动获取一个 Entra ID 令牌。 钩子从JWT令牌声明中提取用户名(upnappid),并在连接配置中设置用户名和密码。

Example

import { DefaultAzureCredential } from "@azure/identity";

const { configureEntraAuthentication } = await import("@azure/postgresql-auth");
const { Sequelize } = await import("sequelize");
const sequelize = new Sequelize({
  dialect: "postgres",
  host: process.env.PGHOST,
  port: Number(process.env.PGPORT || 5432),
  database: process.env.PGDATABASE,
});
const credential = new DefaultAzureCredential();
configureEntraAuthentication(sequelize, credential);
await sequelize.authenticate();
entraTokenProvider(TokenCredential, EntraTokenProviderOptions)

创建密码提供者函数,获取适合用作 PostgreSQL 密码的 Entra ID 访问令牌。

该函数返回回调,调用时会利用Azure Database for PostgreSQL作用域从提供的 <xref:TokenCredential> 请求一个令牌。 返回的回调可以直接作为 password 选项传递给 pg.Clientpg.Pool或类似的 PostgreSQL 客户端配置。

Example

import { DefaultAzureCredential } from "@azure/identity";

const { entraTokenProvider } = await import("@azure/postgresql-auth");
const pg = await import("pg");
const credential = new DefaultAzureCredential();
const pool = new pg.Pool({
  host: process.env.PGHOST,
  port: Number(process.env.PGPORT || 5432),
  database: process.env.PGDATABASE,
  user: process.env.PGUSER,
  password: entraTokenProvider(credential),
  ssl: { rejectUnauthorized: true },
});

函数详细信息

configureEntraAuthentication(SequelizeBeforeConnectHook, TokenCredential, ConfigureEntraAuthenticationOptions)

配置一个 Sequelize 实例以使用 Entra ID 认证。

该函数在 Sequelize 实例上注册一个 beforeConnect 钩子,在每次新的数据库连接前自动获取一个 Entra ID 令牌。 钩子从JWT令牌声明中提取用户名(upnappid),并在连接配置中设置用户名和密码。

Example

import { DefaultAzureCredential } from "@azure/identity";

const { configureEntraAuthentication } = await import("@azure/postgresql-auth");
const { Sequelize } = await import("sequelize");
const sequelize = new Sequelize({
  dialect: "postgres",
  host: process.env.PGHOST,
  port: Number(process.env.PGPORT || 5432),
  database: process.env.PGDATABASE,
});
const credential = new DefaultAzureCredential();
configureEntraAuthentication(sequelize, credential);
await sequelize.authenticate();
function configureEntraAuthentication(sequelizeInstance: SequelizeBeforeConnectHook, credential: TokenCredential, options?: ConfigureEntraAuthenticationOptions)

参数

sequelizeInstance
SequelizeBeforeConnectHook

需要配置的是 Sequelize 实例。 必须支持 beforeConnect 生命周期钩子。

credential
TokenCredential

用于获取代币的Azure <xref:TokenCredential>(例如DefaultAzureCredential)。

options
ConfigureEntraAuthenticationOptions

认证行为的可选配置。

entraTokenProvider(TokenCredential, EntraTokenProviderOptions)

创建密码提供者函数,获取适合用作 PostgreSQL 密码的 Entra ID 访问令牌。

该函数返回回调,调用时会利用Azure Database for PostgreSQL作用域从提供的 <xref:TokenCredential> 请求一个令牌。 返回的回调可以直接作为 password 选项传递给 pg.Clientpg.Pool或类似的 PostgreSQL 客户端配置。

Example

import { DefaultAzureCredential } from "@azure/identity";

const { entraTokenProvider } = await import("@azure/postgresql-auth");
const pg = await import("pg");
const credential = new DefaultAzureCredential();
const pool = new pg.Pool({
  host: process.env.PGHOST,
  port: Number(process.env.PGPORT || 5432),
  database: process.env.PGDATABASE,
  user: process.env.PGUSER,
  password: entraTokenProvider(credential),
  ssl: { rejectUnauthorized: true },
});
function entraTokenProvider(credential: TokenCredential, options?: EntraTokenProviderOptions): () => Promise<string>

参数

credential
TokenCredential

用于获取代币的Azure <xref:TokenCredential>(例如DefaultAzureCredential)。

options
EntraTokenProviderOptions

可选设置,比如定制的OAuth示波器。

返回

() => Promise<string>

一个函数,调用时返回一个承诺,解析为访问令牌字符串。