Skip to content

Commit

Permalink
Merge pull request #995 from ushahidi/master
Browse files Browse the repository at this point in the history
Master
  • Loading branch information
willdoran authored Feb 15, 2018
2 parents 3d000b7 + 7147914 commit d600d19
Showing 1 changed file with 21 additions and 18 deletions.
39 changes: 21 additions & 18 deletions app/common/auth/authentication-interceptor.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,15 +80,7 @@ function AuthInterceptor($rootScope, $injector, $q, CONST, Session, _) {
function request(config) {
var deferred = $q.defer();

if (_.has(config, 'params') && config.params.ignore403) {
delete config.params.ignore403;
config.ignorable = true;
}

if (config.url.indexOf('oauth/token') !== -1) {
config.ignorable = true;
}

config.ignorable = shouldIgnoreAuthError(config);

if (config.url.indexOf(CONST.API_URL) === -1) {
deferred.resolve(config);
Expand All @@ -106,20 +98,12 @@ function AuthInterceptor($rootScope, $injector, $q, CONST, Session, _) {
config.headers.Authorization = 'Bearer ' + accessToken;

}
// else {
// // We are going to attempt to send the request without
// // any access token in it.
// // If the operation fails because authentication/
// // authorization is needed, we will handle in
// // responseError() below
// }
deferred.resolve(config);
return deferred.promise;
}

function responseError(rejection) {
var deferred = $q.defer();

// When a request is rejected there are
// a few possible reasons. If its a 401
// either our token expired, or we didn't have one.
Expand Down Expand Up @@ -165,10 +149,29 @@ function AuthInterceptor($rootScope, $injector, $q, CONST, Session, _) {
$rootScope.$broadcast('event:forbidden');
}
deferred.reject(rejection);
// For anything else, just forward the rejection
// For anything else, just forward the rejection
} else {
deferred.reject(rejection);
}
return deferred.promise;
}

/**
* Returns true if url is ignorable, false if not
* @param config
*/
function shouldIgnoreAuthError(config) {
var isIgnorable = false;
if (_.has(config, 'params') && config.params.ignore403) {
delete config.params.ignore403;
isIgnorable = true;
}
var i = 0;
var matchers = ['/oauth/token(/|$)', '/users(/|$)([0-9]+|$)', '/roles(/|$)'];
while (isIgnorable === false && i < matchers.length) {
isIgnorable = !!config.url.match(matchers[i]);
i++;
}
return isIgnorable;
}
}

0 comments on commit d600d19

Please sign in to comment.