File size: 3,164 Bytes
ebd06cc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
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