Skip to content

Commit 17952d2

Browse files
refactor(NODE-5953): move promisifying of randomBytes to utils (#3999)
1 parent 46b7bbb commit 17952d2

File tree

3 files changed

+8
-11
lines changed

3 files changed

+8
-11
lines changed

Diff for: src/cmap/auth/mongodb_aws.ts

+2-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
import * as crypto from 'crypto';
21
import * as process from 'process';
3-
import { promisify } from 'util';
42

53
import type { Binary, BSONSerializeOptions } from '../../bson';
64
import * as BSON from '../../bson';
@@ -11,7 +9,7 @@ import {
119
MongoMissingCredentialsError,
1210
MongoRuntimeError
1311
} from '../../error';
14-
import { ByteUtils, maxWireVersion, ns, request } from '../../utils';
12+
import { ByteUtils, maxWireVersion, ns, randomBytes, request } from '../../utils';
1513
import { type AuthContext, AuthProvider } from './auth_provider';
1614
import { MongoCredentials } from './mongo_credentials';
1715
import { AuthMechanism } from './providers';
@@ -59,11 +57,9 @@ interface AWSSaslContinuePayload {
5957
export class MongoDBAWS extends AuthProvider {
6058
static credentialProvider: ReturnType<typeof getAwsCredentialProvider>;
6159
provider?: () => Promise<AWSCredentials>;
62-
randomBytesAsync: (size: number) => Promise<Buffer>;
6360

6461
constructor() {
6562
super();
66-
this.randomBytesAsync = promisify(crypto.randomBytes);
6763
MongoDBAWS.credentialProvider ??= getAwsCredentialProvider();
6864

6965
let { AWS_STS_REGIONAL_ENDPOINTS = '', AWS_REGION = '' } = process.env;
@@ -131,7 +127,7 @@ export class MongoDBAWS extends AuthProvider {
131127
: undefined;
132128

133129
const db = credentials.source;
134-
const nonce = await this.randomBytesAsync(32);
130+
const nonce = await randomBytes(32);
135131

136132
const saslStart = {
137133
saslStart: 1,

Diff for: src/cmap/auth/scram.ts

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
import { saslprep } from '@mongodb-js/saslprep';
22
import * as crypto from 'crypto';
3-
import { promisify } from 'util';
43

54
import { Binary, type Document } from '../../bson';
65
import {
76
MongoInvalidArgumentError,
87
MongoMissingCredentialsError,
98
MongoRuntimeError
109
} from '../../error';
11-
import { ns } from '../../utils';
10+
import { ns, randomBytes } from '../../utils';
1211
import type { HandshakeDocument } from '../connect';
1312
import { type AuthContext, AuthProvider } from './auth_provider';
1413
import type { MongoCredentials } from './mongo_credentials';
@@ -18,11 +17,10 @@ type CryptoMethod = 'sha1' | 'sha256';
1817

1918
class ScramSHA extends AuthProvider {
2019
cryptoMethod: CryptoMethod;
21-
randomBytesAsync: (size: number) => Promise<Buffer>;
20+
2221
constructor(cryptoMethod: CryptoMethod) {
2322
super();
2423
this.cryptoMethod = cryptoMethod || 'sha1';
25-
this.randomBytesAsync = promisify(crypto.randomBytes);
2624
}
2725

2826
override async prepare(
@@ -35,7 +33,7 @@ class ScramSHA extends AuthProvider {
3533
throw new MongoMissingCredentialsError('AuthContext must provide credentials.');
3634
}
3735

38-
const nonce = await this.randomBytesAsync(24);
36+
const nonce = await randomBytes(24);
3937
// store the nonce for later use
4038
authContext.nonce = nonce;
4139

Diff for: src/utils.ts

+3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import * as http from 'http';
44
import { clearTimeout, setTimeout } from 'timers';
55
import * as url from 'url';
66
import { URL } from 'url';
7+
import { promisify } from 'util';
78

89
import { type Document, ObjectId, resolveBSONOptions } from './bson';
910
import type { Connection } from './cmap/connection';
@@ -1292,3 +1293,5 @@ export function promiseWithResolvers<T>() {
12921293
});
12931294
return { promise, resolve, reject } as const;
12941295
}
1296+
1297+
export const randomBytes = promisify(crypto.randomBytes);

0 commit comments

Comments
 (0)