Spaces:
Running
Running
Update getGoogleEAB.py
Browse files- getGoogleEAB.py +50 -50
getGoogleEAB.py
CHANGED
@@ -1,50 +1,50 @@
|
|
1 |
-
import os
|
2 |
-
import json # Import json to parse the string
|
3 |
-
from dotenv import load_dotenv
|
4 |
-
from google.oauth2 import service_account
|
5 |
-
from google.cloud.security.publicca import PublicCertificateAuthorityServiceClient
|
6 |
-
|
7 |
-
def gen_google_eab_data():
|
8 |
-
load_dotenv()
|
9 |
-
data = {
|
10 |
-
"type": "service_account",
|
11 |
-
"project_id": os.getenv("PROJECT_ID"),
|
12 |
-
"private_key_id": os.getenv("PRIVATE_KEY_ID"),
|
13 |
-
"private_key": os.getenv("PRIVATE_KEY"),
|
14 |
-
"client_email": os.getenv("CLIENT_EMAIL"),
|
15 |
-
"client_id": os.getenv("CLIENT_ID"),
|
16 |
-
"auth_uri": os.getenv("AUTH_URI"),
|
17 |
-
"token_uri": os.getenv("TOKEN_URI"),
|
18 |
-
"auth_provider_x509_cert_url": os.getenv("AUTH_PROVIDER_X509_CERT_URL"),
|
19 |
-
"client_x509_cert_url": os.getenv("CLIENT_X509_CERT_URL"),
|
20 |
-
"universe_domain": os.getenv("UNIVERSE_DOMAIN")
|
21 |
-
}
|
22 |
-
return data
|
23 |
-
|
24 |
-
def gen_google_eab():
|
25 |
-
service_account_info = gen_google_eab_data()
|
26 |
-
if service_account_info is None:
|
27 |
-
return None, None # Handle the case where credentials could not be loaded
|
28 |
-
|
29 |
-
try:
|
30 |
-
credentials = service_account.Credentials.from_service_account_info(service_account_info)
|
31 |
-
client = PublicCertificateAuthorityServiceClient(credentials=credentials)
|
32 |
-
project_id = service_account_info['project_id']
|
33 |
-
parent = f"projects/{project_id}"
|
34 |
-
|
35 |
-
# Call the method to create an external account key
|
36 |
-
response = client.create_external_account_key(parent=parent)
|
37 |
-
kid = response.key_id
|
38 |
-
hmac = response.b64_mac_key
|
39 |
-
return kid, hmac.decode()
|
40 |
-
except Exception as e:
|
41 |
-
print(f"Error generating Google EAB: {e}")
|
42 |
-
return None, None # Or appropriate error handling
|
43 |
-
|
44 |
-
# Example usage
|
45 |
-
if __name__ == "__main__":
|
46 |
-
kid, hmac = gen_google_eab()
|
47 |
-
if kid and hmac:
|
48 |
-
print(f"KID: {kid}, HMAC: {hmac}")
|
49 |
-
else:
|
50 |
-
print("Failed to generate KID and HMAC.")
|
|
|
1 |
+
import os
|
2 |
+
import json # Import json to parse the string
|
3 |
+
from dotenv import load_dotenv
|
4 |
+
from google.oauth2 import service_account
|
5 |
+
from google.cloud.security.publicca import PublicCertificateAuthorityServiceClient
|
6 |
+
|
7 |
+
def gen_google_eab_data():
|
8 |
+
load_dotenv()
|
9 |
+
data = {
|
10 |
+
"type": "service_account",
|
11 |
+
"project_id": os.getenv("PROJECT_ID"),
|
12 |
+
"private_key_id": os.getenv("PRIVATE_KEY_ID"),
|
13 |
+
"private_key": os.getenv("PRIVATE_KEY"),
|
14 |
+
"client_email": os.getenv("CLIENT_EMAIL"),
|
15 |
+
"client_id": os.getenv("CLIENT_ID"),
|
16 |
+
"auth_uri": os.getenv("AUTH_URI"),
|
17 |
+
"token_uri": os.getenv("TOKEN_URI"),
|
18 |
+
"auth_provider_x509_cert_url": os.getenv("AUTH_PROVIDER_X509_CERT_URL"),
|
19 |
+
"client_x509_cert_url": os.getenv("CLIENT_X509_CERT_URL"),
|
20 |
+
"universe_domain": os.getenv("UNIVERSE_DOMAIN")
|
21 |
+
}
|
22 |
+
return data
|
23 |
+
|
24 |
+
def gen_google_eab():
|
25 |
+
service_account_info = gen_google_eab_data()
|
26 |
+
if service_account_info is None:
|
27 |
+
return None, None # Handle the case where credentials could not be loaded
|
28 |
+
|
29 |
+
try:
|
30 |
+
credentials = service_account.Credentials.from_service_account_info(service_account_info)
|
31 |
+
client = PublicCertificateAuthorityServiceClient(credentials=credentials)
|
32 |
+
project_id = service_account_info['project_id']
|
33 |
+
parent = f"projects/{project_id}"
|
34 |
+
|
35 |
+
# Call the method to create an external account key
|
36 |
+
response = client.create_external_account_key(parent=parent)
|
37 |
+
kid = response.key_id
|
38 |
+
hmac = response.b64_mac_key
|
39 |
+
return kid, hmac.decode()
|
40 |
+
except Exception as e:
|
41 |
+
print(f"Error generating Google EAB: {e}")
|
42 |
+
return None, None # Or appropriate error handling
|
43 |
+
|
44 |
+
# Example usage
|
45 |
+
if __name__ == "__main__":
|
46 |
+
kid, hmac = gen_google_eab()
|
47 |
+
if kid and hmac:
|
48 |
+
print(f"KID: {kid}, HMAC: {hmac}")
|
49 |
+
else:
|
50 |
+
print("Failed to generate KID and HMAC.")
|