Skip to content

Commit

Permalink
Merge pull request #108 from sharetribe/fix-scopes
Browse files Browse the repository at this point in the history
Fix authInfo for tokens that lack scope attribute
  • Loading branch information
lyyder authored Feb 18, 2020
2 parents 93b122e + 8041db9 commit 61c60e8
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 12 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@ adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [Unreleased] - xxxx-xx-xx

## [v1.9.1] - 2020-02-18

### Fixed

- Handling old API tokens that lack the `scope` attribute in `sdk.authInfo`
[108](https://github.com/sharetribe/flex-sdk-js/pull/108)

## [v1.9.0] - 2020-02-12

### Added
Expand Down
25 changes: 21 additions & 4 deletions build/sharetribe-flex-sdk-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -8096,14 +8096,31 @@ function () {
return Promise.resolve().then(tokenStore.getToken).then(function (storedToken) {
if (storedToken) {
var tokenScope = storedToken.scope;
var scopes = tokenScope.split(' ');
var isAnonymous = tokenScope === 'public-read'; // Deprecated attribute, maintained here for client implementations
// that rely on this attribute

if (tokenScope) {
var scopes = tokenScope.split(' ');

var _isAnonymous = tokenScope === 'public-read'; // Deprecated attribute, maintained here for client implementations
// that rely on this attribute


var _grantType = _isAnonymous ? 'client_credentials' : 'refresh_token';

return _objectSpread({}, ctx, {
res: {
scopes: scopes,
isAnonymous: _isAnonymous,
grantType: _grantType
}
});
} // Support old tokens that are stored in the client's token store
// and possibly do not have the scope attribute


var isAnonymous = !storedToken.refresh_token;
var grantType = isAnonymous ? 'client_credentials' : 'refresh_token';
return _objectSpread({}, ctx, {
res: {
scopes: scopes,
isAnonymous: isAnonymous,
grantType: grantType
}
Expand Down
2 changes: 1 addition & 1 deletion build/sharetribe-flex-sdk-web.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "sharetribe-flex-sdk",
"version": "1.9.0",
"version": "1.9.1",
"description": "Sharetribe Flex SDK for JavaScript",
"main": "build/sharetribe-flex-sdk-node.js",
"browser": "build/sharetribe-flex-sdk-web.js",
Expand Down
21 changes: 15 additions & 6 deletions src/interceptors/auth_info.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,23 @@ export default class AuthInfo {
.then(storedToken => {
if (storedToken) {
const tokenScope = storedToken.scope;
const scopes = tokenScope.split(' ');
const isAnonymous = tokenScope === 'public-read';

// Deprecated attribute, maintained here for client implementations
// that rely on this attribute
const grantType = isAnonymous ? 'client_credentials' : 'refresh_token';
if (tokenScope) {
const scopes = tokenScope.split(' ');
const isAnonymous = tokenScope === 'public-read';

// Deprecated attribute, maintained here for client implementations
// that rely on this attribute
const grantType = isAnonymous ? 'client_credentials' : 'refresh_token';

return { ...ctx, res: { scopes, isAnonymous, grantType } };
return { ...ctx, res: { scopes, isAnonymous, grantType } };
}

// Support old tokens that are stored in the client's token store
// and possibly do not have the scope attribute
const isAnonymous = !storedToken.refresh_token;
const grantType = isAnonymous ? 'client_credentials' : 'refresh_token';
return { ...ctx, res: { isAnonymous, grantType } };
}

return { ...ctx, res: {} };
Expand Down
33 changes: 33 additions & 0 deletions src/sdk.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -685,6 +685,39 @@ describe('new SharetribeSdk', () => {
)
);
});

it('supports anonymous tokens without scope attribute', () => {
const { sdk, sdkTokenStore, adapterTokenStore } = createSdk();
const anonToken = adapterTokenStore.createAnonToken();
const { scope, ...rest } = anonToken;
sdkTokenStore.setToken({ ...rest });

return report(
sdk.authInfo().then(authInfo => {
expect(authInfo.grantType).toEqual('client_credentials');
expect(authInfo.isAnonymous).toEqual(true);
expect(authInfo.scopes).toBeUndefined();
})
);
});

it('supports access tokens without scope attribute', () => {
const { sdk, sdkTokenStore, adapterTokenStore } = createSdk();
const accessToken = adapterTokenStore.createTokenWithCredentials(
'[email protected]',
'secret-joe'
);
const { scope, ...rest } = accessToken;
sdkTokenStore.setToken({ ...rest });

return report(
sdk.authInfo().then(authInfo => {
expect(authInfo.grantType).toEqual('refresh_token');
expect(authInfo.isAnonymous).toEqual(false);
expect(authInfo.scopes).toBeUndefined();
})
);
});
});

it('allows sending query params in POST request (such as `expand=true`)', () => {
Expand Down

0 comments on commit 61c60e8

Please sign in to comment.