diff --git a/src/providers/oauth-provider.js b/src/providers/oauth-provider.js index da2c5e2..c00b0cb 100644 --- a/src/providers/oauth-provider.js +++ b/src/providers/oauth-provider.js @@ -100,7 +100,7 @@ function OAuthProvider() { */ isAuthenticated() { - return !!OAuthToken.token; + return !!OAuthToken.getToken(); } /** @@ -136,7 +136,7 @@ function OAuthProvider() { }, options); return $http.post(`${config.baseUrl}${config.grantPath}`, data, options).then((response) => { - OAuthToken.token = response.data; + OAuthToken.setToken(response.data); return response; }); @@ -167,7 +167,7 @@ function OAuthProvider() { }; return $http.post(`${config.baseUrl}${config.grantPath}`, data, options).then((response) => { - OAuthToken.token = response.data; + OAuthToken.setToken(response.data); return response; }); diff --git a/src/providers/oauth-token-provider.js b/src/providers/oauth-token-provider.js index 2e92c6b..7637ee2 100644 --- a/src/providers/oauth-token-provider.js +++ b/src/providers/oauth-token-provider.js @@ -48,7 +48,7 @@ function OAuthTokenProvider() { * Set token. */ - set token(data) { + setToken(data) { return ipCookie(config.name, data, config.options); } @@ -56,7 +56,7 @@ function OAuthTokenProvider() { * Get token. */ - get token() { + getToken() { return ipCookie(config.name); } @@ -65,7 +65,7 @@ function OAuthTokenProvider() { */ getAccessToken() { - return this.token ? this.token.access_token : undefined; + return this.getToken() ? this.getToken().access_token : undefined; } /** @@ -85,7 +85,7 @@ function OAuthTokenProvider() { */ getRefreshToken() { - return this.token ? this.token.refresh_token : undefined; + return this.getToken() ? this.getToken().refresh_token : undefined; } /** @@ -93,7 +93,7 @@ function OAuthTokenProvider() { */ getTokenType() { - return this.token ? this.token.token_type : undefined; + return this.getToken() ? this.getToken().token_type : undefined; } /** diff --git a/test/unit/interceptors/oauth-interceptor.spec.js b/test/unit/interceptors/oauth-interceptor.spec.js index f1208b8..9611589 100644 --- a/test/unit/interceptors/oauth-interceptor.spec.js +++ b/test/unit/interceptors/oauth-interceptor.spec.js @@ -24,7 +24,7 @@ describe('oauthInterceptor', function() { })); it('should inject `Authorization` header if `token` exists', inject(function($http, $httpBackend, OAuthToken) { - OAuthToken.token = { token_type: 'bearer', access_token: 'foo', expires_in: 3600, refresh_token: 'bar' }; + OAuthToken.setToken({ token_type: 'bearer', access_token: 'foo', expires_in: 3600, refresh_token: 'bar' }); $httpBackend.expectGET('https://website.com', function(headers) { headers.should.have.property('Authorization'); diff --git a/test/unit/providers/oauth-provider.spec.js b/test/unit/providers/oauth-provider.spec.js index 1eda874..c2af377 100644 --- a/test/unit/providers/oauth-provider.spec.js +++ b/test/unit/providers/oauth-provider.spec.js @@ -139,7 +139,7 @@ describe('OAuthProvider', function() { describe('isAuthenticated()', function() { it('should be true when there is a stored `token` cookie', inject(function(OAuth, OAuthToken) { - OAuthToken.token = { token_type: 'bearer', access_token: 'foo', expires_in: 3600, refresh_token: 'bar' }; + OAuthToken.setToken({ token_type: 'bearer', access_token: 'foo', expires_in: 3600, refresh_token: 'bar' }); OAuth.isAuthenticated().should.be.true; })); @@ -254,7 +254,7 @@ describe('OAuthProvider', function() { username: 'foo', password: 'bar' }).then(function(response) { - OAuthToken.token.should.eql(response.data); + OAuthToken.getToken().should.eql(response.data); }).catch(function() { should.fail(); }); @@ -277,7 +277,7 @@ describe('OAuthProvider', function() { it('should call `queryString.stringify`', inject(function(OAuth, OAuthToken) { sinon.spy(queryString, 'stringify'); - OAuthToken.token = { token_type: 'bearer', access_token: 'foo', expires_in: 3600, refresh_token: 'bar' }; + OAuthToken.setToken({ token_type: 'bearer', access_token: 'foo', expires_in: 3600, refresh_token: 'bar' }); OAuth.getRefreshToken(); @@ -310,7 +310,7 @@ describe('OAuthProvider', function() { })); it('should return an error if `refresh_token` is invalid', inject(function($httpBackend, OAuth, OAuthToken) { - OAuthToken.token = { token_type: 'bearer', access_token: 'foo', expires_in: 3600, refresh_token: 'bar' }; + OAuthToken.setToken({ token_type: 'bearer', access_token: 'foo', expires_in: 3600, refresh_token: 'bar' }); $httpBackend.expectPOST(defaults.baseUrl + defaults.grantPath, queryString.stringify(data)) .respond(400, { error: 'invalid_grant' }); @@ -329,7 +329,7 @@ describe('OAuthProvider', function() { })); it('should retrieve and store `refresh_token` if request is successful', inject(function($httpBackend, OAuth, OAuthToken) { - OAuthToken.token = { token_type: 'bearer', access_token: 'foo', expires_in: 3600, refresh_token: 'bar' }; + OAuthToken.setToken({ token_type: 'bearer', access_token: 'foo', expires_in: 3600, refresh_token: 'bar' }); $httpBackend.expectPOST(defaults.baseUrl + defaults.grantPath, queryString.stringify(data)) .respond({ token_type: 'bearer', access_token: 'qux', expires_in: 3600, refresh_token: 'biz' }); @@ -356,7 +356,7 @@ describe('OAuthProvider', function() { it('should call `queryString.stringify`', inject(function(OAuth, OAuthToken) { sinon.spy(queryString, 'stringify'); - OAuthToken.token = { token_type: 'bearer', access_token: 'foo', expires_in: 3600, refresh_token: 'bar' }; + OAuthToken.setToken({ token_type: 'bearer', access_token: 'foo', expires_in: 3600, refresh_token: 'bar' }); OAuth.revokeToken(); @@ -371,7 +371,7 @@ describe('OAuthProvider', function() { it('should call `queryString.stringify` with `access_token` if `refresh_token` is not available', inject(function(OAuth, OAuthToken) { sinon.spy(queryString, 'stringify'); - OAuthToken.token = { token_type: 'bearer', access_token: 'foo', expires_in: 3600 }; + OAuthToken.setToken({ token_type: 'bearer', access_token: 'foo', expires_in: 3600 }); OAuth.revokeToken(); @@ -404,7 +404,7 @@ describe('OAuthProvider', function() { })); it('should revoke and remove `token` if request is successful', inject(function($httpBackend, OAuth, OAuthToken) { - OAuthToken.token = { token_type: 'bearer', access_token: 'foo', expires_in: 3600, refresh_token: 'bar' }; + OAuthToken.setToken({ token_type: 'bearer', access_token: 'foo', expires_in: 3600, refresh_token: 'bar' }); var data = queryString.stringify({ token: 'bar' @@ -414,7 +414,7 @@ describe('OAuthProvider', function() { .respond(200); OAuth.revokeToken().then(function() { - (undefined === OAuthToken.token).should.be.true; + (undefined === OAuthToken.getToken()).should.be.true; }).catch(function() { should.fail(); }); diff --git a/test/unit/providers/oauth-token-provider.spec.js b/test/unit/providers/oauth-token-provider.spec.js index 561057b..e778059 100644 --- a/test/unit/providers/oauth-token-provider.spec.js +++ b/test/unit/providers/oauth-token-provider.spec.js @@ -43,7 +43,7 @@ describe('OAuthTokenProvider', function() { angular.mock.module('angular-oauth2', 'angular-oauth2.test'); angular.mock.inject(function(OAuthToken) { - OAuthToken.token = { token_type: 'bearer', access_token: 'foo', expires_in: 3600, refresh_token: 'bar' }; + OAuthToken.setToken({ token_type: 'bearer', access_token: 'foo', expires_in: 3600, refresh_token: 'bar' }); }); }); @@ -65,9 +65,9 @@ describe('OAuthTokenProvider', function() { })); it('setToken()', inject(function(OAuthToken) { - OAuthToken.token = { token_type: 'bearer', access_token: 'qux', expires_in: 3600, refresh_token: 'biz' }; + OAuthToken.setToken({ token_type: 'bearer', access_token: 'qux', expires_in: 3600, refresh_token: 'biz' }); - OAuthToken.token.should.eql({ + OAuthToken.getToken().should.eql({ token_type: 'bearer', access_token: 'qux', expires_in: 3600, @@ -76,7 +76,7 @@ describe('OAuthTokenProvider', function() { })); it('getToken()', inject(function(OAuthToken) { - OAuthToken.token.should.eql({ + OAuthToken.getToken().should.eql({ token_type: 'bearer', access_token: 'foo', expires_in: 3600, @@ -91,7 +91,7 @@ describe('OAuthTokenProvider', function() { it('removeToken()', inject(function(OAuthToken) { OAuthToken.removeToken(); - (undefined === OAuthToken.token).should.true; + (undefined === OAuthToken.getToken()).should.true; })); }); });