Implement fetching userInfo and opening shared journals.

master
Tom Hacohen 7 years ago
parent 80e9805d22
commit cf823f5efe

@ -14,8 +14,8 @@ import Pim from './Pim';
import * as EteSync from './api/EteSync';
import { store, JournalsType, EntriesType, StoreState, CredentialsData } from './store';
import { fetchAll } from './store/actions';
import { store, JournalsType, EntriesType, StoreState, CredentialsData, UserInfoData } from './store';
import { fetchAll, fetchUserInfo } from './store/actions';
export interface SyncInfoJournal {
journal: EteSync.Journal;
@ -33,28 +33,33 @@ interface PropsType {
interface PropsTypeInner extends PropsType {
journals: JournalsType;
entries: EntriesType;
userInfo: UserInfoData;
}
const syncInfoSelector = createSelector(
(props: PropsTypeInner) => props.etesync,
(props: PropsTypeInner) => props.journals.value as List<EteSync.Journal>,
(props: PropsTypeInner) => props.entries,
(etesync, journals, entries) => {
(props: PropsTypeInner) => props.userInfo,
(etesync, journals, entries, userInfo) => {
const derived = etesync.encryptionKey;
const keyPair = userInfo.getKeyPair(new EteSync.CryptoManager(derived, 'userInfo', userInfo.version));
const asymmetricCryptoManager = new EteSync.AsymmetricCryptoManager(keyPair);
return journals.reduce(
(ret, journal) => {
const derived = etesync.encryptionKey;
const journalEntries = entries.get(journal.uid);
const cryptoManager = new EteSync.CryptoManager(derived, journal.uid, journal.version);
let prevUid: string | null = null;
if (!journalEntries || !journalEntries.value) {
return ret;
}
// FIXME: Skip shared journals for now
let cryptoManager: EteSync.CryptoManager;
if (journal.key) {
return ret;
const derivedJournalKey = asymmetricCryptoManager.decryptBytes(journal.key);
cryptoManager = EteSync.CryptoManager.fromDerivedKey(derivedJournalKey, journal.version);
} else {
cryptoManager = new EteSync.CryptoManager(derived, journal.uid, journal.version);
}
const collectionInfo = journal.getInfo(cryptoManager);
@ -89,7 +94,16 @@ class SyncGate extends React.PureComponent {
}
componentDidMount() {
const sync = () => {
store.dispatch(fetchAll(this.props.etesync, this.props.entries));
};
if (this.props.userInfo) {
sync();
} else {
const fetching = store.dispatch(fetchUserInfo(this.props.etesync, this.props.etesync.credentials.email)) as any;
fetching.then(sync, sync);
}
}
render() {
@ -115,7 +129,7 @@ class SyncGate extends React.PureComponent {
}
}
if ((journals === null) ||
if ((this.props.userInfo === null) || (journals === null) ||
(entryArrays.size === 0) ||
!entryArrays.every((x: any) => (x.value !== null))) {
return (<LoadingIndicator style={{display: 'block', margin: '40px auto'}} />);
@ -160,6 +174,7 @@ const mapStateToProps = (state: StoreState, props: PropsType) => {
return {
journals: state.cache.journals,
entries: state.cache.entries,
userInfo: state.cache.userInfo.value,
};
};

@ -7,7 +7,7 @@ import * as fetch from 'isomorphic-fetch';
import { byte, base64, stringToByteArray } from './Helpers';
import { CryptoManager, AsymmetricKeyPair, HMAC_SIZE_BYTES } from './Crypto';
export { CryptoManager, AsymmetricKeyPair, deriveKey } from './Crypto';
export { CryptoManager, AsymmetricCryptoManager, AsymmetricKeyPair, deriveKey } from './Crypto';
class ExtendableError extends Error {
constructor(message: any) {
@ -126,8 +126,12 @@ export class Journal extends BaseJournal<JournalJson> {
this._json.version = version;
}
get key(): base64 | undefined {
return this._json.key;
get key(): byte[] | undefined {
if (this._json.key) {
return sjcl.codec.bytes.fromBits(sjcl.codec.base64.toBits(this._json.key));
}
return undefined;
}
get owner(): string | undefined {

Loading…
Cancel
Save