-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathcreate-authenticator.ts
45 lines (41 loc) · 1.46 KB
/
create-authenticator.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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import { AuthenticationProviderArgs, Authenticator } from "kafkajs";
import {
CreatePayload,
createSaslAuthenticationRequest,
createSaslAuthenticationResponse,
TYPE,
Options,
} from ".";
export const createAuthenticator =
(options: Options) =>
(args: AuthenticationProviderArgs): Authenticator => ({
authenticate: async () => {
const { host, port, logger, saslAuthenticate } = args;
const broker = `${host}:${port}`;
const payloadFactory = new CreatePayload(options);
try {
const payload = await payloadFactory.create({ brokerHost: host });
const authenticateResponse = await saslAuthenticate({
request: createSaslAuthenticationRequest(payload),
response: createSaslAuthenticationResponse,
});
logger.info("Authentication response", { authenticateResponse });
const isValidResponse =
authenticateResponse &&
typeof authenticateResponse === "object" &&
"version" in authenticateResponse &&
authenticateResponse.version;
if (!isValidResponse) {
throw new Error("Invalid response from broker");
}
logger.info(`SASL ${TYPE} authentication successful`, { broker });
} catch (error) {
if (error instanceof Error) {
logger.error(error?.message, { broker });
} else if (typeof error === "string") {
logger.error(error, { broker });
}
throw error;
}
},
});