From 4833a98763bc68bd87ccf28671927a3a58533dc6 Mon Sep 17 00:00:00 2001 From: Tom Hacohen Date: Sun, 17 Dec 2017 18:05:00 +0000 Subject: [PATCH] Split fetching credentials and key derivation. --- src/LoginGate.tsx | 4 ++-- src/store/actions.ts | 13 +++++++++---- src/store/reducers.ts | 10 ---------- 3 files changed, 11 insertions(+), 16 deletions(-) diff --git a/src/LoginGate.tsx b/src/LoginGate.tsx index 06e1fc5..67b0e40 100644 --- a/src/LoginGate.tsx +++ b/src/LoginGate.tsx @@ -7,7 +7,7 @@ import LoginForm from './components/LoginForm'; import EncryptionLoginForm from './components/EncryptionLoginForm'; import { store, CredentialsType } from './store'; -import { fetchCredentials, deriveKey } from './store/actions'; +import { login, deriveKey } from './store/actions'; import * as C from './constants'; @@ -24,7 +24,7 @@ class LoginGate extends React.Component { onFormSubmit(username: string, password: string, encryptionPassword: string, serviceApiUrl?: string) { serviceApiUrl = serviceApiUrl ? serviceApiUrl : C.serviceApiBase; - store.dispatch(fetchCredentials(username, password, encryptionPassword, serviceApiUrl)); + store.dispatch(login(username, password, encryptionPassword, serviceApiUrl)); } onEncryptionFormSubmit(encryptionPassword: string) { diff --git a/src/store/actions.ts b/src/store/actions.ts index 769688b..caa2474 100644 --- a/src/store/actions.ts +++ b/src/store/actions.ts @@ -5,19 +5,17 @@ import * as EteSync from '../api/EteSync'; import { CredentialsData, EntriesType } from './'; export const { fetchCredentials, logout } = createActions({ - FETCH_CREDENTIALS: (username: string, password: string, encryptionPassword: string, server: string) => { + FETCH_CREDENTIALS: (username: string, password: string, server: string) => { const authenticator = new EteSync.Authenticator(server); return new Promise((resolve, reject) => { authenticator.getAuthToken(username, password).then( (authToken) => { const creds = new EteSync.Credentials(username, authToken); - const derived = EteSync.deriveKey(username, encryptionPassword); const context = { serviceApiUrl: server, credentials: creds, - encryptionKey: derived, }; resolve(context); @@ -31,13 +29,20 @@ export const { fetchCredentials, logout } = createActions({ LOGOUT: () => undefined, }); -// FIXME: This is duplicating behaviour from the fetchCredentials above export const { deriveKey } = createActions({ DERIVE_KEY: (username: string, encryptionPassword: string) => { return EteSync.deriveKey(username, encryptionPassword); }, }); +export const login = (username: string, password: string, encryptionPassword: string, server: string) => { + return (dispatch: any) => { + dispatch(fetchCredentials(username, password, server)).then(() => + dispatch(deriveKey(username, encryptionPassword)) + ); + }; +}; + export const { fetchJournals } = createActions({ FETCH_JOURNALS: (etesync: CredentialsData) => { const creds = etesync.credentials; diff --git a/src/store/reducers.ts b/src/store/reducers.ts index b866e48..7053afb 100644 --- a/src/store/reducers.ts +++ b/src/store/reducers.ts @@ -80,16 +80,6 @@ function fetchTypeIdentityReducer( const encryptionKeyReducer = handleActions( { - [actions.fetchCredentials.toString()]: ( - state: {key: string | null}, action: any) => { - if (action.error) { - return {key: null}; - } else if (action.payload === undefined) { - return {key: null}; - } else { - return {key: action.payload.encryptionKey}; - } - }, [actions.deriveKey.toString()]: (state: {key: string | null}, action: any) => ( {key: action.payload} ),