Skip to content

Commit a6a0e15

Browse files
committed
chore: cleanup
1 parent a7a5206 commit a6a0e15

File tree

4 files changed

+22
-9
lines changed

4 files changed

+22
-9
lines changed

src/cmap/auth/mongodb_oidc.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ import type { Workflow } from './mongodb_oidc/workflow';
1212
*/
1313
export const OIDC_VERSION = 0;
1414

15+
/** Error when credentials are missing. */
16+
const MISSING_CREDENTIALS_ERROR = 'AuthContext must provide credentials.';
17+
1518
/**
1619
* @public
1720
* @experimental
@@ -110,7 +113,7 @@ export class MongoDBOIDC extends AuthProvider {
110113
function getCredentials(authContext: AuthContext): MongoCredentials {
111114
const { credentials } = authContext;
112115
if (!credentials) {
113-
throw new MongoMissingCredentialsError('AuthContext must provide credentials.');
116+
throw new MongoMissingCredentialsError(MISSING_CREDENTIALS_ERROR);
114117
}
115118
return credentials;
116119
}

src/cmap/auth/mongodb_oidc/aws_service_workflow.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ import { readFile } from 'fs/promises';
33
import { MongoAWSError } from '../../../error';
44
import { ServiceWorkflow } from './service_workflow';
55

6+
/** Error for when the token is missing in the environment. */
7+
const TOKEN_MISSING_ERROR = 'AWS_WEB_IDENTITY_TOKEN_FILE must be set in the environment.';
8+
69
/**
710
* Device workflow implementation for AWS.
811
*
@@ -19,7 +22,7 @@ export class AwsServiceWorkflow extends ServiceWorkflow {
1922
async getToken(): Promise<string> {
2023
const tokenFile = process.env.AWS_WEB_IDENTITY_TOKEN_FILE;
2124
if (!tokenFile) {
22-
throw new MongoAWSError('AWS_WEB_IDENTITY_TOKEN_FILE must be set in the environment.');
25+
throw new MongoAWSError(TOKEN_MISSING_ERROR);
2326
}
2427
return readFile(tokenFile, 'utf8');
2528
}

src/cmap/auth/mongodb_oidc/callback_workflow.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,14 @@ const TIMEOUT_S = 300;
2727
/** Properties allowed on results of callbacks. */
2828
const RESULT_PROPERTIES = ['accessToken', 'expiresInSeconds', 'refreshToken'];
2929

30+
/** Error message when the callback result is invalid. */
31+
const CALLBACK_RESULT_ERROR =
32+
'User provided OIDC callbacks must return a valid object with an accessToken.';
33+
34+
/** Error message for when request callback is missing. */
35+
const REQUEST_CALLBACK_REQUIRED_ERROR =
36+
'Auth mechanism property REQUEST_TOKEN_CALLBACK is required.';
37+
3038
/**
3139
* OIDC implementation of a callback based workflow.
3240
* @internal
@@ -64,9 +72,7 @@ export class CallbackWorkflow implements Workflow {
6472
const refreshCallback = credentials.mechanismProperties.REFRESH_TOKEN_CALLBACK;
6573
// At minimum a request callback must be provided by the user.
6674
if (!requestCallback) {
67-
throw new MongoInvalidArgumentError(
68-
'Auth mechanism property REQUEST_TOKEN_CALLBACK is required.'
69-
);
75+
throw new MongoInvalidArgumentError(REQUEST_CALLBACK_REQUIRED_ERROR);
7076
}
7177
// Look for an existing entry in the cache.
7278
const entry = this.cache.getEntry(
@@ -239,15 +245,14 @@ export class CallbackWorkflow implements Workflow {
239245
// Validate that the result returned by the callback is acceptable. If it is not
240246
// we must clear the token result from the cache.
241247
if (isCallbackResultInvalid(result)) {
248+
console.log('GOT ERROR, DELETE FROM CACHE AND THROW');
242249
this.cache.deleteEntry(
243250
connection.address,
244251
credentials.username || '',
245252
requestCallback,
246253
refreshCallback || null
247254
);
248-
throw new MongoMissingCredentialsError(
249-
'User provided OIDC callbacks must return a valid object with an accessToken.'
250-
);
255+
throw new MongoMissingCredentialsError(CALLBACK_RESULT_ERROR);
251256
}
252257
// Cleanup the cache.
253258
this.cache.deleteExpiredEntries();

test/manual/mongodb_oidc.prose.test.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -783,7 +783,9 @@ describe('MONGODB-OIDC', function () {
783783
try {
784784
await collection.findOne();
785785
expect.fail('Expected OIDC auth to fail with invalid fields from refresh callback');
786-
} catch (e) {
786+
} catch (error) {
787+
expect(error).to.be.instanceOf(MongoMissingCredentialsError);
788+
expect(error.message).to.include('');
787789
expect(cache.entries.size).to.equal(0);
788790
}
789791
});

0 commit comments

Comments
 (0)