- restore generic signatures, scoped variables and missing code from original bytecode - remove decompiler artifacts and document classes, methods, fields and packages - restore resources, add Batik/BouncyCastle dependencies and build/runtime docs
198 lines
8.5 KiB
Java
198 lines
8.5 KiB
Java
package com.midm.dpc;
|
||
|
||
import com.google.common.base.Joiner;
|
||
import com.google.common.base.Strings;
|
||
import com.midm.model.message.MidmResponse;
|
||
import com.midm.model.org.Site;
|
||
import com.midm.model.org.SiteInfo;
|
||
import com.midm.model.org.User;
|
||
import com.midm.model.store.StoreAppFile;
|
||
import com.midm.service.org.SiteService;
|
||
import com.midm.service.org.UserService;
|
||
import com.midm.service.store.StoreAppService;
|
||
import com.mitac.cell.barcode.QrCode;
|
||
import com.mitac.cell.io.McResource;
|
||
import java.io.File;
|
||
import java.net.MalformedURLException;
|
||
import java.net.URL;
|
||
import java.util.UUID;
|
||
import javax.servlet.http.HttpServletRequest;
|
||
import javax.servlet.http.HttpServletResponse;
|
||
import org.springframework.beans.factory.annotation.Autowired;
|
||
import org.springframework.stereotype.Controller;
|
||
import org.springframework.web.bind.annotation.RequestMapping;
|
||
import org.springframework.web.bind.annotation.RequestMethod;
|
||
import org.springframework.web.bind.annotation.RequestParam;
|
||
import org.springframework.web.bind.annotation.ResponseBody;
|
||
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
|
||
import org.springframework.web.util.UriComponentsBuilder;
|
||
|
||
/**
|
||
* ======================================================================================
|
||
* Android Enterprise 设备策略控制器配置与零接触二维码生成器 (DpcController)
|
||
* ======================================================================================
|
||
*
|
||
* <p>【Android Enterprise / 谷歌企业移动管理 (EMM) 规范实现】
|
||
* 1. 业务场景(零接触配置与开箱即用 - Zero-Touch Enrollment):
|
||
* - 当企业采购批量的全新 Android 手持终端/平板开机时,在开机欢迎界面连续点击 6 次,
|
||
* 系统将自动唤起系统级摄像头扫描器;
|
||
* - 扫描由本控制器 {@link #qrCode} 生成的特制二维码,即可实现无人工干预的静默绑定。
|
||
* 2. 【DPC 配置规范 (Provisioning Bundle)】:
|
||
* - 动态组装包含应用下载地址、SHA-256 校验哈希(Checksum)、
|
||
* DeviceAdminReceiver 组件名称及企业 Wi-Fi 预置参数的 JSON 捆绑包;
|
||
* - 设备扫描后,Android 操作系统会自动静默下载 DPC 客户端并将其提权激活为设备所有者(Device Owner),
|
||
* 获得最高系统级管控权限(如锁屏禁用、静默安装、Kiosk单应用专机模式)。
|
||
*
|
||
* @author MiDM Core Platform Team
|
||
*/
|
||
@Controller
|
||
public class DpcController {
|
||
/**
|
||
* store应用服务;Spring 自动注入的依赖。
|
||
*
|
||
* 注解:{@code @Autowired}。
|
||
*/
|
||
@Autowired
|
||
private StoreAppService storeAppService;
|
||
/**
|
||
* 用户服务;Spring 自动注入的依赖。
|
||
*
|
||
* 注解:{@code @Autowired}。
|
||
*/
|
||
@Autowired
|
||
private UserService userService;
|
||
/**
|
||
* 站点服务;Spring 自动注入的依赖。
|
||
*
|
||
* 注解:{@code @Autowired}。
|
||
*/
|
||
@Autowired
|
||
private SiteService siteService;
|
||
|
||
/**
|
||
* 生成供 Android 设备开箱扫描的 DPC 零接触配置二维码图片流
|
||
*
|
||
* @param request HTTP 请求
|
||
* @param response HTTP 响应(直接输出 PNG 图像二进制流)
|
||
* @param size 图像尺寸大小
|
||
* @param margin 图像白边边距
|
||
* @param dpc DPC 参数载荷模型
|
||
*/
|
||
@RequestMapping(
|
||
value = {"/dpc/qr"},
|
||
method = {RequestMethod.GET}
|
||
)
|
||
public void qrCode(HttpServletRequest request, HttpServletResponse response, @RequestParam(required = false) Integer size, @RequestParam(required = false) Integer margin, DpcModel dpc) {
|
||
this.toDpcConfig(request, dpc);
|
||
McResource.copyToResponse(response, QrCode.toImage(dpc.toDpcConfig(), size, margin));
|
||
}
|
||
|
||
/**
|
||
* 获取 JSON 格式的 DPC 原生配置文本(供批量打单或定制初始化应用提取)
|
||
*/
|
||
@RequestMapping(
|
||
value = {"/dpc/config"},
|
||
method = {RequestMethod.GET}
|
||
)
|
||
@ResponseBody
|
||
public String intent(HttpServletRequest request, HttpServletResponse response, @RequestParam(required = false) Integer size, @RequestParam(required = false) Integer margin, DpcModel dpc) {
|
||
return this.toDpcConfig(request, dpc);
|
||
}
|
||
|
||
/**
|
||
* 处理转换为Dpc配置。
|
||
*
|
||
* 实现要点:包含异常捕获与降级/回滚分支;包含 12 个条件分支、1 个循环;主要调用:new File、new URL。
|
||
*
|
||
* @param request HTTP 请求({@code HttpServletRequest})
|
||
* @param dpc 参数 {@code dpc}({@code DpcModel})
|
||
* @return 字符串结果
|
||
*/
|
||
private String toDpcConfig(HttpServletRequest request, DpcModel dpc) {
|
||
String adminPackage = dpc.getDeviceAdmin();
|
||
if (!Strings.isNullOrEmpty(adminPackage) && adminPackage.indexOf(47) < 0) {
|
||
if (adminPackage.equals("com.mitac.midm.dpc")) {
|
||
dpc.setDeviceAdmin(Joiner.on("").join(adminPackage, "/", new Object[]{adminPackage, ".MidmDeviceAdminReceiver"}));
|
||
StoreAppFile appFile = this.storeAppService.findLatestAppFile(adminPackage, -1, -1, (String)null);
|
||
if (appFile != null) {
|
||
String filePath = this.storeAppService.getStoreAppPath(adminPackage, appFile.getVersionCode(), appFile.getFileUuid());
|
||
dpc.setPackageChecksum(DpcUtils.getChecksum(new File(filePath)));
|
||
}
|
||
|
||
String downloadUrl = dpc.getDownloadUrl();
|
||
if (Strings.isNullOrEmpty(downloadUrl)) {
|
||
UriComponentsBuilder builder = ServletUriComponentsBuilder.fromRequestUri(request).replacePath(Joiner.on("").join(request.getContextPath(), "/store/apps/download/", new Object[]{adminPackage}));
|
||
if (builder.build().getHost().endsWith("mic.com.tw")) {
|
||
builder.scheme("https");
|
||
}
|
||
|
||
downloadUrl = builder.build().toUriString();
|
||
dpc.setDownloadUrl(downloadUrl);
|
||
}
|
||
} else if (adminPackage.equals("com.mitac.must.service")) {
|
||
dpc.setDeviceAdmin(Joiner.on("").join(adminPackage, "/", new Object[]{adminPackage, ".MustDeviceAdminReceiver"}));
|
||
} else if (adminPackage.equals("com.mitac.midm.system")) {
|
||
dpc.setDeviceAdmin(Joiner.on("").join(adminPackage, "/", new Object[]{adminPackage, ".MidmDeviceAdminReceiver"}));
|
||
}
|
||
}
|
||
|
||
if (!Strings.isNullOrEmpty(dpc.getDownloadUrl()) && Strings.isNullOrEmpty(dpc.getPackageChecksum())) {
|
||
try {
|
||
URL url = new URL(dpc.getDownloadUrl());
|
||
dpc.setPackageChecksum(DpcUtils.getChecksum(url));
|
||
} catch (MalformedURLException var13) {
|
||
}
|
||
}
|
||
|
||
DpcModel.MidmConfig midmConfig = dpc.getMidm();
|
||
if (midmConfig != null) {
|
||
String email = midmConfig.getEmail();
|
||
String password = midmConfig.getPassword();
|
||
String siteModel = midmConfig.getSiteId();
|
||
if (!Strings.isNullOrEmpty(siteModel) && !Strings.isNullOrEmpty(email) && !Strings.isNullOrEmpty(password)) {
|
||
User user = this.userService.findUserByEmail(email);
|
||
if (user != null && this.userService.matchPassword(user, password)) {
|
||
for(SiteInfo siteInfo : user.getSites()) {
|
||
UUID siteUuid = siteInfo.getSiteUuid();
|
||
Site site = this.siteService.findByUuid(siteUuid);
|
||
if (site != null && siteModel.equals(site.getModel())) {
|
||
midmConfig.setSiteId(siteUuid.toString());
|
||
midmConfig.setEmail((String)null);
|
||
midmConfig.setPassword((String)null);
|
||
break;
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
return dpc.toDpcConfig();
|
||
}
|
||
|
||
/**
|
||
* 获取Checksum。
|
||
*
|
||
* 实现要点:包含异常捕获与降级/回滚分支;主要调用:new URL。
|
||
*
|
||
* 框架注解:{@code @RequestMapping(value = {"/dpc/checksum"}, method = {RequestMethod.GET})}、{@code @ResponseBody}。
|
||
*
|
||
* @param request HTTP 请求({@code HttpServletRequest})
|
||
* @param response HTTP 响应({@code HttpServletResponse})
|
||
* @param url 参数 {@code url}({@code String})
|
||
* @return 字符串结果
|
||
*/
|
||
@RequestMapping(
|
||
value = {"/dpc/checksum"},
|
||
method = {RequestMethod.GET}
|
||
)
|
||
@ResponseBody
|
||
public String getChecksum(HttpServletRequest request, HttpServletResponse response, @RequestParam String url) {
|
||
try {
|
||
String checksum = DpcUtils.getChecksum(new URL(url));
|
||
return MidmResponse.getSucceedResponse((Object)checksum);
|
||
} catch (MalformedURLException var5) {
|
||
return MidmResponse.getFailedResponse();
|
||
}
|
||
}
|
||
}
|