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

BasicAuthAutofillManager.js « Manager « js « src - github.com/marius-wieschollek/passwords-webextension.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 4d128be8b701be06978f23596182e1a47b48e66b (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
import SystemService         from "@js/Services/SystemService";
import RecommendationManager from "@js/Manager/RecommendationManager";
import SettingsService       from '@js/Services/SettingsService';

export default new class BasicAuthAutofillManager {

    /**
     *
     */
    constructor() {
        this._pendingRequests = [];
        this._setting = null;
    }

    /**
     *
     */
    async init() {
        let api = SystemService.getBrowserApi();

        this._setting = await SettingsService.get('paste.basic-auth');

        api.webRequest.onAuthRequired.addListener(
            (d) => { return this._provideAuthData(d); },
            {urls: ['https://*/*']},
            ['blocking']
        );

        api.webRequest.onCompleted.addListener(
            (d) => { this._cleanUpRequest(d); },
            {urls: ['https://*/*']}
        );

        api.webRequest.onErrorOccurred.addListener(
            (d) => { this._cleanUpRequest(d); },
            {urls: ['https://*/*']}
        );
    }

    /**
     *
     * @param requestDetails
     * @return {(void|{authCredentials: {password: String, username: String}})}
     * @private
     */
    _provideAuthData(requestDetails) {
        if(!this._setting.getValue()) return;
        let recommendations = RecommendationManager.getRecommendationsByUrl(`https://${requestDetails.challenger.host}`, requestDetails.incognito);

        if(recommendations.length !== 0 && this._pendingRequests.indexOf(requestDetails.requestId) === -1) {
            this._pendingRequests.push(requestDetails.requestId);
            let recommendation  = recommendations[0];

            return {
                authCredentials: {
                    username: recommendation.getUserName(),
                    password: recommendation.getPassword()
                }
            };
        }
    }

    /**
     *
     * @param requestDetails
     * @private
     */
    _cleanUpRequest(requestDetails) {
        let index = this._pendingRequests.indexOf(requestDetails.requestId);
        if(index > -1) this._pendingRequests.splice(index, 1);
    }
};