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

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

class PassLink {

    /**
     *
     * @param {String} link
     * @return {{server: String, action: String, parameters: {}}}
     */
    analyzeLink(link) {
        let url = new URL(link);

        if(['ext+passlink:', 'web+passlink:', 'passlink:'].indexOf(url.protocol) === -1 || url.pathname.indexOf('/do/') === -1) {
            throw new InvalidLink();
        }

        let [server, action] = url.pathname.split('do/'),
            parameters       = {path: action};

        for(let key of url.searchParams.keys()) {
            parameters[key] = url.searchParams.get(key);
        }
        parameters.baseUrl = `https://${server}`;
        if(action.indexOf('/') !== -1) action = action.substr(0, action.indexOf('/'));

        return {server, action, parameters};
    }

    /**
     *
     * @param {String} action
     * @param {Object} parameters
     * @return {PassLinkAction}
     */
    getAction(action, parameters) {
        if(action.substr(0, 7) === 'connect') return new Connect(parameters);

        throw new UnknownAction(action);
    }
}

export default new PassLink();