diff options
author | Azul <azul@riseup.net> | 2012-11-14 12:34:45 +0100 |
---|---|---|
committer | Azul <azul@riseup.net> | 2012-11-14 12:34:45 +0100 |
commit | 527eb51ad4b943bde7e2ba411f5772611a80463c (patch) | |
tree | 4be112443f48a29763b6adae5f68d521022dcc55 /src | |
parent | 23350b54ec2723e1b2e333626567c9fe9d1e2644 (diff) | |
parent | 2859af0287d7672df0a8965be43fb9859fca8bf8 (diff) |
Merge branch 'release/0.2.0'
Diffstat (limited to 'src')
-rw-r--r-- | src/jqueryRest.js | 52 | ||||
-rw-r--r-- | src/plainXHR.js | 120 |
2 files changed, 3 insertions, 169 deletions
diff --git a/src/jqueryRest.js b/src/jqueryRest.js index f50080b..54a0908 100644 --- a/src/jqueryRest.js +++ b/src/jqueryRest.js @@ -1,51 +1,5 @@ jqueryRest = function() { - function parseResponse() { - if (responseIsXML()) { - return parseXML(xhr.responseXML); - } else if (responseIsJSON()) { - return JSON.parse(xhr.responseText); - } - } - - function responseIsXML() { - return (xhr.responseType == 'document') || - (xhr.getResponseHeader("Content-Type").indexOf('application/xml') >= 0); - } - - function responseIsJSON() { - return (xhr.responseType == 'json') || - (xhr.getResponseHeader("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; - } - // we do not fetch the salt from the server function register(session, callback) { @@ -54,7 +8,7 @@ jqueryRest = function() { function sendVerifier(session, callback) { var salt = session.getSalt(); - $.post("users", { user: + $.post("users.json", { user: { login: session.getI(), password_salt: salt, password_verifier: session.getV(salt).toString(16)} @@ -62,13 +16,13 @@ jqueryRest = function() { } function handshake(session, callback) { - $.post("sessions", { login: session.getI(), + $.post("sessions.json", { login: session.getI(), A: session.getAstr()}, callback); } function authenticate(session, success) { $.ajax({ - url: "sessions/" + session.getI(), + url: "sessions/" + session.getI() + ".json", type: 'PUT', data: {client_auth: session.getM()}, success: success diff --git a/src/plainXHR.js b/src/plainXHR.js deleted file mode 100644 index c03c90a..0000000 --- a/src/plainXHR.js +++ /dev/null @@ -1,120 +0,0 @@ -// -// SRP JS - Plain XHR module -// -// This is deprecated - unless you are using srp-js with the original drupal -// server side I recommend you use a different API such as restful.js -// -// This code has been largely refactored, tests are still passing but I did -// not test it with the server itself. - -SRP.prototype.Remote = function() { - - // Perform ajax requests at the specified path, with the specified parameters - // Calling back the specified function. - function ajaxRequest(url, params, callback) - { - if( window.XMLHttpRequest) { - xhr = new XMLHttpRequest(); - } - else if (window.ActiveXObject){ - try{ - xhr = new ActiveXObject("Microsoft.XMLHTTP"); - }catch (e){} - } - else - { - session.error_message("Ajax not supported."); - return; - } - if(xhr){ - xhr.onreadystatechange = function() { - if(xhr.readyState == 4 && xhr.status == 200) { - callback(parseResponse()); - } - }; - xhr.open("POST", url, true); - xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); - xhr.setRequestHeader("Content-length", params.length); - xhr.send(params); - } - else - { - session.error_message("Ajax failed."); - } - } - - function parseResponse() { - if (responseIsXML()) { - return parseXML(xhr.responseXML); - } else if (responseIsJSON()) { - return JSON.parse(xhr.responseText); - } - } - - function responseIsXML() { - return (xhr.responseType == 'document') || - (xhr.getResponseHeader("Content-Type").indexOf('application/xml') >= 0); - } - - function responseIsJSON() { - return (xhr.responseType == 'json') || - (xhr.getResponseHeader("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; - } - - // Drupal version fetches the salt from the server. No idea why but this - // should still do it. - this.register = function(session, callback) - { - function receive_salt(response) { - if(response.salt) - { - var s = response.salt; - var v = session.getV(s); - that.sendVerifier(session, callback); - } - } - - var that = this; - ajaxRequest("register/salt/", "I="+session.getI(), receive_salt); - }; - - this.sendVerifier = function(session, callback) { - ajaxRequest("register/user/", "v="+session.getV().toString(16), callback); - }; - - this.handshake = function(session, callback) { - ajaxRequest("handshake/", "I="+session.getI()+"&A="+session.getAstr(), callback); - }; - - this.authenticate = function(session, callback) { - ajaxRequest("authenticate/", "M="+session.getM(), callback); - }; -}; |