-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add user-friendly password validation
- Loading branch information
Showing
30 changed files
with
760 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// SPDX-FileCopyrightText: 2017-2025 City of Espoo | ||
// | ||
// SPDX-License-Identifier: LGPL-2.1-or-later | ||
|
||
import { PasswordConstraints } from './generated/api-types/shared' | ||
import { isPasswordStructureValid } from './password' | ||
|
||
describe('isPasswordStructureValid', () => { | ||
const unconstrained: PasswordConstraints = { | ||
minLength: 1, | ||
maxLength: 128, | ||
minDigits: 0, | ||
minLowers: 0, | ||
minSymbols: 0, | ||
minUppers: 0 | ||
} | ||
it('checks minLength correctly', () => { | ||
const constraints = { ...unconstrained, minLength: 4 } | ||
expect(isPasswordStructureValid(constraints, '123')).toBeFalsy() | ||
expect(isPasswordStructureValid(constraints, '1234')).toBeTruthy() | ||
expect(isPasswordStructureValid(constraints, '12345')).toBeTruthy() | ||
}) | ||
it('checks maxLength correctly', () => { | ||
const constraints = { ...unconstrained, maxLength: 4 } | ||
expect(isPasswordStructureValid(constraints, '12345')).toBeFalsy() | ||
expect(isPasswordStructureValid(constraints, '1234')).toBeTruthy() | ||
expect(isPasswordStructureValid(constraints, '123')).toBeTruthy() | ||
}) | ||
it('checks minLowers correctly', () => { | ||
const constraints = { ...unconstrained, minLowers: 1 } | ||
expect(isPasswordStructureValid(constraints, '1_2')).toBeFalsy() | ||
expect(isPasswordStructureValid(constraints, '1A2')).toBeFalsy() | ||
expect(isPasswordStructureValid(constraints, '1a2')).toBeTruthy() | ||
expect(isPasswordStructureValid(constraints, '1ab')).toBeTruthy() | ||
expect(isPasswordStructureValid(constraints, '1ä2')).toBeTruthy() | ||
}) | ||
it('checks minUppers correctly', () => { | ||
const constraints = { ...unconstrained, minUppers: 1 } | ||
expect(isPasswordStructureValid(constraints, '1_2')).toBeFalsy() | ||
expect(isPasswordStructureValid(constraints, '1a2')).toBeFalsy() | ||
expect(isPasswordStructureValid(constraints, '1A2')).toBeTruthy() | ||
expect(isPasswordStructureValid(constraints, '1AB')).toBeTruthy() | ||
expect(isPasswordStructureValid(constraints, '1Ä2')).toBeTruthy() | ||
}) | ||
it('checks minDigits correctly', () => { | ||
const constraints = { ...unconstrained, minDigits: 1 } | ||
expect(isPasswordStructureValid(constraints, 'abc')).toBeFalsy() | ||
expect(isPasswordStructureValid(constraints, 'a1c')).toBeTruthy() | ||
expect(isPasswordStructureValid(constraints, 'a12')).toBeTruthy() | ||
}) | ||
it('checks minSymbols correctly', () => { | ||
const constraints = { ...unconstrained, minSymbols: 1 } | ||
expect(isPasswordStructureValid(constraints, '123')).toBeFalsy() | ||
expect(isPasswordStructureValid(constraints, 'abc')).toBeFalsy() | ||
expect(isPasswordStructureValid(constraints, 'a#c')).toBeTruthy() | ||
expect(isPasswordStructureValid(constraints, 'a#2')).toBeTruthy() | ||
expect(isPasswordStructureValid(constraints, '💩')).toBeTruthy() | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// SPDX-FileCopyrightText: 2017-2025 City of Espoo | ||
// | ||
// SPDX-License-Identifier: LGPL-2.1-or-later | ||
|
||
import { PasswordConstraints } from './generated/api-types/shared' | ||
|
||
export function isPasswordStructureValid( | ||
constraints: PasswordConstraints, | ||
password: string | ||
) { | ||
function count(iter: Iterator<RegExpMatchArray>) { | ||
let result = iter.next() | ||
let count = 0 | ||
while (!result.done) { | ||
count += 1 | ||
result = iter.next() | ||
} | ||
return count | ||
} | ||
if (password.length < constraints.minLength) return false | ||
if (password.length > constraints.maxLength) return false | ||
// \p{...} check unicode categories: https://unicode.org/reports/tr18/#General_Category_Property | ||
if (count(password.matchAll(/\p{Ll}/gu)) < constraints.minLowers) return false | ||
if (count(password.matchAll(/\p{Lu}/gu)) < constraints.minUppers) return false | ||
if (count(password.matchAll(/\p{Nd}/gu)) < constraints.minDigits) return false | ||
if (count(password.matchAll(/[^\p{L}\p{N}]/gu)) < constraints.minSymbols) | ||
return false | ||
return true | ||
} |
Oops, something went wrong.