forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[7.x] Add username/password validation to login form (elastic#60681) (e…
- Loading branch information
Showing
4 changed files
with
201 additions
and
22 deletions.
There are no files selected for viewing
16 changes: 12 additions & 4 deletions
16
.../public/authentication/login/components/login_form/__snapshots__/login_form.test.tsx.snap
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
63 changes: 63 additions & 0 deletions
63
...plugins/security/public/authentication/login/components/login_form/validate_login.test.ts
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,63 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import { LoginValidator, LoginValidationResult } from './validate_login'; | ||
|
||
function expectValid(result: LoginValidationResult) { | ||
expect(result.isInvalid).toBe(false); | ||
} | ||
|
||
function expectInvalid(result: LoginValidationResult) { | ||
expect(result.isInvalid).toBe(true); | ||
} | ||
|
||
describe('LoginValidator', () => { | ||
describe('#validateUsername', () => { | ||
it(`returns 'valid' if validation is disabled`, () => { | ||
expectValid(new LoginValidator().validateUsername('')); | ||
}); | ||
|
||
it(`returns 'invalid' if username is missing`, () => { | ||
expectInvalid(new LoginValidator({ shouldValidate: true }).validateUsername('')); | ||
}); | ||
|
||
it(`returns 'valid' for correct usernames`, () => { | ||
expectValid(new LoginValidator({ shouldValidate: true }).validateUsername('u')); | ||
}); | ||
}); | ||
|
||
describe('#validatePassword', () => { | ||
it(`returns 'valid' if validation is disabled`, () => { | ||
expectValid(new LoginValidator().validatePassword('')); | ||
}); | ||
|
||
it(`returns 'invalid' if password is missing`, () => { | ||
expectInvalid(new LoginValidator({ shouldValidate: true }).validatePassword('')); | ||
}); | ||
|
||
it(`returns 'valid' for correct passwords`, () => { | ||
expectValid(new LoginValidator({ shouldValidate: true }).validatePassword('p')); | ||
}); | ||
}); | ||
|
||
describe('#validateForLogin', () => { | ||
it(`returns 'valid' if validation is disabled`, () => { | ||
expectValid(new LoginValidator().validateForLogin('', '')); | ||
}); | ||
|
||
it(`returns 'invalid' if username is invalid`, () => { | ||
expectInvalid(new LoginValidator({ shouldValidate: true }).validateForLogin('', 'p')); | ||
}); | ||
|
||
it(`returns 'invalid' if password is invalid`, () => { | ||
expectInvalid(new LoginValidator({ shouldValidate: true }).validateForLogin('u', '')); | ||
}); | ||
|
||
it(`returns 'valid' if username and password are valid`, () => { | ||
expectValid(new LoginValidator({ shouldValidate: true }).validateForLogin('u', 'p')); | ||
}); | ||
}); | ||
}); |
97 changes: 97 additions & 0 deletions
97
x-pack/plugins/security/public/authentication/login/components/login_form/validate_login.ts
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,97 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import { i18n } from '@kbn/i18n'; | ||
|
||
interface LoginValidatorOptions { | ||
shouldValidate?: boolean; | ||
} | ||
|
||
export interface LoginValidationResult { | ||
isInvalid: boolean; | ||
error?: string; | ||
} | ||
|
||
export class LoginValidator { | ||
private shouldValidate?: boolean; | ||
|
||
constructor(options: LoginValidatorOptions = {}) { | ||
this.shouldValidate = options.shouldValidate; | ||
} | ||
|
||
public enableValidation() { | ||
this.shouldValidate = true; | ||
} | ||
|
||
public disableValidation() { | ||
this.shouldValidate = false; | ||
} | ||
|
||
public validateUsername(username: string): LoginValidationResult { | ||
if (!this.shouldValidate) { | ||
return valid(); | ||
} | ||
|
||
if (!username) { | ||
// Elasticsearch has more stringent requirements for usernames in the Native realm. However, the login page is used for other realms, | ||
// such as LDAP and Active Directory. Because of that, the best validation we can do here is to ensure the username is not empty. | ||
return invalid( | ||
i18n.translate( | ||
'xpack.security.authentication.login.validateLogin.requiredUsernameErrorMessage', | ||
{ | ||
defaultMessage: 'Username is required', | ||
} | ||
) | ||
); | ||
} | ||
|
||
return valid(); | ||
} | ||
|
||
public validatePassword(password: string): LoginValidationResult { | ||
if (!this.shouldValidate) { | ||
return valid(); | ||
} | ||
|
||
if (!password) { | ||
// Elasticsearch has more stringent requirements for passwords in the Native realm. However, the login page is used for other realms, | ||
// such as LDAP and Active Directory. Because of that, the best validation we can do here is to ensure the password is not empty. | ||
return invalid( | ||
i18n.translate( | ||
'xpack.security.authentication.login.validateLogin.requiredPasswordErrorMessage', | ||
{ | ||
defaultMessage: 'Password is required', | ||
} | ||
) | ||
); | ||
} | ||
return valid(); | ||
} | ||
|
||
public validateForLogin(username: string, password: string): LoginValidationResult { | ||
const { isInvalid: isUsernameInvalid } = this.validateUsername(username); | ||
const { isInvalid: isPasswordInvalid } = this.validatePassword(password); | ||
|
||
if (isUsernameInvalid || isPasswordInvalid) { | ||
return invalid(); | ||
} | ||
|
||
return valid(); | ||
} | ||
} | ||
|
||
function invalid(error?: string): LoginValidationResult { | ||
return { | ||
isInvalid: true, | ||
error, | ||
}; | ||
} | ||
|
||
function valid(): LoginValidationResult { | ||
return { | ||
isInvalid: false, | ||
}; | ||
} |