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

Connect.js « Action « PassLink « src - git.mdns.eu/nextcloud/passwords-client.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 9b27e77ff7a352ef313a819d87d113a404de9629 (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
import PassLinkAction from './PassLinkAction';
import HttpRequest from '../../Network/HttpRequest';

export default class Connect extends PassLinkAction {

    /**
     *
     * @param {Object} parameters
     */
    constructor(parameters) {
        super(parameters);
        this._codes = null;
        this._clientLabel = null;
        this._theme = null;
        this._promise = null;
    }

    /**
     * Get the suggested name of the client
     *
     * @return {(null|String)}
     */
    getClientLabel() {
        return this._clientLabel;
    }

    /**
     * Set the suggested name of the client
     *
     * @param {String} value
     * @return {Connect}
     */
    setClientLabel(value) {
        this._clientLabel = value;

        return this;
    }

    /**
     * Get the theme from the link
     *
     * @return {Promise<null|object>|null}
     */
    getTheme() {
        if(this._theme !== null) return this._theme;

        return this._decodeTheme();
    }

    /**
     *
     * @return {[]}
     */
    getCodes() {
        if(this._codes !== null) return this._codes;

        let codes = [],
            spec  = new RegExp('^(?=\\S*[a-z])(?=\\S*[A-Z])(?=\\S*[\\d])\\S*$');

        while(codes.length < 4) {
            let code = Array(4)
                .fill('0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz')
                .map(x => x[Math.floor(crypto.getRandomValues(new Uint32Array(1))[0] / (0xffffffff + 1) * x.length)])
                .join('');

            if(spec.test(code)) codes.push(code);
        }

        this._codes = codes;
        return codes;
    }

    /**
     * Apply for the registration
     *
     * @return {Promise<void>}
     */
    apply() {
        if(this._promise === null) {
            this._promise = this._sendRequest();
        }

        return this._promise;
    }

    /**
     *
     * @return {Promise<void>}
     * @private
     */
    async _sendRequest() {
        let url     = `${this._parameters.baseUrl}index.php/apps/passwords/link/connect/apply`,
            request = new HttpRequest(url);

        let data = {
            id   : this._parameters.id,
            codes: this.getCodes()
        };

        if(this._clientLabel !== null) data.label = this._clientLabel;

        let response = await request.setData(data).send();

        return response.getData();
    }

    /**
     *
     * @private
     */
    async _decodeTheme() {
        if(!this._parameters.hasOwnProperty('theme')) return null;

        let pako   = await import(/* webpackChunkName: "pako" */ 'pako'),
            base64 = this._parameters.theme,
            zipped = atob(base64),
            json   = pako.inflate(zipped, {to: 'string'}),
            theme  = JSON.parse(json);

        if(theme.hasOwnProperty('color')) {
            theme['color.primary'] = theme.color;
            delete theme.color;
        }
        if(theme.hasOwnProperty('txtColor')) {
            theme['color.text'] = theme.txtColor;
            delete theme.txtColor;
        }
        if(theme.hasOwnProperty('bgColor')) {
            theme['color.background'] = theme.bgColor;
            delete theme.bgColor;
        }
        if(theme.hasOwnProperty('background') && theme.background.substr(0, 8) !== 'https://') {
            theme.background = this._parameters.baseUrl + theme.background;
        }
        if(theme.hasOwnProperty('logo')) {
            theme.logo = this._parameters.baseUrl + theme.logo;
        }

        this._theme = theme;

        return theme;
    }
}