2626from restalchemy .api import controllers
2727from restalchemy .api import resources
2828from restalchemy .common import contexts
29+ from restalchemy .common import exceptions as ra_e
2930from restalchemy .dm import filters as ra_filters
3031from restalchemy .openapi import utils as oa_utils
3132import 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