square-authentication 5.1.0__py3-none-any.whl → 5.1.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -3,23 +3,20 @@ from typing import Annotated, List
3
3
 
4
4
  import bcrypt
5
5
  import jwt
6
- from fastapi import APIRouter, status, Header, HTTPException
6
+ from fastapi import APIRouter, Header, HTTPException, status
7
7
  from fastapi.params import Query
8
8
  from fastapi.responses import JSONResponse
9
9
  from requests import HTTPError
10
10
  from square_commons import get_api_output_in_standard_format
11
11
  from square_database_helper.main import SquareDatabaseHelper
12
- from square_database_helper.pydantic_models import (
13
- FiltersV0,
14
- FilterConditionsV0,
15
- )
12
+ from square_database_helper.pydantic_models import FilterConditionsV0, FiltersV0
16
13
  from square_database_structure.square import global_string_database_name
17
14
  from square_database_structure.square.authentication import global_string_schema_name
18
15
  from square_database_structure.square.authentication.tables import (
19
16
  User,
17
+ UserApp,
20
18
  UserCredential,
21
19
  UserSession,
22
- UserApp,
23
20
  )
24
21
  from square_database_structure.square.public import (
25
22
  global_string_schema_name as global_string_public_schema_name,
@@ -27,23 +24,23 @@ from square_database_structure.square.public import (
27
24
  from square_database_structure.square.public.tables import App
28
25
 
29
26
  from square_authentication.configuration import (
30
- global_object_square_logger,
31
- config_str_secret_key_for_access_token,
32
27
  config_int_access_token_valid_minutes,
33
28
  config_int_refresh_token_valid_minutes,
29
+ config_int_square_database_port,
30
+ config_str_secret_key_for_access_token,
34
31
  config_str_secret_key_for_refresh_token,
35
32
  config_str_square_database_ip,
36
- config_int_square_database_port,
37
33
  config_str_square_database_protocol,
34
+ global_object_square_logger,
38
35
  )
39
36
  from square_authentication.messages import messages
40
37
  from square_authentication.pydantic_models.core import (
41
- RegisterUsernameV0,
42
- LoginUsernameV0,
43
38
  DeleteUserV0,
44
- UpdatePasswordV0,
45
- TokenType,
39
+ LoginUsernameV0,
46
40
  LogoutAppsV0,
41
+ RegisterUsernameV0,
42
+ TokenType,
43
+ UpdatePasswordV0,
47
44
  )
48
45
  from square_authentication.utils.token import get_jwt_payload
49
46
 
@@ -213,6 +210,7 @@ async def register_username_v0(
213
210
  content=output_content,
214
211
  )
215
212
  except HTTPException as http_exception:
213
+ global_object_square_logger.logger.error(http_exception, exc_info=True)
216
214
  return JSONResponse(
217
215
  status_code=http_exception.status_code, content=http_exception.detail
218
216
  )
@@ -257,9 +255,9 @@ async def get_user_details_v0(
257
255
  output_content = get_api_output_in_standard_format(
258
256
  message=messages["INCORRECT_ACCESS_TOKEN"], log=str(error)
259
257
  )
260
- return JSONResponse(
258
+ raise HTTPException(
261
259
  status_code=status.HTTP_400_BAD_REQUEST,
262
- content=output_content,
260
+ detail=output_content,
263
261
  )
264
262
  user_id = local_dict_access_token_payload["user_id"]
265
263
  """
@@ -348,6 +346,7 @@ async def get_user_details_v0(
348
346
  content=output_content,
349
347
  )
350
348
  except HTTPException as http_exception:
349
+ global_object_square_logger.logger.error(http_exception, exc_info=True)
351
350
  return JSONResponse(
352
351
  status_code=http_exception.status_code, content=http_exception.detail
353
352
  )
@@ -387,9 +386,9 @@ async def update_user_app_ids_v0(
387
386
  output_content = get_api_output_in_standard_format(
388
387
  message=messages["INCORRECT_ACCESS_TOKEN"], log=str(error)
389
388
  )
390
- return JSONResponse(
389
+ raise HTTPException(
391
390
  status_code=status.HTTP_400_BAD_REQUEST,
392
- content=output_content,
391
+ detail=output_content,
393
392
  )
394
393
  user_id = local_dict_access_token_payload["user_id"]
395
394
 
@@ -508,6 +507,7 @@ async def update_user_app_ids_v0(
508
507
  content=output_content,
509
508
  )
510
509
  except HTTPException as http_exception:
510
+ global_object_square_logger.logger.error(http_exception, exc_info=True)
511
511
  return JSONResponse(
512
512
  status_code=http_exception.status_code, content=http_exception.detail
513
513
  )
@@ -560,8 +560,9 @@ async def login_username_v0(body: LoginUsernameV0):
560
560
  message=messages["INCORRECT_USERNAME"],
561
561
  log=f"incorrect username {username}",
562
562
  )
563
- return JSONResponse(
564
- status_code=status.HTTP_400_BAD_REQUEST, content=output_content
563
+ raise HTTPException(
564
+ status_code=status.HTTP_400_BAD_REQUEST,
565
+ detail=output_content,
565
566
  )
566
567
  # validate if app_id is assigned to user
567
568
  # this will also validate if app_id is valid
@@ -597,16 +598,17 @@ async def login_username_v0(body: LoginUsernameV0):
597
598
  message=messages["GENERIC_400"],
598
599
  log=str(he),
599
600
  )
600
- return JSONResponse(
601
- status_code=he.response.status_code, content=output_content
601
+ raise HTTPException(
602
+ status_code=he.response.status_code, detail=output_content
602
603
  )
603
604
  else:
604
605
  output_content = get_api_output_in_standard_format(
605
606
  message=messages["GENERIC_400"],
606
607
  log=f"user_id {local_str_user_id}({username}) not assigned to app {app_id}.",
607
608
  )
608
- return JSONResponse(
609
- status_code=status.HTTP_400_BAD_REQUEST, content=output_content
609
+ raise HTTPException(
610
+ status_code=status.HTTP_400_BAD_REQUEST,
611
+ detail=output_content,
610
612
  )
611
613
 
612
614
  # validate password
@@ -622,9 +624,9 @@ async def login_username_v0(body: LoginUsernameV0):
622
624
  message=messages["INCORRECT_PASSWORD"],
623
625
  log=f"incorrect password for user_id {local_str_user_id}({username}).",
624
626
  )
625
- return JSONResponse(
627
+ raise HTTPException(
626
628
  status_code=status.HTTP_400_BAD_REQUEST,
627
- content=output_content,
629
+ detail=output_content,
628
630
  )
629
631
  """
630
632
  main process
@@ -692,6 +694,7 @@ async def login_username_v0(body: LoginUsernameV0):
692
694
  content=output_content,
693
695
  )
694
696
  except HTTPException as http_exception:
697
+ global_object_square_logger.logger.error(http_exception, exc_info=True)
695
698
  return JSONResponse(
696
699
  status_code=http_exception.status_code, content=http_exception.detail
697
700
  )
@@ -740,9 +743,9 @@ async def generate_access_token_v0(
740
743
  message=messages["INCORRECT_REFRESH_TOKEN"],
741
744
  log=f"incorrect refresh token: {refresh_token}.",
742
745
  )
743
- return JSONResponse(
746
+ raise HTTPException(
744
747
  status_code=status.HTTP_400_BAD_REQUEST,
745
- content=output_content,
748
+ detail=output_content,
746
749
  )
747
750
  # validating if the refresh token is valid, active and of the same user.
748
751
  try:
@@ -753,9 +756,9 @@ async def generate_access_token_v0(
753
756
  output_content = get_api_output_in_standard_format(
754
757
  message=messages["INCORRECT_REFRESH_TOKEN"], log=str(error)
755
758
  )
756
- return JSONResponse(
759
+ raise HTTPException(
757
760
  status_code=status.HTTP_400_BAD_REQUEST,
758
- content=output_content,
761
+ detail=output_content,
759
762
  )
760
763
  """
761
764
  main process
@@ -782,6 +785,7 @@ async def generate_access_token_v0(
782
785
  content=output_content,
783
786
  )
784
787
  except HTTPException as http_exception:
788
+ global_object_square_logger.logger.error(http_exception, exc_info=True)
785
789
  return JSONResponse(
786
790
  status_code=http_exception.status_code, content=http_exception.detail
787
791
  )
@@ -830,9 +834,9 @@ async def logout_v0(
830
834
  message=messages["INCORRECT_REFRESH_TOKEN"],
831
835
  log=f"incorrect refresh token: {refresh_token}.",
832
836
  )
833
- return JSONResponse(
837
+ raise HTTPException(
834
838
  status_code=status.HTTP_400_BAD_REQUEST,
835
- content=output_content,
839
+ detail=output_content,
836
840
  )
837
841
  # validating if the refresh token is valid, active and of the same user.
838
842
  try:
@@ -844,9 +848,9 @@ async def logout_v0(
844
848
  message=messages["INCORRECT_REFRESH_TOKEN"],
845
849
  log=str(error),
846
850
  )
847
- return JSONResponse(
851
+ raise HTTPException(
848
852
  status_code=status.HTTP_400_BAD_REQUEST,
849
- content=output_content,
853
+ detail=output_content,
850
854
  )
851
855
  # ======================================================================================
852
856
  # NOTE: if refresh token has expired no need to delete it during this call
@@ -875,6 +879,7 @@ async def logout_v0(
875
879
  )
876
880
  return JSONResponse(status_code=status.HTTP_200_OK, content=output_content)
877
881
  except HTTPException as http_exception:
882
+ global_object_square_logger.logger.error(http_exception, exc_info=True)
878
883
  return JSONResponse(
879
884
  status_code=http_exception.status_code, content=http_exception.detail
880
885
  )
@@ -911,9 +916,9 @@ async def logout_apps_v0(
911
916
  output_content = get_api_output_in_standard_format(
912
917
  message=messages["INCORRECT_ACCESS_TOKEN"], log=str(error)
913
918
  )
914
- return JSONResponse(
919
+ raise HTTPException(
915
920
  status_code=status.HTTP_400_BAD_REQUEST,
916
- content=output_content,
921
+ detail=output_content,
917
922
  )
918
923
  user_id = local_dict_access_token_payload["user_id"]
919
924
  # validate app_ids
@@ -967,6 +972,7 @@ async def logout_apps_v0(
967
972
  )
968
973
  return JSONResponse(status_code=status.HTTP_200_OK, content=output_content)
969
974
  except HTTPException as http_exception:
975
+ global_object_square_logger.logger.error(http_exception, exc_info=True)
970
976
  return JSONResponse(
971
977
  status_code=http_exception.status_code, content=http_exception.detail
972
978
  )
@@ -1002,9 +1008,9 @@ async def logout_all_v0(
1002
1008
  output_content = get_api_output_in_standard_format(
1003
1009
  message=messages["INCORRECT_ACCESS_TOKEN"], log=str(error)
1004
1010
  )
1005
- return JSONResponse(
1011
+ raise HTTPException(
1006
1012
  status_code=status.HTTP_400_BAD_REQUEST,
1007
- content=output_content,
1013
+ detail=output_content,
1008
1014
  )
1009
1015
  user_id = local_dict_access_token_payload["user_id"]
1010
1016
 
@@ -1030,6 +1036,7 @@ async def logout_all_v0(
1030
1036
  )
1031
1037
  return JSONResponse(status_code=status.HTTP_200_OK, content=output_content)
1032
1038
  except HTTPException as http_exception:
1039
+ global_object_square_logger.logger.error(http_exception, exc_info=True)
1033
1040
  return JSONResponse(
1034
1041
  status_code=http_exception.status_code, content=http_exception.detail
1035
1042
  )
@@ -1066,9 +1073,9 @@ async def update_username_v0(
1066
1073
  output_content = get_api_output_in_standard_format(
1067
1074
  message=messages["INCORRECT_ACCESS_TOKEN"], log=str(error)
1068
1075
  )
1069
- return JSONResponse(
1076
+ raise HTTPException(
1070
1077
  status_code=status.HTTP_400_BAD_REQUEST,
1071
- content=output_content,
1078
+ detail=output_content,
1072
1079
  )
1073
1080
  user_id = local_dict_access_token_payload["user_id"]
1074
1081
 
@@ -1089,9 +1096,9 @@ async def update_username_v0(
1089
1096
  message=messages["INCORRECT_USER_ID"],
1090
1097
  log=f"incorrect user_id: {user_id}.",
1091
1098
  )
1092
- return JSONResponse(
1099
+ raise HTTPException(
1093
1100
  status_code=status.HTTP_400_BAD_REQUEST,
1094
- content=output_content,
1101
+ detail=output_content,
1095
1102
  )
1096
1103
 
1097
1104
  # validate new username
@@ -1116,9 +1123,9 @@ async def update_username_v0(
1116
1123
  message=messages["USERNAME_ALREADY_EXISTS"],
1117
1124
  log=f"{new_username} is taken.",
1118
1125
  )
1119
- return JSONResponse(
1120
- status_code=status.HTTP_409_CONFLICT,
1121
- content=output_content,
1126
+ raise HTTPException(
1127
+ status_code=status.HTTP_400_BAD_REQUEST,
1128
+ detail=output_content,
1122
1129
  )
1123
1130
  """
1124
1131
  main process
@@ -1146,6 +1153,7 @@ async def update_username_v0(
1146
1153
  )
1147
1154
  return JSONResponse(status_code=status.HTTP_200_OK, content=output_content)
1148
1155
  except HTTPException as http_exception:
1156
+ global_object_square_logger.logger.error(http_exception, exc_info=True)
1149
1157
  return JSONResponse(
1150
1158
  status_code=http_exception.status_code, content=http_exception.detail
1151
1159
  )
@@ -1183,9 +1191,9 @@ async def delete_user_v0(
1183
1191
  output_content = get_api_output_in_standard_format(
1184
1192
  message=messages["INCORRECT_ACCESS_TOKEN"], log=str(error)
1185
1193
  )
1186
- return JSONResponse(
1194
+ raise HTTPException(
1187
1195
  status_code=status.HTTP_400_BAD_REQUEST,
1188
- content=output_content,
1196
+ detail=output_content,
1189
1197
  )
1190
1198
  user_id = local_dict_access_token_payload["user_id"]
1191
1199
 
@@ -1205,8 +1213,9 @@ async def delete_user_v0(
1205
1213
  message=messages["INCORRECT_USER_ID"],
1206
1214
  log=f"incorrect user_id: {user_id}.",
1207
1215
  )
1208
- return JSONResponse(
1209
- status_code=status.HTTP_400_BAD_REQUEST, content=output_content
1216
+ raise HTTPException(
1217
+ status_code=status.HTTP_400_BAD_REQUEST,
1218
+ detail=output_content,
1210
1219
  )
1211
1220
 
1212
1221
  # validate password
@@ -1223,9 +1232,9 @@ async def delete_user_v0(
1223
1232
  message=messages["INCORRECT_PASSWORD"],
1224
1233
  log=f"incorrect password for user_id {user_id}.",
1225
1234
  )
1226
- return JSONResponse(
1235
+ raise HTTPException(
1227
1236
  status_code=status.HTTP_400_BAD_REQUEST,
1228
- content=output_content,
1237
+ detail=output_content,
1229
1238
  )
1230
1239
  """
1231
1240
  main process
@@ -1250,6 +1259,7 @@ async def delete_user_v0(
1250
1259
  )
1251
1260
  return JSONResponse(status_code=status.HTTP_200_OK, content=output_content)
1252
1261
  except HTTPException as http_exception:
1262
+ global_object_square_logger.logger.error(http_exception, exc_info=True)
1253
1263
  return JSONResponse(
1254
1264
  status_code=http_exception.status_code, content=http_exception.detail
1255
1265
  )
@@ -1288,9 +1298,9 @@ async def update_password_v0(
1288
1298
  output_content = get_api_output_in_standard_format(
1289
1299
  message=messages["INCORRECT_ACCESS_TOKEN"], log=str(error)
1290
1300
  )
1291
- return JSONResponse(
1301
+ raise HTTPException(
1292
1302
  status_code=status.HTTP_400_BAD_REQUEST,
1293
- content=output_content,
1303
+ detail=output_content,
1294
1304
  )
1295
1305
  user_id = local_dict_access_token_payload["user_id"]
1296
1306
 
@@ -1310,8 +1320,9 @@ async def update_password_v0(
1310
1320
  message=messages["INCORRECT_USER_ID"],
1311
1321
  log=f"incorrect user_id: {user_id}.",
1312
1322
  )
1313
- return JSONResponse(
1314
- status_code=status.HTTP_400_BAD_REQUEST, content=output_content
1323
+ raise HTTPException(
1324
+ status_code=status.HTTP_400_BAD_REQUEST,
1325
+ detail=output_content,
1315
1326
  )
1316
1327
 
1317
1328
  # validate password
@@ -1328,9 +1339,9 @@ async def update_password_v0(
1328
1339
  message=messages["INCORRECT_PASSWORD"],
1329
1340
  log=f"incorrect password for user_id {user_id}.",
1330
1341
  )
1331
- return JSONResponse(
1342
+ raise HTTPException(
1332
1343
  status_code=status.HTTP_400_BAD_REQUEST,
1333
- content=output_content,
1344
+ detail=output_content,
1334
1345
  )
1335
1346
  """
1336
1347
  main process
@@ -1361,6 +1372,7 @@ async def update_password_v0(
1361
1372
  )
1362
1373
  return JSONResponse(status_code=status.HTTP_200_OK, content=output_content)
1363
1374
  except HTTPException as http_exception:
1375
+ global_object_square_logger.logger.error(http_exception, exc_info=True)
1364
1376
  return JSONResponse(
1365
1377
  status_code=http_exception.status_code, content=http_exception.detail
1366
1378
  )
@@ -1424,11 +1436,12 @@ async def validate_and_get_payload_from_token_v0(
1424
1436
  message=messages["INCORRECT_REFRESH_TOKEN"],
1425
1437
  log="refresh token valid but not present in database.",
1426
1438
  )
1427
- return JSONResponse(
1439
+ raise HTTPException(
1428
1440
  status_code=status.HTTP_400_BAD_REQUEST,
1429
- content=output_content,
1441
+ detail=output_content,
1430
1442
  )
1431
-
1443
+ except HTTPException as http_exception:
1444
+ raise
1432
1445
  except Exception as error:
1433
1446
  output_content = None
1434
1447
  if token_type == TokenType.access_token:
@@ -1440,9 +1453,9 @@ async def validate_and_get_payload_from_token_v0(
1440
1453
  message=messages["INCORRECT_REFRESH_TOKEN"], log=str(error)
1441
1454
  )
1442
1455
 
1443
- return JSONResponse(
1456
+ raise HTTPException(
1444
1457
  status_code=status.HTTP_400_BAD_REQUEST,
1445
- content=output_content,
1458
+ detail=output_content,
1446
1459
  )
1447
1460
 
1448
1461
  """
@@ -1458,6 +1471,7 @@ async def validate_and_get_payload_from_token_v0(
1458
1471
  )
1459
1472
  return JSONResponse(status_code=status.HTTP_200_OK, content=output_content)
1460
1473
  except HTTPException as http_exception:
1474
+ global_object_square_logger.logger.error(http_exception, exc_info=True)
1461
1475
  return JSONResponse(
1462
1476
  status_code=http_exception.status_code, content=http_exception.detail
1463
1477
  )
@@ -4,7 +4,10 @@ from cryptography.hazmat.backends import default_backend
4
4
  from cryptography.hazmat.primitives import padding
5
5
  from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
6
6
 
7
+ from square_authentication.configuration import global_object_square_logger
7
8
 
9
+
10
+ @global_object_square_logger.auto_logger
8
11
  def encrypt(key, plaintext):
9
12
  # Ensure the key length is 16, 24, or 32 bytes for AES
10
13
  key = key.ljust(32)[:32].encode('utf-8')
@@ -28,7 +31,7 @@ def encrypt(key, plaintext):
28
31
 
29
32
  return encoded_ciphertext
30
33
 
31
-
34
+ @global_object_square_logger.auto_logger
32
35
  def decrypt(key, encoded_ciphertext):
33
36
  # Ensure the key length is 16, 24, or 32 bytes for AES
34
37
  key = key.ljust(32)[:32].encode('utf-8')
@@ -1,7 +1,10 @@
1
1
  import jwt
2
2
  from jwt.exceptions import ExpiredSignatureError, DecodeError, InvalidTokenError
3
3
 
4
+ from square_authentication.configuration import global_object_square_logger
4
5
 
6
+
7
+ @global_object_square_logger.auto_logger
5
8
  def get_jwt_payload(token, secret_key):
6
9
  try:
7
10
  # Decode the token and verify the signature
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: square-authentication
3
- Version: 5.1.0
3
+ Version: 5.1.1
4
4
  Summary: authentication layer for my personal server.
5
5
  Home-page: https://github.com/thepmsquare/square_authentication
6
6
  Author: thePmSquare
@@ -45,6 +45,11 @@ pip install square_authentication
45
45
 
46
46
  ## changelog
47
47
 
48
+ ### v5.1.1
49
+
50
+ - add logger decorator in all functions.
51
+ - add error logs in all endpoints.
52
+
48
53
  ### v5.1.0
49
54
 
50
55
  - Core
@@ -135,3 +140,4 @@ pip install square_authentication
135
140
 
136
141
  ## Feedback is appreciated. Thank you!
137
142
 
143
+
@@ -6,12 +6,12 @@ square_authentication/data/config.ini,sha256=_740RvKpL5W2bUDGwZ7ePwuP-mAasr5cXXB
6
6
  square_authentication/pydantic_models/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
7
7
  square_authentication/pydantic_models/core.py,sha256=qeNETcJv7mnRKGhATOW2bg0NlHuyzvot1dZ1b1qqhwU,610
8
8
  square_authentication/routes/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
9
- square_authentication/routes/core.py,sha256=ZeHpAwpAl3vdHSa6oisqH8XPHrQIZlJxNVJHfrs5Ob0,54794
9
+ square_authentication/routes/core.py,sha256=CFjlsTF8XXcsImJG_3LVBfncG9RiENYnAQ4Msve-CM4,55858
10
10
  square_authentication/routes/utility.py,sha256=ocLWj39JbKVOxgyTsM0xBUgTpHFmKIvvaT3UnjFvuOY,1783
11
11
  square_authentication/utils/__init__.py,sha256=AbpHGcgLb-kRsJGnwFEktk7uzpZOCcBY74-YBdrKVGs,1
12
- square_authentication/utils/encryption.py,sha256=T6BShoUr_xeGpbfPgTK-GxTlXPwcjwU4c4KW7KPzrF8,1865
13
- square_authentication/utils/token.py,sha256=Y_arg5LegX-aprMj9YweUK8jjNZLGDjLUGgxbUA12w4,560
14
- square_authentication-5.1.0.dist-info/METADATA,sha256=ySQXT0M6iBmuoCxptmYAN805tpbFZB-ERi48QsGCaJY,3746
15
- square_authentication-5.1.0.dist-info/WHEEL,sha256=tZoeGjtWxWRfdplE7E3d45VPlLNQnvbKiYnx7gwAy8A,92
16
- square_authentication-5.1.0.dist-info/top_level.txt,sha256=wDssVJIl9KIEJPj5rR3rv4uRI7yCndMBrvHd_6BGXQA,22
17
- square_authentication-5.1.0.dist-info/RECORD,,
12
+ square_authentication/utils/encryption.py,sha256=amlTNbGvq59eFLX6pq084UDLI3deM-urLjb4fOYIeNw,2023
13
+ square_authentication/utils/token.py,sha256=2psHT6CGqQLmkhsPHVYS8_2jEjN96J9gLAM4lB_fa88,678
14
+ square_authentication-5.1.1.dist-info/METADATA,sha256=9mvUZaxzbuRB-tnyeUTyzTQ4vFrXIejx2ybKTuP57tQ,3836
15
+ square_authentication-5.1.1.dist-info/WHEEL,sha256=tZoeGjtWxWRfdplE7E3d45VPlLNQnvbKiYnx7gwAy8A,92
16
+ square_authentication-5.1.1.dist-info/top_level.txt,sha256=wDssVJIl9KIEJPj5rR3rv4uRI7yCndMBrvHd_6BGXQA,22
17
+ square_authentication-5.1.1.dist-info/RECORD,,