@azure/postgresql-auth package

インターフェイス

ConfigureEntraAuthenticationOptions

SequelizeでEntra ID認証を設定するオプション。

EntraTokenProviderOptions

entraTokenProviderのオプション。

SequelizeBeforeConnectHook

beforeConnectライフサイクルフックをサポートするSequelizeのようなインスタンスを表す構造型です。 これにより、 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アクセストークンを取得するパスワードプロバイダー関数を作成します。

この関数はコールバックを返し、呼び出すとAzure Database for PostgreSQLスコープを使って提供された <xref:TokenCredential> からトークンを要求します。 返されたコールバックは、pg.Clientpg.Pool、または類似のPostgreSQLクライアント構成のpasswordオプションとして直接渡すことができます。

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アクセストークンを取得するパスワードプロバイダー関数を作成します。

この関数はコールバックを返し、呼び出すとAzure Database for PostgreSQLスコープを使って提供された <xref:TokenCredential> からトークンを要求します。 返されたコールバックは、pg.Clientpg.Pool、または類似のPostgreSQLクライアント構成のpasswordオプションとして直接渡すことができます。

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>

呼び出しするとアクセストークン文字列に解決される約束を返す関数です。