@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 權杖權利中upn 擷取使用者名稱(或 appid),並在連線設定中同時設定使用者名稱和密碼。

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 存取權杖。

此函式回傳一個回調,當呼叫時,會利用 適用於 PostgreSQL 的 Azure 資料庫 範圍從提供的 <xref:TokenCredential> 請求一個令牌。 回傳的回調可直接作為 password 選項傳遞給 pg.Client、 或 pg.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 權杖權利中upn 擷取使用者名稱(或 appid),並在連線設定中同時設定使用者名稱和密碼。

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 存取權杖。

此函式回傳一個回調,當呼叫時,會利用 適用於 PostgreSQL 的 Azure 資料庫 範圍從提供的 <xref:TokenCredential> 請求一個令牌。 回傳的回調可直接作為 password 選項傳遞給 pg.Client、 或 pg.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>

一個函式,當被呼叫時,會回傳一個承諾,解析為存取權杖字串。