Permalink
Browse files

Cleaned up encoding libs; removed SHA1 and legacy digest auth support…

…; renamed strophe.js to core.js.
  • Loading branch information...
1 parent 1eea96d commit 2c94de30762a24dd6ca7cc1c7297e0fb755647c8 @metajack metajack committed Nov 23, 2009
Showing with 348 additions and 547 deletions.
  1. +2 −3 Makefile
  2. +0 −74 src/b64.js
  3. +80 −0 src/base64.js
  4. +18 −31 src/{strophe.js → core.js}
  5. +248 −232 src/md5.js
  6. +0 −207 src/sha1.js
View
@@ -3,10 +3,9 @@ DOC_DIR = doc
PLUGIN_DIR = plugins
NDPROJ_DIR = ndproj
-BASE_FILES = $(SRC_DIR)/b64.js \
+BASE_FILES = $(SRC_DIR)/base64.js \
$(SRC_DIR)/md5.js \
- $(SRC_DIR)/sha1.js \
- $(SRC_DIR)/strophe.js
+ $(SRC_DIR)/core.js
STROPHE = strophe.js
STROPHE_MIN = strophe.min.js
View
@@ -1,74 +0,0 @@
-// This code was written by Tyler Akins and has been placed in the
-// public domain. It would be nice if you left this header intact.
-// Base64 code from Tyler Akins -- http://rumkin.com
-
-var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
-
-/**
- * Encodes a string in base64
- * @param {String} input The string to encode in base64.
- */
-function encode64(input) {
- var output = "";
- var chr1, chr2, chr3;
- var enc1, enc2, enc3, enc4;
- var i = 0;
-
- do {
- chr1 = input.charCodeAt(i++);
- chr2 = input.charCodeAt(i++);
- chr3 = input.charCodeAt(i++);
-
- enc1 = chr1 >> 2;
- enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
- enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
- enc4 = chr3 & 63;
-
- if (isNaN(chr2)) {
- enc3 = enc4 = 64;
- } else if (isNaN(chr3)) {
- enc4 = 64;
- }
-
- output = output + keyStr.charAt(enc1) + keyStr.charAt(enc2) +
- keyStr.charAt(enc3) + keyStr.charAt(enc4);
- } while (i < input.length);
-
- return output;
-}
-
-/**
- * Decodes a base64 string.
- * @param {String} input The string to decode.
- */
-function decode64(input) {
- var output = "";
- var chr1, chr2, chr3;
- var enc1, enc2, enc3, enc4;
- var i = 0;
-
- // remove all characters that are not A-Z, a-z, 0-9, +, /, or =
- input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
-
- do {
- enc1 = keyStr.indexOf(input.charAt(i++));
- enc2 = keyStr.indexOf(input.charAt(i++));
- enc3 = keyStr.indexOf(input.charAt(i++));
- enc4 = keyStr.indexOf(input.charAt(i++));
-
- chr1 = (enc1 << 2) | (enc2 >> 4);
- chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
- chr3 = ((enc3 & 3) << 6) | enc4;
-
- output = output + String.fromCharCode(chr1);
-
- if (enc3 != 64) {
- output = output + String.fromCharCode(chr2);
- }
- if (enc4 != 64) {
- output = output + String.fromCharCode(chr3);
- }
- } while (i < input.length);
-
- return output;
-}
View
@@ -0,0 +1,80 @@
+// This code was written by Tyler Akins and has been placed in the
+// public domain. It would be nice if you left this header intact.
+// Base64 code from Tyler Akins -- http://rumkin.com
+
+var Base64 = (function () {
+ var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
+
+ var obj = {
+ /**
+ * Encodes a string in base64
+ * @param {String} input The string to encode in base64.
+ */
+ encode: function (input) {
+ var output = "";
+ var chr1, chr2, chr3;
+ var enc1, enc2, enc3, enc4;
+ var i = 0;
+
+ do {
+ chr1 = input.charCodeAt(i++);
+ chr2 = input.charCodeAt(i++);
+ chr3 = input.charCodeAt(i++);
+
+ enc1 = chr1 >> 2;
+ enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
+ enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
+ enc4 = chr3 & 63;
+
+ if (isNaN(chr2)) {
+ enc3 = enc4 = 64;
+ } else if (isNaN(chr3)) {
+ enc4 = 64;
+ }
+
+ output = output + keyStr.charAt(enc1) + keyStr.charAt(enc2) +
+ keyStr.charAt(enc3) + keyStr.charAt(enc4);
+ } while (i < input.length);
+
+ return output;
+ },
+
+ /**
+ * Decodes a base64 string.
+ * @param {String} input The string to decode.
+ */
+ decode: function (input) {
+ var output = "";
+ var chr1, chr2, chr3;
+ var enc1, enc2, enc3, enc4;
+ var i = 0;
+
+ // remove all characters that are not A-Z, a-z, 0-9, +, /, or =
+ input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
+
+ do {
+ enc1 = keyStr.indexOf(input.charAt(i++));
+ enc2 = keyStr.indexOf(input.charAt(i++));
+ enc3 = keyStr.indexOf(input.charAt(i++));
+ enc4 = keyStr.indexOf(input.charAt(i++));
+
+ chr1 = (enc1 << 2) | (enc2 >> 4);
+ chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
+ chr3 = ((enc3 & 3) << 6) | enc4;
+
+ output = output + String.fromCharCode(chr1);
+
+ if (enc3 != 64) {
+ output = output + String.fromCharCode(chr2);
+ }
+ if (enc4 != 64) {
+ output = output + String.fromCharCode(chr3);
+ }
+ } while (i < input.length);
+
+ return output;
+ }
+ };
+
+ return obj;
+})();
@@ -2531,7 +2531,7 @@ Strophe.Connection.prototype = {
this._sasl_failure_cb.bind(this), null,
"failure", null, null);
- hashed_auth_str = encode64(auth_str);
+ hashed_auth_str = Base64.encode(auth_str);
this.send($build("auth", {
xmlns: Strophe.NS.SASL,
mechanism: "PLAIN"
@@ -2564,8 +2564,8 @@ Strophe.Connection.prototype = {
{
var attribMatch = /([a-z]+)=("[^"]+"|[^,"]+)(?:,|$)/;
- var challenge = decode64(Strophe.getText(elem));
- var cnonce = hex_md5(Math.random() * 1234567890);
+ var challenge = Base64.decode(Strophe.getText(elem));
+ var cnonce = MD5.hexdigest(Math.random() * 1234567890);
var realm = "";
var host = null;
var nonce = "";
@@ -2600,8 +2600,8 @@ Strophe.Connection.prototype = {
digest_uri = digest_uri + "/" + host;
}
- var A1 = str_md5(Strophe.getNodeFromJid(this.jid) +
- ":" + realm + ":" + this.pass) +
+ var A1 = MD5.hash(Strophe.getNodeFromJid(this.jid) +
+ ":" + realm + ":" + this.pass) +
":" + nonce + ":" + cnonce;
var A2 = 'AUTHENTICATE:' + digest_uri;
@@ -2614,10 +2614,11 @@ Strophe.Connection.prototype = {
responseText += 'nc="00000001",';
responseText += 'qop="auth",';
responseText += 'digest-uri=' + this._quote(digest_uri) + ',';
- responseText += 'response=' + this._quote(hex_md5(hex_md5(A1) + ":" +
- nonce + ":00000001:" +
- cnonce + ":auth:" +
- hex_md5(A2))) + ',';
+ responseText += 'response=' + this._quote(
+ MD5.hexdigest(MD5.hexdigest(A1) + ":" +
+ nonce + ":00000001:" +
+ cnonce + ":auth:" +
+ MD5.hexdigest(A2))) + ',';
responseText += 'charset="utf-8"';
this._sasl_challenge_handler = this._addSysHandler(
@@ -2632,7 +2633,7 @@ Strophe.Connection.prototype = {
this.send($build('response', {
xmlns: Strophe.NS.SASL
- }).t(encode64(responseText)).tree());
+ }).t(Base64.encode(responseText)).tree());
return false;
},
@@ -2648,7 +2649,8 @@ Strophe.Connection.prototype = {
*/
_quote: function (str)
{
- return '"' + str.replace(/\\/g, "\\\\").replace(/"/g, '\\"') + '"';
+ return '"' + str.replace(/\\/g, "\\\\").replace(/"/g, '\\"') + '"';
+ //" end string workaround for emacs
},
@@ -2693,30 +2695,15 @@ Strophe.Connection.prototype = {
*/
_auth1_cb: function (elem)
{
- var use_digest = false;
var check_query, check_digest;
- if (elem.getAttribute("type") == "result") {
- // Find digest
- check_query = elem.childNodes[0];
- if (check_query) {
- check_digest = check_query.getElementsByTagName("digest")[0];
- if (check_digest) {
- use_digest = true;
- }
- }
- }
-
- // Use digest or plaintext depending on the server features
+ // build plaintext auth iq
var iq = $iq({type: "set", id: "_auth_2"})
.c('query', {xmlns: Strophe.NS.AUTH})
- .c('username', {}).t(Strophe.getNodeFromJid(this.jid));
- if (use_digest) {
- iq.up().c("digest", {})
- .t(hex_sha1(this.stream_id + this.pass));
- } else {
- iq.up().c('password', {}).t(this.pass);
- }
+ .c('username', {}).t(Strophe.getNodeFromJid(this.jid))
+ .up()
+ .c('password').t(this.pass);
+
if (!Strophe.getResourceFromJid(this.jid)) {
// since the user has not supplied a resource, we pick
// a default one here. unlike other auth methods, the server
Oops, something went wrong.

0 comments on commit 2c94de3

Please sign in to comment.