This repository was archived by the owner on Jul 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcredential_loader.py
More file actions
113 lines (102 loc) · 4.23 KB
/
credential_loader.py
File metadata and controls
113 lines (102 loc) · 4.23 KB
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import os
from firebase_admin import credentials
class Credentials:
"""
Class to manage credentials for Firebase and OpenAI.
Attributes:
firebase_credentials (service_account.Credentials): Firestore credentials.
firebase_config (dict): Firebase configuration.
openai_credentials (str): OpenAI API key.
db_url (str): URL of the Firebase database.
Methods:
get_firestore_credentials: Retrieves the Firestore credentials from the secrets file.
get_openai_credentials: Retrieves the OpenAI API key from the secrets file.
get_firebase_config: Retrieves the Firebase configuration from the secrets file.
"""
def __init__(self) -> None:
try:
self.firebase_cert = self.make_firebase_cert()
except Exception as e:
print(
"""
There was an error initializing admin credentials.
It's only for development purposes. You can ignore this error.
Continuing with the program...
"""
)
try:
self.firebase_config = self.get_firebase_config()
except Exception as e:
print(
"""
There was an error initializing Firebase configuration.
You must provide the Firebase configuration in the environment variables.
Exiting...
"""
)
exit(1)
try:
self.openai_credentials = self.get_openai_credentials()
except Exception as e:
print(
"""
There was an error initializing OpenAI credentials.
It's only for development purposes. You can ignore this error.
Continuing with the program...
"""
)
try:
self.db_url = os.environ["FIREBASE_CONFIG_DATABASEURL"]
except Exception as e:
print(
"""
There was an error initializing the Firebase database URL.
You must provide the Firebase configuration in the environment variables.
Exiting...
"""
)
exit(1)
def make_firebase_cert(self) -> credentials.Certificate:
"""
Retrieves the Firestore credentials from the environment variables.
Returns:
credentials (service_account.Credentials): Firestore credentials.
"""
credentials_dict = {
"type": os.environ["FIREBASE_AUTH_TYPE"],
"project_id": os.environ["FIREBASE_AUTH_PROJECT_ID"],
"private_key_id": os.environ["FIREBASE_AUTH_PRIVATE_KEY_ID"],
"private_key": os.environ["FIREBASE_AUTH_PRIVATE_KEY"],
"client_email": os.environ["FIREBASE_AUTH_CLIENT_EMAIL"],
"client_id": os.environ["FIREBASE_AUTH_CLIENT_ID"],
"auth_uri": os.environ["FIREBASE_AUTH_AUTH_URI"],
"token_uri": os.environ["FIREBASE_AUTH_TOKEN_URI"],
"auth_provider_x509_cert_url": os.environ[
"FIREBASE_AUTH_AUTH_PROVIDER_X509_CERT_URL"
],
"client_x509_cert_url": os.environ["FIREBASE_AUTH_CLIENT_X509_CERT_URL"],
}
return credentials.Certificate(credentials_dict)
def get_openai_credentials(self) -> str:
"""
Retrieves the OpenAI API key from the environment variables.
Returns:
openai_api_key (str): OpenAI API key.
"""
return os.environ["OPENAI_OPENAI_API_KEY"]
def get_firebase_config(self) -> dict:
"""
Retrieves the Firebase configuration from the environment variables.
Returns:
firebase_config (dict): Firebase configuration.
"""
return {
"apiKey": os.environ["FIREBASE_CONFIG_APIKEY"],
"authDomain": os.environ["FIREBASE_CONFIG_AUTHDOMAIN"],
"projectId": os.environ["FIREBASE_CONFIG_PROJECTID"],
"storageBucket": os.environ["FIREBASE_CONFIG_STORAGEBUCKET"],
"messagingSenderId": os.environ["FIREBASE_CONFIG_MESSAGINGSENDERID"],
"appId": os.environ["FIREBASE_CONFIG_APPID"],
"measurementId": os.environ["FIREBASE_CONFIG_MEASUREMENTID"],
"databaseURL": os.environ["FIREBASE_CONFIG_DATABASEURL"],
}