Advertisement
Guest User

Untitled

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