summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAzul <azul@leap.se>2012-07-02 14:26:44 +0200
committerAzul <azul@leap.se>2012-07-02 14:26:44 +0200
commit6bb45b271def50935bea8869ccb39c35c0c725be (patch)
tree4827cf655b53435f62bb8657044e3c5e80ca1175
parentf2930d4f6f0310a4e764e58cd5ef3dc674d11e14 (diff)
factored out parsing the responses
-rw-r--r--javascript/srp.js104
-rw-r--r--javascript/srp_register.js16
2 files changed, 78 insertions, 42 deletions
diff --git a/javascript/srp.js b/javascript/srp.js
index 9d1a91b..7bb6e27 100644
--- a/javascript/srp.js
+++ b/javascript/srp.js
@@ -84,12 +84,6 @@ function SRP()
}
};
- // Get the text content of an XML node
- this.innerxml = function(node)
- {
- return node.firstChild.nodeValue;
- };
-
// Check whether or not a variable is defined
function isdefined ( variable)
{
@@ -117,7 +111,7 @@ function SRP()
if(xhr){
xhr.onreadystatechange = function() {
if(xhr.readyState == 4 && xhr.status == 200) {
- callback();
+ callback(parseResponse());
}
};
xhr.open("POST", full_url, true);
@@ -131,6 +125,52 @@ function SRP()
}
};
+ function parseResponse() {
+ if (responseIsXML()) {
+ return parseXML(xhr.responseXML);
+ } else if (responseIsJSON()) {
+ return JSON.parse(xhr.responseText);
+ }
+ };
+
+ function responseIsXML() {
+ return (xhr.responseType == 'document') ||
+ (xhr.responseHeaders["Content-Type"].indexOf('application/xml') >= 0)
+ }
+
+ function responseIsJSON() {
+ return (xhr.responseType == 'json') ||
+ (xhr.responseHeaders["Content-Type"].indexOf('application/json') > 0)
+ }
+
+ function parseXML(xml) {
+ if (xml.getElementsByTagName("r").length > 0) {
+ return parseAttributesOfElement(xml.getElementsByTagName("r")[0]);
+ } else {
+ return parseNodes(xml.childNodes);
+ }
+ };
+
+ function parseAttributesOfElement(elem) {
+ var response = {};
+ for (var i = 0; i < elem.attributes.length; i++) {
+ var attrib = elem.attributes[i];
+ if (attrib.specified) {
+ response[attrib.name] = attrib.value;
+ }
+ }
+ return response;
+ };
+
+ function parseNodes(nodes) {
+ var response = {};
+ for (var i = 0; i < nodes.length; i++) {
+ var node = nodes[i];
+ response[node.tagName] = node.textContent || true;
+ }
+ return response;
+ };
+
// Start the login process by identifying the user
this.identify = function()
{
@@ -140,23 +180,21 @@ function SRP()
};
// Receive login salts from the server, start calculations
- function receive_salts()
+ function receive_salts(response)
{
- if(xhr.responseXML.getElementsByTagName("r").length > 0)
+ if(response.error) {
+ that.error_message(response.error);
+ }
+ // If there is no algorithm specified, calculate M given s, B, and P
+ else if(!response.a)
{
- var response = xhr.responseXML.getElementsByTagName("r")[0];
- // If there is no algorithm specified, calculate M given s, B, and P
- if(!response.getAttribute("a"))
- {
- calculations(response.getAttribute("s"), response.getAttribute("B"), p);
- that.ajaxRequest(url+that.paths("authenticate/"), "M="+M, confirm_authentication);
- }
- // If there is an algorithm specified, start the login process
- else
- upgrade(response.getAttribute("s"), response.getAttribute("B"), response.getAttribute("a"), response.getAttribute("d"));
+ calculations(response.s, response.B, p);
+ that.ajaxRequest(url+that.paths("authenticate/"), "M="+M, confirm_authentication);
}
- else if(xhr.responseXML.getElementsByTagName("error").length > 0)
- that.error_message(xhr.responseXML.getElementsByTagName("error")[0]);
+ // If there is an algorithm specified, start the login process
+ else {
+ upgrade(response.s, response.B, response.a, response.d);
+ }
};
// Calculate S, M, and M2
// This is the client side of the SRP specification
@@ -181,11 +219,11 @@ function SRP()
};
// Receive M2 from the server and verify it
- function confirm_authentication()
+ function confirm_authentication(response)
{
- if(xhr.responseXML.getElementsByTagName("M").length > 0)
+ if(response.M)
{
- if(that.innerxml(xhr.responseXML.getElementsByTagName("M")[0]) == M2)
+ if(response.M == M2)
{
authenticated = true;
success();
@@ -193,8 +231,8 @@ function SRP()
else
that.error_message("Server key does not match");
}
- else if (xhr.responseXML.getElementsByTagName("error").length > 0)
- that.error_message(that.innerxml(xhr.responseXML.getElementsByTagName("error")[0]));
+ else if (response.error)
+ that.error_message(response.error);
};
// *** Upgrades ***
@@ -242,11 +280,11 @@ function SRP()
// Receive the server's M, confirming that the server has HASH(p)
// Next, send P in plaintext (this is the **only** time it should ever be sent plain text)
- function confirm_upgrade()
+ function confirm_upgrade(response)
{
- if(xhr.responseXML.getElementsByTagName("M").length > 0)
+ if(response.M)
{
- if(that.innerxml(xhr.responseXML.getElementsByTagName("M")[0]) == M2)
+ if(response.M == M2)
{
K = SHA256(S.toString(16));
var auth_url = url + that.paths("upgrade/verifier/");
@@ -255,17 +293,17 @@ function SRP()
else
that.error_message("Server key does not match");
}
- else if (xhr.responseXML.getElementsByTagName("error").length > 0)
+ else if (response.error)
{
- that.error_message(that.innerxml(xhr.responseXML.getElementsByTagName("error")[0]));
+ that.error_message(response.error);
}
};
// After sending the password, check that the response is OK, then reidentify
- function confirm_verifier()
+ function confirm_verifier(response)
{
K = null;
- if(xhr.responseXML.getElementsByTagName("ok").length > 0)
+ if(response.ok)
that.identify();
else
that.error_message("Verifier could not be confirmed");
diff --git a/javascript/srp_register.js b/javascript/srp_register.js
index ffadf81..053c4e4 100644
--- a/javascript/srp_register.js
+++ b/javascript/srp_register.js
@@ -12,19 +12,18 @@ function SRP_REGISTER()
};
// Receive the salt for registration
- SRP.prototype.register_receive_salt = function()
+ SRP.prototype.register_receive_salt = function(response)
{
- var xhr = that.getxhr();
- if(xhr.responseXML.getElementsByTagName("salt").length > 0)
+ if(response.salt)
{
- var s = that.innerxml(xhr.responseXML.getElementsByTagName("salt")[0]);
+ var s = response.salt;
var x = that.calcX(s);
var v = that.getg().modPow(x, that.getN());
that.register_send_verifier(v.toString(16));
}
- else if(xhr.responseXML.getElementsByTagName("error").length > 0)
+ else if(response.error)
{
- that.error_message(that.innerxml(xhr.responseXML.getElementsByTagName("error")[0]));
+ that.error_message(response.error);
}
};
// Send the verifier to the server
@@ -36,10 +35,9 @@ function SRP_REGISTER()
};
// The user has been registered successfully, now login
- SRP.prototype.register_user = function()
+ SRP.prototype.register_user = function(response)
{
- var xhr = that.getxhr();
- if(xhr.responseXML.getElementsByTagName("ok").length > 0)
+ if(response.ok)
{
that.identify();
}