[ARVADOS-WORKBENCH2] updated: 1.4.1-429-g2a15974a
Git user
git at public.arvados.org
Mon Sep 7 20:41:38 UTC 2020
Summary of changes:
src/common/config.ts | 2 +
src/services/auth-service/auth-service.ts | 46 ++++++++++++-------
src/services/services.ts | 7 ++-
src/store/auth/auth-middleware.ts | 2 +
.../auto-logout/auto-logout.test.tsx | 42 +++++++++++++++++
src/views-components/auto-logout/auto-logout.tsx | 53 +++++++++++-----------
6 files changed, 108 insertions(+), 44 deletions(-)
create mode 100644 src/views-components/auto-logout/auto-logout.test.tsx
via 2a15974a93e2fce120e27a07953464764e930c22 (commit)
via 8ca41c49857e0109f9c38f7eac67db89a064ecbc (commit)
from 0cb01f7da9fcb4fc3bb49cc5039f6b712466bf74 (commit)
Those revisions listed above that are new to this repository have
not appeared on any other notification email; so we list those
revisions in full, below.
commit 2a15974a93e2fce120e27a07953464764e930c22
Author: Lucas Di Pentima <lucas at di-pentima.com.ar>
Date: Mon Sep 7 17:37:22 2020 -0300
16679: Uses sessionStorage when Login.TokenLifetime is set to non-zero.
Also, removes sessions list on logout, as tokens are also saved there.
Arvados-DCO-1.1-Signed-off-by: Lucas Di Pentima <lucas at di-pentima.com.ar>
diff --git a/src/common/config.ts b/src/common/config.ts
index 0f935602..96152248 100644
--- a/src/common/config.ts
+++ b/src/common/config.ts
@@ -62,6 +62,7 @@ export interface ClusterConfigJSON {
};
Login: {
LoginCluster: string;
+ TokenLifetime: string;
Google: {
Enable: boolean;
}
@@ -221,6 +222,7 @@ export const mockClusterConfigJSON = (config: Partial<ClusterConfigJSON>): Clust
},
Login: {
LoginCluster: "",
+ TokenLifetime: "0s",
Google: {
Enable: false,
},
diff --git a/src/services/auth-service/auth-service.ts b/src/services/auth-service/auth-service.ts
index 61db625c..5e382fba 100644
--- a/src/services/auth-service/auth-service.ts
+++ b/src/services/auth-service/auth-service.ts
@@ -39,38 +39,46 @@ export class AuthService {
constructor(
protected apiClient: AxiosInstance,
protected baseUrl: string,
- protected actions: ApiActions) { }
+ protected actions: ApiActions,
+ protected useSessionStorage: boolean = false) { }
+
+ private getStorage() {
+ if (this.useSessionStorage) {
+ return sessionStorage;
+ }
+ return localStorage;
+ }
public saveApiToken(token: string) {
- localStorage.setItem(API_TOKEN_KEY, token);
+ this.getStorage().setItem(API_TOKEN_KEY, token);
const sp = token.split('/');
if (sp.length === 3) {
- localStorage.setItem(HOME_CLUSTER, sp[1].substr(0, 5));
+ this.getStorage().setItem(HOME_CLUSTER, sp[1].substr(0, 5));
}
}
public removeApiToken() {
- localStorage.removeItem(API_TOKEN_KEY);
+ this.getStorage().removeItem(API_TOKEN_KEY);
}
public getApiToken() {
- return localStorage.getItem(API_TOKEN_KEY) || undefined;
+ return this.getStorage().getItem(API_TOKEN_KEY) || undefined;
}
public getHomeCluster() {
- return localStorage.getItem(HOME_CLUSTER) || undefined;
+ return this.getStorage().getItem(HOME_CLUSTER) || undefined;
}
public removeUser() {
- localStorage.removeItem(USER_EMAIL_KEY);
- localStorage.removeItem(USER_FIRST_NAME_KEY);
- localStorage.removeItem(USER_LAST_NAME_KEY);
- localStorage.removeItem(USER_UUID_KEY);
- localStorage.removeItem(USER_OWNER_UUID_KEY);
- localStorage.removeItem(USER_IS_ADMIN);
- localStorage.removeItem(USER_IS_ACTIVE);
- localStorage.removeItem(USER_USERNAME);
- localStorage.removeItem(USER_PREFS);
+ this.getStorage().removeItem(USER_EMAIL_KEY);
+ this.getStorage().removeItem(USER_FIRST_NAME_KEY);
+ this.getStorage().removeItem(USER_LAST_NAME_KEY);
+ this.getStorage().removeItem(USER_UUID_KEY);
+ this.getStorage().removeItem(USER_OWNER_UUID_KEY);
+ this.getStorage().removeItem(USER_IS_ADMIN);
+ this.getStorage().removeItem(USER_IS_ACTIVE);
+ this.getStorage().removeItem(USER_USERNAME);
+ this.getStorage().removeItem(USER_PREFS);
}
public login(uuidPrefix: string, homeCluster: string, loginCluster: string, remoteHosts: { [key: string]: string }) {
@@ -113,7 +121,7 @@ export class AuthService {
public getSessions(): Session[] {
try {
- const sessions = JSON.parse(localStorage.getItem("sessions") || '');
+ const sessions = JSON.parse(this.getStorage().getItem("sessions") || '');
return sessions;
} catch {
return [];
@@ -121,7 +129,11 @@ export class AuthService {
}
public saveSessions(sessions: Session[]) {
- localStorage.setItem("sessions", JSON.stringify(sessions));
+ this.getStorage().setItem("sessions", JSON.stringify(sessions));
+ }
+
+ public removeSessions() {
+ this.getStorage().removeItem("sessions");
}
public buildSessions(cfg: Config, user?: User) {
diff --git a/src/services/services.ts b/src/services/services.ts
index 41dc831e..9a7b1e04 100644
--- a/src/services/services.ts
+++ b/src/services/services.ts
@@ -32,6 +32,7 @@ import { VocabularyService } from '~/services/vocabulary-service/vocabulary-serv
import { NodeService } from '~/services/node-service/node-service';
import { FileViewersConfigService } from '~/services/file-viewers-config-service/file-viewers-config-service';
import { LinkAccountService } from "./link-account-service/link-account-service";
+import parse from "parse-duration";
export type ServiceRepository = ReturnType<typeof createServices>;
@@ -78,7 +79,11 @@ export const createServices = (config: Config, actions: ApiActions, useApiClient
const linkAccountService = new LinkAccountService(apiClient, actions);
const ancestorsService = new AncestorService(groupsService, userService);
- const authService = new AuthService(apiClient, config.rootUrl, actions);
+
+ const tokenLifetime = config && config.clusterConfig && config.clusterConfig.Login.TokenLifetime || '0s';
+ const authService = new AuthService(apiClient, config.rootUrl, actions,
+ (parse(tokenLifetime, 's') || 0) > 0);
+
const collectionService = new CollectionService(apiClient, webdavClient, authService, actions);
const favoriteService = new FavoriteService(linkService, groupsService);
const tagService = new TagService(linkService);
diff --git a/src/store/auth/auth-middleware.ts b/src/store/auth/auth-middleware.ts
index 76f85984..6eef5e5e 100644
--- a/src/store/auth/auth-middleware.ts
+++ b/src/store/auth/auth-middleware.ts
@@ -30,6 +30,7 @@ export const authMiddleware = (services: ServiceRepository): Middleware => store
setAuthorizationHeader(services, state.auth.apiToken);
} else {
services.authService.removeApiToken();
+ services.authService.removeSessions();
removeAuthorizationHeader(services);
}
@@ -64,6 +65,7 @@ export const authMiddleware = (services: ServiceRepository): Middleware => store
services.linkAccountService.removeAccountToLink();
}
services.authService.removeApiToken();
+ services.authService.removeSessions();
services.authService.removeUser();
removeAuthorizationHeader(services);
services.authService.logout();
commit 8ca41c49857e0109f9c38f7eac67db89a064ecbc
Author: Lucas Di Pentima <lucas at di-pentima.com.ar>
Date: Mon Sep 7 13:46:29 2020 -0300
16679: Adds tests for AutoLogout component.
Arvados-DCO-1.1-Signed-off-by: Lucas Di Pentima <lucas at di-pentima.com.ar>
diff --git a/src/views-components/auto-logout/auto-logout.test.tsx b/src/views-components/auto-logout/auto-logout.test.tsx
new file mode 100644
index 00000000..f8daa764
--- /dev/null
+++ b/src/views-components/auto-logout/auto-logout.test.tsx
@@ -0,0 +1,42 @@
+// Copyright (C) The Arvados Authors. All rights reserved.
+//
+// SPDX-License-Identifier: AGPL-3.0
+
+import * as React from 'react';
+import { configure, mount } from "enzyme";
+import * as Adapter from 'enzyme-adapter-react-16';
+import { AutoLogoutComponent, AutoLogoutProps } from './auto-logout';
+
+configure({ adapter: new Adapter() });
+
+describe('<AutoLogoutComponent />', () => {
+ let props: AutoLogoutProps;
+ const sessionIdleTimeout = 300;
+ const lastWarningDuration = 60;
+ jest.useFakeTimers();
+
+ beforeEach(() => {
+ props = {
+ sessionIdleTimeout: sessionIdleTimeout,
+ lastWarningDuration: lastWarningDuration,
+ doLogout: jest.fn(),
+ doWarn: jest.fn(),
+ doCloseWarn: jest.fn(),
+ };
+ mount(<div><AutoLogoutComponent {...props} /></div>);
+ });
+
+ it('should logout after idle timeout', () => {
+ jest.runTimersToTime((sessionIdleTimeout-1)*1000);
+ expect(props.doLogout).not.toBeCalled();
+ jest.runTimersToTime(1*1000);
+ expect(props.doLogout).toBeCalled();
+ });
+
+ it('should warn the user previous to close the session', () => {
+ jest.runTimersToTime((sessionIdleTimeout-lastWarningDuration-1)*1000);
+ expect(props.doWarn).not.toBeCalled();
+ jest.runTimersToTime(1*1000);
+ expect(props.doWarn).toBeCalled();
+ });
+});
\ No newline at end of file
diff --git a/src/views-components/auto-logout/auto-logout.tsx b/src/views-components/auto-logout/auto-logout.tsx
index 52a1950a..f1464ce1 100644
--- a/src/views-components/auto-logout/auto-logout.tsx
+++ b/src/views-components/auto-logout/auto-logout.tsx
@@ -39,34 +39,35 @@ const mapDispatchToProps = (dispatch: Dispatch): AutoLogoutActionProps => ({
doCloseWarn: () => dispatch(snackbarActions.CLOSE_SNACKBAR()),
});
-type AutoLogoutProps = AutoLogoutDataProps & AutoLogoutActionProps;
+export type AutoLogoutProps = AutoLogoutDataProps & AutoLogoutActionProps;
-export const AutoLogout = connect(mapStateToProps, mapDispatchToProps)(
- (props: AutoLogoutProps) => {
- let logoutTimer: NodeJS.Timer;
- const lastWarningDuration = min([props.lastWarningDuration, props.sessionIdleTimeout])! * 1000 ;
+export const AutoLogoutComponent = (props: AutoLogoutProps) => {
+ let logoutTimer: NodeJS.Timer;
+ const lastWarningDuration = min([props.lastWarningDuration, props.sessionIdleTimeout])! * 1000 ;
- const handleOnIdle = () => {
- logoutTimer = setTimeout(
- () => props.doLogout(), lastWarningDuration);
- props.doWarn(
- "Your session is about to be closed due to inactivity",
- lastWarningDuration);
- };
-
- const handleOnActive = () => {
- clearTimeout(logoutTimer);
- props.doCloseWarn();
- };
+ const handleOnIdle = () => {
+ logoutTimer = setTimeout(
+ () => props.doLogout(), lastWarningDuration);
+ props.doWarn(
+ "Your session is about to be closed due to inactivity",
+ lastWarningDuration);
+ };
- useIdleTimer({
- timeout: (props.lastWarningDuration < props.sessionIdleTimeout)
- ? 1000 * (props.sessionIdleTimeout - props.lastWarningDuration)
- : 1,
- onIdle: handleOnIdle,
- onActive: handleOnActive,
- debounce: 500
- });
+ const handleOnActive = () => {
+ clearTimeout(logoutTimer);
+ props.doCloseWarn();
+ };
- return <span />;
+ useIdleTimer({
+ timeout: (props.lastWarningDuration < props.sessionIdleTimeout)
+ ? 1000 * (props.sessionIdleTimeout - props.lastWarningDuration)
+ : 1,
+ onIdle: handleOnIdle,
+ onActive: handleOnActive,
+ debounce: 500
});
+
+ return <span />;
+};
+
+export const AutoLogout = connect(mapStateToProps, mapDispatchToProps)(AutoLogoutComponent);
-----------------------------------------------------------------------
hooks/post-receive
--
More information about the arvados-commits
mailing list