-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
plain.ts
25 lines (22 loc) · 858 Bytes
/
plain.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import { Binary } from '../../bson';
import { AuthProvider, AuthContext } from './auth_provider';
import { MongoMissingCredentialsError } from '../../error';
import { Callback, ns } from '../../utils';
export class Plain extends AuthProvider {
auth(authContext: AuthContext, callback: Callback): void {
const { connection, credentials } = authContext;
if (!credentials) {
return callback(new MongoMissingCredentialsError('AuthContext must provide credentials.'));
}
const username = credentials.username;
const password = credentials.password;
const payload = new Binary(Buffer.from(`\x00${username}\x00${password}`));
const command = {
saslStart: 1,
mechanism: 'PLAIN',
payload: payload,
autoAuthorize: 1
};
connection.command(ns('$external.$cmd'), command, undefined, callback);
}
}