-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathangular-http-auth-interceptor.js
executable file
·177 lines (160 loc) · 5.16 KB
/
angular-http-auth-interceptor.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/*global angular:true, browser:true */
/**
* @license HTTP Auth Interceptor Module for AngularJS /
* modified License: MIT
*/
(function() {
'use strict';
angular.module('angularHttpAuthInterceptor', [ 'HttpAuthInterceptorBuffer' ])
/**
* Stores the config received upon login confirmation for the next requests
*/
.factory('AuthInterceptorConfigService', function() {
return {
config : {
headers : {}
}
};
})
.factory(
'AuthInterceptorService',
[ '$rootScope', 'HttpBuffer', '$http', 'AuthInterceptorConfigService',
function($rootScope, HttpBuffer, $http, AuthInterceptorConfigService) {
return {
/**
* Call this function to indicate that
* authentication was successfull and trigger a
* retry of all deferred requests.
*
* @param data
* an optional argument to pass on to
* $broadcast which may be useful for
* example if you need to pass through
* details of the administrator that was
* logged in
* @param configUpdater
* an optional transformation function
* that can modify the requests that are
* retried after having logged in. This
* can be used for example to add an
* authentication token. It must return
* the request.
*/
loginConfirmed : function(data, configUpdater) {
var updater = configUpdater || function(config) {
return config;
};
configUpdater(AuthInterceptorConfigService.config);
$rootScope.$broadcast('event:auth-loginConfirmed', data);
HttpBuffer.retryAll(updater);
},
/**
* Call this function to indicate that
* authentication should not proceed. All deferred
* requests will be abandoned or rejected (if reason
* is provided).
*
* @param data
* an optional argument to pass on to
* $broadcast.
* @param reason
* if provided, the requests are
* rejected; abandoned otherwise.
*/
loginCancelled : function(data, reason) {
HttpBuffer.rejectAll(reason);
$rootScope.$broadcast('event:auth-loginCancelled', data);
}
};
} ])
/**
* $http interceptor. On 401 response (without 'ignoreAuthModule' option)
* stores the request and broadcasts 'event:auth-loginRequired'. On 403
* response (without 'ignoreAuthModule' option) discards the request and
* broadcasts 'event:auth-forbidden'.
*/
.config(
[
'$httpProvider',
function($httpProvider) {
$httpProvider.defaults.withCredentials = true;
$httpProvider.interceptors.push([ '$rootScope', '$q', 'HttpBuffer', 'AuthInterceptorConfigService',
function($rootScope, $q, HttpBuffer, AuthInterceptorConfigService) {
return {
responseError : function(rejection) {
if (!rejection.config.ignoreAuthModule) {
switch (rejection.status) {
case 401:
var deferred = $q.defer();
HttpBuffer.append(rejection.config, deferred);
$rootScope.$broadcast('event:auth-loginRequired', rejection);
return deferred.promise;
case 403:
$rootScope.$broadcast('event:auth-forbidden', rejection);
break;
}
}
// otherwise, default behaviour
return $q.reject(rejection);
},
request : function(config) {
angular.merge(config, AuthInterceptorConfigService.config);
return config;
}
};
} ]);
} ]);
/**
* Private module, a utility, required internally by
* 'http-auth-interceptor'.
*/
angular.module('HttpAuthInterceptorBuffer', [])
.factory('HttpBuffer', [ '$injector', function($injector) {
/** Holds all the requests, so they can be re-requested in future. */
var buffer = [];
/** Service initialized later because of circular dependency problem. */
var $http;
function retryHttpRequest(config, deferred) {
function successCallback(response) {
deferred.resolve(response);
}
function errorCallback(response) {
deferred.reject(response);
}
$http = $http || $injector.get('$http');
$http(config).then(successCallback, errorCallback);
}
return {
/**
* Appends HTTP request configuration object with deferred response
* attached to buffer.
*/
append : function(config, deferred) {
buffer.push({
config : config,
deferred : deferred
});
},
/**
* Abandon or reject (if reason provided) all the buffered requests.
*/
rejectAll : function(reason) {
if (reason) {
for (var i = 0; i < buffer.length; ++i) {
buffer[i].deferred.reject(reason);
}
}
buffer = [];
},
/**
* Retries all the buffered requests clears the buffer.
*/
retryAll : function(updater) {
for (var i = 0; i < buffer.length; ++i) {
retryHttpRequest(updater(buffer[i].config), buffer[i].deferred);
}
buffer = [];
}
};
} ]);
})();