Permalink
Browse files

use ack objects in secureConnect instead of onSecureConnect tls options

  • Loading branch information...
1 parent b216bf6 commit f8c4f766100abcac04e66dde9689ea5e3b4abe5a @substack committed Feb 3, 2012
Showing with 37 additions and 32 deletions.
  1. +22 −11 index.js
  2. +15 −21 test/tls.js
View
@@ -37,20 +37,31 @@ exports.connect = function () {
var tlsOpts = options.tls;
if (tlsOpts) {
- var onSecureConnect = tlsOpts.onSecureConnect;
- delete tlsOpts.onSecureConnect;
-
stream = tls.connect(port, host, tlsOpts, function () {
- var ok = onSecureConnect
- ? onSecureConnect(stream)
- : stream.authorized
- ;
+ var pending = stream.listeners('secureConnect').length;
+ var allOk = pending === 0 ? stream.authorized : true;
+ if (pending === 0) done()
+ else {
+ var ack = {
+ accept : function (ok) {
+ allOk = allOk && (ok !== false);
+ if (--pending === 0) done();
+ },
+ reject : function () {
+ allOk = false;
+ if (--pending === 0) done();
+ }
+ };
+ stream.emit('secureConnect', stream, ack);
+ }
- if (!ok) {
- stream.end();
- stream.emit('error', new Error(stream.authorizationError));
+ function done () {
+ if (!allOk) {
+ stream.end();
+ stream.emit('error', new Error(stream.authorizationError));
+ }
+ else cb(proto.server(stream));
}
- else cb(proto.server(stream));
});
}
View
@@ -13,7 +13,7 @@ var keys = {
function makeServer () {
var server = tls.createServer({key: keys.key, cert: keys.cert});
- server.on('secureConnection', function(s) {
+ server.on('secureConnection', function (s) {
s.write("220 localhost ESMTP\r\n");
s.on('data', function(data) {
s.end("421 Service unavailable\r\n");
@@ -45,17 +45,8 @@ test('TLS - unauthorized with callback', {timeout: 1000}, function(t) {
t.plan(3);
var server = makeServer();
- var options = {
- tls: {
- onSecureConnect: function(s) {
- t.ok(!s.authorized);
- return true;
- }
- }
- };
-
server.listen(serverPort, function() {
- var c = smtp.connect(serverPort, options, function (session) {
+ var c = smtp.connect(serverPort, { tls : true }, function (session) {
server.close();
session.on('greeting', function (code, messages) {
t.equal(code, 220);
@@ -64,25 +55,22 @@ test('TLS - unauthorized with callback', {timeout: 1000}, function(t) {
session.quit();
})
});
+
+ c.on('secureConnect', function (s, ack) {
+ t.ok(!s.authorized);
+ ack.accept();
+ });
});
});
test('TLS - authorized', {timeout: 1000}, function(t) {
t.plan(3);
var server = makeServer();
- var options = {
- tls: {
- ca: [ keys.ca ],
- onSecureConnect: function(s) {
- t.ok(s.authorized, "should be authorized");
- return s.authorized;
- }
- }
- };
+ var options = { tls: { ca: [ keys.ca ] } };
server.listen(serverPort, function() {
- smtp.connect(serverPort, options, function (session) {
+ var c = smtp.connect(serverPort, options, function (session) {
server.close();
session.on('greeting', function(code, messages) {
@@ -92,5 +80,11 @@ test('TLS - authorized', {timeout: 1000}, function(t) {
session.quit();
})
});
+
+ c.on('secureConnect', function (s, ack) {
+ t.ok(s.authorized, "should be authorized");
+ if (s.authorized) ack.accept()
+ else ack.reject()
+ });
});
});

0 comments on commit f8c4f76

Please sign in to comment.