Skip to content

Commit 5c6d31c

Browse files
committed
test: get token audience from props
1 parent 65c10db commit 5c6d31c

File tree

3 files changed

+25
-9
lines changed

3 files changed

+25
-9
lines changed

src/cmap/auth/mongo_credentials.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import type { Document } from '../../bson';
33
import {
44
MongoAPIError,
5+
MongoAzureError,
56
MongoInvalidArgumentError,
67
MongoMissingCredentialsError
78
} from '../../error';
@@ -43,6 +44,10 @@ export const DEFAULT_ALLOWED_HOSTS = [
4344
'::1'
4445
];
4546

47+
/** Error for when the token audience is missing in the environment. */
48+
const TOKEN_AUDIENCE_MISSING_ERROR =
49+
'TOKEN_AUDIENCE must be set in the auth mechanism properties when PROVIDER_NAME is azure.';
50+
4651
/** @public */
4752
export interface AuthMechanismProperties extends Document {
4853
SERVICE_HOST?: string;
@@ -55,9 +60,11 @@ export interface AuthMechanismProperties extends Document {
5560
/** @experimental */
5661
REFRESH_TOKEN_CALLBACK?: OIDCRefreshFunction;
5762
/** @experimental */
58-
PROVIDER_NAME?: 'aws';
63+
PROVIDER_NAME?: 'aws' | 'azure';
5964
/** @experimental */
6065
ALLOWED_HOSTS?: string[];
66+
/** @experimental */
67+
TOKEN_AUDIENCE?: string;
6168
}
6269

6370
/** @public */
@@ -177,6 +184,13 @@ export class MongoCredentials {
177184
);
178185
}
179186

187+
if (
188+
this.mechanismProperties.PROVIDER_NAME === 'azure' &&
189+
!this.mechanismProperties.TOKEN_AUDIENCE
190+
) {
191+
throw new MongoAzureError(TOKEN_AUDIENCE_MISSING_ERROR);
192+
}
193+
180194
if (
181195
this.mechanismProperties.PROVIDER_NAME &&
182196
!ALLOWED_PROVIDER_NAMES.includes(this.mechanismProperties.PROVIDER_NAME)

src/cmap/auth/mongodb_oidc/azure_service_workflow.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
import { MongoAzureError } from '../../../error';
22
import { request } from '../../../utils';
3+
import type { MongoCredentials } from '../mongo_credentials';
34
import { AzureTokenCache } from './azure_token_cache';
45
import { ServiceWorkflow } from './service_workflow';
56

6-
/** Error for when the token audience is missing in the environment. */
7-
const TOKEN_AUDIENCE_MISSING_ERROR = 'TOKEN_AUDIENCE must be set in the environment.';
8-
97
/** Base URL for getting Azure tokens. */
108
const AZURE_BASE_URL =
119
'http://169.254.169.254/metadata/identity/oauth2/token?api-version=2018-02-01';
@@ -20,6 +18,10 @@ const RESULT_PROPERTIES = ['access_token', 'expires_in'];
2018
const ENDPOINT_RESULT_ERROR =
2119
'Azure endpoint did not return a value with only access_token and expires_in properties';
2220

21+
/** Error for when the token audience is missing in the environment. */
22+
const TOKEN_AUDIENCE_MISSING_ERROR =
23+
'TOKEN_AUDIENCE must be set in the auth mechanism properties when PROVIDER_NAME is azure.';
24+
2325
/**
2426
* The Azure access token format.
2527
* @internal
@@ -48,8 +50,8 @@ export class AzureServiceWorkflow extends ServiceWorkflow {
4850
/**
4951
* Get the token from the environment.
5052
*/
51-
async getToken(): Promise<string> {
52-
const tokenAudience = process.env.TOKEN_AUDIENCE;
53+
async getToken(credentials?: MongoCredentials): Promise<string> {
54+
const tokenAudience = credentials?.mechanismProperties.TOKEN_AUDIENCE;
5355
if (!tokenAudience) {
5456
throw new MongoAzureError(TOKEN_AUDIENCE_MISSING_ERROR);
5557
}

src/cmap/auth/mongodb_oidc/service_workflow.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export abstract class ServiceWorkflow implements Workflow {
1616
* and then attempts to read the token from that path.
1717
*/
1818
async execute(connection: Connection, credentials: MongoCredentials): Promise<Document> {
19-
const token = await this.getToken();
19+
const token = await this.getToken(credentials);
2020
const command = commandDocument(token);
2121
return connection.commandAsync(ns(credentials.source), command, undefined);
2222
}
@@ -25,7 +25,7 @@ export abstract class ServiceWorkflow implements Workflow {
2525
* Get the document to add for speculative authentication.
2626
*/
2727
async speculativeAuth(credentials: MongoCredentials): Promise<Document> {
28-
const token = await this.getToken();
28+
const token = await this.getToken(credentials);
2929
const document = commandDocument(token);
3030
document.db = credentials.source;
3131
return { speculativeAuthenticate: document };
@@ -34,7 +34,7 @@ export abstract class ServiceWorkflow implements Workflow {
3434
/**
3535
* Get the token from the environment or endpoint.
3636
*/
37-
abstract getToken(): Promise<string>;
37+
abstract getToken(credentials: MongoCredentials): Promise<string>;
3838
}
3939

4040
/**

0 commit comments

Comments
 (0)