@@ -19,6 +19,7 @@ class Core():
19
19
basic_auth_callback : Callable [[str , str ], bool ] = None
20
20
enc_dec_jwt_callback : dict = None
21
21
get_user_roles_callback : list = None
22
+ personal_credentials : tuple [str , str ] = None
22
23
23
24
def enc_dec_jwt_config (self , func : Callable [[None ], dict ]) -> Callable [[None ], dict ]:
24
25
"""Decorator to verify the JWT token
@@ -29,6 +30,23 @@ def enc_dec_jwt_config(self, func: Callable[[None], dict]) -> Callable[[None], d
29
30
self .enc_dec_jwt_callback = func ()
30
31
return func
31
32
33
+ def personal_credentials_field (self , func : Callable [[None ], tuple [str , str ]]) -> Callable [[None ], tuple [str , str ]]:
34
+ """
35
+ Decorator to set the personal credentials, if youu dont want to use username and password inside the token
36
+ then with this you can return a tuple in which the first element is the username and the second is the password
37
+ but as you want to name that respective fields so the library will validate using the fields you set
38
+ :param func: function to be decorated
39
+ :return: the tuple with the username and password with personal names
40
+
41
+ :Example:
42
+ @dec_jwt.personal_credentials_field
43
+
44
+ def get_personal_credentials():
45
+ return "my_username_personal_name_field", "my_password_personal_name_field"
46
+ """
47
+ self .personal_credentials = func ()
48
+ return func
49
+
32
50
def verify_dict_config (self , config : str ) -> None :
33
51
"""Method that veryfies the JWT configuration generator and for basic auth
34
52
:param config: str to identify which configuration to verify"""
@@ -89,6 +107,11 @@ def __create_jwt_payload(self, bauth_credentials: dict) -> dict:
89
107
"""
90
108
if not self .jwt_fields_attr :
91
109
self .gen_abort_error ("jwt_claims decorator and function is not defined" , 500 )
110
+ if self .personal_credentials is not None :
111
+ bauth_credentials [self .personal_credentials [0 ]] = bauth_credentials ["username" ]
112
+ bauth_credentials [self .personal_credentials [1 ]] = bauth_credentials ["password" ]
113
+ del bauth_credentials ["username" ]
114
+ del bauth_credentials ["password" ]
92
115
payload = bauth_credentials
93
116
payload .update (self .jwt_fields_attr )
94
117
@@ -226,8 +249,14 @@ def __verify_token(self, token: dict) -> None:
226
249
self .gen_abort_error (f"The claim { claim } is not in the token" , 400 )
227
250
if len (token ) < 1 :
228
251
self .gen_abort_error ("Invalid token" , 401 )
229
- if ("username" not in token ) or ("password" not in token ):
230
- self .gen_abort_error ("Invalid token" , 401 )
252
+ if self .personal_credentials is not None :
253
+ per_username = self .personal_credentials [0 ]
254
+ per_password = self .personal_credentials [1 ]
255
+ if (per_username not in token ) or (per_password not in token ):
256
+ self .gen_abort_error ("Invalid token" , 401 )
257
+ else :
258
+ if ("username" not in token ) or ("password" not in token ):
259
+ self .gen_abort_error ("Invalid token" , 401 )
231
260
keys_to_validate = self .get_jwt_claims_to_verify_callback
232
261
for key in keys_to_validate :
233
262
if key not in token :
@@ -240,8 +269,12 @@ def __authenticate_credentials(self, token: dict) -> bool:
240
269
"""
241
270
if self .credentials_success_callback is None :
242
271
self .gen_abort_error ("get_credentials_success decorator is not set" , 500 )
243
- username_jwt = token ["username" ]
244
- password_jwt = token ["password" ]
272
+ if self .personal_credentials is None :
273
+ username_jwt = token ["username" ]
274
+ password_jwt = token ["password" ]
275
+ else :
276
+ username_jwt = token [self .personal_credentials [0 ]]
277
+ password_jwt = token [self .personal_credentials [1 ]]
245
278
return self .ensure_sync (self .credentials_success_callback )(username_jwt , password_jwt )
246
279
247
280
def __set_token_as_attr (self , token : dict ) -> None :
@@ -282,11 +315,14 @@ def wrapper(*args, **kwargs):
282
315
else :
283
316
token = self .__decode_jwt ()
284
317
self .__verify_token (token )
285
- self .verify_user_roles (roles , token ["username" ])
286
318
287
319
grant_access = self .__authenticate_credentials (token )
288
320
if not grant_access :
289
321
self .gen_abort_error ("The credentials are not correct" , 401 )
322
+ if self .personal_credentials is not None :
323
+ self .verify_user_roles (roles , token [self .personal_credentials [0 ]])
324
+ else :
325
+ self .verify_user_roles (roles , token ["username" ])
290
326
self .__set_token_as_attr (token )
291
327
292
328
return self .ensure_sync (func )(* args , ** kwargs )
0 commit comments