def session(): sess = next(db.session()) yield sess clear_all_table_data(session=sess, metadata=Base.metadata, except_tables=[]) sess.rollback()
def filter(cls, session: Session = None, **kwargs): """ Simply get a Row :param session: :param kwargs: :return: """ cond = [] for key, val in kwargs.items(): key = key.split("__") if len(key) > 2: raise Exception("No 2 more dunders") col = getattr(cls, key[0]) if len(key) == 1: cond.append((col == val)) elif len(key) == 2 and key[1] == 'gt': cond.append((col > val)) elif len(key) == 2 and key[1] == 'gte': cond.append((col >= val)) elif len(key) == 2 and key[1] == 'lt': cond.append((col < val)) elif len(key) == 2 and key[1] == 'lte': cond.append((col <= val)) elif len(key) == 2 and key[1] == 'in': cond.append((col.in_(val))) obj = cls() if session: obj._session = session obj.served = True else: obj._session = next(db.session()) obj.served = False query = obj._session.query(cls) query = query.filter(*cond) obj._q = query return obj
def get(cls, **kwargs): session = next(db.session()) query = session.query(cls) for key, val in kwargs.items(): col = getattr(cls, key) query = query.filter(col == val) if query.count() > 1: raise Exception( "Only one row is supposed to be returned, but got more than one." ) return query.first()
def get(cls, session: Session = None, **kwargs): """ Simply get a Row :param kwargs: :return: """ sess = next(db.session()) if not session else session query = sess.query(cls) for key, val in kwargs.items(): col = getattr(cls, key) query = query.filter(col == val) if query.count() > 1: raise Exception( "Only one row is supposed to be returned, but got more than one." ) result = query.first() if not session: sess.close() return result
async def access_control(request: Request, call_next): request.state.req_time = D.datetime() request.state.start = time.time() request.state.inspect = None request.state.user = None request.state.service = None ip = request.headers[ "x-forwarded-for"] if "x-forwarded-for" in request.headers.keys( ) else request.client.host request.state.ip = ip.split(",")[0] if "," in ip else ip headers = request.headers cookies = request.cookies url = request.url.path if await url_pattern_check(url, EXCEPT_PATH_REGEX) or url in EXCEPT_PATH_LIST: response = await call_next(request) if url != "/": await api_logger(request=request, response=response) return response try: if url.startswith("/api"): # api 인경우 헤더로 토큰 검사 if url.startswith("/api/services"): qs = str(request.query_params) qs_list = qs.split("&") session = next(db.session()) if not config.conf().DEBUG: try: qs_dict = { qs_split.split("=")[0]: qs_split.split("=")[1] for qs_split in qs_list } except Exception: raise ex.APIQueryStringEx() qs_keys = qs_dict.keys() if "key" not in qs_keys or "timestamp" not in qs_keys: raise ex.APIQueryStringEx() if "secret" not in headers.keys(): raise ex.APIHeaderInvalidEx() api_key = ApiKeys.get(session=session, access_key=qs_dict["key"]) if not api_key: raise ex.NotFoundAccessKeyEx(api_key=qs_dict["key"]) mac = hmac.new(bytes(api_key.secret_key, encoding='utf8'), bytes(qs, encoding='utf-8'), digestmod='sha256') d = mac.digest() validating_secret = str( base64.b64encode(d).decode('utf-8')) if headers["secret"] != validating_secret: raise ex.APIHeaderInvalidEx() now_timestamp = int(D.datetime(diff=9).timestamp()) if now_timestamp - 10 > int( qs_dict["timestamp"]) or now_timestamp < int( qs_dict["timestamp"]): raise ex.APITimestampEx() user_info = to_dict(api_key.users) request.state.user = UserToken(**user_info) else: # Request User 가 필요함 if "authorization" in headers.keys(): key = headers.get("Authorization") api_key_obj = ApiKeys.get(session=session, access_key=key) user_info = to_dict( Users.get(session=session, id=api_key_obj.user_id)) request.state.user = UserToken(**user_info) # 토큰 없음 else: if "Authorization" not in headers.keys(): raise ex.NotAuthorized() session.close() response = await call_next(request) return response else: if "authorization" in headers.keys(): token_info = await token_decode( access_token=headers.get("Authorization")) request.state.user = UserToken(**token_info) # 토큰 없음 else: if "Authorization" not in headers.keys(): raise ex.NotAuthorized() else: # 템플릿 렌더링인 경우 쿠키에서 토큰 검사 cookies[ "Authorization"] = "Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6MTQsImVtYWlsIjoia29hbGFAZGluZ3JyLmNvbSIsIm5hbWUiOm51bGwsInBob25lX251bWJlciI6bnVsbCwicHJvZmlsZV9pbWciOm51bGwsInNuc190eXBlIjpudWxsfQ.4vgrFvxgH8odoXMvV70BBqyqXOFa2NDQtzYkGywhV48" if "Authorization" not in cookies.keys(): raise ex.NotAuthorized() token_info = await token_decode( access_token=cookies.get("Authorization")) request.state.user = UserToken(**token_info) response = await call_next(request) await api_logger(request=request, response=response) except Exception as e: error = await exception_handler(e) error_dict = dict(status=error.status_code, msg=error.msg, detail=error.detail, code=error.code) response = JSONResponse(status_code=error.status_code, content=error_dict) await api_logger(request=request, error=error) return response