Welcome to mirror list, hosted at ThFree Co, Russian Federation.

PasswordConverter.js « Converter « src - git.mdns.eu/nextcloud/passwords-client.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: e5ff2ef03c6e70da3d62cfbb150728e292ef2c07 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import AbstractConverter from './AbstractConverter';
import ObjectClone from '../Utility/ObjectClone';

export default class PasswordConverter extends AbstractConverter {

    /**
     * @param {BasicPasswordsClient} client
     */
    constructor(client) {
        super(client, 'password');
        /** @type {CustomFieldConverter} **/
        this._customFieldConverter = this._client.getInstance('converter.field');
        this._hashService = /** @type {HashService} **/ this._client.getInstance('service.hash');
    }

    /**
     * @param {Object} object
     * @return {AbstractRevisionModel}
     */
    fromObject(object) {
        let clone = ObjectClone.clone(object);

        if(typeof clone.customFields === 'string') {
            try {
                clone.customFields = this._customFieldConverter.fromJSON(clone.customFields);
            } catch(e) {
                this._client.getLogger().warning('Could not read custom fields', {password: object});
                this._client.getLogger().exception(e, {password: object});
                clone.customFields = this._customFieldConverter.fromArray([]);
            }
        } else {
            clone.customFields = this._customFieldConverter.fromArray([]);
        }

        if(clone.hasOwnProperty('created') && !(clone.created instanceof Date)) {
            clone.created = new Date(clone.created * 1e3);
        }
        if(clone.hasOwnProperty('updated') && !(clone.updated instanceof Date)) {
            clone.updated = new Date(clone.updated * 1e3);
        }
        if(clone.hasOwnProperty('edited') && !(clone.edited instanceof Date)) {
            clone.edited = new Date(clone.edited * 1e3);
        }

        return this._client.getClass(`model.${this._type}`, clone);
    }

    /**
     *
     * @param {(Password|AbstractRevisionModel)} model
     * @returns {Promise<void>}
     */
    async toObject(model) {
        let data = super.toObject(model);

        data.customFields = this._customFieldConverter.toJSON(model.getCustomFields());
        data.hash = await this._hashService.getHash(model.getPassword(), this._hashService.HASH_SHA_1);

        return data;
    }
}