Skip to content

Commit 83937c4

Browse files
authored
Separate SASL and server passwords (#251)
* Separate SASL and server passwords * SASL auth logic tweaks * let/const
1 parent 69309ed commit 83937c4

File tree

2 files changed

+37
-4
lines changed

2 files changed

+37
-4
lines changed

docs/clientapi.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ new Irc.Client({
1717
auto_reconnect_max_retries: 3,
1818
ping_interval: 30,
1919
ping_timeout: 120,
20+
account: {
21+
account: 'username',
22+
password: 'account_password',
23+
},
2024
webirc: {
2125
password: '',
2226
username: '*',

src/commands/handlers/registration.js

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,8 @@ var handlers = {
131131
];
132132

133133
// Optional CAPs depending on settings
134-
if (handler.connection.options.password || handler.connection.options.sasl_mechanism === 'EXTERNAL') {
134+
const saslAuth = getSaslAuth(handler);
135+
if (saslAuth || handler.connection.options.sasl_mechanism === 'EXTERNAL') {
135136
want.push('sasl');
136137
}
137138
if (handler.connection.options.enable_chghost) {
@@ -256,9 +257,10 @@ var handlers = {
256257
return;
257258
}
258259

259-
const auth_str = handler.connection.options.nick + '\0' +
260-
handler.connection.options.nick + '\0' +
261-
handler.connection.options.password;
260+
const saslAuth = getSaslAuth(handler);
261+
const auth_str = saslAuth.account + '\0' +
262+
saslAuth.account + '\0' +
263+
saslAuth.password;
262264
const b = Buffer.from(auth_str, 'utf8');
263265
let b64 = b.toString('base64');
264266

@@ -354,6 +356,33 @@ var handlers = {
354356
}
355357
};
356358

359+
/**
360+
* Only use the nick+password combo if an account has not been specifically given.
361+
* If an account:{account,password} has been given, use it for SASL auth.
362+
*/
363+
function getSaslAuth(handler) {
364+
const options = handler.connection.options;
365+
if (options.account && options.account.account) {
366+
// An account username has been given, use it for SASL auth
367+
return {
368+
account: options.account.account,
369+
password: options.account.password || '',
370+
};
371+
} else if (options.account) {
372+
// An account object existed but without auth credentials
373+
return null;
374+
} else if (options.password) {
375+
// No account credentials found but we have a server password. Also use it for SASL
376+
// for ease of use
377+
return {
378+
account: options.nick,
379+
password: options.password,
380+
};
381+
}
382+
383+
return null;
384+
}
385+
357386
module.exports = function AddCommandHandlers(command_controller) {
358387
_.each(handlers, function(handler, handler_command) {
359388
command_controller.addHandler(handler_command, handler);

0 commit comments

Comments
 (0)