Clean code
This commit is contained in:
@@ -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>
|
||||
@@ -30,8 +30,6 @@ const REMOTE: string = 'https://juliencalixte.ddns.net/database'
|
||||
|
||||
class CouchService {
|
||||
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 user: IUser | null = null
|
||||
private sync: PouchDB.Replication.Sync<{}> | null = null
|
||||
@@ -64,21 +62,6 @@ class CouchService {
|
||||
return false
|
||||
}
|
||||
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
|
||||
.sync(this.remote, {
|
||||
@@ -143,10 +126,6 @@ class CouchService {
|
||||
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()
|
||||
} else {
|
||||
this.cancelSync()
|
||||
@@ -220,102 +199,6 @@ class CouchService {
|
||||
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> {
|
||||
if (!this.db) {
|
||||
return false
|
||||
@@ -326,23 +209,7 @@ class CouchService {
|
||||
...doc,
|
||||
_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 false
|
||||
@@ -358,10 +225,6 @@ class CouchService {
|
||||
await this.db.destroy()
|
||||
this.db = null
|
||||
}
|
||||
if (this.userDb) {
|
||||
await this.userDb.destroy()
|
||||
this.userDb = null
|
||||
}
|
||||
const hexUser: string = toHex(this.user.userId)
|
||||
this.setUser(this.user, hexUser, false)
|
||||
}
|
||||
|
||||
@@ -52,7 +52,7 @@ class TransactionService {
|
||||
|
||||
public async get(id: string): Promise<ITransaction | null> {
|
||||
const transaction: ITransaction = await couchService.get(id)
|
||||
return await this.decrypt(transaction)
|
||||
return transaction
|
||||
}
|
||||
|
||||
public async add(
|
||||
@@ -89,9 +89,9 @@ class TransactionService {
|
||||
return []
|
||||
}
|
||||
|
||||
const response: PouchDB.Core.AllDocsResponse<
|
||||
const response: PouchDB.Core.AllDocsResponse<ITransaction> = await couchService.getByPrefix<
|
||||
ITransaction
|
||||
> = await couchService.getByPrefix<ITransaction>(`tra-${accountId}`)
|
||||
>(`tra-${accountId}`)
|
||||
const transactions = response.rows.map((row) => row.doc) as ITransaction[]
|
||||
return transactions.sort((a: ITransaction, b: ITransaction) =>
|
||||
a.date < b.date ? -1 : 1
|
||||
@@ -155,20 +155,6 @@ class TransactionService {
|
||||
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()
|
||||
|
||||
@@ -192,7 +192,6 @@ import { primary, main, findContrastColor, findDarkValue } from '@/utils'
|
||||
@Component({
|
||||
components: {
|
||||
'account-share': () => import('@/components/AccountShare.vue'),
|
||||
'account-encrypted': () => import('@/components/AccountEncrypted.vue'),
|
||||
'account-transaction-list': () =>
|
||||
import('@/components/AccountTransactionList.vue'),
|
||||
'tag-list': () => import('@/components/TagList.vue'),
|
||||
|
||||
Reference in New Issue
Block a user