File size: 5,655 Bytes
9096e54
 
 
 
 
 
 
 
07481e4
9096e54
 
 
 
07481e4
9096e54
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
07481e4
9096e54
 
 
 
 
 
 
 
 
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
"""One-time AMAPI setup script - creates enterprise, policy, and enrollment token."""
import json
import sys
from google.oauth2 import service_account
from googleapiclient.discovery import build

SCOPES = ["https://www.googleapis.com/auth/androidmanagement"]
PROJECT_ID = "blooger-project"
CALLBACK_URL = "https://riadrayhan111-rr-locker-api.hf.space/api/v1/amapi/callback"
SERVICE_ACCOUNT_FILE = "service-account.json"

# EMI Locker config
EMI_LOCKER_PACKAGE = "com.riad.rrlkr"
APK_URL = "https://riadrayhan111-rr-locker-api.hf.space/api/v1/zte/apk"

def get_service():
    creds = service_account.Credentials.from_service_account_file(
        SERVICE_ACCOUNT_FILE, scopes=SCOPES
    )
    return build("androidmanagement", "v1", credentials=creds)

def step1_create_enterprise():
    """Create a project-bound enterprise (no browser/Google account needed)."""
    service = get_service()
    
    print("Step 1: Creating project-bound enterprise...")
    print("(No browser signup needed - enterprise is bound to the GCP project)\n")
    
    enterprise = service.enterprises().create(
        projectId=PROJECT_ID,
        agreementAccepted=True,
        body={
            "enterpriseDisplayName": "RR Locker EMI Finance",
        }
    ).execute()
    
    enterprise_name = enterprise.get("name", "")
    print(f"=== ENTERPRISE CREATED ===")
    print(f"Enterprise name: {enterprise_name}")
    print(f"Full response: {json.dumps(enterprise, indent=2)}")
    return enterprise_name

def step2_create_policy(enterprise_name):
    """Create device management policy."""
    service = get_service()
    
    print(f"\nStep 3: Creating policy for {enterprise_name}...")
    policy = {
        # Allow installing apps from unknown sources (our APK server)
        "advancedSecurityOverrides": {
            "untrustedAppsPolicy": "ALLOW_INSTALL_DEVICE_WIDE",
            "developerSettings": "DEVELOPER_SETTINGS_ALLOWED",
        },
        "factoryResetDisabled": True,
        "safeBootDisabled": True,
        "screenCaptureDisabled": True,
        "addUserDisabled": True,
        "removeUserDisabled": True,
        "modifyAccountsDisabled": False,
        "systemUpdate": {
            "type": "WINDOWED",
            "startMinutes": 120,
            "endMinutes": 300
        },
        "skipFirstUseHintsEnabled": True,
        "adjustVolumeDisabled": False,
        "funDisabled": True,
        "networkEscapeHatchEnabled": True,
        "playStoreMode": "BLACKLIST",
    }
    
    result = service.enterprises().policies().patch(
        name=f"{enterprise_name}/policies/emi-locker-policy",
        body=policy
    ).execute()
    
    print(f"Policy created: {result.get('name', '')}")
    return result

def step3_create_enrollment_token(enterprise_name):
    """Generate enrollment token for QR code."""
    service = get_service()
    
    print(f"\nStep 4: Creating enrollment token...")
    token = service.enterprises().enrollmentTokens().create(
        parent=enterprise_name,
        body={
            "policyName": f"{enterprise_name}/policies/emi-locker-policy",
            "duration": "86400s",
            "allowPersonalUsage": "PERSONAL_USAGE_DISALLOWED",
            "oneTimeOnly": False,
        }
    ).execute()
    
    token_value = token.get("value", "")
    print(f"Token value: {token_value}")
    print(f"Token name: {token.get('name', '')}")
    print(f"Expiry: {token.get('expirationTimestamp', '')}")
    
    # Generate QR payload
    qr_payload = {
        "android.app.extra.PROVISIONING_DEVICE_ADMIN_COMPONENT_NAME": 
            "com.google.android.apps.work.clouddpc/.receivers.CloudDeviceAdminReceiver",
        "android.app.extra.PROVISIONING_DEVICE_ADMIN_SIGNATURE_CHECKSUM": 
            "I5YvS0O5hXY46mb01BlRjq4oJJGs2kuUcHvVkAPEXlg",
        "android.app.extra.PROVISIONING_DEVICE_ADMIN_PACKAGE_DOWNLOAD_LOCATION": 
            "https://play.google.com/managed/downloadManagingApp?identifier=setup",
        "android.app.extra.PROVISIONING_ADMIN_EXTRAS_BUNDLE": {
            "com.google.android.apps.work.clouddpc.EXTRA_ENROLLMENT_TOKEN": token_value
        },
        "android.app.extra.PROVISIONING_SKIP_ENCRYPTION": True,
        "android.app.extra.PROVISIONING_LEAVE_ALL_SYSTEM_APPS_ENABLED": True,
    }
    
    qr_string = json.dumps(qr_payload, separators=(",", ":"))
    print(f"\n=== QR CODE STRING ===")
    print(qr_string)
    print(f"\n=== QR STRING LENGTH: {len(qr_string)} chars ===")
    
    return token_value, qr_string

if __name__ == "__main__":
    import os
    os.chdir(os.path.dirname(os.path.abspath(__file__)))
    
    try:
        # Use existing enterprise if available
        enterprise_name = "enterprises/LC02fkc86a"
        print(f"Using existing enterprise: {enterprise_name}")
        
        # Step 2: Update policy
        step2_create_policy(enterprise_name)
        
        # Step 3: Create enrollment token + QR
        token_value, qr_string = step3_create_enrollment_token(enterprise_name)
        
        print("\n" + "=" * 60)
        print("SETUP COMPLETE!")
        print("=" * 60)
        print(f"\nEnterprise: {enterprise_name}")
        print(f"\nSet these env vars on your host:")
        print(f"  AMAPI_ENTERPRISE_NAME = {enterprise_name}")
        print(f"  AMAPI_PROJECT_ID = {PROJECT_ID}")
        print(f"\nQR string is ready for scanning!")
        
    except Exception as e:
        print(f"\nERROR: {e}")
        import traceback
        traceback.print_exc()
        sys.exit(1)