maya-persistence / src /baseInfra /firebase_handler.py
anubhav77's picture
v0.1
ebd06cc
from dotenv import load_dotenv
load_dotenv()
from firebase import firebase
import os
secret=os.environ['FIREBASE_TOKEN']
user_id="db" #this file is for dropbox token management through firebase only
auth=firebase.FirebaseAuthentication(secret=secret,email="[email protected]",extra={"id":user_id})
fb_url="https://device-1a455.firebaseio.com"
fb= firebase.FirebaseApplication(fb_url,auth)
base_dir="/users/"+user_id
def fb_get(item):
""" item need to be in format d2_refreshtoken same name will be present in the firebase
d2 means second dropbox instance or app.
"""
return fb.get(base_dir,item)
def fb_update(item,value):
""" item need to be in format d2_refreshtoken same name will be present in the firebase
d2 means second dropbox instance or app.
"""
return fb.patch(base_dir,{item:value})
import re
def convert_to_firebase_key(inStr):
"""Converts a string to a Firebase key.
Args:
inStr: The string to convert.
Returns:
A Firebase key, or None if the inStr is not a valid Firebase key.
"""
# Firebase keys must be between 3 and 128 characters long.
# They must start with a letter, number, or underscore.
# They can only contain letters, numbers, underscores, hyphens, and periods.
# They cannot contain spaces or other special characters.
# Convert the inStr to lowercase.
outStr = inStr.lower()
# Replace all spaces with underscores.
outStr = outStr.replace(' ', '_')
# Remove any leading or trailing underscores.
outStr = outStr.lstrip('_').rstrip('_')
# Remove any character not matching firebase_key_Regex.
outStr=re.sub(r'[^\w\-.]', '', outStr)
firebase_key_regex = re.compile(r'^[a-zA-Z0-9_][a-zA-Z0-9-_.]*$')
# The following string values are not allowed for Firebase keys:
# - "firebase"
# - "google"
# - "android"
# - "ios"
# - "web"
# - "console"
# - "auth"
# - "database"
# - "storage"
# - "hosting"
# - "functions"
# - "firestore"
# - "realtime-database"
# - "remote-config"
# - "analytics"
# - "crashlytics"
# - "performance-monitoring"
# - "test-lab"
# - "cloud-messaging"
# - "dynamic-links"
# - "identity-toolkit"
# - "cloud-functions"
# - "cloud-firestore"
# - "cloud-storage"
# - "cloud-hosting"
# - "cloud-functions-v2"
# - "cloud-firestore-v2"
# - "cloud-storage-v2"
# - "cloud-hosting-v2"
# - "cloud-functions-v3"
# - "cloud-firestore-v3"
# - "cloud-storage-v3"
# - "cloud-hosting-v3"
if not firebase_key_regex.match(inStr) or inStr in ['firebase', 'google', 'android', 'ios', 'web', 'console', 'auth', 'database', 'storage', 'hosting', 'functions', 'firestore', 'realtime-database', 'remote-config', 'analytics', 'crashlytics', 'performance-monitoring', 'test-lab', 'cloud-messaging', 'dynamic-links', 'identity-toolkit', 'cloud-functions', 'cloud-firestore', 'cloud-storage', 'cloud-hosting', 'cloud-functions-v2', 'cloud-firestore-v2', 'cloud-storage-v2', 'cloud-hosting-v2', 'cloud-functions-v3', 'cloud-firestore-v3', 'cloud-storage-v3', 'cloud-hosting-v3']:
return "fb_"+outStr
if len(outStr) > 120:
outStr = "fbx_"+outStr[:120]
return outStr