|
@@ -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;
|
|
@@ -26,6 +28,14 @@ public class HuaweiAuthService {
|
|
|
|
|
|
|
|
private static final Logger log = LoggerFactory.getLogger(HuaweiAuthService.class);
|
|
private static final Logger log = LoggerFactory.getLogger(HuaweiAuthService.class);
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 华为 Health Service Kit 云侧 URL 形式 scope 前缀(H-B3 fail-fast 锚点)。
|
|
|
|
|
+ *
|
|
|
|
|
+ * <p>华为 OAuth scope 是 URL 形式(如 {@code https://www.huawei.com/healthkit/step.read}),
|
|
|
|
|
+ * 简写 scope(step,heart_rate...)华为不识别 → 授权回调失败。real 模式启动前校验缺失则 fail-fast。</p>
|
|
|
|
|
+ */
|
|
|
|
|
+ public static final String SCOPE_HUAWEI_URL_PREFIX = "https://www.huawei.com/healthkit/";
|
|
|
|
|
+
|
|
|
@Resource
|
|
@Resource
|
|
|
private HuaweiHealthTokenMapper tokenMapper;
|
|
private HuaweiHealthTokenMapper tokenMapper;
|
|
|
|
|
|
|
@@ -53,7 +63,7 @@ public class HuaweiAuthService {
|
|
|
@Value("${huawei.health.revoke-url:https://health-api.cloud.huawei.com/oauth/v1/token/revoke}")
|
|
@Value("${huawei.health.revoke-url:https://health-api.cloud.huawei.com/oauth/v1/token/revoke}")
|
|
|
private String revokeUrl;
|
|
private String revokeUrl;
|
|
|
|
|
|
|
|
- @Value("${huawei.health.scope:step,distance,heart_rate,sleep,body_weight}")
|
|
|
|
|
|
|
+ @Value("${huawei.health.scope:https://www.huawei.com/auth/account/base.profile https://www.huawei.com/healthkit/step.read https://www.huawei.com/healthkit/distance.read https://www.huawei.com/healthkit/heartrate.read https://www.huawei.com/healthkit/sleep.read https://www.huawei.com/healthkit/heightweight.read}")
|
|
|
private String scope;
|
|
private String scope;
|
|
|
|
|
|
|
|
@Value("${huawei.health.mock-enabled:true}")
|
|
@Value("${huawei.health.mock-enabled:true}")
|
|
@@ -66,18 +76,50 @@ 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 端点,带 access_type=offline;
|
|
|
|
|
+ * mock 模式返回非华为授权域名占位 URL,保证前端判为 mock 分支、不触发 web-view)
|
|
|
*/
|
|
*/
|
|
|
public String generateAuthUrl(Long memberId, String scope) {
|
|
public String generateAuthUrl(Long memberId, String scope) {
|
|
|
- String effectiveScope = (scope == null || scope.isEmpty()) ? this.scope : scope;
|
|
|
|
|
if (memberId == null) {
|
|
if (memberId == null) {
|
|
|
return "";
|
|
return "";
|
|
|
}
|
|
}
|
|
|
|
|
+ String effectiveScope = (scope == null || scope.isEmpty()) ? this.scope : scope;
|
|
|
|
|
+ // H3 硬约束:mock 通道返回非 oauth-login.cloud.huawei.com 域名占位 URL(前端判据为授权域名,
|
|
|
|
|
+ // mock 分支不得触发 web-view 白屏)
|
|
|
|
|
+ if (mockEnabled) {
|
|
|
|
|
+ return "https://mock.healthkit.etotem.com/oauth/authorize"
|
|
|
|
|
+ + "?response_type=code&client_id=" + encode(clientId)
|
|
|
|
|
+ + "&redirect_uri=" + encode(redirectUri)
|
|
|
|
|
+ + "&scope=" + encode(effectiveScope)
|
|
|
|
|
+ + "&state=" + memberId
|
|
|
|
|
+ + "&access_type=offline";
|
|
|
|
|
+ }
|
|
|
|
|
+ // H-B3:real 模式 scope 必须为 URL 形式(fail-fast,缺失立即报错)
|
|
|
|
|
+ validateScope(effectiveScope);
|
|
|
|
|
+ // H-B1:追加 access_type=offline(换取 refresh_token 必需,否则 2h 后无法续期)
|
|
|
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";
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * real 模式 scope 前置校验(H-B3 fail-fast)。
|
|
|
|
|
+ *
|
|
|
|
|
+ * <p>华为 OAuth scope 必须是 URL 形式(含 {@link #SCOPE_HUAWEI_URL_PREFIX} 前缀,空格分隔多值),
|
|
|
|
|
+ * 简写 scope(step,heart_rate...)华为不识别 → 授权回调失败。</p>
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param scopeValue 待校验的 scope 字符串
|
|
|
|
|
+ * @throws IllegalStateException scope 不含 URL 前缀时立即抛出(fail-fast 并给出明确报错)
|
|
|
|
|
+ */
|
|
|
|
|
+ public void validateScope(String scopeValue) {
|
|
|
|
|
+ if (!mockEnabled && (scopeValue == null || !scopeValue.contains(SCOPE_HUAWEI_URL_PREFIX))) {
|
|
|
|
|
+ throw new IllegalStateException("华为 real 模式 scope 必须为 URL 形式(含 " + SCOPE_HUAWEI_URL_PREFIX
|
|
|
|
|
+ + " 前缀,空格分隔多值),当前取到:" + scopeValue
|
|
|
|
|
+ + "。请用环境变量 HUAWEI_SCOPE 注入 URL 形式 scope 后重启。");
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// ==================== code 换 token ====================
|
|
// ==================== code 换 token ====================
|
|
@@ -106,15 +148,16 @@ 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:华为 token 端点要求 application/x-www-form-urlencoded 表单体(JSON body 被拒 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 +240,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: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) {
|