@platformatic/kafka
Version:
Modern and performant client for Apache Kafka
33 lines (32 loc) • 1.28 kB
JavaScript
import { createPromisifiedCallback, kCallbackPromise } from "../../apis/callbacks.js";
import { AuthenticationError } from "../../errors.js";
import { getCredential } from "./credential-provider.js";
export function jwtValidateAuthenticationBytes(authBytes, callback) {
let authData = {};
try {
if (authBytes.length > 0) {
authData = JSON.parse(authBytes.toString('utf-8'));
}
/* c8 ignore next 8 - Hard to test */
}
catch (e) {
callback(new AuthenticationError('Invalid authBytes in SASL/OAUTHBEARER response', { authBytes }), undefined);
return;
}
if (authData.status === 'invalid_token') {
callback(new AuthenticationError('Invalid SASL/OAUTHBEARER token.', { authData }), undefined);
}
callback(null, authBytes);
}
export function authenticate(authenticateAPI, connection, tokenOrProvider, callback) {
if (!callback) {
callback = createPromisifiedCallback();
}
getCredential('SASL/OAUTHBEARER token', tokenOrProvider, (error, token) => {
if (error) {
return callback(error, undefined);
}
authenticateAPI(connection, Buffer.from(`n,,\x01auth=Bearer ${token}\x01\x01`), callback);
});
return callback[kCallbackPromise];
}