Spaces:
Sleeping
Sleeping
File size: 6,573 Bytes
839aef2 | 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 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 | /**
* Android Management API (AMAPI) service — port of amapi_service.py using the
* Node `googleapis` client. Used for Google Cloud DPC QR provisioning and
* enterprise/device administration.
*/
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
import { google } from 'googleapis';
import { settings } from '../config.js';
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const SCOPES = ['https://www.googleapis.com/auth/androidmanagement'];
const CLOUD_DPC_COMPONENT = 'com.google.android.apps.work.clouddpc/.receivers.CloudDeviceAdminReceiver';
const CLOUD_DPC_SIGNATURE = 'I5YvS0O5hXY46mb01BlRjq4oJJGs2kuUcHvVkAPEXlg';
const CLOUD_DPC_DOWNLOAD = 'https://play.google.com/managed/downloadManagingApp?identifier=setup';
function localServiceAccountPaths() {
return [
path.resolve(__dirname, '..', '..', 'service-account.json'),
path.resolve(__dirname, '..', '..', '..', 'backend', 'service-account.json'),
'service-account.json',
];
}
function loadCredentials() {
const credValue = settings.AMAPI_SERVICE_ACCOUNT_JSON;
for (const p of localServiceAccountPaths()) {
if (fs.existsSync(p)) {
return JSON.parse(fs.readFileSync(p, 'utf8'));
}
}
if (!credValue) {
throw new Error('AMAPI_SERVICE_ACCOUNT_JSON not configured.');
}
if (fs.existsSync(credValue)) {
return JSON.parse(fs.readFileSync(credValue, 'utf8'));
}
// base64-encoded JSON
try {
return JSON.parse(Buffer.from(credValue, 'base64').toString('utf8'));
} catch { /* fall through */ }
// raw JSON
try {
return JSON.parse(credValue);
} catch {
throw new Error('AMAPI_SERVICE_ACCOUNT_JSON is not a valid file path, base64 JSON, or raw JSON.');
}
}
async function getService() {
const credentials = loadCredentials();
const auth = new google.auth.GoogleAuth({ credentials, scopes: SCOPES });
const authClient = await auth.getClient();
return google.androidmanagement({ version: 'v1', auth: authClient });
}
export function isConfigured() {
if (settings.AMAPI_SERVICE_ACCOUNT_JSON && settings.AMAPI_PROJECT_ID) return true;
if (settings.AMAPI_PROJECT_ID) {
return localServiceAccountPaths().some((p) => fs.existsSync(p));
}
return false;
}
export function getEnterpriseName() {
return settings.AMAPI_ENTERPRISE_NAME || null;
}
export async function createEnterprise() {
const service = await getService();
const { data } = await service.enterprises.create({
projectId: settings.AMAPI_PROJECT_ID,
agreementAccepted: true,
requestBody: { enterpriseDisplayName: 'RR Locker EMI Finance' },
});
return { enterprise_name: data.name || '', enterprise: data };
}
export async function createPolicy(enterpriseName, policyName = 'emi-locker-policy') {
const service = await getService();
const policy = {
advancedSecurityOverrides: {
untrustedAppsPolicy: 'ALLOW_INSTALL_DEVICE_WIDE',
googlePlayProtectVerifyApps: 'VERIFY_APPS_USER_CHOICE',
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',
};
const { data } = await service.enterprises.policies.patch({
name: `${enterpriseName}/policies/${policyName}`,
requestBody: policy,
});
return data;
}
export async function createEnrollmentToken(enterpriseName, policyName = 'emi-locker-policy') {
const service = await getService();
const { data } = await service.enterprises.enrollmentTokens.create({
parent: enterpriseName,
requestBody: {
policyName: `${enterpriseName}/policies/${policyName}`,
duration: '86400s',
allowPersonalUsage: 'PERSONAL_USAGE_DISALLOWED',
oneTimeOnly: false,
},
});
return data;
}
export function generateQrPayload(tokenValue) {
return {
'android.app.extra.PROVISIONING_DEVICE_ADMIN_COMPONENT_NAME': CLOUD_DPC_COMPONENT,
'android.app.extra.PROVISIONING_DEVICE_ADMIN_SIGNATURE_CHECKSUM': CLOUD_DPC_SIGNATURE,
'android.app.extra.PROVISIONING_DEVICE_ADMIN_PACKAGE_DOWNLOAD_LOCATION': CLOUD_DPC_DOWNLOAD,
'android.app.extra.PROVISIONING_ADMIN_EXTRAS_BUNDLE': {
'com.google.android.apps.work.clouddpc.EXTRA_ENROLLMENT_TOKEN': tokenValue,
},
'android.app.extra.PROVISIONING_SKIP_ENCRYPTION': true,
'android.app.extra.PROVISIONING_LEAVE_ALL_SYSTEM_APPS_ENABLED': true,
};
}
export function generateQrString(tokenValue) {
return JSON.stringify(generateQrPayload(tokenValue));
}
export async function listDevices(enterpriseName) {
const service = await getService();
const { data } = await service.enterprises.devices.list({ parent: enterpriseName });
return data.devices || [];
}
export async function deleteDevice(deviceName) {
const service = await getService();
try {
await service.enterprises.devices.delete({ name: deviceName });
return true;
} catch {
return false;
}
}
export async function cleanupAllDevices(enterpriseName) {
const devices = await listDevices(enterpriseName);
let deleted = 0;
let failed = 0;
for (const device of devices) {
if (device.name) {
// eslint-disable-next-line no-await-in-loop
if (await deleteDevice(device.name)) deleted += 1; else failed += 1;
}
}
return { total: devices.length, deleted, failed };
}
export async function createNewEnterprise() {
const service = await getService();
const { data } = await service.enterprises.create({
projectId: settings.AMAPI_PROJECT_ID,
agreementAccepted: true,
requestBody: { enterpriseDisplayName: 'RR Locker EMI Finance' },
});
const newName = data.name || '';
settings.AMAPI_ENTERPRISE_NAME = newName;
try {
await createPolicy(newName);
} catch { /* best effort */ }
return { enterprise_name: newName, enterprise: data };
}
export default {
isConfigured,
getEnterpriseName,
createEnterprise,
createPolicy,
createEnrollmentToken,
generateQrPayload,
generateQrString,
listDevices,
deleteDevice,
cleanupAllDevices,
createNewEnterprise,
};
|