async def get_user_notes(self, request, context): try: metadata = dict(context.invocation_metadata()) access_token_information = KeyCloakUtils.introspect_token( metadata['access_token']) user_id = access_token_information['sub'] user_notes = self.service.get_user_notes(user_id) return note_pb2.GetUserNotesResponse( user_notes=[ note_pb2.UserNoteResponse( id=note.id, title=note.title, content=note.content, note_type=note.note_type, created_at=int(note.created_at.timestamp() * 1000)) for note in user_notes ], base_response=note_pb2.BaseResponse()) except Exception as e: logger.error(e) if not e.args or e.args[0] not in Message.msg_dict: errors = [ Message.get_error_object(Message.AUTH_USER_NOT_FOUND) ] else: errors = [Message.get_error_object(e.args[0])] context.set_details( json.dumps(errors, default=lambda x: x.__dict__)) context.set_code(grpc.StatusCode.INTERNAL)
def _token_check(access_token): try: token_info = KeyCloakUtils.introspect_token(access_token) logger.info({"token info": token_info}) if token_info['active']: return True else: return False except Exception as e: return False
def _fd_server_check(self, access_token): try: token_info = KeyCloakUtils.introspect_token(access_token) print("auth_interceptor.py => token info here=", token_info) if token_info['active']: return True else: return False except Exception as e: return False
async def get_joined_groups(self, request, context): try: header_data = dict(context.invocation_metadata()) introspect_token = KeyCloakUtils.introspect_token(header_data['access_token']) client_id = introspect_token['sub'] obj_res = self.service.get_joined_group(client_id) return obj_res except Exception as e: logger.error(e) if not e.args or e.args[0] not in Message.msg_dict: errors = [Message.get_error_object(Message.REGISTER_CLIENT_SIGNAL_KEY_FAILED)] else: errors = [Message.get_error_object(e.args[0])] context.set_details(json.dumps( errors, default=lambda x: x.__dict__)) context.set_code(grpc.StatusCode.INTERNAL)
async def edit_note(self, request, context): try: metadata = dict(context.invocation_metadata()) access_token_information = KeyCloakUtils.introspect_token( metadata['access_token']) user_id = access_token_information['sub'] self.service.edit_note(request.note_id, request.title, request.content, request.note_type, user_id) return note_pb2.BaseResponse() except Exception as e: logger.error(e) if not e.args or e.args[0] not in Message.msg_dict: errors = [ Message.get_error_object(Message.AUTH_USER_NOT_FOUND) ] else: errors = [Message.get_error_object(e.args[0])] context.set_details( json.dumps(errors, default=lambda x: x.__dict__)) context.set_code(grpc.StatusCode.INTERNAL)
async def get_group(self, request, context): try: header_data = dict(context.invocation_metadata()) introspect_token = KeyCloakUtils.introspect_token(header_data['access_token']) client_id = introspect_token['sub'] group_id = request.group_id obj_res = self.service.get_group(group_id, client_id) if obj_res is not None: return obj_res else: raise Exception(Message.GROUP_CHAT_NOT_FOUND) except Exception as e: logger.error(e) if not e.args or e.args[0] not in Message.msg_dict: errors = [Message.get_error_object(Message.GET_GROUP_CHAT_FAILED)] else: errors = [Message.get_error_object(e.args[0])] context.set_details(json.dumps( errors, default=lambda x: x.__dict__)) context.set_code(grpc.StatusCode.INTERNAL)