Skip to content

Commit aa6f563

Browse files
Kulv3rgmelikov
authored andcommitted
Dynamic login field null-check validation added (with tests)
1 parent ebbe24b commit aa6f563

2 files changed

Lines changed: 51 additions & 35 deletions

File tree

genesis_core/tests/functional/restapi/iam/test_users.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,26 @@ def test_fields_in_me_info_success(
428428
# auth by phone is not implemented yet
429429
),
430430
("invalid_grant_type", "username", pytest.raises(ValueError)),
431+
(
432+
c.GRANT_TYPE_PASSWORD,
433+
"login",
434+
pytest.raises(bazooka_exc.BadRequestError),
435+
),
436+
(
437+
c.GRANT_TYPE_PASSWORD,
438+
"email",
439+
pytest.raises(bazooka_exc.BadRequestError),
440+
),
441+
(
442+
c.GRANT_TYPE_PASSWORD_EMAIL,
443+
"username",
444+
pytest.raises(bazooka_exc.BadRequestError),
445+
),
446+
(
447+
c.GRANT_TYPE_PASSWORD_LOGIN,
448+
"username",
449+
pytest.raises(bazooka_exc.BadRequestError),
450+
),
431451
],
432452
)
433453
def test_auth_with_param(
@@ -439,7 +459,7 @@ def test_auth_with_param(
439459
auth_test1_user,
440460
):
441461
params = {
442-
"username": "dummy_username",
462+
"username": None,
443463
"password": auth_test1_user.password,
444464
"grant_type": grant_type,
445465
}

genesis_core/user_api/iam/api/controllers.py

Lines changed: 30 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
from restalchemy.api import controllers
2727
from restalchemy.api import resources
2828
from restalchemy.common import contexts
29+
from restalchemy.common import exceptions as ra_e
2930
from restalchemy.dm import filters as ra_filters
3031
from restalchemy.openapi import utils as oa_utils
3132
import pyotp
@@ -537,13 +538,29 @@ def login(self, resource, user, password, **kwargs):
537538
@oa_utils.extend_schema(**oa_specs.OA_SPEC_GET_TOKEN_KWARGS)
538539
@actions.post
539540
def get_token(self, resource, grant_type, **kwargs):
540-
if grant_type in (
541-
c.GRANT_TYPE_PASSWORD,
542-
c.GRANT_TYPE_PASSWORD_USERNAME,
543-
c.GRANT_TYPE_PASSWORD_EMAIL,
544-
c.GRANT_TYPE_PASSWORD_PHONE,
545-
c.GRANT_TYPE_PASSWORD_LOGIN,
546-
):
541+
grant_type_map = {
542+
c.GRANT_TYPE_PASSWORD: (
543+
c.PARAM_USERNAME,
544+
resource.get_token_by_password,
545+
),
546+
c.GRANT_TYPE_PASSWORD_USERNAME: (
547+
c.PARAM_USERNAME,
548+
resource.get_token_by_password_username,
549+
),
550+
c.GRANT_TYPE_PASSWORD_EMAIL: (
551+
c.PARAM_EMAIL,
552+
resource.get_token_by_password_email,
553+
),
554+
c.GRANT_TYPE_PASSWORD_PHONE: (
555+
c.PARAM_PHONE,
556+
resource.get_token_by_password_phone,
557+
),
558+
c.GRANT_TYPE_PASSWORD_LOGIN: (
559+
c.PARAM_LOGIN,
560+
resource.get_token_by_password_login,
561+
),
562+
}
563+
if grant_type in grant_type_map:
547564
client_id = kwargs.get(
548565
c.PARAM_CLIENT_ID,
549566
self._req.headers.get(c.HEADER_CLIENT_ID, ""),
@@ -564,33 +581,12 @@ def get_token(self, resource, grant_type, **kwargs):
564581
otp_code=self._req.headers.get(c.HEADER_OTP_CODE, None),
565582
root_endpoint=resource.redirect_url,
566583
)
567-
if grant_type == c.GRANT_TYPE_PASSWORD:
568-
token = resource.get_token_by_password(
569-
username=kwargs.get(c.PARAM_USERNAME),
570-
**payload,
571-
)
572-
elif grant_type == c.GRANT_TYPE_PASSWORD_USERNAME:
573-
token = resource.get_token_by_password(
574-
username=kwargs.get(c.PARAM_USERNAME),
575-
**payload,
576-
)
577-
elif grant_type == c.GRANT_TYPE_PASSWORD_EMAIL:
578-
token = resource.get_token_by_password_email(
579-
email=kwargs.get(c.PARAM_EMAIL),
580-
**payload,
581-
)
582-
elif grant_type == c.GRANT_TYPE_PASSWORD_PHONE:
583-
token = resource.get_token_by_password_phone(
584-
phone=kwargs.get(c.PARAM_PHONE),
585-
**payload,
586-
)
587-
elif grant_type == c.GRANT_TYPE_PASSWORD_LOGIN:
588-
token = resource.get_token_by_password_login(
589-
login=kwargs.get(c.PARAM_LOGIN),
590-
**payload,
591-
)
592-
else:
593-
raise ValueError(f"Unexpected {grant_type=}")
584+
login_attr, token_getter = grant_type_map[grant_type]
585+
payload[login_attr] = kwargs.get(login_attr)
586+
if not payload[login_attr]:
587+
raise ra_e.ValidationErrorException()
588+
589+
token = token_getter(**payload)
594590
return token.get_response_body()
595591

596592
elif grant_type == c.GRANT_TYPE_REFRESH_TOKEN:

0 commit comments

Comments
 (0)