Completed
Push — master ( 39fb8c...529c14 )
by Sander
01:17
created

js/background/service/httpAuth.js   A

Complexity

Total Complexity 6
Complexity/F 2

Size

Lines of Code 53
Function Count 3

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 0
c 1
b 0
f 0
nc 1
dl 0
loc 53
rs 10
wmc 6
mnd 1
bc 7
fnc 3
bpm 2.3333
cpm 2
noi 2

2 Functions

Rating   Name   Duplication   Size   Complexity  
B httpAuth.js ➔ provideCredentialsSync 0 24 3
A httpAuth.js ➔ completed 0 6 2
1
/* global API */
2
3
/**
4
 * Nextcloud - passman
5
 *
6
 * @copyright Copyright (c) 2016, Sander Brand ([email protected])
7
 * @copyright Copyright (c) 2016, Marcos Zuriaga Miguel ([email protected])
8
 * @license GNU AGPL version 3 or any later version
9
 *
10
 * This program is free software: you can redistribute it and/or modify
11
 * it under the terms of the GNU Affero General Public License as
12
 * published by the Free Software Foundation, either version 3 of the
13
 * License, or (at your option) any later version.
14
 *
15
 * This program is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 * GNU Affero General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU Affero General Public License
21
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22
 *
23
 */
24
25
(function () {
26
    'use strict';
27
    var pendingRequests = [];
28
29
    API.runtime.connect();
30
    // A request has completed.
31
    // We can stop worrying about it.
32
    function completed(requestDetails) {
33
        var index = pendingRequests.indexOf(requestDetails.requestId);
34
        if (index > -1) {
35
            pendingRequests.splice(index, 1);
36
        }
37
    }
38
39
    var auth_tries = [];
40
    var provideCredentialsSync = function (requestDetails) {
41
        if (!auth_tries[requestDetails.requestId]) {
42
            auth_tries[requestDetails.requestId] = 0;
43
        }
44
45
        var login = background.getCredentialForHTTPAuth(requestDetails);
0 ignored issues
show
Bug introduced by
The variable background seems to be never declared. If this is a global, consider adding a /** global: background */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
46
47
        // If we have seen this request before, then
48
        // assume our credentials were bad, and give up.
49
        if (pendingRequests.indexOf(requestDetails.requestId) === -1) {
50
            pendingRequests.push(requestDetails.requestId);
51
            return {
52
                authCredentials: {
53
                    username: login.username,
54
                    password: login.password
55
                }
56
            };
57
58
        } else {
59
            console.warn("bad credentials for: " + requestDetails.url + ', Showing login dialog');
0 ignored issues
show
Best Practice introduced by
There is no return statement in this branch, but you do return something in other branches. Did you maybe miss it? If you do not want to return anything, consider adding return undefined; explicitly.
Loading history...
60
            //return {cancel: true};
61
        }
62
63
    };
64
65
66
    API.webRequest.onAuthRequired.addListener(provideCredentialsSync, {urls: ["<all_urls>"]}, ["blocking"]);
67
68
    API.webRequest.onCompleted.addListener(
69
        completed,
70
        {urls: ["<all_urls>"]}
71
    );
72
73
    API.webRequest.onErrorOccurred.addListener(
74
        completed,
75
        {urls: ["<all_urls>"]}
76
    );
77
}());