PageSourceSearch

https://cdkarportal.com/js/auth/http-auth-interceptor.js

js cdkarportal.com collected 2026-10-07 20:56:01 UTC 5,699 bytes, 166 lines download raw bytes

1/**
2 * @license HTTP Auth Interceptor Module for AngularJS
3 * (c) 2012 Witold Szczerba
4 * License: MIT
5 */
6(function() {
7  'use strict';
8
9  angular
10    .module('http-auth-interceptor', ['http-auth-interceptor-buffer'])
11
12    .factory('authService', [
13      '$rootScope',
14      'httpBuffer',
15      'eventService',
16      function($rootScope, httpBuffer, eventService) {
17        return {
18          /**
19           * call this function to indicate that authentication was
20           * successful and trigger a
21           * retry of all deferred requests.
22           * @param data an optional argument to pass on to $broadcast
23           * which may be useful for
24           * example if you need to pass through details of the
25           * user that was logged in
26           */
27          loginConfirmed: function(data, configUpdater) {
28            var updater = configUpdater || function(config) {console.log('http-auth-interceptor updater fired'); return config;};
29            // $rootScope.$broadcast('event:auth-loginConfirmed', data);
30            $rootScope.$broadcast(eventService.authLoginConfirmed, data);
31            httpBuffer.retryAll(updater);
32          }
33        };
34      }
35    ])
36
37    /**
38     * $http interceptor.
39     * On 401 response (without 'ignoreAuthModule' option) stores the request
40     * and broadcasts 'event:angular-auth-loginRequired'.
41     */
42    .config(['$httpProvider', function($httpProvider) {
43      var responseInterceptor = [
44        '$rootScope',
45        '$q',
46        'httpBuffer',
47        'eventService',
48        function($rootScope, $q, httpBuffer, eventService) {
49          function success(response) {
50            return response;
51          }
52          function error(response) {
53            // originally:response.status === 401 &&
54            //  !response.config.ignoreAuthModule
55            if (response.status === 401 && response.config.retryOnAuthFailure) {
56              var deferred = $q.defer();
57              httpBuffer.append(response.config, deferred);
58              // $rootScope.$broadcast('event:auth-loginRequired');
59              $rootScope.$broadcast(eventService.authLoginRequired);
60              return deferred.promise;
61            }
62            // otherwise, default behaviour
63            if (response.status === 401) {
64              // $rootScope.$broadcast('event:auth-loginRequired');
65              $rootScope.$broadcast(eventService.authLoginRequired);
66              return $q.reject(response);
67            }
68            return $q.reject(response);
69          }
70
71          return function(promise) {
72            return promise.then(success, error);
73          };
74        }];
75
76      var requestInterceptor = [
77        '$rootScope',
78        '$q',
79        'httpBuffer',
80        'eventService',
81        'appConstantsService',
82        function($rootScope, $q, httpBuffer, eventService, appConstantsService) {
83
84          return {
85            request: function request(config) {
86              // Add authentication header
87              var xAuthToken = sessionStorage.getItem(appConstantsService.authToken);
88              if (xAuthToken){
89                var tokenAry = xAuthToken.split(',');
90                if (tokenAry.length > 1) xAuthToken = tokenAry[tokenAry.length - 1];
91                config.headers[appConstantsService.authToken] = xAuthToken;
92              }
93
94              return config;
95            },
96
97            response: function response(resp) {
98              // Extract authentication header
99              var xAuthToken = resp.headers(appConstantsService.authToken);
100              if (xAuthToken) {
101                var tokenAry = xAuthToken.split(',');
102                if (tokenAry.length > 1) xAuthToken = tokenAry[tokenAry.length - 1];
103                sessionStorage.setItem(appConstantsService.authToken,xAuthToken);
104              }
105
106              return resp;
107            }
108          }
109        }];
110
111      $httpProvider.interceptors.push(requestInterceptor);
112      $httpProvider.responseInterceptors.push(responseInterceptor);
113
114    }]);
115
116  /**
117   * Private module, a utility, required internally by 'http-auth-interceptor'.
118   */
119  angular.module('http-auth-interceptor-buffer', [])
120
121        .factory('httpBuffer', ['$injector', function($injector) {
122          // Holds all the requests, so they can be re-requested in future.
123          var buffer = [];
124
125          // Service initialized later because of circular dependency problem.
126          var $http;
127
128          function retryHttpRequest(config, deferred) {
129            function successCallback(response) {
130              deferred.resolve(response);
131            }
132            function errorCallback(response) {
133              deferred.reject(response);
134            }
135            console.log(
136              'retrying http request which previously failed with 401: ' +
137              config.method + ':' + config.url
138            );
139            $http = $http || $injector.get('$http');
140            $http(config).then(successCallback, errorCallback);
141          }
142
143          return {
144            /**
145             * Appends HTTP request configuration object with deferred
146             * response attached to buffer.
147             */
148            append: function(config, deferred) {
149              buffer.push({
150                config: config,
151                deferred: deferred
152              });
153            },
154
155            /**
156             * Retries all the buffered requests clears the buffer.
157             */
158            retryAll: function(updater) {
159              for (var i = 0; i < buffer.length; ++i) {
160                retryHttpRequest(updater(buffer[i].config), buffer[i].deferred);
161              }
162              buffer = [];
163            }
164          };
165        }]);
166})();

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.