diff --git a/types/owasp-password-strength-test/index.d.ts b/types/owasp-password-strength-test/index.d.ts new file mode 100644 index 0000000000..352bc694cf --- /dev/null +++ b/types/owasp-password-strength-test/index.d.ts @@ -0,0 +1,73 @@ +// Type definitions for owasp-password-strength-test 1.3 +// Project: https://github.com/nowsecure/owasp-password-strength-test +// Definitions by: Stephan Troyer +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.1 + +export as namespace owaspPasswordStrengthTest; + +export interface TestResult { + /** error messages associated with the failed tests */ + errors: string[]; + /** enumerates which tests have failed, beginning from 0 with the first required test */ + failedTests: number[]; + /** enumerates which tests have succeeded, beginning from 0 with the first required test */ + passedTests: number[]; + /** error messages of required tests that have failed */ + requiredTestErrors: string[]; + /** error messages of optional tests that have failed */ + optionalTestErrors: string[]; + /** indicates whether or not the password was considered to be a passphrase */ + isPassphrase: boolean; + /** indicates whether or not the user's password satisfied the strength requirements */ + strong: boolean; + /** + * indicates how many of the optional tests were passed; + * In order for the password to be considered "strong", it (by default) must either be a passphrase, + * or must pass a number of optional tests that is equal to or greater than configs.minOptionalTestsToPass + */ + optionalTestsPassed: number; +} +export interface TestConfig { + /** + * toggles the "passphrase" mechanism on and off; + * If set to false, the strength-checker will abandon the notion of "passphrases", + * and will subject all passwords to the same complexity requirements. + */ + allowPassphrases: boolean; + /** constraint on a password's maximum length */ + maxLength: number; + /** constraint on a password's minimum length */ + minLength: number; + /** + * minimum length a password needs to achieve in order to be considered a "passphrase" + * (and thus exempted from the optional complexity tests by default) + */ + minPhraseLength: number; + /** + * minimum number of optional tests that a password must pass in order to be considered "strong"; + * By default (per the OWASP guidelines), four optional complexity tests are made, + * and a password must pass at least three of them in order to be considered "strong" + */ + minOptionalTestsToPass: number; + + // // to add support for https://github.com/nowsecure/owasp-password-strength-test/pull/5 + // /** + // * toggles the i18n error keys in place of english error messages. + // * This can be useful when translating the errors using a 3rd party i18n library. + // * When true the following keys can be returned: + // * `failedMinLength`, `failedMaxLength`, `failedThreeRepeatedChars`, `optionalLowercaseRequired`, `optionalUppercaseRequired`, `optionalNumberRequired` and `optionalSpecialCharRequired`. + // */ + // i18nErrorKeys?: boolean; +} + +export type PasswordTest = (password: string) => string | undefined; + +export function test(password: string): TestResult; +export function config(configuration: Partial): void; + +export let tests: { + required: PasswordTest[]; + optional: PasswordTest[]; +}; +export const configs: Readonly; diff --git a/types/owasp-password-strength-test/owasp-password-strength-test-tests.ts b/types/owasp-password-strength-test/owasp-password-strength-test-tests.ts new file mode 100644 index 0000000000..0cdd336a20 --- /dev/null +++ b/types/owasp-password-strength-test/owasp-password-strength-test-tests.ts @@ -0,0 +1,18 @@ +import * as owasp from 'owasp-password-strength-test'; + +owasp.config({ + allowPassphrases: true, + maxLength: 128, + minLength: 10, + minPhraseLength: 20, + minOptionalTestsToPass: 4, +}); + +owasp.tests.required.push(password => { + if (password === 'one two three four five') { + return "That's the kind of thing an idiot would have on his luggage!"; + } +}); + +const result = owasp.test('correct horse battery staple'); +result.errors.length; diff --git a/types/owasp-password-strength-test/tsconfig.json b/types/owasp-password-strength-test/tsconfig.json new file mode 100644 index 0000000000..0206d30034 --- /dev/null +++ b/types/owasp-password-strength-test/tsconfig.json @@ -0,0 +1,23 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "strictFunctionTypes": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "owasp-password-strength-test-tests.ts" + ] +} diff --git a/types/owasp-password-strength-test/tslint.json b/types/owasp-password-strength-test/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/owasp-password-strength-test/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" }