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

Added ability to configure OAuthToken at runtime #125

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions dist/angular-oauth2.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,31 @@
}
})(this, function(angular, ngCookies, queryString) {
var ngModule = angular.module("angular-oauth2", [ ngCookies ]).config(oauthConfig).factory("oauthInterceptor", oauthInterceptor).provider("OAuth", OAuthProvider).provider("OAuthToken", OAuthTokenProvider);
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" ];
function oauthConfig($httpProvider) {
$httpProvider.interceptors.push("oauthInterceptor");
}
Expand Down Expand Up @@ -199,12 +224,18 @@
angular.extend(config, params);
return config;
};
var providerConfigure = this.configure;
this.$get = function($cookies) {
var OAuthToken = function() {
function OAuthToken() {
_classCallCheck(this, OAuthToken);
}
_createClass(OAuthToken, [ {
key: "configure",
value: function configure(params) {
return providerConfigure(params);
}
}, {
key: "setToken",
value: function setToken(data) {
return $cookies.putObject(config.name, data, config.options);
Expand Down
2 changes: 1 addition & 1 deletion dist/angular-oauth2.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions src/providers/oauth-token-provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,19 @@ function OAuthTokenProvider() {
return config;
};

const providerConfigure = this.configure;

/**
* OAuthToken service.
*/

this.$get = function($cookies) {
class OAuthToken {

configure(params) {
return providerConfigure(params);
}

/**
* Set token.
*/
Expand Down
5 changes: 5 additions & 0 deletions test/unit/providers/oauth-token-provider.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ describe('OAuthTokenProvider', function() {
OAuthToken.removeToken();
}));

it('configure()', inject(function(OAuthToken) {
var params = { options: { expires: new Date() } };
OAuthToken.configure(params).should.eql(params);
}));

it('getAuthorizationHeader()', inject(function(OAuthToken) {
OAuthToken.getAuthorizationHeader().should.eql('Bearer foo');
}));
Expand Down