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

AbstractConverter.js « Converter « src - git.mdns.eu/nextcloud/passwords-client.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: acc7bb7ba4f10409b9b5200b7faabb078bc0f453 (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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import ObjectClone from '../Utility/ObjectClone';

export default class AbstractConverter {

    /**
     * @param {BasicPasswordsClient} client
     * @param {String} type
     */
    constructor(client, type) {
        this._client = client;
        this._type = type;
    }

    /**
     * @param {String} string
     * @return {AbstractRevisionModel}
     * @api
     */
    fromJSON(string) {
        let data = JSON.parse(string);

        if(typeof data === 'object' && data instanceof Object) {
            return this.fromObject(data);
        }

        throw new Error('Invalid JSON string');
    }

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

        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.makeModel(clone);
    }

    /**
     *
     * @param properties
     * @return {*}
     */
    fromModel(model) {
        // @TODO actually clone models
        return model;
    }

    /**
     *
     * @param properties
     * @return {*}
     */
    makeModel(properties) {
        return this._client.getClass(`model.${this._type}`, properties);
    }

    /**
     * @param {Object} data
     * @return {Promise<AbstractRevisionModel>}
     * @api
     */
    async fromEncryptedData(data) {
        if(data.hasOwnProperty('cseType')) {
            if(data.cseType === 'CSEv1r1') {
                data = await this._client.getCseV1Encryption().decrypt(data, this._type);
            } else if(data.cseType !== 'none') {
                throw this._client.getClass('exception.encryption.unsupported', data, 'CSEv1r1');
            }
        }

        return this.fromObject(data);
    }

    /**
     * @param {AbstractRevisionModel} model
     * @return {Promise<Object>}
     * @api
     */
    async toEncryptedData(model) {
        let data = await this.toObject(model);

        if(data.cseType === 'none') {
            return await this._client.getInstance('encryption.none').encrypt(data, this._type);
        }

        if(data.cseType === 'CSEv1r1') {
            return await this._client.getCseV1Encryption().encrypt(data, this._type);
        }

        return await this._client.getDefaultEncryption().encrypt(data, this._type);
    }

    /**
     * @param {AbstractRevisionModel} model
     * @return {Promise<Object>}
     * @api
     */
    async toApiObject(model) {
        let data       = await this.toEncryptedData(model),
            properties = model.getPropertyConfiguration();

        for(let key in data) {
            if(!properties.hasOwnProperty(key) || !properties[key].writeable) {
                delete data[key];
            }
        }

        let id = model.getId();
        if(typeof id === 'string' && id.length === 36) {
            data.id = id;
        }

        return data;
    }

    /**
     * @param {AbstractRevisionModel} model
     * @return {Object}
     * @api
     */
    toObject(model) {
        return model.getProperties();
    }

    /**
     * @param {AbstractRevisionModel} model
     * @return {String}
     * @api
     */
    toJSON(model) {
        return JSON.stringify(model);
    }
}