|
@@ -11,6 +11,8 @@ import org.springframework.http.HttpHeaders;
|
|
|
import org.springframework.http.MediaType;
|
|
import org.springframework.http.MediaType;
|
|
|
import org.springframework.http.ResponseEntity;
|
|
import org.springframework.http.ResponseEntity;
|
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
+import org.springframework.util.LinkedMultiValueMap;
|
|
|
|
|
+import org.springframework.util.MultiValueMap;
|
|
|
import org.springframework.web.client.RestTemplate;
|
|
import org.springframework.web.client.RestTemplate;
|
|
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
import javax.annotation.Resource;
|
|
@@ -59,6 +61,13 @@ public class HuaweiAuthService {
|
|
|
@Value("${huawei.health.mock-enabled:true}")
|
|
@Value("${huawei.health.mock-enabled:true}")
|
|
|
private boolean mockEnabled;
|
|
private boolean mockEnabled;
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * H-B3(§7.3):华为 URL 形式 scope 前缀。real 模式下 scope 必须以此前缀开头,
|
|
|
|
|
+ * 简写 scope(step,heart_rate...)华为不识别 → 授权回调失败,故启动期 fail-fast。
|
|
|
|
|
+ * 完整值见 docs/2026-11-18-华为运动健康接入配置清单与联调指引.md §1(5+1 个,空格分隔)。
|
|
|
|
|
+ */
|
|
|
|
|
+ private static final String SCOPE_HUAWEI_URL_PREFIX = "https://www.huawei.com/healthkit/";
|
|
|
|
|
+
|
|
|
// ==================== 授权 URL 生成 ====================
|
|
// ==================== 授权 URL 生成 ====================
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -66,18 +75,45 @@ public class HuaweiAuthService {
|
|
|
*
|
|
*
|
|
|
* @param memberId 家庭成员 ID(作为 state 原样带回,防 CSRF)
|
|
* @param memberId 家庭成员 ID(作为 state 原样带回,防 CSRF)
|
|
|
* @param scope 授权范围;为空时使用默认 scope
|
|
* @param scope 授权范围;为空时使用默认 scope
|
|
|
- * @return 授权 URL(real 模式返回华为账号服务 OAuth2 端点)
|
|
|
|
|
|
|
+ * @return 授权 URL(real 模式返回华为账号服务 OAuth2 端点;mock 模式返回占位 URL,严禁含华为授权域名)
|
|
|
*/
|
|
*/
|
|
|
public String generateAuthUrl(Long memberId, String scope) {
|
|
public String generateAuthUrl(Long memberId, String scope) {
|
|
|
String effectiveScope = (scope == null || scope.isEmpty()) ? this.scope : scope;
|
|
String effectiveScope = (scope == null || scope.isEmpty()) ? this.scope : scope;
|
|
|
if (memberId == null) {
|
|
if (memberId == null) {
|
|
|
return "";
|
|
return "";
|
|
|
}
|
|
}
|
|
|
|
|
+ if (mockEnabled) {
|
|
|
|
|
+ // H3 硬约束:mock 通道返回非华为授权域名占位 URL,前端判据(authUrl 含
|
|
|
|
|
+ // oauth-login.cloud.huawei.com 才走 web-view)落到 mock 分支,全程不触发 web-view。
|
|
|
|
|
+ return "mock://huawei-health/authorize"
|
|
|
|
|
+ + "?response_type=code&client_id=" + encode(clientId)
|
|
|
|
|
+ + "&redirect_uri=" + encode(redirectUri)
|
|
|
|
|
+ + "&scope=" + encode(effectiveScope)
|
|
|
|
|
+ + "&state=" + memberId;
|
|
|
|
|
+ }
|
|
|
|
|
+ validateScope(effectiveScope);
|
|
|
return authorizeUrl
|
|
return authorizeUrl
|
|
|
+ "?response_type=code&client_id=" + encode(clientId)
|
|
+ "?response_type=code&client_id=" + encode(clientId)
|
|
|
+ "&redirect_uri=" + encode(redirectUri)
|
|
+ "&redirect_uri=" + encode(redirectUri)
|
|
|
+ "&scope=" + encode(effectiveScope)
|
|
+ "&scope=" + encode(effectiveScope)
|
|
|
- + "&state=" + memberId;
|
|
|
|
|
|
|
+ + "&state=" + memberId
|
|
|
|
|
+ + "&access_type=offline";
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * H-B3(§7.3):real 模式下 scope 必须为 URL 形式(含华为 healthkit 前缀),否则 fail-fast。
|
|
|
|
|
+ * 简写 scope(step,heart_rate...)华为 OAuth 不识别,会在授权回调阶段失败(难排查),
|
|
|
|
|
+ * 因此在生成授权 URL 时前置校验并给出明确报错。
|
|
|
|
|
+ */
|
|
|
|
|
+ private void validateScope(String scope) {
|
|
|
|
|
+ if (mockEnabled) {
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+ if (scope == null || !scope.contains(SCOPE_HUAWEI_URL_PREFIX)) {
|
|
|
|
|
+ throw new IllegalStateException(
|
|
|
|
|
+ "huawei.health.scope 配置非法(real 模式):必须为 URL 形式、含前缀 " + SCOPE_HUAWEI_URL_PREFIX
|
|
|
|
|
+ + ",空格分隔多值(参考 docs/2026-11-18-华为运动健康接入配置清单与联调指引.md §1)。当前值:" + scope);
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// ==================== code 换 token ====================
|
|
// ==================== code 换 token ====================
|
|
@@ -106,15 +142,17 @@ public class HuaweiAuthService {
|
|
|
log.info("[Mock] 华为 OAuth code 换 token memberId={} code={}", memberId, code);
|
|
log.info("[Mock] 华为 OAuth code 换 token memberId={} code={}", memberId, code);
|
|
|
} else {
|
|
} else {
|
|
|
// real:OAuth2 授权码流程,向 {tokenUrl} POST 换取访问/刷新令牌(Health Service Kit 云侧)。
|
|
// real:OAuth2 授权码流程,向 {tokenUrl} POST 换取访问/刷新令牌(Health Service Kit 云侧)。
|
|
|
|
|
+ // H-B2(§7.2):token 端点必须 application/x-www-form-urlencoded 表单体(华为官方要求),
|
|
|
|
|
+ // 禁用 application/json —— 华为会拒绝(400)。
|
|
|
try {
|
|
try {
|
|
|
HttpHeaders headers = new HttpHeaders();
|
|
HttpHeaders headers = new HttpHeaders();
|
|
|
- headers.setContentType(MediaType.APPLICATION_JSON);
|
|
|
|
|
- Map<String, Object> body = new HashMap<>();
|
|
|
|
|
- body.put("grant_type", "authorization_code");
|
|
|
|
|
- body.put("code", code);
|
|
|
|
|
- body.put("client_id", clientId);
|
|
|
|
|
- body.put("client_secret", clientSecret);
|
|
|
|
|
- body.put("redirect_uri", redirectUri);
|
|
|
|
|
|
|
+ headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
|
|
|
|
|
+ MultiValueMap<String, String> body = new LinkedMultiValueMap<>();
|
|
|
|
|
+ body.add("grant_type", "authorization_code");
|
|
|
|
|
+ body.add("code", code);
|
|
|
|
|
+ body.add("client_id", clientId);
|
|
|
|
|
+ body.add("client_secret", clientSecret);
|
|
|
|
|
+ body.add("redirect_uri", redirectUri);
|
|
|
ResponseEntity<Map> resp = restTemplate.postForEntity(tokenUrl,
|
|
ResponseEntity<Map> resp = restTemplate.postForEntity(tokenUrl,
|
|
|
new HttpEntity<>(body, headers), Map.class);
|
|
new HttpEntity<>(body, headers), Map.class);
|
|
|
if (!resp.getStatusCode().is2xxSuccessful() || resp.getBody() == null) {
|
|
if (!resp.getStatusCode().is2xxSuccessful() || resp.getBody() == null) {
|
|
@@ -197,14 +235,15 @@ public class HuaweiAuthService {
|
|
|
log.info("[Mock] 华为 OAuth 刷新 token memberId={}", memberId);
|
|
log.info("[Mock] 华为 OAuth 刷新 token memberId={}", memberId);
|
|
|
} else {
|
|
} else {
|
|
|
// real:OAuth2 刷新流程,向 {tokenUrl} POST grant_type=refresh_token 换取新访问令牌。
|
|
// real:OAuth2 刷新流程,向 {tokenUrl} POST grant_type=refresh_token 换取新访问令牌。
|
|
|
|
|
+ // H-B2(§7.2):token 端点统一 application/x-www-form-urlencoded 表单体(华为官方要求)。
|
|
|
try {
|
|
try {
|
|
|
HttpHeaders headers = new HttpHeaders();
|
|
HttpHeaders headers = new HttpHeaders();
|
|
|
- headers.setContentType(MediaType.APPLICATION_JSON);
|
|
|
|
|
- Map<String, Object> body = new HashMap<>();
|
|
|
|
|
- body.put("grant_type", "refresh_token");
|
|
|
|
|
- body.put("refresh_token", refreshTokenPlain);
|
|
|
|
|
- body.put("client_id", clientId);
|
|
|
|
|
- body.put("client_secret", clientSecret);
|
|
|
|
|
|
|
+ headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
|
|
|
|
|
+ MultiValueMap<String, String> body = new LinkedMultiValueMap<>();
|
|
|
|
|
+ body.add("grant_type", "refresh_token");
|
|
|
|
|
+ body.add("refresh_token", refreshTokenPlain);
|
|
|
|
|
+ body.add("client_id", clientId);
|
|
|
|
|
+ body.add("client_secret", clientSecret);
|
|
|
ResponseEntity<Map> resp = restTemplate.postForEntity(tokenUrl,
|
|
ResponseEntity<Map> resp = restTemplate.postForEntity(tokenUrl,
|
|
|
new HttpEntity<>(body, headers), Map.class);
|
|
new HttpEntity<>(body, headers), Map.class);
|
|
|
if (!resp.getStatusCode().is2xxSuccessful() || resp.getBody() == null) {
|
|
if (!resp.getStatusCode().is2xxSuccessful() || resp.getBody() == null) {
|