-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathchannel-binding.js
More file actions
47 lines (38 loc) · 1.65 KB
/
Copy pathchannel-binding.js
File metadata and controls
47 lines (38 loc) · 1.65 KB
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
46
47
'use strict'
// Support for libpq's channel_binding parameter, which says whether SCRAM authentication
// has to be bound to the server's certificate:
// https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-CONNECT-CHANNEL-BINDING
const nodeUtils = require('util')
const defaults = require('./defaults')
const channelBindingLevels = ['disable', 'prefer', 'require']
const emitEnableChannelBindingDeprecationNotice = nodeUtils.deprecate(
() => {},
'enableChannelBinding is deprecated: instead, please set channel_binding to "disable", "prefer" or "require"'
)
function channelBindingFromDeprecatedBoolean(value, force = false) {
if (value === undefined && !force) return // pass undefined straight through, no warning, unless forced
emitEnableChannelBindingDeprecationNotice()
// note: we pass through valid string values to avoid confusion, otherwise
// "disable" and "require" would both resolve as truthy and mean "prefer"
return channelBindingLevels.includes(value) ? value : value ? 'prefer' : 'disable'
}
function validatedChannelBinding(value) {
if (!channelBindingLevels.includes(value)) {
throw new Error(`Invalid channel_binding value: "${value}". Valid values are "disable", "prefer" and "require".`)
}
return value
}
function resolveChannelBinding(channelBinding, enableChannelBinding) {
const value =
channelBinding ??
channelBindingFromDeprecatedBoolean(enableChannelBinding) ??
process.env.PGCHANNELBINDING ??
defaults.channel_binding
return validatedChannelBinding(value)
}
module.exports = {
channelBindingLevels,
channelBindingFromDeprecatedBoolean,
validatedChannelBinding,
resolveChannelBinding,
}