MCPcopy Create free account
hub / github.com/Effect-TS/effect / handleMessage

Function handleMessage

packages/sql/pg/src/PgConnection.ts:2493–2593  ·  view source on GitHub ↗
(message: PgProtocol.BackendMessage<unknown>)

Source from the content-addressed store, hash-verified

2491 }
2492
2493 const handleMessage = (message: PgProtocol.BackendMessage<unknown>): void => {
2494 switch (message._tag) {
2495 case "AuthenticationOk":
2496 case "NoticeResponse":
2497 case "NegotiateProtocolVersion":
2498 case "ParameterStatus":
2499 return
2500 case "AuthenticationCleartextPassword": {
2501 const secret = password()
2502 if (secret === undefined) return
2503 socket.write(PgProtocol.encodePasswordMessage({ password: secret }))
2504 return
2505 }
2506 case "AuthenticationMD5Password": {
2507 const secret = password()
2508 if (secret === undefined) return
2509 const hashed = PgAuth.md5Password({ user: config.username, password: secret, salt: message.salt })
2510 if (EffectResult.isFailure(hashed)) {
2511 return failAuth(hashed.failure, "PgConnection: MD5 authentication failed")
2512 }
2513 socket.write(PgProtocol.encodePasswordMessage({ password: hashed.success }))
2514 return
2515 }
2516 case "AuthenticationSASL": {
2517 if (!message.mechanisms.includes(PgAuth.SCRAM_SHA_256)) {
2518 return failAuth(
2519 new Error(`Unsupported SASL mechanisms: ${message.mechanisms.join(", ")}`),
2520 `PgConnection: Only ${PgAuth.SCRAM_SHA_256} is supported`
2521 )
2522 }
2523 const secret = password()
2524 if (secret === undefined) return
2525 const init = PgAuth.scramInit({ password: secret, nonce: randomBytes(18).toString("base64") })
2526 if (EffectResult.isFailure(init)) {
2527 return failAuth(init.failure, "PgConnection: SCRAM authentication failed")
2528 }
2529 scram = init.success.state
2530 socket.write(PgProtocol.encodeSASLInitialResponse({
2531 mechanism: PgAuth.SCRAM_SHA_256,
2532 initialResponse: init.success.response
2533 }))
2534 return
2535 }
2536 case "AuthenticationSASLContinue": {
2537 if (scram === undefined || scram._tag !== "ScramFirst") {
2538 return failConnect(new Error("Unexpected AuthenticationSASLContinue"), "PgConnection: Protocol desync")
2539 }
2540 const next = PgAuth.scramContinue(scram, message.data, config.scramSlot?.current)
2541 if (EffectResult.isFailure(next)) {
2542 return failAuth(next.failure, "PgConnection: SCRAM authentication failed")
2543 }
2544 if (config.scramSlot !== undefined) {
2545 config.scramSlot.current = next.success.saltedPassword
2546 }
2547 scram = next.success.state
2548 socket.write(PgProtocol.encodeSASLResponse({ data: next.success.response }))
2549 return
2550 }

Callers 1

onDataFunction · 0.70

Calls 11

failAuthFunction · 0.85
randomBytesFunction · 0.85
failConnectFunction · 0.85
failErrorResponseFunction · 0.85
passwordFunction · 0.70
writeMethod · 0.65
toStringMethod · 0.65
onMethod · 0.65
resumeFunction · 0.50
joinMethod · 0.45
succeedMethod · 0.45

Tested by

no test coverage detected