1// **************************************************** 2// Auth - Gestion des Autentifications 3// **************************************************** 4 5angular.module('AboShopApp').factory('Auth', 6 function($q, $http, $rootScope, $localStorage, $location, commonConfig, $interval, $timeout){ 7 8 function _goAdministration(){ 9 $timeout(function() { 10 if($location.path() == "/message/administration"){ 11 location.reload(); 12 } else { 13 $location.path('message/administration'); 14 } 15 }); 16 } 17 18 function _getUrlVars(url) { 19 var vars = {}; 20 var parts = url.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) { 21 vars[key] = value; 22 }); 23 return vars; 24 } 25 26 function _setLoginInProgress(value){ 27 //console.error("#### loginInProgress="+value); 28 factory.loginInProgress = value; 29 } 30 31 function _endLoginFromCookie (deferred, token){ 32 if(token === undefined){ 33 let response= {}; 34 response.error_description = "DICO_ERROR_RETRY_LOGIN"; 35 $rootScope.signin.error= response; 36 deferred.reject(); 37 factory.loginError = true; 38 _setLoginInProgress(false); 39 } else { 40 console.log("_endLoginFromCookie with Token"); 41 factory.loginFromCookie(token).then(function(user) { 42 deferred.resolve(user); 43 }, function(e, status) { 44 deferred.reject(e); 45 factory.loginError = true; 46 }) 47 } 48 } 49 50 51 var factory = {}; 52 _setLoginInProgress(false); 53 factory.loginError = false; 54 factory.wasConnecte = false; 55 factory.deconnexionAuto = true; 56 57 factory.dologin = function dologin(user){ 58 let isClient = (user.scope !== undefined && user.scope.indexOf('private_') >= 0?true:false); 59 if($rootScope.user !== undefined && $rootScope.user.login === user.login && isClient){ 60 //aucun changement de user, on ne fait rien 61 if($rootScope.user.countReload !== undefined && $rootScope.user.countReload < 2) { 62 //on met un garde fou pour éviter le rechargement infini, après 2 tentatives, on reinit le user 63 return; 64 } 65 } 66 67 // Stockage de l'access_token et implémentation dans le header http 68 if(user && user.access_token) { 69 $localStorage[commonConfig.currentInstance.keylogin].access_token = user.access_token; 70 $http.defaults.headers.common[commonConfig.authentificationheader] = 'Bearer ' + $localStorage[commonConfig.currentInstance.keylogin].access_token; 71 } 72 73 // Alimentation des Variables 74 var currentUser = { 75 name: user.name, 76 login: user.login, 77 isClient: isClient, 78 scopes: (user.scope !== undefined?user.scope.split(" "):""), 79 info: user.info, 80 countReload : 0, 81 checksecurity : (user.checksecurity === true ? true : false) 82 }; 83 if(currentUser.isClient){ 84 $rootScope.signin.resetInProgress(); 85 } 86 87 88 $localStorage[commonConfig.currentInstance.keylogin].user = currentUser; 89 90 //console.log(JSON.stringify(user)); 91 //console.log(JSON.stringify($localStorage[commonConfig.currentInstance.keylogin])); 92 $rootScope.user = currentUser; 93 }; 94 95 factory.loginFromCookie = function loginFromCookie(tokenFromCookie) { 96 var deferred = $q.defer(); 97 $http.defaults.useXDomain = true; 98 99 var security = {}; 100 security.scope = 'private'; 101 security.editor = commonConfig.currentInstance.editor; 102 security.company = commonConfig.currentInstance.company; 103 if(commonConfig.currentInstance.idshop !== undefined){ 104 security.idshop = commonConfig.currentInstance.idshop; 105 } 106 107 var alldatas = {}; 108 alldatas.datas = {}; 109 alldatas.datas.oauth = security; 110 alldatas.datas.service = "getinformationtoken"; 111 alldatas.datas.target = commonConfig.target; 112 alldatas.datas.scope = commonConfig.scope; 113 114 delete $http.defaults.headers.common[commonConfig.authentificationheader]; 115 if (tokenFromCookie) { 116 $http.defaults.headers.common[commonConfig.authentificationheader] = 'Bearer ' + tokenFromCookie; 117 } 118
119 _setLoginInProgress(true); 120 factory.loginError = false; 121 122 $http( 123 { 124 method: 'POST', 125 url: factory.getUrlParams(alldatas), 126 data: factory.getQueryParams(alldatas,commonConfig.usegetapi), 127 headers: { 'Accept': 'application/json', 128 'Cache-Control': 'no-cache', 129 'Content-Type': commonConfig.contenttype} 130 } 131 ).success(function(user){ 132 factory.dologin(user); 133 deferred.resolve($rootScope.user); 134 _setLoginInProgress(false); 135 136 }).error(function(e, status){ 137 138 console.error("Erreur loginFromCookie"); 139 console.error(e); 140 console.error(status); 141 //Deconnexion du sso pour pouvoir se logguer avec un autre compte 142 factory.tryDoExternalLogout(true); 143 144 deferred.reject(e); 145 _setLoginInProgress(false); 146 }); 147 148 149 return deferred.promise; 150 }; 151 152 factory.tryDoExternalLogout = function tryDoExternalLogout(withLogMessage){ 153 if (commonConfig.oAuthExterne !== undefined && commonConfig.oAuthExterne.urlLogout != null) { 154 if(withLogMessage){ 155 console.error("On force la deconnexion externe sur "+commonConfig.oAuthExterne.urlLogout); 156 } 157 if(commonConfig.oAuthExterne.ssoQiota) { 158 let redirect_uri = commonConfig.commonroot + "/content/sso/backconnected.html"; 159 let logoutUrl = commonConfig.oAuthExterne.urlLogout; 160 161 let clocation = logoutUrl+'?redirect_uri='+ redirect_uri; 162 console.log(clocation); 163 164 factory.setLogoutIsInProgress(); 165 166 window.location = clocation; 167 } else { 168 $rootScope.initSilentIFrame("silentAboshopLogoutIframe", "silentiframedologout.html", undefined, $rootScope.callBackLogout); 169 } 170 } 171 } 172 173 factory.endLoginExternalToken = function endLoginExternalToken(code, deferred) { 174 let localDeferred = false; 175 if(deferred === undefined){ 176 console.log("localDeferred"); 177 localDeferred = true; 178 deferred = $q.defer(); 179 } 180 console.log("endLoginExternalToken code="+code); 181 if (code !== undefined && code !== "") { 182 console.log("call getExternalToken"); 183 factory.getExternalToken(code).then( 184 function(token){ 185 console.log("token="+token); 186 _endLoginFromCookie (deferred, token); 187 }, function(error){ 188 console.error(error); 189 _endLoginFromCookie (deferred); 190 } 191 ); 192 } else { 193 _endLoginFromCookie (deferred); 194 } 195 commonConfig.oAuthExterne.callBackCodeEnabled = false; 196 if(localDeferred){ 197 console.log("localDeferred promise"); 198 return deferred.promise; 199 } 200 }; 201 202 factory.login = function login(user) { 203 var deferred = $q.defer(); 204 $http.defaults.useXDomain = true; 205 206 if(factory.loginInProgress){ 207 //debugger 208 //On tente de synchronizer les demandes lorsque un login est en cours 209 var myVar = setInterval(function(){ wakeUp(); }, 500); 210 211 function wakeUp() { 212 if(!factory.loginInProgress){ 213 myStopFunction(); 214 } 215 } 216 217 function myStopFunction() { 218 clearInterval(myVar); 219 if(factory.loginError){ 220 deferred.reject(false); 221 } else { 222 deferred.resolve($rootScope.user); 223 } 224 225 } 226 } else { 227 _setLoginInProgress(true); 228 factory.loginError = false; 229 230 if(user == null){ 231 user = {}; 232 } 233 var createcookie=0; 234 var shopLogin = false; 235 if(user.login === undefined && user.password === undefined){ 236 shopLogin = true; 237 user.login = commonConfig.currentInstance.login; 238 user.password = commonConfig.currentInstance.pwd; 239 } else if(commonConfig.createcookie){ 240 //Le cookie est cree uniquement dans le cas d'une authentification cliente 241 createcookie = 1; 242 } 243 if(user.scope === undefined ){ 244 user.scope = ""; 245 } 246 247 var security = {}; 248 security.scope = (user.scope||commonConfig.currentInstance.code); 249 security.editor = commonConfig.currentInstance.editor; 250 security.company = commonConfig.currentInstance.company; 251 if(commonConfig.currentInstance.idshop !== undefined){ 252 security.idshop = commonConfig.currentInstance.idshop; 253 } 254 255 security.cookies = createcookie; 256 if(commonConfig.cookieforcename !== undefined) { 257 security.cookieforcename = commonConfig.cookieforcename; 258 } 259 if(commonConfig.cookiehttponly !== undefined) { 260 security.cookiehttponly = (commonConfig.cookiehttponly ? 1 : 0); 261 } 262 if(commonConfig.cookiesecure !== undefined) { 263 security.cookiesecure = (commonConfig.cookiesecure ? 1 : 0); 264 } 265 266 if(user.login.property !== undefined && user.login.value !== undefined){ 267 security[user.login.property] = encodeURIComponent(user.login.value); 268 } else { 269 security.email = encodeURIComponent(user.login); 270 } 271 security.password = encodeURIComponent(user.password); 272 273 if (user.captcha != '' && user.captcha !== undefined) { 274 security.captcha = encodeURIComponent(user.captcha); 275 } 276 277 var alldatas = {}; 278 alldatas.datas = {}; 279 alldatas.datas.oauth = security; 280 alldatas.datas.service = "login"; 281 alldatas.datas.target = commonConfig.target; 282 alldatas.datas.scope = commonConfig.scope; 283 284 if(alldatas.datas.forcetimeout === undefined && commonConfig.forcetimeout !== undefined){ 285 alldatas.datas.forcetimeout = commonConfig.forcetimeout; 286 } 287 288 //Avec un pooling de connexion, ça devrait pouvoir marcher 289 290 //var canceler = $q.defer(); 291 //$http.get('/someUrl', {timeout: canceler.promise}).success(successCallback); 292 // later... 293 //canceler.resolve(); // Aborts the $http request if it isn't finished. 294 if($localStorage[commonConfig.currentInstance.keylogin].access_token === undefined){ 295 delete $http.defaults.headers.common[commonConfig.authentificationheader]; 296 } 297 298 // Login avec un module OAuth externe 299 if (!shopLogin && commonConfig.oAuthExterne !== undefined && commonConfig.oAuthExterne.urlLogin != null) { 300 const oAuthParams = {};
301 let champOption = []; 302 oAuthParams.grant_type = commonConfig.oAuthExterne.grant_type; 303 oAuthParams.scope = commonConfig.oAuthExterne.scope; 304 305 if (oAuthParams.grant_type === 'password') { 306 if(user.login.property !== undefined && user.login.value !== undefined){ 307 oAuthParams.email = user.login.value; 308 } else { 309 oAuthParams.email = user.login; 310 } 311 oAuthParams.response_type = 'code'; 312 oAuthParams.password = user.password; 313 oAuthParams.redirect_uri = commonConfig.commonroot + "/content/sso/silentiframe.html"; 314 oAuthParams.error_uri = commonConfig.commonroot + "/content/sso/silentiframenotconnected.html"; 315 //oAuthParams.referer = window.location.origin; 316 317 champOption = ['client_id','response_type','redirect_uri','error_uri']; 318 319 } 320 /*if (oAuthParams.grant_type === 'password') { 321 if(user.login.property !== undefined && user.login.value !== undefined){ 322 oAuthParams.email = user.login.value; 323 } else { 324 oAuthParams.email = user.login; 325 } 326 oAuthParams.response_type = 'code'; 327 oAuthParams.password = user.password; 328 oAuthParams.redirect_uri = commonConfig.commonroot + "/content/sso/silentiframe.html"; 329 oAuthParams.error_uri = commonConfig.commonroot + "/content/sso/silentiframenotconnected.html"; 330 331 champOption = ['client_id','response_type','redirect_uri','error_uri']; 332 333 }*/ else if (oAuthParams.grant_type === 'authorization_code') { 334 oAuthParams.code = commonConfig.oAuthExterne.code; 335 champOption = ['client_id', 'redirect_uri']; 336 337 } else if (oAuthParams.grant_type === 'client_credentials') { 338 oAuthParams.client_id = commonConfig.oAuthExterne.client_id; 339 oAuthParams.client_secret = commonConfig.oAuthExterne.client_secret; 340 } 341 342 if (champOption.length > 0) { 343 for (var p in commonConfig.oAuthExterne) { 344 if (_.contains(champOption, p) && commonConfig.oAuthExterne[p] != null) { 345 oAuthParams[p] = commonConfig.oAuthExterne[p]; 346 } 347 } 348 } 349 350 let callBackCode = function callBackCode(code){ 351 console.log("callBackCode"); 352 factory.endLoginExternalToken(code, deferred); 353 } 354 355 if(commonConfig.oAuthExterne.ssoQiota) { 356 _setLoginInProgress(false); 357 commonConfig.oAuthExterne.callBackCodeEnabled = true; 358 commonConfig.oAuthExterne.callBackCode = callBackCode; 359 let idFrame = "silentAboshopLoginIframe"; 360 let onloadLogin = function onloadLogin() { 361 console.log(onloadLogin); 362 console.log(oAuthParams); 363 var ob = Object.assign({}, oAuthParams); 364 let iframe = document.getElementById(idFrame); 365 if(iframe !== null) { 366 iframe.onload = undefined; 367 iframe.contentWindow.postMessage(ob,iframe.contentWindow.origin); 368 } 369 }; 370 371 $rootScope.initSilentIFrame(idFrame, "silentiframeloginqiota.html", onloadLogin, $rootScope.callBackAuthorize); 372 } else { 373 factory.callExterne(commonConfig.oAuthExterne.urlLogin, oAuthParams).then( 374 function(response) { 375 let code; 376 if(response.data.url !== undefined && response.data.url !== ""){ 377 code = _getUrlVars(response.data.url)["code"]; 378 callBackCode(code); 379 return; 380 } 381 _endLoginFromCookie(deferred); 382 }, function(e){ 383 deferred.reject(e); 384 factory.loginError = true; 385 _setLoginInProgress(false); 386 } 387 ); 388 } 389 } else { 390 $http( 391 { 392 method: 'POST', 393 url: factory.getUrlParams(alldatas), 394 data: factory.getQueryParams(alldatas,commonConfig.usegetapi), 395 headers: { 'Accept': 'application/json', 396 'Cache-Control': 'no-cache', 397 'Content-Type': commonConfig.contenttype} 398 } 399 ).success(function(user){ 400 factory.dologin(user); 401 deferred.resolve($rootScope.user); 402 _setLoginInProgress(false); 403 404 }).error(function(e, status){ 405 deferred.reject(e); 406 factory.loginError = true; 407 _setLoginInProgress(false); 408 });
409 } 410 } 411 412 413 return deferred.promise; 414 }; 415 416 factory.getUrlParams = function getUrlParams(alldatas){ 417 let url = commonConfig.url; 418 if(commonConfig.usegetapi == false){ 419 url += "/" + alldatas.datas.oauth.scope + "/" + alldatas.datas.oauth.editor + "/" + alldatas.datas.oauth.company + "/" + alldatas.datas.service; 420 421 //console.log(url); 422 if(alldatas.datas.oauth.cookies !== undefined) delete alldatas.datas.oauth.cookies; 423 if(alldatas.datas.oauth.scope !== undefined) delete alldatas.datas.oauth.scope; 424 if(alldatas.datas.oauth.editor !== undefined) delete alldatas.datas.oauth.editor; 425 if(alldatas.datas.oauth.company !== undefined) delete alldatas.datas.oauth.company; 426 if(alldatas.datas.oauth.idshop !== undefined) delete alldatas.datas.oauth.idshop; 427 428 var i = 0; 429 for(var p in alldatas.datas.oauth){ 430 ++i; 431 } 432 if(i==0) delete alldatas.datas.oauth; 433 434 if(alldatas.datas.service !== undefined) delete alldatas.datas.service; 435 if(alldatas.datas.forcetimeout !== undefined) delete alldatas.datas.forcetimeout; 436 if(alldatas.datas.target !== undefined) delete alldatas.datas.target; 437 438 //console.log(alldatas); 439 //console.log(url); 440 441 } 442 return url; 443 }; 444 445 /** 446 * Cryptage des donnees avant envoie 447 * @param {Object} alldatas 448 */ 449 factory.getQueryParams = function getQueryParams(alldatas, usegetapi){ 450 if(usegetapi){ 451 if(commonConfig.doencrypt){ 452 alldatas.datas = factory.encrypt(JSON.stringify(alldatas.datas)); 453 } 454 let param = $.param(alldatas); 455 //console.log(param); 456 return param; 457 } else { 458 console.log(JSON.stringify(alldatas.datas)); 459 return alldatas.datas; 460 } 461 462 }; 463 464 factory.isClientConnecte = function isClientConnecte() { 465 if($rootScope.user && $rootScope.user !== undefined && $rootScope.user.isClient === true){ 466 return true; 467 } 468 return false; 469 }; 470 471 factory.setClientWasConnecte = function setClientWasConnecte(status) { 472 factory.wasConnecte = status; 473 }; 474 475 factory.clientWasConnecte = function clientWasConnecte() { 476 return factory.wasConnecte; 477 }; 478 479 factory.setDeconnexionAuto = function setDeconnexionAuto(status) { 480 factory.deconnexionAuto = status; 481 }; 482 483 factory.getDeconnexionAuto = function getDeconnexionAuto() { 484 return factory.deconnexionAuto; 485 }; 486 487 factory.getScopeEditor = function getScopeEditor() { 488 var deferred = $q.defer(); 489 $http.defaults.useXDomain = true; 490 491 var security = {}; 492 security.editor = commonConfig.currentInstance.editor; 493 security.company = commonConfig.currentInstance.company; 494 if(commonConfig.currentInstance.idshop !== undefined){ 495 security.idshop = commonConfig.currentInstance.idshop; 496 } 497 //security.email = encodeURIComponent(commonConfig.currentInstance.login); 498 //security.password = encodeURIComponent(commonConfig.currentInstance.pwd); 499 security.scope = 'public'; 500 501 var alldatas = {}; 502 alldatas.datas = {}; 503 alldatas.datas.oauth = security; 504 alldatas.datas.target = commonConfig.target; 505 alldatas.datas.scope = commonConfig.scope; 506 alldatas.datas.service = "scope"; 507 508 $http( 509 { 510 method: 'POST', 511 url: commonConfig.urlold, 512 data: factory.getQueryParams(alldatas,true), 513 headers: { 'Accept': 'application/json', 514 'Cache-Control': 'no-cache', 515 'Content-Type': commonConfig.contenttypeold} 516 } 517 ).success(function(token){ 518 deferred.resolve(token); 519 520 }).error(function(e, status){ 521 deferred.resolve(""); 522 }); 523 524 return deferred.promise; 525 }; 526 527 factory.isCookieTokenExist = function isCookieTokenExist() { 528 var deferred = $q.defer(); 529 $http.defaults.useXDomain = true; 530 531 var security = {}; 532 security.scope = commonConfig.currentInstance.code; 533 security.editor = commonConfig.currentInstance.editor; 534 security.company = commonConfig.currentInstance.company; 535 if(commonConfig.currentInstance.idshop !== undefined){ 536 security.idshop = commonConfig.currentInstance.idshop; 537 } 538 539 security.cookies = 1; 540 if(commonConfig.cookieforcename !== undefined) { 541 security.cookieforcename = commonConfig.cookieforcename; 542 } 543 if(commonConfig.cookiehttponly !== undefined) { 544 security.cookiehttponly = (commonConfig.cookiehttponly ? 1 : 0); 545 } 546 if(commonConfig.cookiesecure !== undefined) { 547 security.cookiesecure = (commonConfig.cookiesecure ? 1 : 0); 548 } 549 550 var alldatas = {}; 551 alldatas.datas = {}; 552 alldatas.datas.oauth = security; 553 alldatas.datas.target = commonConfig.target; 554 alldatas.datas.scope = commonConfig.scope; 555 alldatas.datas.service = "getcookiephp"; 556 557 $http( 558 { 559 method: 'POST', 560 url: commonConfig.urlold, 561 data: factory.getQueryParams(alldatas,true), 562 headers: { 'Accept': 'application/json', 563 'Cache-Control': 'no-cache', 564 'Content-Type': commonConfig.contenttypeold} 565 } 566 ).success(function(token){ 567 deferred.resolve(token); 568 569 }).error(function(e, status){ 570 deferred.resolve(""); 571 }); 572 573 return deferred.promise; 574 }; 575 576 factory.doLogout = function doLogout() { 577 return deleteCookieToken(); 578 }; 579 580 factory.isValidConnexion = function isValidConnexion(tokenFromCookie) { 581 //var token = getCookieToken(); 582 if(tokenFromCookie !== "" && $localStorage[commonConfig.currentInstance.keylogin].access_token !== undefined && 583 $localStorage[commonConfig.currentInstance.keylogin].access_token !== tokenFromCookie){ 584 return false; 585 } 586 587 if($localStorage[commonConfig.currentInstance.keylogin].access_token !== undefined){ 588 return true; 589 } 590 //alert("isValidConnexion == false"); 591 return false; 592 }; 593 594 factory.encodeUrl = function encodeUrl(url) { 595 return _base64url_encode(encodeURI(url)); 596 } 597 598 var Base64={_keyStr:"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789
598+/=",encode:function(e){var t="";var n,r,i,s,o,u,a;var f=0;e=Base64._utf8_encode(e);while(f<e.length){n=e.charCodeAt(f++);r=e.charCodeAt(f++);i=e.charCodeAt(f++);s=n>>2;o=(n&3)<<4|r>>4;u=(r&15)<<2|i>>6;a=i&63;if(isNaN(r)){u=a=64}else if(isNaN(i)){a=64}t=t+this._keyStr.charAt(s)+this._keyStr.charAt(o)+this._keyStr.charAt(u)+this._keyStr.charAt(a)}return t},decode:function(e){var t="";var n,r,i;var s,o,u,a;var f=0;e=e.replace(/[^A-Za-z0-9+/=]/g,"");while(f<e.length){s=this._keyStr.indexOf(e.charAt(f++));o=this._keyStr.indexOf(e.charAt(f++));u=this._keyStr.indexOf(e.charAt(f++));a=this._keyStr.indexOf(e.charAt(f++));n=s<<2|o>>4;r=(o&15)<<4|u>>2;i=(u&3)<<6|a;t=t+String.fromCharCode(n);if(u!=64){t=t+String.fromCharCode(r)}if(a!=64){t=t+String.fromCharCode(i)}}t=Base64._utf8_decode(t);return t},_utf8_encode:function(e){e=e.replace(/rn/g,"n");var t="";for(var n=0;n<e.length;n++){var r=e.charCodeAt(n);if(r<128){t+=String.fromCharCode(r)}else if(r>127&&r<2048){t+=String.fromCharCode(r>>6|192);t+=String.fromCharCode(r&63|128)}else{t+=String.fromCharCode(r>>12|224);t+=String.fromCharCode(r>>6&63|128);t+=String.fromCharCode(r&63|128)}}return t},_utf8_decode:function(e){var t="";var n=0;var r=c1=c2=0;while(n<e.length){r=e.charCodeAt(n);if(r<128){t+=String.fromCharCode(r);n++}else if(r>191&&r<224){c2=e.charCodeAt(n+1);t+=String.fromCharCode((r&31)<<6|c2&63);n+=2}else{c2=e.charCodeAt(n+1);c3=e.charCodeAt(n+2);t+=String.fromCharCode((r&15)<<12|(c2&63)<<6|c3&63);n+=3}}return t}}; 599 600 function _base64url_encode(param) { 601 var b64 = btoa(param); 602 b64 = b64.replace(/\+/g, "-"); 603 b64 = b64.replace(/\//g, "_"); 604 605 return b64; 606 } 607 608 function _base64url_decode(b64) { 609 var b64i =b64; 610 b64i = b64i.replace(/-/g, "+"); 611 b64i = b64i.replace(/_/g, "/"); 612 var param = atob(b64i); 613 614 return param; 615 } 616 617 /** FONCTION DE GESTION DU COOKIE TOKEN **/ 618 619 function getDomain(){ 620 var currentdomain = window.location.hostname; 621 $ip = currentdomain.replace(".", ""); 622 if(isNaN($ip)==false){ 623 return $currentdomain; 624 } else { 625 //On garde que le domaine 626 return currentdomain.substring(currentdomain.lastIndexOf(".", currentdomain.lastIndexOf(".") - 1) + 1); 627 } 628 } 629 630 function deleteCookieToken() { 631 var deferred = $q.defer(); 632 $http.defaults.useXDomain = true; 633 634 var security = {}; 635 security.editor = commonConfig.currentInstance.editor; 636 security.company = commonConfig.currentInstance.company; 637 security.scope = commonConfig.currentInstance.code; 638 if(commonConfig.currentInstance.idshop !== undefined){ 639 security.idshop = commonConfig.currentInstance.idshop; 640 } 641 642 security.cookies = 1; 643 if(commonConfig.cookieforcename !== undefined) { 644 security.cookieforcename = commonConfig.cookieforcename; 645 } 646 if(commonConfig.cookiehttponly !== undefined) { 647 security.cookiehttponly = (commonConfig.cookiehttponly ? 1 : 0); 648 } 649 if(commonConfig.cookiesecure !== undefined) { 650 security.cookiesecure = (commonConfig.cookiesecure ? 1 : 0); 651 } 652 653 var alldatas = {}; 654 alldatas.datas = {}; 655 alldatas.datas.oauth = security; 656 alldatas.datas.target = commonConfig.target; 657 alldatas.datas.scope = commonConfig.scope; 658 alldatas.datas.service = "deletecookiephp"; 659 660 $http( 661 { 662 method: 'POST', 663 url: factory.getUrlParams(alldatas), 664 data: factory.getQueryParams(alldatas,commonConfig.usegetapi), 665 headers: { 'Accept': 'application/json', 666 'Cache-Control': 'no-cache', 667 'Content-Type': commonConfig.contenttype} 668 } 669 ).success(function(datas){ 670 deferred.resolve(""); 671 }).error(function(e, status){ 672 deferred.resolve(""); 673 }); 674 675 return deferred.promise;; 676 }; 677 678 /* 679 * La gestion du cookie (creation, lecture et suppression est géré en php au niveau du getapi.php 680 * En effet le cookie est declaré en HttpOnly pour des raisons de sécurités et ne peut être lu en Javascript 681 * 682 */ 683 /*function deleteCookieToken() { 684 var expires = ""; 685 var date = new Date(); 686 date.setTime(date.getTime() + (-10 * 24 * 60 * 60 * 1000)); 687 expires = "; expires=" + date.toGMTString(); 688 console.log(getDomain()); 689 document.cookie = getCookieTokenName() + "=" + expires + "; path=/; domain="+getDomain()+";"; 690 }; 691 692 function getCookieTokenName(){ 693 return "token-"+commonConfig.currentInstance.editor+"-"+commonConfig.currentInstance.company; 694 } 695 696 function getCookieToken() { 697 var name = getCookieTokenName()+ "="; 698 var decodedCookie = decodeURIComponent(document.cookie); 699 var token =""; 700 //console.log(decodedCookie); 701 var ca = decodedCookie.split(';'); 702 for(var i = 0; i <ca.length; i++) { 703 var c = ca[i];
704 while (c.charAt(0) == ' ') { 705 c = c.substring(1); 706 } 707 if (c.indexOf(name) == 0) { 708 token=c.substring(name.length, c.length); 709 console.log("Token found in cookie: "+token); 710 break; 711 } 712 } 713 return ""; 714 }*/ 715 716 factory.decrypt = function decrypt(encrypted){ 717 718 var passphrase = 'd4b494e4502a62edd695a903a94c2701'; 719 720 var obj_json = JSON.parse(_base64url_decode(encrypted_json_string)); 721 722 var encrypted = obj_json.ciphertext; 723 var salt = CryptoJS.enc.Hex.parse(obj_json.salt); 724 var iv = CryptoJS.enc.Hex.parse(obj_json.iv); 725 726 var key = CryptoJS.PBKDF2(passphrase, salt, { hasher: CryptoJS.algo.SHA512, keySize: 64/8, iterations: 999}); 727 728 729 var decrypted = CryptoJS.AES.decrypt(_base64url_decode(encrypted), key, { iv: iv}); 730 731 return decrypted.toString(CryptoJS.enc.Utf8); 732 }; 733 734 factory.encrypt = function encrypt(param){ 735 736 var passphrase = 'd4b494e4502a62edd695a903a94c2701'; 737 738 var salt = CryptoJS.lib.WordArray.random(256); 739 var iv = CryptoJS.lib.WordArray.random(16); 740 //for more random entropy can use : https://github.com/wwwtyro/cryptico/blob/master/random.js instead CryptoJS random() or another js PRNG 741 742 var key = CryptoJS.PBKDF2(passphrase, salt, { hasher: CryptoJS.algo.SHA512, keySize: 64/8, iterations: 999 }); 743 744 var encrypted = CryptoJS.AES.encrypt(param, key, {iv: iv}); 745 746 var data = { 747 ciphertext : CryptoJS.enc.Base64.stringify(encrypted.ciphertext), 748 salt : CryptoJS.enc.Hex.stringify(salt), 749 iv : CryptoJS.enc.Hex.stringify(iv) 750 } 751 752 return _base64url_encode(JSON.stringify(data)); 753 }; 754 755 factory.doAutoLogin = function doAutoLogin(search) { 756 var deferred = $q.defer(); 757 if(search !== undefined){ 758 var alldatas = {}; 759 alldatas.datas = {}; 760 761 if(search.param === undefined){ 762 let param = $.param(search); 763 alldatas.datas.param = _base64url_encode(param); 764 } else { 765 alldatas.datas.param = search.param; 766 } 767 768 $http.defaults.useXDomain = true; 769 770 var security = {}; 771 security.editor = commonConfig.currentInstance.editor; 772 security.company = commonConfig.currentInstance.company; 773 security.scope = commonConfig.currentInstance.code; 774 if(commonConfig.currentInstance.idshop !== undefined){ 775 security.idshop = commonConfig.currentInstance.idshop; 776 } 777 778 alldatas.datas.oauth = security; 779 alldatas.datas.target = commonConfig.target; 780 alldatas.datas.scope = commonConfig.scope; 781 alldatas.datas.service = "autologin"; 782 783 $http( 784 { 785 method: 'POST', 786 url: factory.getUrlParams(alldatas), 787 data: factory.getQueryParams(alldatas,commonConfig.usegetapi), 788 headers: { 'Accept': 'application/json', 789 'Cache-Control': 'no-cache', 790 'Content-Type': commonConfig.contenttype} 791 } 792 ).success(function(result){ 793 if(result && result.access_token !== undefined){ 794 //autovalidation + autoconnexion 795 factory.dologin(result); 796 deferred.resolve(true); 797 } else { 798 deferred.resolve(false); 799 } 800 }).error(function(e, status){ 801 console.error("Erreur lors de la tentative doAutoLogin"); 802 deferred.resolve(false); 803 }); 804 } 805 806 return deferred.promise; 807 }; 808 809 factory.displayMessageIfWasConnecte = function displayMessageIfWasConnecte() { 810 if(factory.clientWasConnecte()){ 811 factory.setClientWasConnecte(false); 812 813 var message = ""; 814 if(factory.getDeconnexionAuto()){ 815 message = "DICO_CONTROLLER_DECONNECTION_APRES_PERIODE_INACTIF"; 816 } else { 817 message = "DICO_LOGOUT_VOUS_ETES_DECONNECTE"; 818 } 819 factory.setDeconnexionAuto(true); 820 821 function updateTime(){ 822 if (typeof $rootScope.openModalInformation === 'function') { 823 $rootScope.openModalInformation( "DICO_INFORMATION", message ); 824 $interval.cancel(stopTime); 825 } 826 } 827 var stopTime = $interval(updateTime, 50); 828 } 829 } 830 831 factory.callTokenInfoQiota = function callTokenInfoQiota(url){ 832 var deferred = $q.defer(); 833 834 var saveAuthent; 835 if($http.defaults.headers.common[commonConfig.authentificationheader] !== undefined){ 836 saveAuthent = $http.defaults.headers.common[commonConfig.authentificationheader]; 837 delete $http.defaults.headers.common[commonConfig.authentificationheader]; 838 $http.defaults.headers.common["Authorization"] = saveAuthent; 839 } 840 841 $http({ 842 method: 'GET', 843 cache: false, 844 //withCredentials : true, 845 url: url, 846 headers: { 'Accept': 'application/json', 847 'Content-Type': 'application/x-www-form-urlencoded'} 848 }).then(function(response) { 849 delete $http.defaults.headers.common["Authorization"]; 850 if(saveAuthent !== undefined){ 851 $http.defaults.headers.common[commonConfig.authentificationheader] = saveAuthent; 852 } 853 deferred.resolve(response); 854 }, function(e, status){ 855 delete $http.defaults.headers.common["Authorization"]; 856 if(saveAuthent !== undefined){ 857 $http.defaults.headers.common[commonConfig.authentificationheader] = saveAuthent; 858 } 859 deferred.reject(e); 860 }); 861 862 return deferred.promise; 863 } 864 865 factory.callExterne = function callExterne(url, datas){ 866 var deferred = $q.defer(); 867 868 var saveAuthent; 869 if($http.defaults.headers.common[commonConfig.authentificationheader] !== undefined){ 870 saveAuthent = $http.defaults.headers.common[commonConfig.authentificationheader]; 871 delete $http.defaults.headers.common[commonConfig.authentificationheader]; 872 } 873 874 console.log($.param(datas)); 875 876 $http({ 877 method: 'POST', 878 cache: false, 879 //withCredentials : true, 880 url: url, 881 data: $.param(datas), 882 headers: { 'Accept': 'application/json', 883 'Content-Type': 'application/x-www-form-urlencoded'} 884 }).then(function(response) { 885 if(saveAuthent !== undefined){ 886 $http.defaults.headers.common[commonConfig.authentificationheader] = saveAuthent; 887 } 888 deferred.resolve(response); 889 }, function(e, status){ 890 if(saveAuthent !== undefined){ 891 $http.defaults.headers.common[commonConfig.authentificationheader] = saveAuthent; 892 } 893 deferred.reject(e); 894 }); 895 896 return deferred.promise; 897 } 898 899 factory.call = function call(service,scope,datas,captcha) { 900 901 var deferred = $q.defer(); 902 903 if (datas == null || datas === undefined || datas.constructor !== {}.constructor){ 904 datas={}; 905 } 906 907 if(datas.forcetimeout === undefined && commonConfig.forcetimeout !== undefined){ 908 datas.forcetimeout = commonConfig.forcetimeout; 909 } 910 911 datas.oauth = {}; 912 datas.oauth.scope = scope; 913 datas.oauth.editor = commonConfig.currentInstance.editor; 914 datas.oauth.company = commonConfig.currentInstance.company; 915 if(commonConfig.currentInstance.idshop !== undefined){ 916 datas.oauth.idshop = commonConfig.currentInstance.idshop; 917 } 918 919 if (captcha != '' && captcha !== undefined) { 920 datas.oauth.captcha = encodeURIComponent(captcha); 921 } 922 923 var alldatas = {}; 924 //Fixed on serialize et deserialize via angular pour enlever les index rajouter par angular sur les elements de types array 925 alldatas.datas = angular.fromJson(angular.toJson(datas)); 926 alldatas.datas.service = encodeURI(service); 927 alldatas.datas.target = commonConfig.target; 928 alldatas.datas.scope = commonConfig.scope; 929 930 $http.defaults.useXDomain = true; 931 932 //debugger 933 if($localStorage[commonConfig.currentInstance.keylogin].access_token === undefined){ 934 delete $http.defaults.headers.common[commonConfig.authentificationheader];
935 if(factory.isClientConnecte()){ 936 $rootScope.doSilentLogout(); 937 } else { 938 factory.login().then(function(){ 939 if(commonConfig.logcall){ 940 console.log(service); 941 console.log(alldatas); 942 } 943 $http( { 944 method: 'POST', 945 url : factory.getUrlParams(alldatas), 946 data: factory.getQueryParams(alldatas,commonConfig.usegetapi), 947 headers: { 'Content-Type': commonConfig.contenttype, 948 'Cache-Control': 'no-cache', 949 'Accept': 'application/json; charset=utf-8'} 950 } 951 ).success(function(data) { 952 deferred.resolve(data); 953 }).error(function(msg, code) { 954 deferred.reject(msg); 955 }); 956 },function(){ 957 deferred.reject("reject"); 958 }); 959 } 960 } else { 961 if(commonConfig.logcall && service != "addtodico/fr"){ 962 console.log(service); 963 console.log(alldatas); 964 } 965 $http( { 966 method: 'POST', 967 url : factory.getUrlParams(alldatas), 968 data: factory.getQueryParams(alldatas,commonConfig.usegetapi), 969 headers: { 'Content-Type': commonConfig.contenttype, 970 'Cache-Control': 'no-cache', 971 'Accept': 'application/json; charset=utf-8'} 972 } 973 ).success(function(data) { 974 deferred.resolve(data); 975 }).error(function(error) { 976 factory.processError(error,false); 977 deferred.reject(error); 978 }); 979 } 980 981 return deferred.promise; 982 }; 983 984 factory.processError = function processError(error,goerror){ 985 986 if(error){ 987 if(commonConfig.logcall){ 988 console.log(error); 989 } 990 if(error.error == "Server AboWeb Error" && error.error_description == "administration"){ 991 _goAdministration(); 992 993 return false; 994 } else if(error.error == "503 Service Unavailable" && error.message == "administration") { 995 _goAdministration(); 996 997 return false; 998 } else if(error.error == "500 RETURN CODE NOT FOUND FROM ABOSHOP SERVER") { 999 _goAdministration(); 1000 1001 return false; 1002 } else if(goerror) { 1003 if(commonConfig.throwerror){ 1004 throw new Error("Error"); 1005 } 1006 $timeout(function() { 1007 $location.path('/message/error'); 1008 }); 1009 return false; 1010 } 1011 } 1012 return true; 1013 } 1014 1015 factory.getExternalToken = function getExternalToken(code){ 1016 var deferred = $q.defer(); 1017 console.log("function getExternalToken"); 1018 1019 factory.call('gettoken/'+code,'public').then( 1020 function success (token){ 1021 if(token.value !== undefined && token.value !== null && token.value !== "") { 1022 deferred.resolve(token.value); 1023 } else { 1024 deferred.resolve(); 1025 } 1026 1027 } 1028 ); 1029 1030 return deferred.promise; 1031 } 1032 1033 factory.readQiotaStorage = function readQiotaStorage(){ 1034 let ob = {}; 1035 if(commonConfig.oAuthExterne !== undefined && commonConfig.oAuthExterne.ssoQiota){ 1036 let strOb = localStorage.getItem('aboshopqiota'); 1037 if(strOb !== undefined && strOb !== null && strOb !== ""){ 1038 try { 1039 ob = JSON.parse(strOb); 1040 } catch(e){ob={}} 1041 } 1042 1043 if(ob === undefined || ob === null){ 1044 ob = {}; 1045 } 1046 1047 if(ob.authorizeIsInProgress === undefined){ 1048 ob.authorizeIsInProgress = false; 1049 } 1050 1051 if(ob.logoutIsInProgress === undefined){ 1052 ob.logoutIsInProgress = false; 1053 } 1054 } 1055 return ob; 1056 } 1057 1058 factory.setAuthorizeIsInProgress = function setAuthorizeIsInProgress(){ 1059 if(commonConfig.oAuthExterne !== undefined && commonConfig.oAuthExterne.ssoQiota){ 1060 let ob = {}; 1061 ob.authorizeIsInProgress = true; 1062 ob.callbackurl = commonConfig.saveCurrentUrl + window.location.search.toString(); 1063 localStorage.setItem('aboshopqiota',JSON.stringify(ob)); 1064 console.log("write localStorage aboshopqiota setAuthorizeIsInProgress"); 1065 retour = false; 1066 } 1067 } 1068 1069 factory.setLogoutIsInProgress = function setLogoutIsInProgress(){ 1070 if(commonConfig.oAuthExterne !== undefined && commonConfig.oAuthExterne.ssoQiota){ 1071 let ob = {}; 1072 ob.logoutIsInProgress = true; 1073 ob.callbackurl = window.location.toString(); 1074 localStorage.setItem('aboshopqiota',JSON.stringify(ob)); 1075 console.log("write localStorage aboshopqiota setLogoutIsInProgress"); 1076 } 1077 } 1078 1079 factory.setCartIsInProgress = function setCartIsInProgress(){ 1080 if(commonConfig.oAuthExterne !== undefined && commonConfig.oAuthExterne.ssoQiota){ 1081 let ob = {}; 1082 ob.authorizeIsInProgress = true; 1083 if(commonConfig.forceRedirectToHomeAfterConnexion) { 1084 ob.callbackurl = $rootScope.getAbsoluteHrefPage(); 1085 } else { 1086 if(commonConfig.forceAskInfoCustomerInfoOnDelivery) { 1087 ob.callbackurl = $rootScope.getAbsoluteHrefPage('common/cart'); 1088 } else { 1089 ob.callbackurl = $rootScope.getAbsoluteHrefPage('common/delivery'); 1090 } 1091 } 1092 localStorage.setItem('aboshopqiota',JSON.stringify(ob)); 1093 console.log("write localStorage aboshopqiota setCartIsInProgress"); 1094 } 1095 } 1096 1097 factory.setLoginIsInProgress = function setLoginIsInProgress(){ 1098 if(commonConfig.oAuthExterne !== undefined && commonConfig.oAuthExterne.ssoQiota){ 1099 let ob = {}; 1100 ob.authorizeIsInProgress = true; 1101 if(commonConfig.forceRedirectToHomeAfterConnexion) { 1102 ob.callbackurl = $rootScope.getAbsoluteHrefPage(); 1103 } else { 1104 ob.callbackurl = $rootScope.getAbsoluteHrefPage('common/profil/init'); 1105 } 1106 localStorage.setItem('aboshopqiota',JSON.stringify(ob)); 1107 console.log("write localStorage aboshopqiota setLoginIsInProgress"); 1108 } 1109 } 1110 1111 factory.deleteStorageQiota = function deleteStorageQiota(){ 1112 localStorage.removeItem('aboshopqiota'); 1113 } 1114 1115 factory.getCodeAuthorizeAndDeleteStorage = function getCodeAuthorizeAndDeleteStorage(){ 1116 let strOb = localStorage.getItem('aboshopqiota'); 1117 let ob = {}; 1118 let code = null; 1119 if(strOb !== undefined && strOb !== null && strOb !== ""){ 1120 try { 1121 ob = JSON.parse(strOb); 1122 } catch(e){ob={}} 1123 } 1124 1125 if(ob.code !== undefined){ 1126 code = ob.code; 1127 } 1128 factory.deleteStorageQiota(); 1129 return code; 1130 } 1131 1132 return factory; 1133 1134});
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.