Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add optional storages #35

Closed
wants to merge 13 commits into from
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,9 @@ Configuration defaults:
```js
OAuthTokenProvider.configure({
name: 'token',
storage:'cookies', // options: 'cookies', 'localstorage', 'sessionstorage'
options: {
secure: true
secure: true // optional, is valid if you're using cookies storage
}
});
```
Expand Down
3 changes: 2 additions & 1 deletion bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
],
"dependencies": {
"angular": "^1.4.0",
"angular-cookies": "^1.4.0",
"angular-cookies": "~1.4.6",
"ngstorage": "^0.3.9",
"query-string": "^1.0.0"
}
}
185 changes: 161 additions & 24 deletions dist/angular-oauth2.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,33 @@
root.angularOAuth2 = factory(root.angular, root.queryString);
}
})(this, function(angular, queryString) {
var ngModule = angular.module("angular-oauth2", [ "ngCookies" ]).config(oauthConfig).factory("oauthInterceptor", oauthInterceptor).provider("OAuth", OAuthProvider).provider("OAuthToken", OAuthTokenProvider);
var ngModule = angular.module("angular-oauth2", [ "ngCookies", "ngStorage" ]).config(oauthConfig).factory("oauthInterceptor", oauthInterceptor).provider("OAuth", OAuthProvider).provider("OAuthToken", OAuthTokenProvider).provider("OAuthStorage", OAuthStorageProvider);
function oauthConfig($httpProvider) {
$httpProvider.interceptors.push("oauthInterceptor");
}
oauthConfig.$inject = [ "$httpProvider" ];
function oauthInterceptor($q, $rootScope, OAuthToken) {
return {
request: function(config) {
if (OAuthToken.getAuthorizationHeader()) {
config.headers = config.headers || {};
config.headers.Authorization = OAuthToken.getAuthorizationHeader();
}
return config;
},
responseError: function(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("www-authenticate") && 0 === rejection.headers("www-authenticate").indexOf("Bearer")) {
$rootScope.$emit("oauth:error", rejection);
}
return $q.reject(rejection);
}
};
}
oauthInterceptor.$inject = [ "$q", "$rootScope", "OAuthToken" ];
var _prototypeProperties = function(child, staticProps, instanceProps) {
if (staticProps) Object.defineProperties(child, staticProps);
if (instanceProps) Object.defineProperties(child.prototype, instanceProps);
Expand Down Expand Up @@ -156,9 +178,10 @@
if (staticProps) Object.defineProperties(child, staticProps);
if (instanceProps) Object.defineProperties(child.prototype, instanceProps);
};
function OAuthTokenProvider() {
function OAuthTokenProvider($injector) {
var config = {
name: "token",
storage: "cookies",
options: {
secure: true
}
Expand All @@ -168,23 +191,26 @@
throw new TypeError("Invalid argument: `config` must be an `Object`.");
}
angular.extend(config, params);
$injector.invoke(function(OAuthStorageProvider) {
OAuthStorageProvider.configure(config);
});
return config;
};
this.$get = function($cookies) {
this.$get = function(OAuthStorage) {
var OAuthToken = function() {
function OAuthToken() {}
_prototypeProperties(OAuthToken, null, {
setToken: {
value: function setToken(data) {
return $cookies.putObject(config.name, data, config.options);
return OAuthStorage.setToken(data);
},
writable: true,
enumerable: true,
configurable: true
},
getToken: {
value: function getToken() {
return $cookies.getObject(config.name);
return OAuthStorage.getToken();
},
writable: true,
enumerable: true,
Expand Down Expand Up @@ -227,7 +253,7 @@
},
removeToken: {
value: function removeToken() {
return $cookies.remove(config.name, config.options);
return OAuthStorage.deleteToken();
},
writable: true,
enumerable: true,
Expand All @@ -238,29 +264,140 @@
}();
return new OAuthToken();
};
this.$get.$inject = [ "$cookies" ];
this.$get.$inject = [ "OAuthStorage" ];
}
function oauthInterceptor($q, $rootScope, OAuthToken) {
return {
request: function(config) {
if (OAuthToken.getAuthorizationHeader()) {
config.headers = config.headers || {};
config.headers.Authorization = OAuthToken.getAuthorizationHeader();
var _prototypeProperties = function(child, staticProps, instanceProps) {
if (staticProps) Object.defineProperties(child, staticProps);
if (instanceProps) Object.defineProperties(child.prototype, instanceProps);
};
function OAuthStorageProvider() {
var config = {
name: "token",
storage: "cookies",
options: {
secure: true
}
};
this.configure = function(params) {
angular.extend(config, params);
return config;
};
this.$get = function($localStorage, $sessionStorage, $cookies, $log) {
var storage;
var ngStorage = config.storage.toLowerCase();
if (ngStorage === "localstorage") {
storage = $localStorage;
} else if (ngStorage === "sessionstorage") {
storage = $sessionStorage;
} else if (ngStorage === "cookies") {
storage = $cookies;
} else {
storage = $cookies;
$log.warn("Set storage to cookies, because storage type is unknown");
}
var BrowserStorage = function() {
function BrowserStorage(storage, name) {
this.storage = storage;
this.name = name;
}
return config;
},
responseError: function(rejection) {
if (400 === rejection.status && rejection.data && ("invalid_request" === rejection.data.error || "invalid_grant" === rejection.data.error)) {
OAuthToken.removeToken();
$rootScope.$emit("oauth:error", rejection);
_prototypeProperties(BrowserStorage, null, {
setToken: {
value: function setToken(data) {
return this.storage[this.name] = angular.toJson(data);
},
writable: true,
enumerable: true,
configurable: true
},
getToken: {
value: function getToken() {
return angular.fromJson(this.storage[this.name]);
},
writable: true,
enumerable: true,
configurable: true
},
deleteToken: {
value: function deleteToken() {
delete this.storage[this.name];
},
writable: true,
enumerable: true,
configurable: true
}
});
return BrowserStorage;
}();
var CookieStorage = function() {
function CookieStorage($cookies, name, options) {
this.$cookies = $cookies;
this.name = name;
this.options = options;
}
if (401 === rejection.status && (rejection.data && "invalid_token" === rejection.data.error) || rejection.headers("www-authenticate") && 0 === rejection.headers("www-authenticate").indexOf("Bearer")) {
$rootScope.$emit("oauth:error", rejection);
_prototypeProperties(CookieStorage, null, {
setToken: {
value: function setToken(value) {
return this.$cookies.putObject(this.name, value, this.options);
},
writable: true,
enumerable: true,
configurable: true
},
getToken: {
value: function getToken() {
return this.$cookies.getObject(this.name);
},
writable: true,
enumerable: true,
configurable: true
},
deleteToken: {
value: function deleteToken() {
return this.$cookies.remove(this.name, this.options);
},
writable: true,
enumerable: true,
configurable: true
}
});
return CookieStorage;
}();
var OAuthStorage = function() {
function OAuthStorage(storage) {
this.storage = storage;
}
return $q.reject(rejection);
}
_prototypeProperties(OAuthStorage, null, {
setToken: {
value: function setToken(value) {
return this.storage.setToken(value);
},
writable: true,
enumerable: true,
configurable: true
},
getToken: {
value: function getToken() {
return this.storage.getToken();
},
writable: true,
enumerable: true,
configurable: true
},
deleteToken: {
value: function deleteToken() {
return this.storage.deleteToken();
},
writable: true,
enumerable: true,
configurable: true
}
});
return OAuthStorage;
}();
storage = ngStorage === "cookies" ? new CookieStorage(storage, config.name, config.options) : new BrowserStorage(storage, config.name);
return new OAuthStorage(storage);
};
this.$get.$inject = [ "$localStorage", "$sessionStorage", "$cookies", "$log" ];
}
oauthInterceptor.$inject = [ "$q", "$rootScope", "OAuthToken" ];
return ngModule;
});
Loading