File size: 1,915 Bytes
da4ac1c
 
 
 
 
 
 
 
 
 
 
 
1baffc8
da4ac1c
 
 
 
 
 
 
 
 
 
 
 
 
a860394
da4ac1c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import json  # Import json to parse the string
from dotenv import load_dotenv
from google.oauth2 import service_account
from google.cloud.security.publicca import PublicCertificateAuthorityServiceClient

def gen_google_eab_data():
    load_dotenv()
    data = {
        "type": "service_account",
        "project_id": os.getenv("PROJECT_ID"),
        "private_key_id": os.getenv("PRIVATE_KEY_ID"),
        "private_key": os.getenv("PRIVATE_KEY").replace("\\n", "\n"),  # Ensure proper formatting
        "client_email": os.getenv("CLIENT_EMAIL"),
        "client_id": os.getenv("CLIENT_ID"),
        "auth_uri": os.getenv("AUTH_URI"),
        "token_uri": os.getenv("TOKEN_URI"),
        "auth_provider_x509_cert_url": os.getenv("AUTH_PROVIDER_X509_CERT_URL"),
        "client_x509_cert_url": os.getenv("CLIENT_X509_CERT_URL"),
        "universe_domain": os.getenv("UNIVERSE_DOMAIN")
    }
    return data

def gen_google_eab():
    service_account_info = gen_google_eab_data()
    if service_account_info is None:
        return None, None
    try:
        credentials = service_account.Credentials.from_service_account_info(service_account_info)
        client = PublicCertificateAuthorityServiceClient(credentials=credentials)
        project_id = service_account_info['project_id']
        parent = f"projects/{project_id}"
        
        # Call the method to create an external account key
        response = client.create_external_account_key(parent=parent)
        kid = response.key_id
        hmac = response.b64_mac_key
        return kid, hmac.decode()
    except Exception as e:
        print(f"Error generating Google EAB: {e}")
        return None, None  # Or appropriate error handling

# Example usage
if __name__ == "__main__":
    kid, hmac = gen_google_eab()
    if kid and hmac:
        print(f"KID: {kid}, HMAC: {hmac}")
    else:
        print("Failed to generate KID and HMAC.")