diff --git a/dist/angular-oauth2.js b/dist/angular-oauth2.js index 3706f93..b6710c7 100644 --- a/dist/angular-oauth2.js +++ b/dist/angular-oauth2.js @@ -1,6 +1,6 @@ /** * angular-oauth2 - Angular OAuth2 - * @version v4.1.1 + * @version v4.2.0 * @link https://github.com/seegno/angular-oauth2 * @license MIT */ @@ -18,6 +18,31 @@ $httpProvider.interceptors.push("oauthInterceptor"); } oauthConfig.$inject = [ "$httpProvider" ]; + function oauthInterceptor($q, $rootScope, OAuthToken) { + return { + request: function request(config) { + config.headers = config.headers || {}; + if (!config.headers.hasOwnProperty("Authorization") && OAuthToken.getAuthorizationHeader()) { + config.headers.Authorization = OAuthToken.getAuthorizationHeader(); + } + return config; + }, + responseError: function responseError(rejection) { + if (!rejection) { + return $q.reject(rejection); + } + if (400 === rejection.status && rejection.data && ("invalid_request" === rejection.data.error || "invalid_grant" === rejection.data.error)) { + OAuthToken.removeToken(); + $rootScope.$emit("oauth:error", rejection); + } + if (401 === rejection.status && rejection.data && "invalid_token" === rejection.data.error || rejection.headers && rejection.headers("www-authenticate") && 0 === rejection.headers("www-authenticate").indexOf("Bearer")) { + $rootScope.$emit("oauth:error", rejection); + } + return $q.reject(rejection); + } + }; + } + oauthInterceptor.$inject = [ "$q", "$rootScope", "OAuthToken" ]; var _createClass = function() { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { @@ -189,7 +214,8 @@ var config = { name: "token", options: { - secure: true + secure: true, + storage: "cookie" } }; this.configure = function(params) { @@ -207,12 +233,30 @@ _createClass(OAuthToken, [ { key: "setToken", value: function setToken(data) { - return $cookies.putObject(config.name, data, config.options); + switch (config.options.storage) { + case "localStorage": + return window.localStorage.setItem(config.name, JSON.stringify(data)); + + case "sessionStorage": + return window.sessionStorage.setItem(config.name, JSON.stringify(data)); + + default: + return $cookies.putObject(config.name, data, config.options); + } } }, { key: "getToken", value: function getToken() { - return $cookies.getObject(config.name); + switch (config.options.storage) { + case "localStorage": + return JSON.parse(window.localStorage.getItem(config.name)); + + case "sessionStorage": + return JSON.parse(window.sessionStorage.getItem(config.name)); + + default: + return $cookies.getObject(config.name); + } } }, { key: "getAccessToken", @@ -248,7 +292,16 @@ }, { key: "removeToken", value: function removeToken() { - return $cookies.remove(config.name, config.options); + switch (config.options.storage) { + case "localStorage": + return window.localStorage.removeItem(config.name); + + case "sessionStorage": + return window.sessionStorage.removeItem(config.name); + + default: + return $cookies.remove(config.name, config.options); + } } } ]); return OAuthToken; @@ -257,30 +310,5 @@ }; this.$get.$inject = [ "$cookies" ]; } - function oauthInterceptor($q, $rootScope, OAuthToken) { - return { - request: function request(config) { - config.headers = config.headers || {}; - if (!config.headers.hasOwnProperty("Authorization") && OAuthToken.getAuthorizationHeader()) { - config.headers.Authorization = OAuthToken.getAuthorizationHeader(); - } - return config; - }, - responseError: function responseError(rejection) { - if (!rejection) { - return $q.reject(rejection); - } - if (400 === rejection.status && rejection.data && ("invalid_request" === rejection.data.error || "invalid_grant" === rejection.data.error)) { - OAuthToken.removeToken(); - $rootScope.$emit("oauth:error", rejection); - } - if (401 === rejection.status && rejection.data && "invalid_token" === rejection.data.error || rejection.headers && rejection.headers("www-authenticate") && 0 === rejection.headers("www-authenticate").indexOf("Bearer")) { - $rootScope.$emit("oauth:error", rejection); - } - return $q.reject(rejection); - } - }; - } - oauthInterceptor.$inject = [ "$q", "$rootScope", "OAuthToken" ]; return ngModule; }); \ No newline at end of file diff --git a/dist/angular-oauth2.min.js b/dist/angular-oauth2.min.js index aed184d..db40876 100644 --- a/dist/angular-oauth2.min.js +++ b/dist/angular-oauth2.min.js @@ -1 +1 @@ -!function(e,t){"function"==typeof define&&define.amd?define(["angular","angular-cookies","query-string"],t):"object"==typeof exports?module.exports=t(require("angular"),require("angular-cookies"),require("query-string")):e.angularOAuth2=t(e.angular,"ngCookies",e.queryString)}(this,function(e,t,n){function r(e){e.interceptors.push("oauthInterceptor")}function o(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function i(){var t=this,r=function(t){if(!(t instanceof Object))throw new TypeError("Invalid argument: `config` must be an `Object`.");var n=e.extend({},f,t);return e.forEach(h,function(e){if(!n[e])throw new Error("Missing parameter: "+e+".")}),"/"===n.baseUrl.substr(-1)&&(n.baseUrl=n.baseUrl.slice(0,-1)),"/"!==n.grantPath[0]&&(n.grantPath="/"+n.grantPath),"/"!==n.revokePath[0]&&(n.revokePath="/"+n.revokePath),n};this.configure=function(e){t.defaultConfig=r(e)},this.$get=function(t,i){var a=function(){function a(e){o(this,a),this.config=e}return s(a,[{key:"configure",value:function(e){this.config=r(e)}},{key:"isAuthenticated",value:function(){return!!i.getToken()}},{key:"getAccessToken",value:function(r,o){return r=e.extend({client_id:this.config.clientId,grant_type:"password"},r),null!==this.config.clientSecret&&(r.client_secret=this.config.clientSecret),r=n.stringify(r),o=e.extend({headers:{Authorization:void 0,"Content-Type":"application/x-www-form-urlencoded"}},o),t.post(""+this.config.baseUrl+this.config.grantPath,r,o).then(function(e){return i.setToken(e.data),e})}},{key:"getRefreshToken",value:function(r,o){return r=e.extend({client_id:this.config.clientId,grant_type:"refresh_token",refresh_token:i.getRefreshToken()},r),null!==this.config.clientSecret&&(r.client_secret=this.config.clientSecret),r=n.stringify(r),o=e.extend({headers:{Authorization:void 0,"Content-Type":"application/x-www-form-urlencoded"}},o),t.post(""+this.config.baseUrl+this.config.grantPath,r,o).then(function(e){return i.setToken(e.data),e})}},{key:"revokeToken",value:function(r,o){var a=i.getRefreshToken();return r=e.extend({client_id:this.config.clientId,token:a?a:i.getAccessToken(),token_type_hint:a?"refresh_token":"access_token"},r),null!==this.config.clientSecret&&(r.client_secret=this.config.clientSecret),r=n.stringify(r),o=e.extend({headers:{"Content-Type":"application/x-www-form-urlencoded"}},o),t.post(""+this.config.baseUrl+this.config.revokePath,r,o).then(function(e){return i.removeToken(),e})}}]),a}();return new a(this.defaultConfig)},this.$get.$inject=["$http","OAuthToken"]}function o(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function a(){var t={name:"token",options:{secure:!0}};this.configure=function(n){if(!(n instanceof Object))throw new TypeError("Invalid argument: `config` must be an `Object`.");return e.extend(t,n),t},this.$get=function(e){var n=function(){function n(){o(this,n)}return s(n,[{key:"setToken",value:function(n){return e.putObject(t.name,n,t.options)}},{key:"getToken",value:function(){return e.getObject(t.name)}},{key:"getAccessToken",value:function(){var e=this.getToken()||{},t=e.access_token;return t}},{key:"getAuthorizationHeader",value:function(){var e=this.getTokenType(),t=this.getAccessToken();if(e&&t)return e.charAt(0).toUpperCase()+e.substr(1)+" "+t}},{key:"getRefreshToken",value:function(){var e=this.getToken()||{},t=e.refresh_token;return t}},{key:"getTokenType",value:function(){var e=this.getToken()||{},t=e.token_type;return t}},{key:"removeToken",value:function(){return e.remove(t.name,t.options)}}]),n}();return new n},this.$get.$inject=["$cookies"]}function u(e,t,n){return{request:function(e){return e.headers=e.headers||{},!e.headers.hasOwnProperty("Authorization")&&n.getAuthorizationHeader()&&(e.headers.Authorization=n.getAuthorizationHeader()),e},responseError:function(r){return r?(400!==r.status||!r.data||"invalid_request"!==r.data.error&&"invalid_grant"!==r.data.error||(n.removeToken(),t.$emit("oauth:error",r)),(401===r.status&&r.data&&"invalid_token"===r.data.error||r.headers&&r.headers("www-authenticate")&&0===r.headers("www-authenticate").indexOf("Bearer"))&&t.$emit("oauth:error",r),e.reject(r)):e.reject(r)}}}var c=e.module("angular-oauth2",[t]).config(r).factory("oauthInterceptor",u).provider("OAuth",i).provider("OAuthToken",a);r.$inject=["$httpProvider"];var s=function(){function e(e,t){for(var n=0;n