Clean code

This commit is contained in:
2019-11-28 21:10:00 +01:00
parent 8f01f90932
commit ad8368abb1
4 changed files with 4 additions and 205 deletions

View File

@@ -1,49 +0,0 @@
<template>
<div class='account-encrypted'>
Le compte est pour l'instant chiffré, la clé sera récupérée dès qu'une
connexion internet est établie.
<button class="button is-link is-rounded is-loading" v-if="fetching"></button>
<online-view @online="fetchKey" />
</div>
</template>
<script lang="ts">
import { Component, Prop, Vue } from 'vue-property-decorator'
import { Getter } from 'vuex-class'
import axios from 'axios'
import { GET_KEY_URL } from '@/utils/serverless-url'
import bus, { SYNC } from '@/utils/bus-event'
import IUser from '@/models/IUser'
import couchService from '@/services/CouchService'
import notif from '@/utils/notif'
@Component({
components: {
'online-view': () => import('@/components/OnlineView.vue')
}
})
export default class AccountEncrypted extends Vue {
@Getter
public user!: IUser
@Prop({ type: String, required: true })
public id!: string
public fetching: boolean = false
public async fetchKey(): Promise<void> {
if (!this.user) {
return
}
this.fetching = true
const retrieved: boolean = await couchService.retrievePassword(this.id || '', this.user)
if (retrieved) {
bus.$emit(SYNC, [this.id])
} else {
notif.error(`
Impossible de récupérer la clé,
veuillez réessayer plus tard...
`)
}
this.fetching = false
}
}
</script>

View File

@@ -30,8 +30,6 @@ const REMOTE: string = 'https://juliencalixte.ddns.net/database'
class CouchService { class CouchService {
public db: PouchDB.Database | null = new PouchDb(LOCALE_DB) public db: PouchDB.Database | null = new PouchDb(LOCALE_DB)
public remoteUserDb: PouchDB.Database | null = null
public userDb: PouchDB.Database | null = null
private innerRemote: PouchDB.Database | null = null private innerRemote: PouchDB.Database | null = null
private user: IUser | null = null private user: IUser | null = null
private sync: PouchDB.Replication.Sync<{}> | null = null private sync: PouchDB.Replication.Sync<{}> | null = null
@@ -64,21 +62,6 @@ class CouchService {
return false return false
} }
await this.cancelSync() await this.cancelSync()
if (this.userDb && this.remoteUserDb) {
this.userSync = this.userDb
.sync(this.remoteUserDb, {
live: true,
retry: true
})
.on('change', (result: PouchDB.Replication.SyncResult<any>) => {
if (result.direction === 'pull') {
emit(
SYNC,
result.change.docs.map((doc: any) => doc._id)
)
}
})
}
this.sync = this.db this.sync = this.db
.sync(this.remote, { .sync(this.remote, {
@@ -143,10 +126,6 @@ class CouchService {
this.db = new PouchDb(`vaquant-${user.userId}`) this.db = new PouchDb(`vaquant-${user.userId}`)
} }
} }
// this.remoteUserDb = hexUser
// ? new PouchDb(`${REMOTE}/userdb-${hexUser}`, remoteConfig)
// : null
// this.userDb = hexUser ? new PouchDb(`userdb-${hexUser}`) : null
this.initLive() this.initLive()
} else { } else {
this.cancelSync() this.cancelSync()
@@ -220,102 +199,6 @@ class CouchService {
return response return response
} }
public async retrievePassword(id: string, user: IUser): Promise<boolean> {
if (!this.userDb || !this.remoteUserDb) {
return false
}
if (this.userDb.get(id)) {
return true
}
const response = await axios.post(GET_KEY_URL, {
accountId: id,
username: user.userId,
salt: user.salt
})
if (response.data) {
const passwordDoc: IEncryptPassword = {
_id: id,
password: response.data
}
const putResponse = await this.userDb.put(passwordDoc)
if (putResponse.ok) {
await this.userDb.sync(this.remoteUserDb)
}
return putResponse.ok
}
return false
}
public async encrypt(
id: string,
doc: IEncryptable,
savePassword: boolean = false,
usernames: string[] = []
): Promise<IEncrypted | null> {
try {
if (!this.userDb) {
return null
}
let passwordDoc: IEncryptPassword | null = null
try {
passwordDoc = (await this.userDb.get(id)) as IEncryptPassword
} catch (error) {
// tslint:disable-next-line:no-console
console.warn('encrypt retrieve password', { error, savePassword })
}
if (!passwordDoc && savePassword) {
try {
passwordDoc = await cryptoService.createPassword()
passwordDoc._id = id
await this.userDb.put(passwordDoc)
if (this.remoteUserDb) {
this.userDb.sync(this.remoteUserDb).then(() => {
if (!passwordDoc || !usernames.length || !this.user) {
return
}
axios.post(SET_KEY_URL, {
accountId: id,
password: passwordDoc.password,
usernames,
username: this.user.userId,
salt: this.user.salt
})
})
}
} catch (error) {
// tslint:disable-next-line:no-console
console.warn('encrypt and save password', { error, savePassword })
}
}
if (!passwordDoc) {
return doc
}
return await cryptoService.encrypt(doc, passwordDoc.password)
} catch (error) {
// tslint:disable-next-line:no-console
console.warn('encrypt and save', { error })
return doc
}
}
public async decrypt(
id: string,
doc: IEncrypted
): Promise<IEncryptable | null> {
try {
if (!this.userDb) {
return doc
}
const passwordDoc = (await this.userDb.get(id)) as IEncryptPassword
if (!passwordDoc) {
return doc
}
return await cryptoService.decrypt(doc, passwordDoc.password)
} catch (error) {
return doc
}
}
public async remove(id: string): Promise<boolean> { public async remove(id: string): Promise<boolean> {
if (!this.db) { if (!this.db) {
return false return false
@@ -326,23 +209,7 @@ class CouchService {
...doc, ...doc,
_deleted: true _deleted: true
}) })
if (this.userDb) {
const userDb = this.userDb
userDb
.get(id)
.then((password: any) => {
if (password) {
userDb.put({
...password,
_deleted: true
})
}
})
.catch((error: any) => {
// tslint:disable-next-line:no-console
console.warn('remove password', { error })
})
}
return response.ok return response.ok
} }
return false return false
@@ -358,10 +225,6 @@ class CouchService {
await this.db.destroy() await this.db.destroy()
this.db = null this.db = null
} }
if (this.userDb) {
await this.userDb.destroy()
this.userDb = null
}
const hexUser: string = toHex(this.user.userId) const hexUser: string = toHex(this.user.userId)
this.setUser(this.user, hexUser, false) this.setUser(this.user, hexUser, false)
} }

View File

@@ -52,7 +52,7 @@ class TransactionService {
public async get(id: string): Promise<ITransaction | null> { public async get(id: string): Promise<ITransaction | null> {
const transaction: ITransaction = await couchService.get(id) const transaction: ITransaction = await couchService.get(id)
return await this.decrypt(transaction) return transaction
} }
public async add( public async add(
@@ -89,9 +89,9 @@ class TransactionService {
return [] return []
} }
const response: PouchDB.Core.AllDocsResponse< const response: PouchDB.Core.AllDocsResponse<ITransaction> = await couchService.getByPrefix<
ITransaction ITransaction
> = await couchService.getByPrefix<ITransaction>(`tra-${accountId}`) >(`tra-${accountId}`)
const transactions = response.rows.map((row) => row.doc) as ITransaction[] const transactions = response.rows.map((row) => row.doc) as ITransaction[]
return transactions.sort((a: ITransaction, b: ITransaction) => return transactions.sort((a: ITransaction, b: ITransaction) =>
a.date < b.date ? -1 : 1 a.date < b.date ? -1 : 1
@@ -155,20 +155,6 @@ class TransactionService {
tag: transaction.tag tag: transaction.tag
} }
} }
private async decrypt(transaction: ITransaction): Promise<ITransaction> {
if (!transaction) {
return transaction
}
const decrypt = await couchService.decrypt(
transaction.accountId || '',
this.toEncrypt(transaction)
)
return {
...transaction,
...decrypt
}
}
} }
export default new TransactionService() export default new TransactionService()

View File

@@ -192,7 +192,6 @@ import { primary, main, findContrastColor, findDarkValue } from '@/utils'
@Component({ @Component({
components: { components: {
'account-share': () => import('@/components/AccountShare.vue'), 'account-share': () => import('@/components/AccountShare.vue'),
'account-encrypted': () => import('@/components/AccountEncrypted.vue'),
'account-transaction-list': () => 'account-transaction-list': () =>
import('@/components/AccountTransactionList.vue'), import('@/components/AccountTransactionList.vue'),
'tag-list': () => import('@/components/TagList.vue'), 'tag-list': () => import('@/components/TagList.vue'),