Permalink
Show file tree
Hide file tree
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
chore: improve schema validation (#1712)
- Loading branch information
Showing
4 changed files
with
96 additions
and
44 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| from typing import Union | ||
|
|
||
| from flask import current_app | ||
| from flask_appbuilder.const import ( | ||
| API_SECURITY_PROVIDER_DB, | ||
| API_SECURITY_PROVIDER_LDAP, | ||
| AUTH_DB, | ||
| AUTH_LDAP, | ||
| ) | ||
| from marshmallow import fields, Schema, ValidationError | ||
| from marshmallow.validate import Length, OneOf | ||
|
|
||
|
|
||
| provider_to_auth_type = {"db": AUTH_DB, "ldap": AUTH_LDAP} | ||
|
|
||
|
|
||
| def validate_password(value: Union[bytes, bytearray, str]) -> None: | ||
| if not value: | ||
| raise ValidationError("Password is required") | ||
| if len(value) == 1 and value.encode()[0] == 0: | ||
| raise ValidationError("Password null is not allowed") | ||
|
|
||
|
|
||
| def validate_provider(value: Union[bytes, bytearray, str]) -> None: | ||
| if not current_app.appbuilder.sm.api_login_allow_multiple_providers: | ||
| provider_name = current_app.appbuilder.sm.auth_type_provider_name | ||
| if provider_name and provider_name != value: | ||
| raise ValidationError("Alternative authentication provider is not allowed") | ||
|
|
||
|
|
||
| class LoginPost(Schema): | ||
| username = fields.String(required=True, allow_none=False, validate=Length(min=1)) | ||
| password = fields.String( | ||
| validate=validate_password, required=True, allow_none=False | ||
| ) | ||
| provider = fields.String( | ||
| validate=[ | ||
| OneOf([API_SECURITY_PROVIDER_DB, API_SECURITY_PROVIDER_LDAP]), | ||
| validate_provider, | ||
| ] | ||
| ) | ||
| refresh = fields.Boolean(required=False) | ||
|
|
||
|
|
||
| login_post = LoginPost() |