|
| 1 | +/*global angular:true, browser:true */ |
| 2 | + |
| 3 | +/** |
| 4 | + * @license HTTP Auth Interceptor Module for AngularJS |
| 5 | + * (c) 2012 Witold Szczerba |
| 6 | + * License: MIT |
| 7 | + */ |
| 8 | +(function () { |
| 9 | + 'use strict'; |
| 10 | + |
| 11 | + angular.module('http-auth-interceptor', ['http-auth-interceptor-buffer']) |
| 12 | + |
| 13 | + .factory('authService', ['$rootScope','httpBuffer', function($rootScope, httpBuffer) { |
| 14 | + return { |
| 15 | + /** |
| 16 | + * Call this function to indicate that authentication was successfull and trigger a |
| 17 | + * retry of all deferred requests. |
| 18 | + * @param data an optional argument to pass on to $broadcast which may be useful for |
| 19 | + * example if you need to pass through details of the user that was logged in |
| 20 | + * @param configUpdater an optional transformation function that can modify the |
| 21 | + * requests that are retried after having logged in. This can be used for example |
| 22 | + * to add an authentication token. It must return the request. |
| 23 | + */ |
| 24 | + loginConfirmed: function(data, configUpdater) { |
| 25 | + var updater = configUpdater || function(config) {return config;}; |
| 26 | + $rootScope.$broadcast('event:auth-loginConfirmed', data); |
| 27 | + httpBuffer.retryAll(updater); |
| 28 | + }, |
| 29 | + |
| 30 | + /** |
| 31 | + * Call this function to indicate that authentication should not proceed. |
| 32 | + * All deferred requests will be abandoned or rejected (if reason is provided). |
| 33 | + * @param data an optional argument to pass on to $broadcast. |
| 34 | + * @param reason if provided, the requests are rejected; abandoned otherwise. |
| 35 | + */ |
| 36 | + loginCancelled: function(data, reason) { |
| 37 | + httpBuffer.rejectAll(reason); |
| 38 | + $rootScope.$broadcast('event:auth-loginCancelled', data); |
| 39 | + } |
| 40 | + }; |
| 41 | + }]) |
| 42 | + |
| 43 | + /** |
| 44 | + * $http interceptor. |
| 45 | + * On 401 response (without 'ignoreAuthModule' option) stores the request |
| 46 | + * and broadcasts 'event:auth-loginRequired'. |
| 47 | + * On 403 response (without 'ignoreAuthModule' option) discards the request |
| 48 | + * and broadcasts 'event:auth-forbidden'. |
| 49 | + */ |
| 50 | + .config(['$httpProvider', function($httpProvider) { |
| 51 | + $httpProvider.interceptors.push(['$rootScope', '$q', 'httpBuffer', function($rootScope, $q, httpBuffer) { |
| 52 | + return { |
| 53 | + responseError: function(rejection) { |
| 54 | + var config = rejection.config || {}; |
| 55 | + if (!config.ignoreAuthModule) { |
| 56 | + switch (rejection.status) { |
| 57 | + case 401: |
| 58 | + var deferred = $q.defer(); |
| 59 | + var bufferLength = httpBuffer.append(config, deferred); |
| 60 | + if (bufferLength === 1) |
| 61 | + $rootScope.$broadcast('event:auth-loginRequired', rejection); |
| 62 | + return deferred.promise; |
| 63 | + case 403: |
| 64 | + $rootScope.$broadcast('event:auth-forbidden', rejection); |
| 65 | + break; |
| 66 | + } |
| 67 | + } |
| 68 | + // otherwise, default behaviour |
| 69 | + return $q.reject(rejection); |
| 70 | + } |
| 71 | + }; |
| 72 | + }]); |
| 73 | + }]); |
| 74 | + |
| 75 | + /** |
| 76 | + * Private module, a utility, required internally by 'http-auth-interceptor'. |
| 77 | + */ |
| 78 | + angular.module('http-auth-interceptor-buffer', []) |
| 79 | + |
| 80 | + .factory('httpBuffer', ['$injector', function($injector) { |
| 81 | + /** Holds all the requests, so they can be re-requested in future. */ |
| 82 | + var buffer = []; |
| 83 | + |
| 84 | + /** Service initialized later because of circular dependency problem. */ |
| 85 | + var $http; |
| 86 | + |
| 87 | + function retryHttpRequest(config, deferred) { |
| 88 | + function successCallback(response) { |
| 89 | + deferred.resolve(response); |
| 90 | + } |
| 91 | + function errorCallback(response) { |
| 92 | + deferred.reject(response); |
| 93 | + } |
| 94 | + $http = $http || $injector.get('$http'); |
| 95 | + $http(config).then(successCallback, errorCallback); |
| 96 | + } |
| 97 | + |
| 98 | + return { |
| 99 | + /** |
| 100 | + * Appends HTTP request configuration object with deferred response attached to buffer. |
| 101 | + * @return {Number} The new length of the buffer. |
| 102 | + */ |
| 103 | + append: function(config, deferred) { |
| 104 | + return buffer.push({ |
| 105 | + config: config, |
| 106 | + deferred: deferred |
| 107 | + }); |
| 108 | + }, |
| 109 | + |
| 110 | + /** |
| 111 | + * Abandon or reject (if reason provided) all the buffered requests. |
| 112 | + */ |
| 113 | + rejectAll: function(reason) { |
| 114 | + if (reason) { |
| 115 | + for (var i = 0; i < buffer.length; ++i) { |
| 116 | + buffer[i].deferred.reject(reason); |
| 117 | + } |
| 118 | + } |
| 119 | + buffer = []; |
| 120 | + }, |
| 121 | + |
| 122 | + /** |
| 123 | + * Retries all the buffered requests clears the buffer. |
| 124 | + */ |
| 125 | + retryAll: function(updater) { |
| 126 | + for (var i = 0; i < buffer.length; ++i) { |
| 127 | + var _cfg = updater(buffer[i].config); |
| 128 | + if (_cfg !== false) |
| 129 | + retryHttpRequest(_cfg, buffer[i].deferred); |
| 130 | + } |
| 131 | + buffer = []; |
| 132 | + } |
| 133 | + }; |
| 134 | + }]); |
| 135 | +})(); |
0 commit comments