Skip to content

Instantly share code, notes, and snippets.

@mustafa-zidan
Last active August 29, 2015 14:10
Show Gist options
  • Save mustafa-zidan/1ef17da23935dfce5491 to your computer and use it in GitHub Desktop.
Save mustafa-zidan/1ef17da23935dfce5491 to your computer and use it in GitHub Desktop.
Security interceptor
/**
Private module, a utility, required internally by 'http-auth-interceptor'.
*/
window.interceptors.factory("httpBuffer", [
"$injector", function($injector) {
var ApiService, buffer, retryHttpRequest;
retryHttpRequest = function(config, deferred) {
var ApiService, errorCallback, successCallback;
successCallback = function(response) {
deferred.resolve(response);
};
errorCallback = function(response) {
deferred.reject(response);
};
ApiService = ApiService || $injector.get("ApiService");
ApiService(config).then(successCallback, errorCallback);
};
/**
Holds all the requests, so they can be re-requested in future.
*/
buffer = [];
/**
Service initialized later because of circular dependency problem.
Will use it to reply the request after authentication
*/
ApiService = void 0;
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) {
var i;
if (reason) {
for (i in buffer) {
buffer[i].deferred.reject(reason);
}
}
buffer = [];
},
/**
Retries all the buffered requests clears the buffer.
*/
retryAll: function(updater) {
var i;
for (i in buffer) {
retryHttpRequest(updater(buffer[i].config), buffer[i].deferred);
}
buffer = [];
}
};
}
]);
window.interceptors.factory("SecurityInterceptor", [
"$rootScope", "httpBuffer", function($rootScope, httpBuffer) {
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 user that was logged in
*/
loginConfirmed: function(data, configUpdater) {
var updater;
updater = configUpdater || function(config) {
return 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);
},
/**
Call this function to indicate that authentication is needed in order to 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.
*/
loginRequired: function(data, reason) {
$rootScope.$broadcast("event:auth-loginRequired", data);
}
};
/**
$http interceptor.
On 401 response (without 'ignoreAuthModule' option) stores the request
and broadcasts 'event:angular-auth-loginRequired'.
*/
}
]).config([
"$httpProvider", function($httpProvider) {
var interceptor;
interceptor = [
"$rootScope", "$q", "httpBuffer", function($rootScope, $q, httpBuffer) {
var error, success;
success = function(response) {
return response;
};
error = function(response) {
var deferred;
if (response.status === 401 && !response.config.ignoreAuthModule) {
deferred = $q.defer();
httpBuffer.append(response.config, deferred);
$rootScope.$broadcast("event:auth-loginRequired", response);
return deferred.promise;
} else if (response.status === 403) {
toastr.error(response.data.info, "Login Failed");
$rootScope.$broadcast("event:auth-limitedAccess", response);
}
return $q.reject(response);
};
return function(promise) {
return promise.then(success, error);
};
}
];
return $httpProvider.responseInterceptors.push(interceptor);
}
]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment