-
Notifications
You must be signed in to change notification settings - Fork 15
/
interceptors.js
69 lines (63 loc) · 2.87 KB
/
interceptors.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
var UnauthorizedResponseInterceptor = angular.module('Rvd').factory('UnauthorizedResponseInterceptor', function($q, $location, $rootScope, notifications) {
var responseInterceptor = {
responseError: function(response) {
//console.log("run UnauthorizedResponseInterceptor");
//console.log(response);
if (response.status === 401) {
if ( response.data && response.data.exception ) {
var exceptionName = response.data.exception.className;
//console.log("exceptionName " + exceptionName);
if ( exceptionName == 'UserNotAuthenticated' ) {
//authentication.clearTicket();
$location.path("/login");
return $q.reject(response);
}
}
} else
if (response.status === 404) {
if ( response.data && response.data.exception ) {
var exceptionName = response.data.exception.className;
console.log("exceptionName " + exceptionName);
}
} else
if (response.status == 403) {
notifications.put({type:'danger',message:"Unauthorized access"});
}
return $q.reject(response); //response;
}
};
return responseInterceptor;
});
angular.module('Rvd').config(['$httpProvider', function($httpProvider) {
$httpProvider.interceptors.push('UnauthorizedResponseInterceptor');
}]);
// injects authentication credentials when Restcomm authentication is used
angular.module('Rvd').factory('RestcommAuthenticationInterceptor', function ($injector) {
function request(config) {
return $injector.invoke(function ($state, authentication, RvdConfiguration) {
if (config.url.startsWith('services/') || config.url.startsWith('/restcomm-rvd/services/')) {
if (authentication.getAuthHeader()) {
// if the request is targeted towards RVD add authorization header and there is no authorization header already
delete config.headers.authorization; // mind the case insensitivity of headers
delete config.headers.Authorization;
config.headers.Authorization = authentication.getAuthHeader();
}
} else
if (config.url.startsWith(RvdConfiguration.restcommBaseUrl + '/restcomm/2012-04-24/')) {
if (authentication.getAuthHeader()) {
delete config.headers.authorization; // mind the case insensitivity of headers
delete config.headers.Authorization;
config.headers.Authorization = authentication.getAuthHeader();
}
}
return config;
});
} // returns the $injector.invokve() return value
// public interface
return {
request: request
}
});
angular.module('Rvd').config( function($httpProvider) {
$httpProvider.interceptors.push('RestcommAuthenticationInterceptor');
});