/usr/lib/nodejs/node-xmpp/xmpp/sasl.js is in node-node-xmpp 0.3.2-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 | var crypto = require('crypto');
var querystring = require('querystring');
var util = require('util');
var EventEmitter = require('events').EventEmitter;
/**
* What's available for client-side authentication (Client)
*
* @param {Array} mechs Server-offered SASL mechanism names
*/
function selectMechanism(offeredMechs, preferredMech) {
var mechClasses = [XFacebookPlatform, DigestMD5,
Plain, Anonymous];
var byName = {};
var mech;
mechClasses.forEach(function(mechClass) {
byName[mechClass.prototype.name] = mechClass;
});
/* Any preferred? */
if (byName[preferredMech]) {
mech = byName[preferredMech];
}
/* By priority */
mechClasses.forEach(function(mechClass) {
if (!mech &&
offeredMechs.indexOf(mechClass.prototype.name) >= 0)
mech = mechClass;
});
return mech ? new mech() : null;
}
exports.selectMechanism = selectMechanism;
/**
* What's available for server-side authentication (C2S)
*/
function availableMechanisms() {
return [new Plain()];
}
exports.availableMechanisms = availableMechanisms;
// Mechanisms
function Mechanism() {
EventEmitter.call(this);
}
util.inherits(Mechanism, EventEmitter);
function Plain() {
}
util.inherits(Plain, Mechanism);
Plain.prototype.name = "PLAIN";
Plain.prototype.auth = function() {
return this.authzid + "\0" +
this.authcid + "\0" +
this.password;
};
Plain.prototype.authServer = function(auth, client) {
var params = auth.split("\x00");
this.username = params[1];
client.authenticate(this.username, params[2]);
};
function XFacebookPlatform() {
}
util.inherits(XFacebookPlatform, Mechanism);
XFacebookPlatform.prototype.name = "X-FACEBOOK-PLATFORM";
XFacebookPlatform.prototype.auth = function() {
return "";
};
XFacebookPlatform.prototype.challenge = function(s) {
var dict = querystring.parse(s);
var response = {
api_key: this.api_key,
call_id: new Date().getTime(),
method: dict.method,
nonce: dict.nonce,
access_token: this.access_token,
v: "1.0"
};
return querystring.stringify(response);
};
function Anonymous() {
}
util.inherits(Anonymous, Mechanism);
Anonymous.prototype.name = "ANONYMOUS";
Anonymous.prototype.auth = function() {
return this.authzid;
};
function DigestMD5() {
this.nonce_count = 0;
this.cnonce = generateNonce();
}
util.inherits(DigestMD5, Mechanism);
DigestMD5.prototype.name = "DIGEST-MD5";
DigestMD5.prototype.auth = function() {
return "";
};
DigestMD5.prototype.getNC = function() {
return rjust(this.nonce_count.toString(), 8, '0');
};
DigestMD5.prototype.responseValue = function(s) {
var dict = parseDict(s);
if (dict.realm)
this.realm = dict.realm;
var value;
if (dict.nonce && dict.qop) {
this.nonce_count++;
var a1 = md5(this.authcid + ':' +
this.realm + ':' +
this.password) + ':' +
dict.nonce + ':' +
this.cnonce + ':' +
this.authzid || "";
var a2 = "AUTHENTICATE:" + this.digest_uri;
if (dict.qop == 'auth-int' || dict.qop == 'auth-conf')
a2 += ":00000000000000000000000000000000";
value = md5_hex(md5_hex(a1) + ':' +
dict.nonce + ':' +
this.getNC() + ':' +
this.cnonce + ':' +
dict.qop + ':' +
md5_hex(a2));
}
return value;
};
DigestMD5.prototype.challenge = function(s) {
var dict = parseDict(s);
if (dict.realm)
this.realm = dict.realm;
var response;
if (dict.nonce && dict.qop) {
var responseValue = this.responseValue(s);
response = {
username: this.authcid,
realm: this.realm,
nonce: dict.nonce,
cnonce: this.cnonce,
nc: this.getNC(),
qop: dict.qop,
'digest-uri': this.digest_uri,
response: responseValue,
authzid: this.authzid || "",
charset: 'utf-8'
};
} else if (dict.rspauth) {
return "";
}
return encodeDict(response);
};
DigestMD5.prototype.serverChallenge = function() {
var dict = {};
dict.realm = "";
this.nonce = dict.nonce = generateNonce();
dict.qop = "auth";
this.charset = dict.charset = "utf-8";
dict.algorithm = "md5-sess";
return encodeDict(dict);
};
// Used on the server to check for auth!
DigestMD5.prototype.response = function(s) {
var dict = parseDict(s);
this.authcid = dict.username;
if(dict.nonce != this.nonce) {
return false;
}
if(!dict.cnonce) {
return false;
}
this.cnonce = dict.cnonce;
if(this.charset != dict.charset) {
return false;
}
this.response = dict.response;
return true;
};
/**
* Parse SASL serialization
*/
function parseDict(s) {
var result = {};
while (s) {
var m;
if((m = /^(.+?)=(.*?[^\\]),(.*)/.exec(s))) {
result[m[1]] = m[2].replace(/\"/g, '');
s = m[3];
} else if ((m = /^(.+?)=(.+?),(.*)/.exec(s))) {
result[m[1]] = m[2];
s = m[3];
} else if ((m = /^(.+?)="(.*?[^\\])"$/.exec(s))) {
result[m[1]] = m[2];
s = m[3];
} else if ((m = /^(.+?)=(.+?)$/.exec(s))) {
result[m[1]] = m[2];
s = m[3];
} else {
s = null;
}
}
return result;
}
/**
* SASL serialization
*/
function encodeDict(dict) {
var s = "";
for(k in dict) {
var v = dict[k];
if (v)
s += ',' + k + '="' + v + '"';
}
return s.substr(1); // without first ','
}
/**
* Right-justify a string,
* eg. pad with 0s
*/
function rjust(s, targetLen, padding) {
while(s.length < targetLen)
s = padding + s;
return s;
}
/**
* Hash a string
*/
function md5(s, encoding) {
var hash = crypto.createHash('md5');
hash.update(s);
return hash.digest(encoding || 'binary');
}
/**
* Hash a string hexadecimally
*/
function md5_hex(s) {
return md5(s, 'hex');
}
/**
* Generate a string of 8 digits
* (number used once)
*/
function generateNonce() {
var result = "";
for(var i = 0; i < 8; i++)
result += String.fromCharCode(48 +
Math.ceil(Math.random() * 10));
return result;
}
|