login.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. */
  15. solrAdminApp.controller('LoginController',
  16. ['$scope', '$routeParams', '$rootScope', '$location', '$window', 'AuthenticationService', 'Constants',
  17. function ($scope, $routeParams, $rootScope, $location, $window, AuthenticationService, Constants) {
  18. $scope.resetMenu("login", Constants.IS_ROOT_PAGE);
  19. $scope.subPath = $routeParams.route;
  20. $rootScope.exceptions = {};
  21. // Session variables set in app.js 401 interceptor
  22. var wwwAuthHeader = sessionStorage.getItem("auth.wwwAuthHeader");
  23. var authScheme = sessionStorage.getItem("auth.scheme");
  24. if (wwwAuthHeader) {
  25. // Parse www-authenticate header
  26. var wwwHeader = wwwAuthHeader.match(/(\w+)(\s+)?(.*)/);
  27. authScheme = "unknown";
  28. var authParams = {};
  29. if (wwwHeader && wwwHeader.length >= 1)
  30. authScheme = wwwHeader[1];
  31. if (wwwHeader && wwwHeader.length >= 3)
  32. authParams = www_auth_parse_params(wwwHeader[3]);
  33. if (typeof authParams === 'string' || authParams instanceof String) {
  34. $scope.authParamsError = authParams;
  35. } else {
  36. $scope.authParamsError = undefined;
  37. }
  38. var realm = authParams['realm'];
  39. sessionStorage.setItem("auth.realm", realm);
  40. if (authScheme === 'Basic' || authScheme === 'xBasic') {
  41. authScheme = 'Basic';
  42. }
  43. sessionStorage.setItem("auth.scheme", authScheme);
  44. }
  45. var supportedSchemes = ['Basic', 'Bearer', 'Negotiate'];
  46. $scope.authSchemeSupported = supportedSchemes.includes(authScheme);
  47. $scope.authScheme = sessionStorage.getItem("auth.scheme");
  48. $scope.authRealm = sessionStorage.getItem("auth.realm");
  49. $scope.wwwAuthHeader = sessionStorage.getItem("auth.wwwAuthHeader");
  50. $scope.statusText = sessionStorage.getItem("auth.statusText");
  51. $scope.authConfig = sessionStorage.getItem("auth.config");
  52. $scope.authLocation = sessionStorage.getItem("auth.location");
  53. $scope.authLoggedinUser = sessionStorage.getItem("auth.username");
  54. $scope.authHeader = sessionStorage.getItem("auth.header");
  55. $scope.login = function () {
  56. AuthenticationService.SetCredentials($scope.username, $scope.password);
  57. $location.path($scope.authLocation); // Redirect to the location that caused the login prompt
  58. };
  59. $scope.logout = function() {
  60. // reset login status
  61. AuthenticationService.ClearCredentials();
  62. $location.path("/");
  63. };
  64. $scope.isLoggedIn = function() {
  65. return (sessionStorage.getItem("auth.username") !== null);
  66. };
  67. }]);
  68. // This function is copied and adapted from MIT-licensed https://github.com/randymized/www-authenticate/blob/master/lib/parsers.js
  69. www_auth_parse_params= function (header) {
  70. // This parser will definitely fail if there is more than one challenge
  71. var params = {};
  72. var tok, last_tok, _i, _len, key, value;
  73. var state= 0; //0: token,
  74. var m= header.split(/([",=])/);
  75. for (_i = 0, _len = m.length; _i < _len; _i++) {
  76. last_tok= tok;
  77. tok = m[_i];
  78. if (!tok.length) continue;
  79. switch (state) {
  80. case 0: // token
  81. key= tok.trim();
  82. state= 1; // expect equals
  83. continue;
  84. case 1: // expect equals
  85. if ('=' != tok) return 'Equal sign was expected after '+key;
  86. state= 2;
  87. continue;
  88. case 2: // expect value
  89. if ('"' == tok) {
  90. value= '';
  91. state= 3; // expect quoted
  92. continue;
  93. }
  94. else {
  95. params[key]= value= tok.trim();
  96. state= 9; // expect comma or end
  97. continue;
  98. }
  99. case 3: // handling quoted string
  100. if ('"' == tok) {
  101. state= 8; // end quoted
  102. continue;
  103. }
  104. else {
  105. value+= tok;
  106. state= 3; // continue accumulating quoted string
  107. continue;
  108. }
  109. case 8: // end quote encountered
  110. if ('"' == tok) {
  111. // double quoted
  112. value+= '"';
  113. state= 3; // back to quoted string
  114. continue;
  115. }
  116. if (',' == tok) {
  117. params[key]= value;
  118. state= 0;
  119. continue;
  120. }
  121. else {
  122. return 'Unexpected token ('+tok+') after '+value+'"';
  123. }
  124. continue;
  125. case 9: // expect commma
  126. if (',' != tok) return 'Comma expected after '+value;
  127. state= 0;
  128. continue;
  129. }
  130. }
  131. switch (state) { // terminal state
  132. case 0: // Empty or ignoring terminal comma
  133. case 9: // Expecting comma or end of header
  134. return params;
  135. case 8: // Last token was end quote
  136. params[key]= value;
  137. return params;
  138. default:
  139. return 'Unexpected end of www-authenticate value.';
  140. }
  141. };