def generate_tokens(self, atdb_connection: IConnection, rtdb_connection: IConnection, at_expires_in: float, rt_expires_in: float, client_id: str ): # Create new tokens access_token = os.urandom(20).hex() refresh_token = os.urandom(20).hex() # Set expiration time access_token_expires_in = time.time() + at_expires_in refresh_token_expires_in = time.time() + rt_expires_in # Update session storage with created tokens atdb_connection.insert({ 'token': str(access_token), 'refresh_token': refresh_token, 'expires_in': access_token_expires_in, 'client_id': client_id }) rtdb_connection.insert({ 'token': str(refresh_token), 'expires_in': refresh_token_expires_in, 'client_id': client_id }) # Remove tokens from session storage after expiration remove_access_token.apply_async(args=(str(access_token), client_id), countdown=at_expires_in) remove_refresh_token.apply_async(args=(str(refresh_token), client_id), countdown=rt_expires_in) return access_token, refresh_token
def generate_code(self, db_connection: IConnection, client_id: str, expires_in: float): # Generate code and remember when it should be expired code = os.urandom(20).hex() expires_in = time.time() + expires_in # Create a record of generated code in database db_connection.insert({ 'code': str(code), 'expires_in': expires_in, 'client_id': client_id }) # Set expiration time for this code remove_code.apply_async(args=(str(code), client_id), countdown=expires_in) return code
def get_token_owner(self, db_connection: IConnection, token: str): client_id = None token = db_connection.find({'token': token}) if len(token) > 0: client_id = token[0].get('client_id', None) return client_id
def delete_user(self, db_connection: IConnection, username: str, client_id: str): _num = db_connection.delete({ 'username': username, 'client_id': client_id }) return _num
def find_user(self, db_connection: IConnection, username: str, client_id: str): user = db_connection.find({ 'username': username, 'client_id': client_id }) return user[0] if len(user) > 0 else None
def create_user(self, db_connection: IConnection, username: str, data: BytesIO, client_id: str): img = Binary(data.read()) _id = db_connection.insert({ 'username': username, 'image': img, 'client_id': client_id }) return _id
def update_username(self, db_connection: IConnection, username: str, new_username: str, client_id: str): _id = db_connection.update( { 'username': username, 'client_id': client_id }, {'$set': { 'username': new_username }}) return _id
def update_user_image(self, db_connection: IConnection, username: str, data: BytesIO, client_id: str): img = Binary(data.read()) _id = db_connection.update( { 'username': username, 'client_id': client_id }, {'$set': { 'image': img }}) return _id
def get_client(self, db_connection: IConnection, client_id): client = db_connection.find({'client_id': str(client_id)}) return client[0] if len(client) > 0 else None