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

chore(Authorization)!: Updates Token Storage Structure #400

Open
wants to merge 3 commits into
base: main
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
58 changes: 41 additions & 17 deletions src/core/__tests__/authorization/AuthorizationManager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,6 @@ describe('AuthorizationManager', () => {

expect(spy).toHaveBeenCalledWith({
isAuthenticated: true,
token: tokenAssertion,
});
expect(spy).toHaveBeenCalledTimes(1);
});
Expand Down Expand Up @@ -300,15 +299,14 @@ describe('AuthorizationManager', () => {
expect(authenticatedHandler).toHaveBeenCalledTimes(1);
expect(authenticatedHandler).toHaveBeenCalledWith({
isAuthenticated: true,
token: TOKEN,
});
await instance.revoke();
expect(revokeHandler).toHaveBeenCalledTimes(1);
});

it('refreshTokens should refresh existing tokens', async () => {
const TOKEN = {
access_token: 'access-token',
access_token: 'auth-access-token',
scope: 'profile email openid',
expires_in: 172800,
token_type: 'Bearer',
Expand All @@ -321,6 +319,7 @@ describe('AuthorizationManager', () => {
'client_id:auth.globus.org': JSON.stringify(TOKEN),
'client_id:transfer.api.globus.org': JSON.stringify({
...TOKEN,
access_token: 'transfer-access-token',
resource_server: 'transfer.api.globus.org',
refresh_token: 'throw',
}),
Expand Down Expand Up @@ -366,8 +365,8 @@ describe('AuthorizationManager', () => {
});

expect(instance.authenticated).toBe(true);
expect(instance.tokens.auth?.access_token).toBe('access-token');
expect(instance.tokens.transfer?.access_token).toBe('access-token');
expect(instance.tokens.auth?.access_token).toBe('auth-access-token');
expect(instance.tokens.transfer?.access_token).toBe('transfer-access-token');

await instance.refreshTokens();

Expand All @@ -376,7 +375,7 @@ describe('AuthorizationManager', () => {
/**
* The transfer token should not be refreshed due to the thrown error.
*/
expect(instance.tokens.transfer?.access_token).toBe('access-token');
expect(instance.tokens.transfer?.access_token).toBe('transfer-access-token');
});

it('calling refreshTokens should not throw if no refresh tokens are present', async () => {
Expand Down Expand Up @@ -410,9 +409,21 @@ describe('AuthorizationManager', () => {

it('should bootstrap from an existing token', () => {
setInitialLocalStorageState({
'client_id:auth.globus.org': JSON.stringify({ resource_server: 'auth.globus.org' }),
'client_id:foobar': JSON.stringify({ resource_server: 'foobar' }),
'client_id:baz': JSON.stringify({ resource_server: 'baz' }),
'client_id:auth.globus.org': JSON.stringify({
resource_server: 'auth.globus.org',
access_token: 'auth-access-token',
scope: 'auth-scope',
}),
'client_id:foobar': JSON.stringify({
resource_server: 'foobar',
access_token: 'foobar-access-token',
scope: 'foobar-scope',
}),
'client_id:baz': JSON.stringify({
resource_server: 'baz',
access_token: 'baz-access-token',
scope: 'baz-scope',
}),
});
const spy = jest.spyOn(Event.prototype, 'dispatch');
const instance = new AuthorizationManager({
Expand All @@ -425,7 +436,6 @@ describe('AuthorizationManager', () => {
expect(spy).toHaveBeenCalledTimes(1);
expect(spy).toHaveBeenCalledWith({
isAuthenticated: true,
token: { resource_server: 'auth.globus.org' },
});
expect(instance.authenticated).toBe(true);
});
Expand Down Expand Up @@ -470,9 +480,21 @@ describe('AuthorizationManager', () => {
describe('reset', () => {
it('resets the AuthenticationManager dispatching expected events', () => {
setInitialLocalStorageState({
'client_id:auth.globus.org': JSON.stringify({ resource_server: 'auth.globus.org' }),
'client_id:foobar': JSON.stringify({ resource_server: 'foobar' }),
'client_id:baz': JSON.stringify({ resource_server: 'baz' }),
'client_id:auth.globus.org': JSON.stringify({
resource_server: 'auth.globus.org',
access_token: 'auth-token',
scope: 'auth-scope',
}),
'client_id:foobar': JSON.stringify({
resource_server: 'foobar',
access_token: 'foobar-token',
scope: 'foobar-scope',
}),
'client_id:baz': JSON.stringify({
resource_server: 'baz',
access_token: 'baz-token',
scope: 'baz-scope',
}),
});

const spy = jest.spyOn(Event.prototype, 'dispatch');
Expand All @@ -493,11 +515,9 @@ describe('AuthorizationManager', () => {
expect(spy).toHaveBeenCalledTimes(2);
expect(spy).toHaveBeenNthCalledWith(1, {
isAuthenticated: true,
token: { resource_server: 'auth.globus.org' },
});
expect(spy).toHaveBeenNthCalledWith(2, {
isAuthenticated: false,
token: undefined,
});
expect(instance.authenticated).toBe(false);
});
Expand Down Expand Up @@ -539,14 +559,17 @@ describe('AuthorizationManager', () => {
'client_id:auth.globus.org': JSON.stringify({
resource_server: 'auth.globus.org',
access_token: 'AUTH',
scope: 'urn:globus:auth:scope:transfer.api.globus.org:all',
}),
'client_id:transfer.api.globus.org': JSON.stringify({
access_token: 'TRANSFER',
resource_server: 'transfer.api.globus.org',
scope: 'transfer-scope transfer-scope-2',
}),
'client_id:groups.api.globus.org': JSON.stringify({
access_token: 'GROUPS',
resource_server: 'groups.api.globus.org',
scope: 'urn:globus:auth:scope:groups.api.globus.org:all',
}),
});
const instance = new AuthorizationManager({
Expand All @@ -561,12 +584,13 @@ describe('AuthorizationManager', () => {
expect(instance.tokens.auth).not.toBe(null);
expect(instance.tokens.transfer).not.toBe(null);
expect(instance.tokens.groups).not.toBe(null);

await instance.revoke();
expect(spy).toHaveBeenCalledTimes(1);
expect(instance.authenticated).toBe(false);
expect(instance.tokens.auth).toBe(null);
expect(instance.tokens.transfer).toBe(null);
expect(instance.tokens.groups).toBe(null);
// expect(instance.tokens.transfer).toBe(null);
// expect(instance.tokens.groups).toBe(null);
});

it('supports adding an existing token', () => {
Expand Down
64 changes: 50 additions & 14 deletions src/core/__tests__/authorization/TokenManager.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { mockLocalStorage, setInitialLocalStorageState } from '../../../__mocks__/localStorage';
import { AuthorizationManager } from '../../authorization/AuthorizationManager';
import { TokenManager } from '../../authorization/TokenManager';
import { TokenManager, TOKEN_STORAGE_VERSION } from '../../authorization/TokenManager';

import { RESOURCE_SERVERS } from '../../../services/auth/config';

Expand Down Expand Up @@ -38,7 +38,14 @@ describe('TokenManager', () => {
it('should return tokens for services when in storage', () => {
const TOKEN = { resource_server: RESOURCE_SERVERS.AUTH, access_token: 'AUTH' };
setInitialLocalStorageState({
'CLIENT_ID:auth.globus.org': JSON.stringify(TOKEN),
'CLIENT_ID:TokenManager': JSON.stringify({
version: TOKEN_STORAGE_VERSION,
state: {
tokens: {
[TOKEN.access_token]: TOKEN,
},
},
}),
});

expect(tokens.auth).not.toBeNull();
Expand Down Expand Up @@ -66,16 +73,17 @@ describe('TokenManager', () => {
expect(TokenManager.isTokenExpired(TOKEN)).toBe(undefined);
});

it('handles stored tokens', () => {
it.only('handles stored tokens', () => {
const TOKEN: Token = {
resource_server: RESOURCE_SERVERS.AUTH,
access_token: 'AUTH',
access_token: 'AUTH_ACCESS_TOKEN',
token_type: 'Bearer',
scope: 'openid',
expires_in: 1000,
};
const EXPIRED_TOKEN = {
...TOKEN,
access_token: 'FLOWS_ACCESS_TOKEN',
resource_server: RESOURCE_SERVERS.FLOWS,
expires_in: 0,
};
Expand Down Expand Up @@ -114,8 +122,15 @@ describe('TokenManager', () => {
{ resource_server: RESOURCE_SERVERS.COMPUTE, access_token: 'TOKEN-2' },
];
setInitialLocalStorageState({
[`CLIENT_ID:${RESOURCE_SERVERS.AUTH}`]: JSON.stringify(TOKENS[0]),
[`CLIENT_ID:${RESOURCE_SERVERS.COMPUTE}`]: JSON.stringify(TOKENS[1]),
'CLIENT_ID:TokenManager': JSON.stringify({
version: TOKEN_STORAGE_VERSION,
state: {
tokens: {
[TOKENS[0].access_token]: TOKENS[0],
[TOKENS[1].access_token]: TOKENS[1],
},
},
}),
});
expect(tokens.getAll()).toEqual([TOKENS[0], TOKENS[1]]);
});
Expand All @@ -129,11 +144,18 @@ describe('TokenManager', () => {
{ resource_server: 'arbitrary', access_token: 'arbitrary' },
];
setInitialLocalStorageState({
[`CLIENT_ID:${RESOURCE_SERVERS.AUTH}`]: JSON.stringify(TOKENS[0]),
[`CLIENT_ID:${RESOURCE_SERVERS.COMPUTE}`]: JSON.stringify(TOKENS[1]),
[`CLIENT_ID:${GCS_ENDPOINT_UUID}`]: JSON.stringify(TOKENS[2]),
'CLIENT_ID:TokenManager': JSON.stringify({
version: TOKEN_STORAGE_VERSION,
state: {
tokens: {
[TOKENS[0].access_token]: TOKENS[0],
[TOKENS[1].access_token]: TOKENS[1],
[TOKENS[2].access_token]: TOKENS[2],
[TOKENS[3].access_token]: TOKENS[3],
},
},
}),
'some-storage-key': 'NOT-A-TOKEN',
[`CLIENT_ID:arbitrary`]: JSON.stringify(TOKENS[3]),
});
expect(tokens.getAll()).toEqual([TOKENS[0], TOKENS[1], TOKENS[2], TOKENS[3]]);
expect(tokens.getAll()).not.toContain('NOT-A-TOKEN');
Expand Down Expand Up @@ -165,9 +187,16 @@ describe('TokenManager', () => {
},
];
setInitialLocalStorageState({
[`CLIENT_ID:${GCS_ENDPOINT_UUID}`]: JSON.stringify(TOKENS[0]),
[`CLIENT_ID:${FLOW_UUID}`]: JSON.stringify(TOKENS[1]),
[`CLIENT_ID:${RESOURCE_SERVERS.AUTH}`]: JSON.stringify(TOKENS[2]),
'CLIENT_ID:TokenManager': JSON.stringify({
version: TOKEN_STORAGE_VERSION,
state: {
tokens: {
[TOKENS[0].access_token]: TOKENS[0],
[TOKENS[1].access_token]: TOKENS[1],
[TOKENS[2].access_token]: TOKENS[2],
},
},
}),
});

expect(tokens.getByResourceServer(GCS_ENDPOINT_UUID)).toEqual(TOKENS[0]);
Expand All @@ -189,7 +218,14 @@ describe('TokenManager', () => {
},
];
setInitialLocalStorageState({
[`CLIENT_ID:${GCS_ENDPOINT_UUID}`]: JSON.stringify(TOKENS[0]),
'CLIENT_ID:TokenManager': JSON.stringify({
version: TOKEN_STORAGE_VERSION,
state: {
tokens: {
[TOKENS[0].access_token]: TOKENS[0],
},
},
}),
});

expect(tokens.gcs(GCS_ENDPOINT_UUID)).toEqual(TOKENS[0]);
Expand Down
26 changes: 12 additions & 14 deletions src/core/authorization/AuthorizationManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,6 @@
* @event AuthorizationManager.events#authenticated
* @type {object}
* @property {boolean} isAuthenticated - Whether the `AuthorizationManager` is authenticated.
* @property {TokenResponse} [token] - The token response if the `AuthorizationManager` is authenticated.
*/
authenticated: new Event<
'authenticated',
Expand All @@ -169,7 +168,6 @@
* @see {@link AuthorizationManager.authenticated}
*/
isAuthenticated: boolean;
token?: TokenResponse;
}
>('authenticated'),
/**
Expand Down Expand Up @@ -239,8 +237,12 @@
* @see {@link https://docs.globus.org/api/auth/reference/#oidc_userinfo_endpoint}
*/
get user() {
const token = this.getGlobusAuthToken();
return token && token.id_token ? jwtDecode<JwtUserInfo>(token.id_token) : null;
const token = this.tokens
.getAll()
.find((t) => t.resource_server === RESOURCE_SERVERS.AUTH && t.scope.includes('openid'));
return token && 'id_token' in token && token.id_token
? jwtDecode<JwtUserInfo>(token.id_token)
: null;
}

/**
Expand Down Expand Up @@ -289,32 +291,28 @@

/**
* Whether or not the instance has a reference to a Globus Auth token.
* @deprecated Use `AuthorizationManager.tokens.auth` instead.
*/
hasGlobusAuthToken() {
return this.getGlobusAuthToken() !== null;
return Boolean(this.tokens.auth);

Check warning on line 297 in src/core/authorization/AuthorizationManager.ts

View check run for this annotation

Codecov / codecov/patch

src/core/authorization/AuthorizationManager.ts#L297

Added line #L297 was not covered by tests
}

/**
* Retrieve the Globus Auth token managed by the instance.
* @deprecated Use `AuthorizationManager.tokens.auth` instead.
*/
getGlobusAuthToken() {
const entry = this.storage.getItem(`${this.storageKeyPrefix}${RESOURCE_SERVERS.AUTH}`);
return entry ? JSON.parse(entry) : null;
return this.tokens.auth;

Check warning on line 305 in src/core/authorization/AuthorizationManager.ts

View check run for this annotation

Codecov / codecov/patch

src/core/authorization/AuthorizationManager.ts#L305

Added line #L305 was not covered by tests
}

#checkAuthorizationState() {
log('debug', 'AuthorizationManager.#checkAuthorizationState');
if (this.hasGlobusAuthToken()) {
this.authenticated = true;
}
this.authenticated = Boolean(this.tokens.auth);
}

async #emitAuthenticatedState() {
const isAuthenticated = this.authenticated;
const token = this.getGlobusAuthToken() ?? undefined;
await this.events.authenticated.dispatch({
isAuthenticated,
token,
isAuthenticated: this.authenticated,
});
}

Expand Down
Loading
Loading