gssapi.js
7.49 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.resolveCname = exports.performGSSAPICanonicalizeHostName = exports.GSSAPI = exports.GSSAPICanonicalizationValue = void 0;
const dns = require("dns");
const deps_1 = require("../../deps");
const error_1 = require("../../error");
const utils_1 = require("../../utils");
const auth_provider_1 = require("./auth_provider");
/** @public */
exports.GSSAPICanonicalizationValue = Object.freeze({
on: true,
off: false,
none: 'none',
forward: 'forward',
forwardAndReverse: 'forwardAndReverse'
});
class GSSAPI extends auth_provider_1.AuthProvider {
auth(authContext, callback) {
const { connection, credentials } = authContext;
if (credentials == null)
return callback(new error_1.MongoMissingCredentialsError('Credentials required for GSSAPI authentication'));
const { username } = credentials;
function externalCommand(command, cb) {
return connection.command((0, utils_1.ns)('$external.$cmd'), command, undefined, cb);
}
makeKerberosClient(authContext, (err, client) => {
if (err)
return callback(err);
if (client == null)
return callback(new error_1.MongoMissingDependencyError('GSSAPI client missing'));
client.step('', (err, payload) => {
if (err)
return callback(err);
externalCommand(saslStart(payload), (err, result) => {
if (err)
return callback(err);
if (result == null)
return callback();
negotiate(client, 10, result.payload, (err, payload) => {
if (err)
return callback(err);
externalCommand(saslContinue(payload, result.conversationId), (err, result) => {
if (err)
return callback(err);
if (result == null)
return callback();
finalize(client, username, result.payload, (err, payload) => {
if (err)
return callback(err);
externalCommand({
saslContinue: 1,
conversationId: result.conversationId,
payload
}, (err, result) => {
if (err)
return callback(err);
callback(undefined, result);
});
});
});
});
});
});
});
}
}
exports.GSSAPI = GSSAPI;
function makeKerberosClient(authContext, callback) {
var _a;
const { hostAddress } = authContext.options;
const { credentials } = authContext;
if (!hostAddress || typeof hostAddress.host !== 'string' || !credentials) {
return callback(new error_1.MongoInvalidArgumentError('Connection must have host and port and credentials defined.'));
}
if ('kModuleError' in deps_1.Kerberos) {
return callback(deps_1.Kerberos['kModuleError']);
}
const { initializeClient } = deps_1.Kerberos;
const { username, password } = credentials;
const mechanismProperties = credentials.mechanismProperties;
const serviceName = (_a = mechanismProperties.SERVICE_NAME) !== null && _a !== void 0 ? _a : 'mongodb';
performGSSAPICanonicalizeHostName(hostAddress.host, mechanismProperties, (err, host) => {
var _a;
if (err)
return callback(err);
const initOptions = {};
if (password != null) {
Object.assign(initOptions, { user: username, password: password });
}
const spnHost = (_a = mechanismProperties.SERVICE_HOST) !== null && _a !== void 0 ? _a : host;
let spn = `${serviceName}${process.platform === 'win32' ? '/' : '@'}${spnHost}`;
if ('SERVICE_REALM' in mechanismProperties) {
spn = `${spn}@${mechanismProperties.SERVICE_REALM}`;
}
initializeClient(spn, initOptions, (err, client) => {
// TODO(NODE-3483)
if (err)
return callback(new error_1.MongoRuntimeError(err));
callback(undefined, client);
});
});
}
function saslStart(payload) {
return {
saslStart: 1,
mechanism: 'GSSAPI',
payload,
autoAuthorize: 1
};
}
function saslContinue(payload, conversationId) {
return {
saslContinue: 1,
conversationId,
payload
};
}
function negotiate(client, retries, payload, callback) {
client.step(payload, (err, response) => {
// Retries exhausted, raise error
if (err && retries === 0)
return callback(err);
// Adjust number of retries and call step again
if (err)
return negotiate(client, retries - 1, payload, callback);
// Return the payload
callback(undefined, response || '');
});
}
function finalize(client, user, payload, callback) {
// GSS Client Unwrap
client.unwrap(payload, (err, response) => {
if (err)
return callback(err);
// Wrap the response
client.wrap(response || '', { user }, (err, wrapped) => {
if (err)
return callback(err);
// Return the payload
callback(undefined, wrapped);
});
});
}
function performGSSAPICanonicalizeHostName(host, mechanismProperties, callback) {
const mode = mechanismProperties.CANONICALIZE_HOST_NAME;
if (!mode || mode === exports.GSSAPICanonicalizationValue.none) {
return callback(undefined, host);
}
// If forward and reverse or true
if (mode === exports.GSSAPICanonicalizationValue.on ||
mode === exports.GSSAPICanonicalizationValue.forwardAndReverse) {
// Perform the lookup of the ip address.
dns.lookup(host, (error, address) => {
// No ip found, return the error.
if (error)
return callback(error);
// Perform a reverse ptr lookup on the ip address.
dns.resolvePtr(address, (err, results) => {
// This can error as ptr records may not exist for all ips. In this case
// fallback to a cname lookup as dns.lookup() does not return the
// cname.
if (err) {
return resolveCname(host, callback);
}
// If the ptr did not error but had no results, return the host.
callback(undefined, results.length > 0 ? results[0] : host);
});
});
}
else {
// The case for forward is just to resolve the cname as dns.lookup()
// will not return it.
resolveCname(host, callback);
}
}
exports.performGSSAPICanonicalizeHostName = performGSSAPICanonicalizeHostName;
function resolveCname(host, callback) {
// Attempt to resolve the host name
dns.resolveCname(host, (err, r) => {
if (err)
return callback(undefined, host);
// Get the first resolve host id
if (r.length > 0) {
return callback(undefined, r[0]);
}
callback(undefined, host);
});
}
exports.resolveCname = resolveCname;
//# sourceMappingURL=gssapi.js.map