All files / src/cmap/auth plain.ts

91.66% Statements 11/12
50% Branches 1/2
100% Functions 1/1
91.66% Lines 11/12

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 26139x 139x 139x 139x   139x   4x 4x       4x   4x 4x             4x      
import { Binary } from '../../bson';
import { MongoMissingCredentialsError } from '../../error';
import { ns } from '../../utils';
import { type AuthContext, AuthProvider } from './auth_provider';
 
export class Plain extends AuthProvider {
  override async auth(authContext: AuthContext): Promise<void> {
    const { connection, credentials } = authContext;
    Iif (!credentials) {
      throw new MongoMissingCredentialsError('AuthContext must provide credentials.');
    }
 
    const { username, password } = credentials;
 
    const payload = new Binary(Buffer.from(`\x00${username}\x00${password}`));
    const command = {
      saslStart: 1,
      mechanism: 'PLAIN',
      payload: payload,
      autoAuthorize: 1
    };
 
    await connection.command(ns('$external.$cmd'), command, undefined);
  }
}