Guest User

Untitled

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