Skip to content
Draft
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
4 changes: 3 additions & 1 deletion src/__mocks__/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ export function createStorageMock() {
* Ensure `Object.keys` calls behave similiar to real `Storage`.
*/
mock = new Proxy(mock, {
ownKeys: (target) => Object.keys(target.store),
ownKeys(target) {
return Reflect.ownKeys(target.store);
},
getOwnPropertyDescriptor: () => ({
enumerable: true,
configurable: true,
Expand Down
71 changes: 54 additions & 17 deletions src/core/__tests__/authorization/AuthorizationManager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,6 @@ describe('AuthorizationManager', () => {

expect(spy).toHaveBeenCalledWith({
isAuthenticated: true,
token: tokenAssertion,
});
expect(spy).toHaveBeenCalledTimes(1);
});
Expand Down Expand Up @@ -301,15 +300,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 @@ -322,6 +320,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 @@ -367,8 +366,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 @@ -377,7 +376,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,10 +409,28 @@ describe('AuthorizationManager', () => {
});

it('should bootstrap from an existing token', () => {
const AUTH_TOKEN = {
resource_server: 'auth.globus.org',
access_token: 'auth-access-token',
scope: 'auth-scope',
};

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 @@ -426,9 +443,15 @@ describe('AuthorizationManager', () => {
expect(spy).toHaveBeenCalledTimes(1);
expect(spy).toHaveBeenCalledWith({
isAuthenticated: true,
token: { resource_server: 'auth.globus.org' },
});
expect(instance.authenticated).toBe(true);

/**
* Coverage for deprecated methods...
* @since v7
*/
expect(instance.hasGlobusAuthToken()).toBe(true);
expect(instance.getGlobusAuthToken()).toEqual(AUTH_TOKEN);
});

describe('user', () => {
Expand Down Expand Up @@ -471,9 +494,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 @@ -494,11 +529,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 @@ -540,14 +573,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 @@ -562,12 +598,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
85 changes: 71 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 @@ -69,13 +76,14 @@ describe('TokenManager', () => {
it('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 All @@ -100,7 +108,7 @@ describe('TokenManager', () => {
expires_in: 1000,
};
tokens.add(TOKEN);
tokens.add({ ...TOKEN, resource_server: RESOURCE_SERVERS.FLOWS });
tokens.add({ ...TOKEN, access_token: 'FLOWS', resource_server: RESOURCE_SERVERS.FLOWS });
expect(tokens.auth).not.toBeNull();
expect(tokens.flows).not.toBeNull();
tokens.remove(TOKEN);
Expand Down Expand Up @@ -131,8 +139,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 @@ -146,11 +161,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 @@ -182,9 +204,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 @@ -206,9 +235,37 @@ 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]);
});

it('supports .clear()', () => {
const TOKENS = [
{ resource_server: RESOURCE_SERVERS.AUTH, access_token: 'TOKEN-1' },
{ resource_server: RESOURCE_SERVERS.COMPUTE, access_token: 'TOKEN-2' },
];
setInitialLocalStorageState({
'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().length).toBe(2);
tokens.clear();
expect(tokens.getAll().length).toBe(0);
});
});
Loading