2
2
import os
3
3
import logging
4
4
import jwt
5
- import sys
6
5
7
6
import requests
8
7
from flask import make_response
@@ -29,17 +28,22 @@ def _get_token(headers: dict):
29
28
30
29
def process_header (headers , body : dict ) -> (dict , bool ):
31
30
token = _get_token (headers )
32
- if not token and not (body and "user_id" in body ): # Check to not allow to bypass the token
31
+ if not token and not (body and "user_id" in body ):
33
32
return body , False
34
33
newBody = body .copy () if body else {}
35
34
try :
36
- processToken = jwt .decode (token , key = os .getenv ("HASH_SECRET" ), algorithms = [os .getenv ("HASH_ALGORITHM" ), ])
35
+ processToken = jwt .decode (token , key = os .getenv ("HASH_SECRET" ),
36
+ algorithms = [os .getenv ("HASH_ALGORITHM" ), ])
37
37
newBody ["user_id" ] = processToken .get ("id" , "" )
38
38
newBody ["email" ] = processToken .get ("email" )
39
39
except jwt .ExpiredSignatureError :
40
- return {"message" : "expired token" , "status" : http .client .UNAUTHORIZED }, True
40
+ return {"message" :
41
+ "expired token" ,
42
+ "status" : http .client .UNAUTHORIZED }, True
41
43
except jwt .InvalidTokenError :
42
- return {"message" : "invalid token" , "status" : http .client .FORBIDDEN }, True
44
+ return {"message" :
45
+ "invalid token" ,
46
+ "status" : http .client .FORBIDDEN }, True
43
47
return newBody , False
44
48
45
49
@@ -61,26 +65,22 @@ def get(self, url, body, headers, query_params):
61
65
response .status_code )
62
66
63
67
def post (self , url , body , headers , query_params ):
64
- print (f"URL: { url } " )
65
- sys .stdout .flush ()
66
- if not (url .startswith ("login" )):
67
- print (f"NO ES UN CASO DE LOG IN !!!!!!! { url } " )
68
- sys .stdout .flush ()
69
- body , error = process_header (headers , body )
70
- if error :
71
- print (f"ES UN CASO DE ERROR(SEGUIMOS EN LOG IN'T ) !!!!!!! { url } " )
72
- sys .stdout .flush ()
73
- return make_response (body , body .get ("status" ))
68
+ # if not (url.startswith("login")):
69
+ # body, error = process_header(headers, body)
70
+ # if error:
71
+ # return make_response(body, body.get("status"))
74
72
response = requests .post (f"{ self .host } { url } "
75
73
f"{ get_query_params (query_params )} " ,
76
74
json = body ,
77
75
headers = headers )
78
76
logging .info (f"USERS | POST | { url } " )
79
77
logging .debug (f"BODY: { body } " )
80
- print (f"headers: { headers } " )
81
- sys .stdout .flush ()
82
- return make_response (self .getResponseJson (response ),
83
- response .status_code )
78
+ headers = dict (response .headers )
79
+ response = make_response (self .getResponseJson (response ),
80
+ response .status_code )
81
+ if headers .get (TOKEN_FIELD_NAME ):
82
+ response .headers [TOKEN_FIELD_NAME ] = headers .get (TOKEN_FIELD_NAME )
83
+ return response
84
84
85
85
def patch (self , url , body , headers , query_params ):
86
86
response = requests .patch (f"{ self .host } { url } "
0 commit comments