Advertisement
Guest User

Untitled

a guest
Feb 1st, 2016
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. .factory('AuthenticationService',
  2.     ['Base64', '$http', '$cookieStore', '$rootScope', '$timeout',
  3.     function (Base64, $http, $cookieStore, $rootScope, $timeout) {
  4.         var service = {};
  5.  
  6.         service.Login = function (username, password, callback) {
  7.  
  8.             /* Dummy authentication for testing, uses $timeout to simulate api call
  9.              ----------------------------------------------*/
  10.             /*$timeout(function () {
  11.                 var response = { success: username === 'test' && password === 'test' };
  12.                 if (!response.success) {
  13.                     response.message = 'Username or password is incorrect';
  14.                 }
  15.                 callback(response);
  16.             }, 1000); */
  17.  
  18.  
  19.             /* Use this for real authentication
  20.              ----------------------------------------------*/
  21.             $http.post('http://localhost:8081/api/user/login', { username: username, password: password })
  22.                 .success(function (response) {
  23.                     callback(response);
  24.                 });
  25.         };
  26.  
  27.         service.SetCredentials = function (username, password, token, level) {
  28.             var authdata = Base64.encode(username + ':' + password);
  29.  
  30.             $rootScope.globals = {
  31.                 currentUser: {
  32.                     username: username,
  33.                     token: token,
  34.                     level: level,
  35.                     authdata: authdata
  36.                 }
  37.             };
  38.  
  39.             $http.defaults.headers.common['Authorization'] = 'Basic ' + authdata; // jshint ignore:line
  40.             $cookieStore.put('globals', $rootScope.globals);
  41.         };
  42.  
  43.         service.ClearCredentials = function () {
  44.             $rootScope.globals = {};
  45.             $cookieStore.remove('globals');
  46.             $http.defaults.headers.common.Authorization = 'Basic ';
  47.         };
  48.  
  49.         return service;
  50.     }])
  51.  
  52. .factory('Base64', function () {
  53.     /* jshint ignore:start */
  54.  
  55.     var keyStr = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
  56.  
  57.     return {
  58.         encode: function (input) {
  59.             var output = "";
  60.             var chr1, chr2, chr3 = "";
  61.             var enc1, enc2, enc3, enc4 = "";
  62.             var i = 0;
  63.  
  64.             do {
  65.                 chr1 = input.charCodeAt(i++);
  66.                 chr2 = input.charCodeAt(i++);
  67.                 chr3 = input.charCodeAt(i++);
  68.  
  69.                 enc1 = chr1 >> 2;
  70.                 enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
  71.                 enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
  72.                 enc4 = chr3 & 63;
  73.  
  74.                 if (isNaN(chr2)) {
  75.                     enc3 = enc4 = 64;
  76.                 } else if (isNaN(chr3)) {
  77.                     enc4 = 64;
  78.                 }
  79.  
  80.                 output = output +
  81.                     keyStr.charAt(enc1) +
  82.                     keyStr.charAt(enc2) +
  83.                     keyStr.charAt(enc3) +
  84.                     keyStr.charAt(enc4);
  85.                 chr1 = chr2 = chr3 = "";
  86.                 enc1 = enc2 = enc3 = enc4 = "";
  87.             } while (i < input.length);
  88.  
  89.             return output;
  90.         },
  91.  
  92.         decode: function (input) {
  93.             var output = "";
  94.             var chr1, chr2, chr3 = "";
  95.             var enc1, enc2, enc3, enc4 = "";
  96.             var i = 0;
  97.  
  98.             // remove all characters that are not A-Z, a-z, 0-9, +, /, or =
  99.             var base64test = /[^A-Za-z0-9\+\/\=]/g;
  100.             if (base64test.exec(input)) {
  101.                 window.alert("There were invalid base64 characters in the input text.\n" +
  102.                     "Valid base64 characters are A-Z, a-z, 0-9, '+', '/',and '='\n" +
  103.                     "Expect errors in decoding.");
  104.             }
  105.             input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
  106.  
  107.             do {
  108.                 enc1 = keyStr.indexOf(input.charAt(i++));
  109.                 enc2 = keyStr.indexOf(input.charAt(i++));
  110.                 enc3 = keyStr.indexOf(input.charAt(i++));
  111.                 enc4 = keyStr.indexOf(input.charAt(i++));
  112.  
  113.                 chr1 = (enc1 << 2) | (enc2 >> 4);
  114.                 chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
  115.                 chr3 = ((enc3 & 3) << 6) | enc4;
  116.  
  117.                 output = output + String.fromCharCode(chr1);
  118.  
  119.                 if (enc3 != 64) {
  120.                     output = output + String.fromCharCode(chr2);
  121.                 }
  122.                 if (enc4 != 64) {
  123.                     output = output + String.fromCharCode(chr3);
  124.                 }
  125.  
  126.                 chr1 = chr2 = chr3 = "";
  127.                 enc1 = enc2 = enc3 = enc4 = "";
  128.  
  129.             } while (i < input.length);
  130.  
  131.             return output;
  132.         }
  133.     };
  134.  
  135.     /* jshint ignore:end */
  136. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement