diff --git a/.depcheckrc b/.depcheckrc new file mode 100644 index 0000000..c1092d1 --- /dev/null +++ b/.depcheckrc @@ -0,0 +1,2 @@ +ignores: ["dotenv", "depcheck", "lint-staged", "ts-node"] +ignorePatterns: [".history/*"] diff --git a/.eslintignore b/.eslintignore new file mode 100644 index 0000000..76add87 --- /dev/null +++ b/.eslintignore @@ -0,0 +1,2 @@ +node_modules +dist \ No newline at end of file diff --git a/.eslintrc.yaml b/.eslintrc.yaml new file mode 100644 index 0000000..ecb6fa5 --- /dev/null +++ b/.eslintrc.yaml @@ -0,0 +1,22 @@ +extends: + - "eslint:recommended" + - "plugin:@typescript-eslint/eslint-recommended" + - "plugin:@typescript-eslint/recommended" + - "prettier" + - "plugin:prettier/recommended" +parser: "@typescript-eslint/parser" +parserOptions: + project: [ + "tsconfig.json", + "tests/tsconfig.json", + ] +plugins: + - "@typescript-eslint" + - "prettier" +root: true +rules: + "@typescript-eslint/no-inferrable-types": "off" + "@typescript-eslint/no-unused-vars": + - error + - argsIgnorePattern: _ + varsIgnorePattern: _ diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..db4e45b --- /dev/null +++ b/.gitignore @@ -0,0 +1,8 @@ +node_modules +.vscode +dist +.nyc_output +*.env +.DS_Store +.idea/ +*.tgz diff --git a/.husky/pre-commit b/.husky/pre-commit new file mode 100755 index 0000000..f370360 --- /dev/null +++ b/.husky/pre-commit @@ -0,0 +1,21 @@ +#!/bin/sh +. "$(dirname "$0")/_/husky.sh" + +echo "* Validating working branch..." +if [[ $(git rev-parse --abbrev-ref HEAD) == master ]];then + echo "ERROR: can't commit to master branch" + exit 1 +fi +echo "* Finished branch validation" + +echo "* Verifying the code is linted..." +npx lint-staged +echo "* Finished lint check" + +# echo "* Validating no .only in tests" +# npm run test -- --dry-run +# echo "* Finished .only check" + +echo "* Validating dependencies" +npx depcheck +echo "* Finished dependecies check" \ No newline at end of file diff --git a/.lintstagedrc.json b/.lintstagedrc.json new file mode 100644 index 0000000..92a7ae8 --- /dev/null +++ b/.lintstagedrc.json @@ -0,0 +1,4 @@ + +{ + "{src,test}/**/(*.ts)": "npm run lint -- --fix" +} \ No newline at end of file diff --git a/.mocharc.js b/.mocharc.js new file mode 100644 index 0000000..f05dd6d --- /dev/null +++ b/.mocharc.js @@ -0,0 +1,12 @@ +const { CI } = process.env; + +module.exports = { + forbidOnly: !!CI, // fail the pipeline whenever .only is committed to the tests + require: [ + 'ts-node/register', // required for running tests with TypeScript + 'tests/utils/chaiPlugins.ts', // import chai plugins + ], + recursive: true, + exit: true, + timeout: 2000 +}; diff --git a/.prettierrc.yaml b/.prettierrc.yaml new file mode 100644 index 0000000..7a09d83 --- /dev/null +++ b/.prettierrc.yaml @@ -0,0 +1,7 @@ +arrowParens: avoid +bracketSpacing: true +endOfLine: auto +printWidth: 120 +singleQuote: true +tabWidth: 2 +trailingComma: all \ No newline at end of file diff --git a/package.json b/package.json new file mode 100644 index 0000000..424fa81 --- /dev/null +++ b/package.json @@ -0,0 +1,55 @@ +{ + "name": "@payu/zxcvbn-custom-matchers", + "version": "0.0.0", + "description": "Extended matchers for zxcvbn package for passwords validation", + "main": "dist/src/index.js", + "types": "dist/src/index.d.ts", + "files": [ + "dist/src" + ], + "scripts": { + "prepare": "husky install", + "prepack": "rm -rf dist; tsc -p tsconfig.json", + "test": "npx mocha tests/**/*.test.ts tests/unit/**/**/*.test.ts", + "test:coverage": "npx nyc npm run test", + "lint": "npx eslint . --ext .ts" + }, + "repository": { + "type": "git", + "url": "https://github.com/PayU/zxcvbn-custom-matchers.git" + }, + "keywords": [ + "logger", + "redact" + ], + "author": "Kobi Carmeli", + "license": "ISC", + "dependencies": { + "@zxcvbn-ts/core": "^3.0.4" + }, + "devDependencies": { + "@types/chai-as-promised": "^7.1.5", + "@types/chai-datetime": "^0.0.37", + "@types/chai-subset": "^1.3.3", + "@types/chai-uuid": "^1.0.2", + "@types/mocha": "^10.0.6", + "@types/sinon-chai": "^3.2.9", + "@typescript-eslint/eslint-plugin": "^6.7.5", + "@typescript-eslint/parser": "^6.7.5", + "chai": "^4.4.1", + "chai-as-promised": "^7.1.1", + "chai-datetime": "^1.8.0", + "chai-subset": "^1.6.0", + "chai-uuid": "^1.0.6", + "depcheck": "^1.4.3", + "eslint": "^8.18.0", + "eslint-config-prettier": "^9.0.0", + "eslint-plugin-prettier": "^5.0.1", + "husky": "^8.0.3", + "lint-staged": "^15.0.1", + "mocha": "^10.2.0", + "sinon-chai": "^3.7.0", + "ts-node": "^10.9.2", + "typescript": "^5.3.3" + } +} diff --git a/src/index.ts b/src/index.ts new file mode 100644 index 0000000..9f4412d --- /dev/null +++ b/src/index.ts @@ -0,0 +1 @@ +export { testMatcher } from './matchers/testMatcher'; diff --git a/src/matchers/testMatcher.ts b/src/matchers/testMatcher.ts new file mode 100644 index 0000000..2e56fed --- /dev/null +++ b/src/matchers/testMatcher.ts @@ -0,0 +1,27 @@ +import { Matcher, Match, MatchEstimated, MatchExtended } from '@zxcvbn-ts/core/dist/types'; + +export const testMatcher: Matcher = { + Matching: class MatchFirst { + match({ password }: { password: string }) { + const matches: Match[] = []; + if (password.includes('123')) { + matches.push({ + pattern: 'firstMatcher', + token: password, + i: 0, + j: password.length - 1, + }); + } + return matches; + } + }, + feedback(_match: MatchEstimated, _isSoleMatch: boolean) { + return { + warning: 'Your password should not contain sequences like "123".', + suggestions: ['Avoid common patterns like numbers in sequence.'], + }; + }, + scoring(_match: MatchExtended) { + return -50; + }, +}; diff --git a/test/utils/chaiPlugins.ts b/test/utils/chaiPlugins.ts new file mode 100644 index 0000000..06d4792 --- /dev/null +++ b/test/utils/chaiPlugins.ts @@ -0,0 +1,12 @@ +import { use } from 'chai'; +import uuid from 'chai-uuid'; +import sinonChai from 'sinon-chai'; +import chaiSubset from 'chai-subset'; +import chaiAsPromised from 'chai-as-promised'; +import chaiDateTime from 'chai-datetime'; + +use(sinonChai); +use(chaiSubset); +use(chaiAsPromised); +use(uuid); +use(chaiDateTime); diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..7b7d09a --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,66 @@ +{ + "compilerOptions": { + /* Visit https://aka.ms/tsconfig.json to read more about this file */ + /* Basic Options */ + // "incremental": true, /* Enable incremental compilation */ + "target": "es2019", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */ + "module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */ + // "lib": [], /* Specify library files to be included in the compilation. */ + // "allowJs": true, /* Allow javascript files to be compiled. */ + // "checkJs": true, /* Report errors in .js files. */ + // "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */ + "declaration": true, /* Generates corresponding '.d.ts' file. */ + "declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */ + "sourceMap": true, /* Generates corresponding '.map' file. */ + // "outFile": "./", /* Concatenate and emit output to single file. */ + "outDir": "./dist", /* Redirect output structure to the directory. */ + // "rootDir": "./", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */ + // "composite": true, /* Enable project compilation */ + // "tsBuildInfoFile": "./", /* Specify file to store incremental compilation information */ + // "removeComments": true, /* Do not emit comments to output. */ + // "noEmit": true, /* Do not emit outputs. */ + // "importHelpers": true, /* Import emit helpers from 'tslib'. */ + // "downlevelIteration": true, /* Provide full support for iterables in 'for-of', spread, and destructuring when targeting 'ES5' or 'ES3'. */ + // "isolatedModules": true, /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */ + /* Strict Type-Checking Options */ + // "strict": true, /* Enable all strict type-checking options. */ + // "noImplicitAny": true, /* Raise error on expressions and declarations with an implied 'any' type. */ + // "strictNullChecks": true, /* Enable strict null checks. */ + // "strictFunctionTypes": true, /* Enable strict checking of function types. */ + // "strictBindCallApply": true, /* Enable strict 'bind', 'call', and 'apply' methods on functions. */ + // "strictPropertyInitialization": true, /* Enable strict checking of property initialization in classes. */ + // "noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */ + // "alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */ + /* Additional Checks */ + // "noUnusedLocals": true, /* Report errors on unused locals. */ + // "noUnusedParameters": true, /* Report errors on unused parameters. */ + // "noImplicitReturns": true, /* Report error when not all code paths in function return a value. */ + // "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */ + /* Module Resolution Options */ + // "moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */ + // "baseUrl": "./", /* Base directory to resolve non-absolute module names. */ + // "paths": {}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */ + // "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */ + // "typeRoots": [], /* List of folders to include type definitions from. */ + // "types": [], /* Type declaration files to be included in compilation. */ + // "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */ + "esModuleInterop": true, /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */ + // "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */ + // "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */ + /* Source Map Options */ + // "sourceRoot": "", /* Specify the location where debugger should locate TypeScript files instead of source locations. */ + // "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */ + // "inlineSourceMap": true, /* Emit a single file with source maps instead of having a separate file. */ + // "inlineSources": true, /* Emit the source alongside the sourcemaps within a single file; requires '--inlineSourceMap' or '--sourceMap' to be set. */ + /* Experimental Options */ + // "experimentalDecorators": true, /* Enables experimental support for ES7 decorators. */ + // "emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */ + /* Advanced Options */ + "skipLibCheck": true, /* Skip type checking of declaration files. */ + "forceConsistentCasingInFileNames": true /* Disallow inconsistently-cased references to the same file. */ + }, + "include": [ + "src", + "test" + ] +}