@azure/postgresql-auth package

Interfacce

ConfigureEntraAuthenticationOptions

Opzioni per configurare l'autenticazione Entra ID con Sequelize.

EntraTokenProviderOptions

Opzioni per entraTokenProvider.

SequelizeBeforeConnectHook

Un tipo strutturale che rappresenta un'istanza simile a Sequelize che supporta l'hook beforeConnect del ciclo di vita. Questo evita una dipendenza netta sequelize dal package pur garantendo la sicurezza del tipo.

Funzioni

configureEntraAuthentication(SequelizeBeforeConnectHook, TokenCredential, ConfigureEntraAuthenticationOptions)

Configura un'istanza Sequelize per utilizzare l'autenticazione Entra ID.

Questa funzione registra un hook beforeConnect sull'istanza Sequelize che acquisisce automaticamente un token Entra ID prima di ogni nuova connessione al database. L'hook estrae il nome utente dalle rivendicazioni del token JWT (upn o appid) e imposta sia il nome utente che la password sulla configurazione della connessione.

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)

Crea una funzione di fornitore di password che acquisisce un token di accesso Entra ID adatto all'uso come password PostgreSQL.

Questa funzione restituisce un callback che, quando invocato, richiede un token dal <xref:TokenCredential> fornito utilizzando l'ambito Database di Azure per PostgreSQL. Il callback restituito può essere passato direttamente come password opzione per pg.Client, pg.Pool, o configurazioni client simili di 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 },
});

Dettagli funzione

configureEntraAuthentication(SequelizeBeforeConnectHook, TokenCredential, ConfigureEntraAuthenticationOptions)

Configura un'istanza Sequelize per utilizzare l'autenticazione Entra ID.

Questa funzione registra un hook beforeConnect sull'istanza Sequelize che acquisisce automaticamente un token Entra ID prima di ogni nuova connessione al database. L'hook estrae il nome utente dalle rivendicazioni del token JWT (upn o appid) e imposta sia il nome utente che la password sulla configurazione della connessione.

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)

Parametri

sequelizeInstance
SequelizeBeforeConnectHook

L'istanza Sequelize da configurare. Deve supportare l'aggancio beforeConnect del ciclo di vita.

credential
TokenCredential

Un Azure <xref:TokenCredential> usato per acquisire token (ad esempio, DefaultAzureCredential).

options
ConfigureEntraAuthenticationOptions

Configurazione opzionale per il comportamento di autenticazione.

entraTokenProvider(TokenCredential, EntraTokenProviderOptions)

Crea una funzione di fornitore di password che acquisisce un token di accesso Entra ID adatto all'uso come password PostgreSQL.

Questa funzione restituisce un callback che, quando invocato, richiede un token dal <xref:TokenCredential> fornito utilizzando l'ambito Database di Azure per PostgreSQL. Il callback restituito può essere passato direttamente come password opzione per pg.Client, pg.Pool, o configurazioni client simili di 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>

Parametri

credential
TokenCredential

Un Azure <xref:TokenCredential> usato per acquisire token (ad esempio, DefaultAzureCredential).

options
EntraTokenProviderOptions

Impostazioni opzionali come un mirino OAuth personalizzato.

Valori restituiti

() => Promise<string>

Una funzione che, quando chiamata, restituisce una promessa che si risolve alla stringa del token di accesso.