All files / lib/Service CredentialFilterService.js

0% Statements 0/135
0% Branches 0/128
0% Functions 0/22
0% Lines 0/123

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225                                                                                                                                                                                                                                                                                                                                                                                                                                                                 
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.CredentialFilterService = exports.FILTERS = void 0;
const core_1 = require("@zxcvbn-ts/core");
const zxcvbnEnPackage = __importStar(require("@zxcvbn-ts/language-en"));
const zxcvbnCommonPackage = __importStar(require("@zxcvbn-ts/language-common"));
var FILTERS;
(function (FILTERS) {
    FILTERS[FILTERS["SHOW_ALL"] = 0] = "SHOW_ALL";
    FILTERS[FILTERS["COMPROMISED"] = 1] = "COMPROMISED";
    FILTERS[FILTERS["STRENGTH_LOW"] = 2] = "STRENGTH_LOW";
    FILTERS[FILTERS["STRENGTH_MEDIUM"] = 3] = "STRENGTH_MEDIUM";
    FILTERS[FILTERS["STRENGTH_GOOD"] = 4] = "STRENGTH_GOOD";
    FILTERS[FILTERS["EXPIRED"] = 5] = "EXPIRED";
    FILTERS[FILTERS["DELETED"] = 6] = "DELETED";
    FILTERS[FILTERS["ENCRYPTION_BROKEN"] = 7] = "ENCRYPTION_BROKEN"; // use FILTERS.ENCRYPTION_BROKEN only when you really know what you are doing
})(FILTERS || (exports.FILTERS = FILTERS = {}));
class CredentialFilterService {
    static getFilteredCredentials = (allCredentials, filter, additionalFilterText = undefined, additionalFilterTags = undefined) => {
        let filtered = [];
        for (const credential of allCredentials) {
            if (filter === FILTERS.ENCRYPTION_BROKEN) {
                // do not care about other major categories (like hidden / deleted / text) for this filter option
                if (credential.hasUnspecifiedEncryptionError()) {
                    filtered.push(credential);
                }
                continue;
            }
            if (CredentialFilterService.isHidden(credential)) {
                // hidden credentials are never shown in the list
                // matches usually only the (required) first test credential
                continue;
            }
            const additionalTextFilterEnabled = additionalFilterText !== undefined && additionalFilterText !== '';
            const additionalTagsFilterEnabled = additionalFilterTags !== undefined && additionalFilterTags.length > 0;
            // filter text if possible, otherwise ignore filter and proceed
            if ((!additionalTextFilterEnabled || (additionalTextFilterEnabled && CredentialFilterService.matchesFilterText(credential, additionalFilterText))) &&
                (!additionalTagsFilterEnabled || (additionalTagsFilterEnabled && CredentialFilterService.matchesFilterTags(credential, additionalFilterTags)))) {
                if (filter === FILTERS.DELETED) {
                    // deleted credentials will not match any other category
                    if (CredentialFilterService.isDeleted(credential)) {
                        filtered.push(credential);
                    }
                }
                else {
                    if (CredentialFilterService.isDeleted(credential)) {
                        // deleted credentials will not match any other category
                        continue;
                    }
                    if (filter === FILTERS.SHOW_ALL) {
                        filtered.push(credential);
                    }
                    else if (filter === FILTERS.COMPROMISED) {
                        if (CredentialFilterService.isCompromised(credential)) {
                            filtered.push(credential);
                        }
                    }
                    else if (filter === FILTERS.STRENGTH_LOW) {
                        if (CredentialFilterService.hasLowStrength(credential)) {
                            filtered.push(credential);
                        }
                    }
                    else if (filter === FILTERS.STRENGTH_MEDIUM) {
                        if (CredentialFilterService.hasMediumStrength(credential)) {
                            filtered.push(credential);
                        }
                    }
                    else if (filter === FILTERS.STRENGTH_GOOD) {
                        if (CredentialFilterService.hasGoodStrength(credential)) {
                            filtered.push(credential);
                        }
                    }
                    else if (filter === FILTERS.EXPIRED) {
                        if (CredentialFilterService.isExpired(credential)) {
                            filtered.push(credential);
                        }
                    }
                }
            }
        }
        return filtered;
    };
    static getFilterStats = (allCredentials) => {
        const stats = {
            allVisible: 0,
            compromised: 0,
            strengthLow: 0,
            strengthMedium: 0,
            strengthGood: 0,
            expired: 0,
            deleted: 0,
            encryptionBroken: 0
        };
        const options = {
            translations: zxcvbnEnPackage.translations,
            graphs: zxcvbnCommonPackage.adjacencyGraphs,
            dictionary: {}, // do not use dictionaries for a faster evaluation
        };
        core_1.zxcvbnOptions.setOptions(options);
        const now = Date.now();
        for (const credential of allCredentials) {
            if (!CredentialFilterService.isHidden(credential)) {
                if (CredentialFilterService.isDeleted(credential)) {
                    stats.deleted++;
                }
                else {
                    stats.allVisible++;
                    if (CredentialFilterService.isCompromised(credential)) {
                        stats.compromised++;
                    }
                    if (CredentialFilterService.isExpired(credential, now)) {
                        stats.expired++;
                    }
                    if (credential.password != null) {
                        // do not categorize credential by strength if it has no primary password set
                        // zxcvbn will fail for credential.password = null
                        const zxcvbnResult = (0, core_1.zxcvbn)(credential.password);
                        if (zxcvbnResult.score >= 3) {
                            stats.strengthGood++;
                        }
                        else if (zxcvbnResult.score == 2) {
                            stats.strengthMedium++;
                        }
                        else {
                            // use strength low also as fallback, if zxcvbnResult is < 0
                            stats.strengthLow++;
                        }
                    }
                }
            }
            // count all types of credentials (also hidden), if its encryption is broken
            if (credential.hasUnspecifiedEncryptionError()) {
                stats.encryptionBroken++;
            }
        }
        return stats;
    };
    static isHidden(credential) {
        return credential.hidden;
    }
    static isDeleted(credential) {
        return credential.delete_time !== null && credential.delete_time > 0;
    }
    static isCompromised(credential) {
        return credential.compromised !== null && credential.compromised != false;
    }
    static isExpired(credential, now = undefined) {
        if (now === undefined) {
            now = Date.now();
        }
        return credential.expire_time !== 0 && credential.expire_time * 1000 <= now;
    }
    static hasLowStrength(credential) {
        if (credential.password == null) {
            // do not categorize credential by strength if it has no primary password set
            return false;
        }
        const zxcvbnResult = (0, core_1.zxcvbn)(credential.password);
        return zxcvbnResult.score <= 1;
    }
    static hasMediumStrength(credential) {
        if (credential.password == null) {
            // do not categorize credential by strength if it has no primary password set
            return false;
        }
        const zxcvbnResult = (0, core_1.zxcvbn)(credential.password);
        return zxcvbnResult.score == 2;
    }
    static hasGoodStrength(credential) {
        if (credential.password == null) {
            // do not categorize credential by strength if it has no primary password set
            return false;
        }
        const zxcvbnResult = (0, core_1.zxcvbn)(credential.password);
        return zxcvbnResult.score >= 3;
    }
    static matchesFilterText(credential, filterText) {
        filterText = filterText.toLowerCase();
        if (credential.label != null && credential.label.toLowerCase().indexOf(filterText) > -1) {
            return true;
        }
        else if (credential.description != null && credential.description.toLowerCase().indexOf(filterText) > -1) {
            return true;
        }
        return false;
    }
    static matchesFilterTags(credential, filterTags) {
        return credential.tags && credential.tags.filter(tag => filterTags.includes(tag.text)).length > 0;
    }
}
exports.CredentialFilterService = CredentialFilterService;