Skip to content

Commit 38b859c

Browse files
Matthew BoentoroKarishmaGhiya
authored andcommitted
address feedback
1 parent 13fe130 commit 38b859c

13 files changed

Lines changed: 63 additions & 63 deletions

File tree

sdk/postgresql/postgresql-auth/CHANGELOG.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44

55
### Features Added
66

7-
- Added `getEntraTokenPassword` function for acquiring Entra ID tokens as PostgreSQL passwords.
8-
- Added `configureEntraIdAuth` function for automatic Sequelize Entra ID authentication via `beforeConnect` hook.
7+
- Added `entraTokenProvider` function for acquiring Entra ID tokens as PostgreSQL passwords.
8+
- Added `configureEntraAuthentication` function for automatic Sequelize Entra ID authentication via `beforeConnect` hook.
99
- Support for `pg` (node-postgres) and Sequelize clients as optional peer dependencies.
1010

1111
### Breaking Changes

sdk/postgresql/postgresql-auth/README.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,37 +42,37 @@ npm install @azure/identity sequelize pg
4242

4343
This library provides two functions for integrating Entra ID authentication with PostgreSQL:
4444

45-
- **`getEntraTokenPassword`** — Acquires an Entra ID access token and returns it as a string suitable for use as a PostgreSQL password. Use this with `pg.Pool` or `pg.Client`.
46-
- **`configureEntraIdAuth`** — Registers a `beforeConnect` hook on a Sequelize instance that automatically acquires a fresh token and sets the username/password before each new connection.
45+
- **`entraTokenProvider`** — Acquires an Entra ID access token and returns it as a string suitable for use as a PostgreSQL password. Use this with `pg.Pool` or `pg.Client`.
46+
- **`configureEntraAuthentication`** — Registers a `beforeConnect` hook on a Sequelize instance that automatically acquires a fresh token and sets the username/password before each new connection.
4747

4848
Both functions accept an Azure `TokenCredential` (from `@azure/identity`) and handle token acquisition against the Azure Database for PostgreSQL scope.
4949

5050
## Examples
5151

5252
### Using with node-postgres (`pg`)
5353

54-
```ts snippet:GetEntraTokenPassword
54+
```ts snippet:entraTokenProvider
5555
import { DefaultAzureCredential } from "@azure/identity";
5656

57-
const { getEntraTokenPassword } = await import("@azure/postgresql-auth");
57+
const { entraTokenProvider } = await import("@azure/postgresql-auth");
5858
const pg = await import("pg");
5959
const credential = new DefaultAzureCredential();
6060
const pool = new pg.Pool({
6161
host: process.env.PGHOST,
6262
port: Number(process.env.PGPORT || 5432),
6363
database: process.env.PGDATABASE,
6464
user: process.env.PGUSER,
65-
password: () => getEntraTokenPassword(credential),
65+
password: () => entraTokenProvider(credential),
6666
ssl: { rejectUnauthorized: true },
6767
});
6868
```
6969

7070
### Using with Sequelize
7171

72-
```ts snippet:ConfigureEntraIdAuth
72+
```ts snippet:configureEntraAuthentication
7373
import { DefaultAzureCredential } from "@azure/identity";
7474

75-
const { configureEntraIdAuth } = await import("@azure/postgresql-auth");
75+
const { configureEntraAuthentication } = await import("@azure/postgresql-auth");
7676
const { Sequelize } = await import("sequelize");
7777
const sequelize = new Sequelize({
7878
dialect: "postgres",
@@ -81,7 +81,7 @@ const sequelize = new Sequelize({
8181
database: process.env.PGDATABASE,
8282
});
8383
const credential = new DefaultAzureCredential();
84-
configureEntraIdAuth(sequelize, credential);
84+
configureEntraAuthentication(sequelize, credential);
8585
await sequelize.authenticate();
8686
```
8787

sdk/postgresql/postgresql-auth/review/postgresql-auth-node.api.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@
77
import type { TokenCredential } from '@azure/core-auth';
88

99
// @public
10-
export function configureEntraIdAuth(sequelizeInstance: SequelizeBeforeConnectHook, credential: TokenCredential, options?: ConfigureEntraIdAuthOptions): typeof sequelizeInstance;
10+
export function configureEntraAuthentication(sequelizeInstance: SequelizeBeforeConnectHook, credential: TokenCredential, options?: ConfigureEntraIdAuthOptions): typeof sequelizeInstance;
1111

1212
// @public
1313
export interface ConfigureEntraIdAuthOptions {
1414
fallbackUsername?: string;
1515
}
1616

1717
// @public
18-
export function getEntraTokenPassword(credential: TokenCredential, options?: GetEntraTokenPasswordOptions): Promise<string>;
18+
export function entraTokenProvider(credential: TokenCredential, options?: GetEntraTokenPasswordOptions): Promise<string>;
1919

2020
// @public
2121
export interface GetEntraTokenPasswordOptions {

sdk/postgresql/postgresql-auth/samples/v1-beta/javascript/pgConnection.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
const pg = require("pg");
1010
const { DefaultAzureCredential } = require("@azure/identity");
11-
const { getEntraTokenPassword } = require("@azure/postgresql-auth");
11+
const { entraTokenProvider } = require("@azure/postgresql-auth");
1212
const dotenv = require("dotenv");
1313

1414
dotenv.config();
@@ -26,7 +26,7 @@ async function main() {
2626
port: Number(process.env.PGPORT || 5432),
2727
database: process.env.PGDATABASE,
2828
user: process.env.PGUSER,
29-
password: () => getEntraTokenPassword(credential),
29+
password: () => entraTokenProvider(credential),
3030
ssl: { rejectUnauthorized: true },
3131
connectionTimeoutMillis: 20000,
3232
idleTimeoutMillis: 30000,

sdk/postgresql/postgresql-auth/samples/v1-beta/javascript/sequelizeConnection.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
const { Sequelize } = require("sequelize");
1010
const { DefaultAzureCredential } = require("@azure/identity");
11-
const { configureEntraIdAuth } = require("@azure/postgresql-auth");
11+
const { configureEntraAuthentication } = require("@azure/postgresql-auth");
1212
const dotenv = require("dotenv");
1313

1414
dotenv.config();
@@ -28,7 +28,7 @@ async function main() {
2828

2929
// Register the Entra ID `beforeConnect` hook.
3030
// This automatically sets the username and password on each new connection.
31-
configureEntraIdAuth(sequelize, credential);
31+
configureEntraAuthentication(sequelize, credential);
3232

3333
try {
3434
console.log("Connecting to Azure Database for PostgreSQL via Sequelize...");

sdk/postgresql/postgresql-auth/samples/v1-beta/typescript/src/pgConnection.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
import pg from "pg";
1010
import { DefaultAzureCredential } from "@azure/identity";
11-
import { getEntraTokenPassword } from "@azure/postgresql-auth";
11+
import { entraTokenProvider } from "@azure/postgresql-auth";
1212
import dotenv from "dotenv";
1313

1414
dotenv.config();
@@ -26,7 +26,7 @@ async function main(): Promise<void> {
2626
port: Number(process.env.PGPORT || 5432),
2727
database: process.env.PGDATABASE,
2828
user: process.env.PGUSER,
29-
password: () => getEntraTokenPassword(credential),
29+
password: () => entraTokenProvider(credential),
3030
ssl: { rejectUnauthorized: true },
3131
connectionTimeoutMillis: 20000,
3232
idleTimeoutMillis: 30000,

sdk/postgresql/postgresql-auth/samples/v1-beta/typescript/src/sequelizeConnection.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
import { Sequelize } from "sequelize";
1010
import { DefaultAzureCredential } from "@azure/identity";
11-
import { configureEntraIdAuth } from "@azure/postgresql-auth";
11+
import { configureEntraAuthentication } from "@azure/postgresql-auth";
1212
import dotenv from "dotenv";
1313

1414
dotenv.config();
@@ -28,7 +28,7 @@ async function main(): Promise<void> {
2828

2929
// Register the Entra ID `beforeConnect` hook.
3030
// This automatically sets the username and password on each new connection.
31-
configureEntraIdAuth(sequelize, credential);
31+
configureEntraAuthentication(sequelize, credential);
3232

3333
try {
3434
console.log("Connecting to Azure Database for PostgreSQL via Sequelize...");

sdk/postgresql/postgresql-auth/src/entraConnection.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import type { TokenCredential } from "@azure/core-auth";
55
import { logger } from "./logger.js";
66

77
/**
8-
* Options for {@link getEntraTokenPassword}.
8+
* Options for {@link entraTokenProvider}.
99
*/
1010
export interface GetEntraTokenPasswordOptions {
1111
/**
@@ -104,10 +104,10 @@ function decodeJwtToken(token: string): DecodedJwtPayload | null {
104104
* @returns The same Sequelize instance, for chaining.
105105
*
106106
* @example
107-
* ```ts snippet:ConfigureEntraIdAuth
107+
* ```ts snippet:configureEntraAuthentication
108108
* import { DefaultAzureCredential } from "@azure/identity";
109109
*
110-
* const { configureEntraIdAuth } = await import("@azure/postgresql-auth");
110+
* const { configureEntraAuthentication } = await import("@azure/postgresql-auth");
111111
* const { Sequelize } = await import("sequelize");
112112
* const sequelize = new Sequelize({
113113
* dialect: "postgres",
@@ -116,11 +116,11 @@ function decodeJwtToken(token: string): DecodedJwtPayload | null {
116116
* database: process.env.PGDATABASE,
117117
* });
118118
* const credential = new DefaultAzureCredential();
119-
* configureEntraIdAuth(sequelize, credential);
119+
* configureEntraAuthentication(sequelize, credential);
120120
* await sequelize.authenticate();
121121
* ```
122122
*/
123-
export function configureEntraIdAuth(
123+
export function configureEntraAuthentication(
124124
sequelizeInstance: SequelizeBeforeConnectHook,
125125
credential: TokenCredential,
126126
options: ConfigureEntraIdAuthOptions = {},
@@ -133,7 +133,7 @@ export function configureEntraIdAuth(
133133
// Runs before every new connection is created by Sequelize
134134
sequelizeInstance.beforeConnect(async (config: { username?: string; password?: string }) => {
135135
logger.info("Fetching Entra ID access token...");
136-
const token = await getEntraTokenPassword(credential);
136+
const token = await entraTokenProvider(credential);
137137

138138
// Derive username from token if you want (optional):
139139
const claims = decodeJwtToken(token);
@@ -163,23 +163,23 @@ export function configureEntraIdAuth(
163163
* @returns A promise that resolves to the access token string.
164164
*
165165
* @example
166-
* ```ts snippet:GetEntraTokenPassword
166+
* ```ts snippet:entraTokenProvider
167167
* import { DefaultAzureCredential } from "@azure/identity";
168168
*
169-
* const { getEntraTokenPassword } = await import("@azure/postgresql-auth");
169+
* const { entraTokenProvider } = await import("@azure/postgresql-auth");
170170
* const pg = await import("pg");
171171
* const credential = new DefaultAzureCredential();
172172
* const pool = new pg.Pool({
173173
* host: process.env.PGHOST,
174174
* port: Number(process.env.PGPORT || 5432),
175175
* database: process.env.PGDATABASE,
176176
* user: process.env.PGUSER,
177-
* password: () => getEntraTokenPassword(credential),
177+
* password: () => entraTokenProvider(credential),
178178
* ssl: { rejectUnauthorized: true },
179179
* });
180180
* ```
181181
*/
182-
export async function getEntraTokenPassword(
182+
export async function entraTokenProvider(
183183
credential: TokenCredential,
184184
options: GetEntraTokenPasswordOptions = {},
185185
): Promise<string> {

sdk/postgresql/postgresql-auth/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
*
1111
* @packageDocumentation
1212
*/
13-
export { configureEntraIdAuth, getEntraTokenPassword } from "./entraConnection.js";
13+
export { configureEntraAuthentication, entraTokenProvider } from "./entraConnection.js";
1414
export type {
1515
ConfigureEntraIdAuthOptions,
1616
GetEntraTokenPasswordOptions,

sdk/postgresql/postgresql-auth/test/public/configureEntraIdAuth.spec.ts

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// Licensed under the MIT License.
33

44
import { describe, it, expect, vi, afterEach } from "vitest";
5-
import { configureEntraIdAuth } from "@azure/postgresql-auth";
5+
import { configureEntraAuthentication } from "@azure/postgresql-auth";
66
import {
77
createValidJwtToken,
88
createJwtTokenWithAppId,
@@ -17,14 +17,14 @@ afterEach(() => {
1717
vi.unstubAllEnvs();
1818
});
1919

20-
describe("configureEntraIdAuth", () => {
20+
describe("configureEntraAuthentication", () => {
2121
describe("hook registration", () => {
2222
it("should register a beforeConnect callback on the sequelize instance", () => {
2323
const mock = createMockSequelizeInstance();
2424
const token = createValidJwtToken(TEST_USERS.ENTRA_USER);
2525
const credential = new TestTokenCredential(token);
2626

27-
configureEntraIdAuth(mock, credential);
27+
configureEntraAuthentication(mock, credential);
2828

2929
expect(mock.capturedCallback).toBeDefined();
3030
expect(typeof mock.capturedCallback).toBe("function");
@@ -35,7 +35,7 @@ describe("configureEntraIdAuth", () => {
3535
const token = createValidJwtToken(TEST_USERS.ENTRA_USER);
3636
const credential = new TestTokenCredential(token);
3737

38-
const result = configureEntraIdAuth(mock, credential);
38+
const result = configureEntraAuthentication(mock, credential);
3939

4040
expect(result).toBe(mock);
4141
});
@@ -44,7 +44,7 @@ describe("configureEntraIdAuth", () => {
4444
const mock = createMockSequelizeInstance();
4545

4646
// eslint-disable-next-line @typescript-eslint/no-explicit-any
47-
expect(() => configureEntraIdAuth(mock, null as any)).toThrow("credential is required");
47+
expect(() => configureEntraAuthentication(mock, null as any)).toThrow("credential is required");
4848
});
4949
});
5050

@@ -54,7 +54,7 @@ describe("configureEntraIdAuth", () => {
5454
const token = createValidJwtToken(TEST_USERS.ENTRA_USER);
5555
const credential = new TestTokenCredential(token);
5656

57-
configureEntraIdAuth(mock, credential);
57+
configureEntraAuthentication(mock, credential);
5858

5959
const config: { username?: string; password?: string } = {};
6060
await mock.capturedCallback!(config);
@@ -68,7 +68,7 @@ describe("configureEntraIdAuth", () => {
6868
const token = createValidJwtToken(TEST_USERS.ENTRA_USER);
6969
const credential = new TestTokenCredential(token);
7070

71-
configureEntraIdAuth(mock, credential);
71+
configureEntraAuthentication(mock, credential);
7272

7373
const config = { username: "old-user", password: "old-password" };
7474
await mock.capturedCallback!(config);
@@ -86,7 +86,7 @@ describe("configureEntraIdAuth", () => {
8686
const token = createJwtTokenWithAppId(TEST_USERS.MANAGED_IDENTITY_APP_ID);
8787
const credential = new TestTokenCredential(token);
8888

89-
configureEntraIdAuth(mock, credential);
89+
configureEntraAuthentication(mock, credential);
9090

9191
const config: { username?: string; password?: string } = {};
9292
await mock.capturedCallback!(config);
@@ -108,7 +108,7 @@ describe("configureEntraIdAuth", () => {
108108
const credential = new TestTokenCredential(token);
109109
const mock = createMockSequelizeInstance();
110110

111-
configureEntraIdAuth(mock, credential, {
111+
configureEntraAuthentication(mock, credential, {
112112
fallbackUsername: TEST_USERS.FALLBACK_USER,
113113
});
114114

@@ -132,7 +132,7 @@ describe("configureEntraIdAuth", () => {
132132
const credential = new TestTokenCredential(token);
133133
const mock = createMockSequelizeInstance();
134134

135-
configureEntraIdAuth(mock, credential);
135+
configureEntraAuthentication(mock, credential);
136136

137137
const config: { username?: string; password?: string } = {};
138138
await mock.capturedCallback!(config);
@@ -153,7 +153,7 @@ describe("configureEntraIdAuth", () => {
153153
const credential = new TestTokenCredential(token);
154154
const mock = createMockSequelizeInstance();
155155

156-
configureEntraIdAuth(mock, credential);
156+
configureEntraAuthentication(mock, credential);
157157

158158
const config: { username?: string; password?: string } = {};
159159
await expect(mock.capturedCallback!(config)).rejects.toThrow(
@@ -167,7 +167,7 @@ describe("configureEntraIdAuth", () => {
167167
const mock = createMockSequelizeInstance();
168168
const credential = new FailingTokenCredential("Simulated auth failure");
169169

170-
configureEntraIdAuth(mock, credential);
170+
configureEntraAuthentication(mock, credential);
171171

172172
const config: { username?: string; password?: string } = {};
173173
await expect(mock.capturedCallback!(config)).rejects.toThrow("Simulated auth failure");
@@ -181,7 +181,7 @@ describe("configureEntraIdAuth", () => {
181181
const credential = new TestTokenCredential(badToken);
182182
const mock = createMockSequelizeInstance();
183183

184-
configureEntraIdAuth(mock, credential);
184+
configureEntraAuthentication(mock, credential);
185185

186186
const config: { username?: string; password?: string } = {};
187187
// With no upn/appid/fallback, this should fail to determine a username
@@ -197,7 +197,7 @@ describe("configureEntraIdAuth", () => {
197197
const token = createValidJwtToken(TEST_USERS.ENTRA_USER);
198198
const credential = new TestTokenCredential(token);
199199

200-
configureEntraIdAuth(mock, credential);
200+
configureEntraAuthentication(mock, credential);
201201

202202
const config1: { username?: string; password?: string } = {};
203203
await mock.capturedCallback!(config1);

0 commit comments

Comments
 (0)