diff --git a/bower.json b/bower.json index c351c6e..769a606 100644 --- a/bower.json +++ b/bower.json @@ -1,6 +1,6 @@ { "name": "angular-oauth2", - "version": "4.0.0", + "version": "4.1.1", "description": "AngularJS OAuth2", "main": "./dist/angular-oauth2.js", "authors": [ @@ -9,7 +9,8 @@ "keywords": [ "AngularJS", "Authentication", - "OAuth2" + "OAuth2", + "localstorage" ], "license": "MIT", "homepage": "https://github.com/seegno/angular-oauth2", @@ -25,6 +26,7 @@ "dependencies": { "angular": "^1.4.0", "angular-cookies": "^1.4.0", - "query-string": "^1.0.0" + "query-string": "^1.0.0", + "ngstorage": "^0.3.11" } } diff --git a/dist/angular-oauth2.js b/dist/angular-oauth2.js index 91122c9..05581ac 100644 --- a/dist/angular-oauth2.js +++ b/dist/angular-oauth2.js @@ -1,19 +1,23 @@ /** * angular-oauth2 - Angular OAuth2 - * @version v4.1.0 + * @version v4.1.1 * @link https://github.com/seegno/angular-oauth2 * @license MIT */ (function(root, factory) { if (typeof define === "function" && define.amd) { - define([ "angular", "angular-cookies", "query-string" ], factory); + define([ "angular", "angular-cookies", "query-string", "ngstorage" ], factory); } else if (typeof exports === "object") { - module.exports = factory(require("angular"), require("angular-cookies"), require("query-string")); + module.exports = factory(require("angular"), require("angular-cookies"), require("query-string"), require("ngstorage")); } else { - root.angularOAuth2 = factory(root.angular, "ngCookies", root.queryString); + root.angularOAuth2 = factory(root.angular, "ngCookies", root.queryString, "ngStorage"); } -})(this, function(angular, ngCookies, queryString) { - var ngModule = angular.module("angular-oauth2", [ ngCookies ]).config(oauthConfig).factory("oauthInterceptor", oauthInterceptor).provider("OAuth", OAuthProvider).provider("OAuthToken", OAuthTokenProvider); +})(this, function(angular, ngCookies, queryString, ngStorage) { + var ngModule = angular.module("angular-oauth2", [ ngCookies, ngStorage ]).config(oauthConfig).factory("oauthInterceptor", oauthInterceptor).provider("OAuth", OAuthProvider).provider("OAuthToken", OAuthTokenProvider); + function oauthConfig($httpProvider) { + $httpProvider.interceptors.push("oauthInterceptor"); + } + oauthConfig.$inject = [ "$httpProvider" ]; function oauthInterceptor($q, $rootScope, OAuthToken) { return { request: function request(config) { @@ -36,10 +40,6 @@ }; } oauthInterceptor.$inject = [ "$q", "$rootScope", "OAuthToken" ]; - function oauthConfig($httpProvider) { - $httpProvider.interceptors.push("oauthInterceptor"); - } - oauthConfig.$inject = [ "$httpProvider" ]; var _createClass = function() { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { @@ -221,7 +221,7 @@ angular.extend(config, params); return config; }; - this.$get = function($cookies) { + this.$get = function($cookies, $localStorage) { var OAuthToken = function() { function OAuthToken() { _classCallCheck(this, OAuthToken); @@ -229,12 +229,14 @@ _createClass(OAuthToken, [ { key: "setToken", value: function setToken(data) { - return $cookies.putObject(config.name, data, config.options); + $cookies.putObject(config.name, data, config.options); + $localStorage[config.name] = data; + return $localStorage[config.name]; } }, { key: "getToken", value: function getToken() { - return $cookies.getObject(config.name); + return $localStorage[config.name] || $cookies.getObject(config.name); } }, { key: "getAccessToken", @@ -262,6 +264,7 @@ }, { key: "removeToken", value: function removeToken() { + delete $localStorage[config.name]; return $cookies.remove(config.name, config.options); } } ]); @@ -269,7 +272,7 @@ }(); return new OAuthToken(); }; - this.$get.$inject = [ "$cookies" ]; + this.$get.$inject = [ "$cookies", "$localStorage" ]; } return ngModule; }); \ No newline at end of file diff --git a/dist/angular-oauth2.min.js b/dist/angular-oauth2.min.js index 98e09a3..88c412d 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,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 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("www-authenticate")&&0===r.headers("www-authenticate").indexOf("Bearer"))&&t.$emit("oauth:error",r),e.reject(r)}}}function o(e){e.interceptors.push("oauthInterceptor")}function i(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function a(){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,o){var a=function(){function a(e){i(this,a),this.config=e}return s(a,[{key:"configure",value:function(e){this.config=r(e)}},{key:"isAuthenticated",value:function(){return!!o.getToken()}},{key:"getAccessToken",value:function(r,i){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),i=e.extend({headers:{Authorization:void 0,"Content-Type":"application/x-www-form-urlencoded"}},i),t.post(""+this.config.baseUrl+this.config.grantPath,r,i).then(function(e){return o.setToken(e.data),e})}},{key:"getRefreshToken",value:function(r,i){return r=e.extend({client_id:this.config.clientId,grant_type:"refresh_token",refresh_token:o.getRefreshToken()},r),null!==this.config.clientSecret&&(r.client_secret=this.config.clientSecret),r=n.stringify(r),i=e.extend({headers:{Authorization:void 0,"Content-Type":"application/x-www-form-urlencoded"}},i),t.post(""+this.config.baseUrl+this.config.grantPath,r,i).then(function(e){return o.setToken(e.data),e})}},{key:"revokeToken",value:function(r,i){var a=o.getRefreshToken();return r=e.extend({client_id:this.config.clientId,token:a?a:o.getAccessToken(),token_type_hint:a?"refresh_token":"access_token"},r),null!==this.config.clientSecret&&(r.client_secret=this.config.clientSecret),r=n.stringify(r),i=e.extend({headers:{"Content-Type":"application/x-www-form-urlencoded"}},i),t.post(""+this.config.baseUrl+this.config.revokePath,r,i).then(function(e){return o.removeToken(),e})}}]),a}();return new a(this.defaultConfig)},this.$get.$inject=["$http","OAuthToken"]}function i(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function u(){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(){i(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(){return this.getToken()?this.getToken().access_token:void 0}},{key:"getAuthorizationHeader",value:function(){if(this.getTokenType()&&this.getAccessToken())return this.getTokenType().charAt(0).toUpperCase()+this.getTokenType().substr(1)+" "+this.getAccessToken()}},{key:"getRefreshToken",value:function(){return this.getToken()?this.getToken().refresh_token:void 0}},{key:"getTokenType",value:function(){return this.getToken()?this.getToken().token_type:void 0}},{key:"removeToken",value:function(){return e.remove(t.name,t.options)}}]),n}();return new n},this.$get.$inject=["$cookies"]}var c=e.module("angular-oauth2",[t]).config(o).factory("oauthInterceptor",r).provider("OAuth",a).provider("OAuthToken",u);r.$inject=["$q","$rootScope","OAuthToken"],o.$inject=["$httpProvider"];var s=function(){function e(e,t){for(var n=0;n = factory(root.angular, 'ngCookies', root.queryString); + root.<%= namespace %> = factory(root.angular, 'ngCookies', root.queryString,'ngStorage'); } - }(this, function(angular, ngCookies, queryString) { + }(this, function(angular, ngCookies, queryString, ngStorage) { <% if (exports) { %> <%= contents %> return <%= exports %>; diff --git a/karma.conf.js b/karma.conf.js index a8c8903..4053e4c 100644 --- a/karma.conf.js +++ b/karma.conf.js @@ -16,6 +16,7 @@ module.exports = function(config) { files: [ 'node_modules/angular/angular.js', 'node_modules/angular-cookies/angular-cookies.js', + 'node_modules/ngstorage/ngStorage.js', 'node_modules/query-string/query-string.js', 'node_modules/lodash/lodash.js', 'node_modules/angular-mocks/angular-mocks.js', diff --git a/package.json b/package.json index 98481f8..9557a9e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "angular-oauth2", - "version": "4.1.0", + "version": "4.1.1", "description": "Angular OAuth2", "main": "./dist/angular-oauth2.js", "repository": { @@ -10,7 +10,8 @@ "keywords": [ "AngularJS", "Authentication", - "OAuth2" + "OAuth2", + "localstorage" ], "author": { "name": "Seegno", @@ -24,6 +25,7 @@ "dependencies": { "angular": "^1.4.0", "angular-cookies": "^1.4.0", + "ngstorage": "^0.3.11", "query-string": "^1.0.0" }, "devDependencies": { diff --git a/src/angular-oauth2.js b/src/angular-oauth2.js index c2e8b16..6c43b3b 100644 --- a/src/angular-oauth2.js +++ b/src/angular-oauth2.js @@ -9,9 +9,10 @@ import OAuthTokenProvider from './providers/oauth-token-provider'; import oauthConfig from './config/oauth-config'; import oauthInterceptor from './interceptors/oauth-interceptor'; import ngCookies from 'angular-cookies'; +import ngStorage from 'ngstorage'; var ngModule = angular.module('angular-oauth2', [ - ngCookies + ngCookies, ngStorage ]) .config(oauthConfig) .factory('oauthInterceptor', oauthInterceptor) diff --git a/src/providers/oauth-token-provider.js b/src/providers/oauth-token-provider.js index 060f1dd..ebc8376 100644 --- a/src/providers/oauth-token-provider.js +++ b/src/providers/oauth-token-provider.js @@ -39,7 +39,7 @@ function OAuthTokenProvider() { * OAuthToken service. */ - this.$get = function($cookies) { + this.$get = function($cookies,$localStorage) { class OAuthToken { /** @@ -47,7 +47,9 @@ function OAuthTokenProvider() { */ setToken(data) { - return $cookies.putObject(config.name, data, config.options); + $cookies.putObject(config.name, data, config.options); + $localStorage[config.name]=data; + return $localStorage[config.name]; } /** @@ -55,7 +57,8 @@ function OAuthTokenProvider() { */ getToken() { - return $cookies.getObject(config.name); + + return $localStorage[config.name] || $cookies.getObject(config.name); } /** @@ -99,6 +102,8 @@ function OAuthTokenProvider() { */ removeToken() { + + delete $localStorage[config.name]; return $cookies.remove(config.name, config.options); } } @@ -106,7 +111,7 @@ function OAuthTokenProvider() { return new OAuthToken(); }; - this.$get.$inject = ['$cookies']; + this.$get.$inject = ['$cookies','$localStorage']; } /** diff --git a/test/mocks/angular-cookies.mock.js b/test/mocks/angular-cookies.mock.js index 9b6a14d..2cd01ec 100644 --- a/test/mocks/angular-cookies.mock.js +++ b/test/mocks/angular-cookies.mock.js @@ -20,4 +20,6 @@ angular.module('angular-cookies.mock', []) } } } - }); + }).config(['$qProvider',function($qProvider){ + $qProvider.errorOnUnhandledRejections(false); // done to fix rejection raised errors during tests, since angular 1.5.9 + }]);