LILiK login and user managment web interface
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

131 lines
4.2 KiB

  1. 'use strict';
  2. angular.module('Authentication')
  3. .factory('AuthenticationService',
  4. ['Base64', '$http', '$cookieStore', '$rootScope', '$timeout',
  5. function (Base64, $http, $cookieStore, $rootScope, $timeout) {
  6. var service = {};
  7. service.Login = function (username, password, callback) {
  8. var authdata = Base64.encode(username + ':' + password);
  9. $http.get('/api/users/'+username, {headers: {Authorization: 'Basic ' + authdata}})
  10. .then(function (response) {
  11. service.SetCredentials(username, password);
  12. callback({success: true});
  13. }).catch(function (response) {
  14. service.ClearCredentials();
  15. callback({success: false, message: 'Username or password is incorrect'});
  16. });
  17. };
  18. service.SetCredentials = function (username, password) {
  19. var authdata = Base64.encode(username + ':' + password);
  20. $rootScope.globals = {
  21. currentUser: {
  22. username: username,
  23. authdata: authdata
  24. }
  25. };
  26. $http.defaults.headers.common['Authorization'] = 'Basic ' + authdata; // jshint ignore:line
  27. $cookieStore.put('globals', $rootScope.globals);
  28. };
  29. service.ClearCredentials = function () {
  30. $rootScope.globals = {};
  31. $cookieStore.remove('globals');
  32. $http.defaults.headers.common.Authorization = 'Basic ';
  33. };
  34. return service;
  35. }])
  36. .factory('Base64', function () {
  37. /* jshint ignore:start */
  38. var keyStr = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
  39. return {
  40. encode: function (input) {
  41. var output = "";
  42. var chr1, chr2, chr3 = "";
  43. var enc1, enc2, enc3, enc4 = "";
  44. var i = 0;
  45. do {
  46. chr1 = input.charCodeAt(i++);
  47. chr2 = input.charCodeAt(i++);
  48. chr3 = input.charCodeAt(i++);
  49. enc1 = chr1 >> 2;
  50. enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
  51. enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
  52. enc4 = chr3 & 63;
  53. if (isNaN(chr2)) {
  54. enc3 = enc4 = 64;
  55. } else if (isNaN(chr3)) {
  56. enc4 = 64;
  57. }
  58. output = output +
  59. keyStr.charAt(enc1) +
  60. keyStr.charAt(enc2) +
  61. keyStr.charAt(enc3) +
  62. keyStr.charAt(enc4);
  63. chr1 = chr2 = chr3 = "";
  64. enc1 = enc2 = enc3 = enc4 = "";
  65. } while (i < input.length);
  66. return output;
  67. },
  68. decode: function (input) {
  69. var output = "";
  70. var chr1, chr2, chr3 = "";
  71. var enc1, enc2, enc3, enc4 = "";
  72. var i = 0;
  73. // remove all characters that are not A-Z, a-z, 0-9, +, /, or =
  74. var base64test = /[^A-Za-z0-9\+\/\=]/g;
  75. if (base64test.exec(input)) {
  76. window.alert("There were invalid base64 characters in the input text.\n" +
  77. "Valid base64 characters are A-Z, a-z, 0-9, '+', '/',and '='\n" +
  78. "Expect errors in decoding.");
  79. }
  80. input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
  81. do {
  82. enc1 = keyStr.indexOf(input.charAt(i++));
  83. enc2 = keyStr.indexOf(input.charAt(i++));
  84. enc3 = keyStr.indexOf(input.charAt(i++));
  85. enc4 = keyStr.indexOf(input.charAt(i++));
  86. chr1 = (enc1 << 2) | (enc2 >> 4);
  87. chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
  88. chr3 = ((enc3 & 3) << 6) | enc4;
  89. output = output + String.fromCharCode(chr1);
  90. if (enc3 != 64) {
  91. output = output + String.fromCharCode(chr2);
  92. }
  93. if (enc4 != 64) {
  94. output = output + String.fromCharCode(chr3);
  95. }
  96. chr1 = chr2 = chr3 = "";
  97. enc1 = enc2 = enc3 = enc4 = "";
  98. } while (i < input.length);
  99. return output;
  100. }
  101. };
  102. /* jshint ignore:end */
  103. });