class SessionKeysBus(): sessionkeys = SessionKeysData() cBus = ErrorBus() def getSessionKeyByUser(self, usuario, password, lang): data = self.sessionkeys.getSessionKeyByUser(usuario, password, lang) test = CipherData() data[0]["uuid"] = test.encrypt(data[0]["uuid"]).decode() return data def getSessionkeysByUUID(self, uuid): return self.sessionkeys.getSessionkeysByUUID(uuid)
resource_fields = { 'id_perfil': fields.Integer, 'perfil': fields.String, 'vigencia_desde': fields.DateTime(dt_format='iso8601'), 'vigencia_hasta': fields.DateTime(dt_format='iso8601'), 'creado_por': fields.String, 'modificado_por': fields.String, } from app.bus import ErrorBus from app.common import PerfilItem from app.bus import PerfilBus perfil = PerfilBus() item = PerfilItem() error = ErrorBus() class PerfilList(Resource, CustomException): def get(self): try: data = perfil.getAll() return support_jsonp_custom(data, resource_fields) except Exception as err: return self.showCustomException(err, request.args) def post(self): try: item.perfil = request.form['perfil'] item.vigencia_desde = request.form['vigencia_desde']
class ApiMiddleWare(object): authorized = 0 body = "" message = "" isApiLogin = False error = ErrorBus() def __init__(self, app): self.app = app def setError(self, value): self.message = self.error.getErrorMessage('', value)[0]["ErrorMessage"] self.body = '{"message": "' + self.message + '","error_code": "' + value + '","status": "ERROR"}' self.authorized = 0 print("++++ self.message", self.message) def setCustomError(self, value): print('* 14 *') self.message = value self.body = '{"message": "' + self.message + '","error_code": "NONE","status": "ERROR"}' self.authorized = 0 def checkSession(self, parameters, environ): print('* 0 *') if len(parameters) == 0: print('* 1 *') self.setError('A0002') else: print('* 2 *') if parameters.get('uuid') == None or parameters.get('uuid') == "": print('* 3 *') self.setError('A0002') else: try: print('* 4 *', parameters.get('uuid')[0]) luuid = parameters.get('uuid')[0] cipher_data = CipherData() uuid = cipher_data.decrypt(luuid.encode()) skey = SessionKeysBus() res = skey.getSessionkeysByUUID(uuid) print("#00001") if len(res) == 0: self.setError('A0004') return print("#00002", res[0]["id_usuario"]) if len(res) == 1: if (datetime.datetime.now() > res[0]["expiration"]): self.setError('A0003') return modulo = environ['PATH_INFO'].upper().replace( "/API/", "").split("/") print("#00003", modulo[0]) ''' #obtener usuario validar perfil-usuario para el modulo llamado ''' print("*** Obtener Datos del usuario ***", modulo[0], res[0]["id_usuario"], environ['REQUEST_METHOD']) pm = PerfilModuloBus() vModuloUsuario = pm.getByModuloUsuario( modulo[0], res[0]["id_usuario"]) print("Modulo usuario: ", vModuloUsuario) if len(vModuloUsuario) == 0: self.setError('A0005') return else: if vModuloUsuario[0]["enabled"] == 0: self.setError('A0005') return print("#00004") except Exception as err: print(type(err), err) if str(type(err)) == "<class 'TypeError'>": self.setCustomError(str(err)) else: try: self.setCustomError(err.msg) except Exception as err: self.setCustomError(str(err)) #print ("** Token Expirado", datetime.datetime.now(),res[0]["expiration"]) #print ("** getSessionkeysByUUID ** LEN",res[0]["id_usuario"], res[0]["expiration"]) def __call__(self, environ, start_response): parameters = parse_qs(environ.get('QUERY_STRING', '')) self.authorized = 1 if ("API" not in environ['PATH_INFO'].upper().split("/") or environ['PATH_INFO'].upper() == '/API/LOGIN'): self.authorized = 1 self.body = '' if environ['PATH_INFO'].upper() == '/API/LOGIN': self.isApiLogin = True else: self.checkSession(parameters, environ) if self.authorized == 1: if self.isApiLogin == True: response_headers = [('Access-Control-Allow-Origin', '*'), ('Access-Control-Allow-Methods', 'POST')] return self.app(environ, start_response) #response_body = response_body.encode('utf-8') #response_headers = [('Content-Type', 'application/json'),('Content-Length', str(len(response_body))),('Access-Control-Allow-Origin' , '*'),('Access-Control-Allow-Methods','POST')] #start_response('200 OK', response_headers) #return [response_body] else: ee = support_jsonp_error(self.body, parameters) print('* 9 *', ee) response_body = ee response_body = response_body.encode('utf-8') response_headers = [('Content-Type', 'application/json'), ('Content-Length', str(len(response_body))), ('Access-Control-Allow-Origin', '*'), ('Access-Control-Allow-Methods', 'PUT'), ('Access-Control-Allow-Methods', 'DELETE')] start_response('200 OK', response_headers) return [response_body]