From 3c04f3cc505a6908f3813183cb698a05e698daef Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Fri, 27 Oct 2017 18:00:53 +0200 Subject: [PATCH] Updated API to manage TokenTree --- public_html/js/lib/api.js | 15 +++++++++++++-- public_html/js/lib/min/api.js | 5 +---- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/public_html/js/lib/api.js b/public_html/js/lib/api.js index a678ac7..32b009f 100755 --- a/public_html/js/lib/api.js +++ b/public_html/js/lib/api.js @@ -67,6 +67,10 @@ APIClass.prototype = { /* si success de requête */ if( [0,200,417].indexOf(ptrAPI.xhr[i].status) > -1 ){ // si fichier existe et reçu + + // Récupération token TreeToken + window._token_ = ptrAPI.xhr[i].getResponseHeader('X-Tree-Token'); + var parsedResponse = {ModuleError:-1, ErrorDescription:'Erreur au niveau de api.js'}; // On essaie de parser le json @@ -107,7 +111,14 @@ APIClass.prototype = { // Gestion du token optionnel - if( pToken != null ) this.xhr[i].setRequestHeader('Authorization', 'Digest '+pToken); + if( pToken != null ) + this.xhr[i].setRequestHeader('Authorization', 'Digest '+pToken); + + // Gestion du token (TreeToken) + if( window._token_ == null ) + this.xhr[i].setRequestHeader('X-Tree-Token', JSON.parse('{"'+document.cookie.replace(/=/g,'":"').replace(/; /g,'","')+'"}')._PUBLIC_); + else + this.xhr[i].setRequestHeader('X-Tree-Token', window._token_); // Header pour dire que c'est AJAX @@ -118,4 +129,4 @@ APIClass.prototype = { this.xhr[i].send( form ); } -}; +}; \ No newline at end of file diff --git a/public_html/js/lib/min/api.js b/public_html/js/lib/min/api.js index b49697f..2036737 100755 --- a/public_html/js/lib/min/api.js +++ b/public_html/js/lib/min/api.js @@ -1,4 +1 @@ -function APIClass(c){this.target=c} -APIClass.prototype={xhr:[],buffer:null,optionalParams:[],send:function(c,f,g){c.hasOwnProperty("path")||f({ModuleError:4});for(var a=0;a3)for(var o=3;o-1){window._token_=n.xhr[s].getResponseHeader("X-Tree-Token");var t={ModuleError:-1,ErrorDescription:"Erreur au niveau de api.js"};try{t=JSON.parse(n.xhr[s].responseText)}catch(e){}console.log("api response",t),t.hasOwnProperty("ModuleError")&&0!=t.ModuleError&&console.error("ModuleError::"+t.ModuleError),r(t,n.optionalParams[s])}else r({ModuleError:-2,ModuleDescription:"Erreur au niveau d'AJAX."})};var a=new FormData;for(var i in e)"path"==i?a.append(i,e[i]):e[i]instanceof File?a.append(i,e[i]):a.append(i,JSON.stringify(e[i]));this.xhr[s].open("POST",this.target,!0),null!=t&&this.xhr[s].setRequestHeader("Authorization","Digest "+t),null==window._token_?this.xhr[s].setRequestHeader("X-Tree-Token",JSON.parse('{"'+document.cookie.replace(/=/g,'":"').replace(/; /g,'","')+'"}')._PUBLIC_):this.xhr[s].setRequestHeader("X-Tree-Token",window._token_),this.xhr[s].setRequestHeader("X-Requested-With","XMLHttpRequest"),this.xhr[s].send(a)}}; \ No newline at end of file