| ¶Ô±ÈÐÂÎļþ |
| | |
| | | package cn.iocoder.yudao.module.system.framework.agent; |
| | | |
| | | import cn.iocoder.yudao.framework.security.core.service.SecurityFrameworkService; |
| | | import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils; |
| | | import jakarta.annotation.Resource; |
| | | import org.springframework.beans.factory.annotation.Value; |
| | | import org.springframework.http.HttpStatus; |
| | | import org.springframework.stereotype.Component; |
| | | |
| | | import java.nio.charset.StandardCharsets; |
| | | import java.security.MessageDigest; |
| | | import java.util.List; |
| | | |
| | | /** æ¡¥æ¥ç»ä¸é´æï¼æå¡ä»¤ç + å½åç»å½ç¨æ· + ä¸å¡æéï¼ç¼ºä¸ä¸å¯ã */ |
| | | @Component |
| | | public class SystemAgentBridgeGuard { |
| | | |
| | | public static final String PERMISSION_ITEM_IMPORT = "mdm:item:import"; |
| | | |
| | | /** åä¸å¡æ¨¡åç导å
¥æéï¼ä¼è¯å建ä¸é件读ååªéå½ä¸å
¶ä¸ä»»æä¸é¡¹ã */ |
| | | public static final List<String> IMPORT_PERMISSIONS = List.of( |
| | | PERMISSION_ITEM_IMPORT, |
| | | "srm:supplier:create", |
| | | "mes:md-item:create", |
| | | "mes:qc-template:create", |
| | | "mes:pro-mps:create", |
| | | "mes:pro-route:create"); |
| | | |
| | | @Value("${yudao.agent.bridge-service-token:}") |
| | | private String bridgeServiceToken; |
| | | |
| | | @Resource |
| | | private SecurityFrameworkService securityFrameworkService; |
| | | @Resource |
| | | private SystemAgentDelegationService delegationService; |
| | | |
| | | public void verifyServiceToken(String provided) { |
| | | if (bridgeServiceToken == null || bridgeServiceToken.isBlank() || provided == null |
| | | || !MessageDigest.isEqual(bridgeServiceToken.getBytes(StandardCharsets.UTF_8), |
| | | provided.getBytes(StandardCharsets.UTF_8))) { |
| | | throw new AgentBridgeException(HttpStatus.FORBIDDEN, "SERVICE_TOKEN_INVALID", |
| | | "æºè½ä½æå¡èº«ä»½æ æï¼è¯·æ£æ¥æå¡å¯é¥é
ç½®"); |
| | | } |
| | | } |
| | | |
| | | /** åªç¡®è®¤æå¡èº«ä»½ä¸å½åç»å½ç¨æ·ï¼å
·ä½è½åç±è°ç¨æ¹é项æ¶ç´§ã */ |
| | | public AgentPrincipal requireAuthenticated(String providedServiceToken) { |
| | | verifyServiceToken(providedServiceToken); |
| | | Long userId = SecurityFrameworkUtils.getLoginUserId(); |
| | | if (userId == null) { |
| | | throw new AgentBridgeException(HttpStatus.UNAUTHORIZED, "UNAUTHORIZED", "æªç»å½æç»å½å·²è¿æ"); |
| | | } |
| | | return principalOf(userId); |
| | | } |
| | | |
| | | public AgentPrincipal require(String providedServiceToken, String permission) { |
| | | verifyServiceToken(providedServiceToken); |
| | | Long userId = SecurityFrameworkUtils.getLoginUserId(); |
| | | if (userId == null) { |
| | | throw new AgentBridgeException(HttpStatus.UNAUTHORIZED, "UNAUTHORIZED", "æªç»å½æç»å½å·²è¿æ"); |
| | | } |
| | | if (!securityFrameworkService.hasPermission(permission)) { |
| | | throw new AgentBridgeException(HttpStatus.FORBIDDEN, "FORBIDDEN", |
| | | "å½åç¨æ·ç¼ºå°æ°æ®å¯¼å
¥æéï¼" + permission + "ï¼"); |
| | | } |
| | | return principalOf(userId); |
| | | } |
| | | |
| | | public AgentPrincipal require(String providedServiceToken, String delegationToken, String taskId, |
| | | String operation, String permission, String moduleId, Long attachmentId) { |
| | | verifyServiceToken(providedServiceToken); |
| | | if (delegationToken != null && !delegationToken.isBlank()) { |
| | | return delegationService.require(delegationToken, taskId, operation, permission, moduleId, attachmentId); |
| | | } |
| | | return require(providedServiceToken, permission); |
| | | } |
| | | |
| | | /** ä¼è¯ä¸éä»¶å
¥å£æ æ³é¢ç¥æ¨¡åï¼åªè¦æ±å
·å¤ä»»æä¸é¡¹å¯¼å
¥æéã */ |
| | | public AgentPrincipal requireAny(String providedServiceToken) { |
| | | verifyServiceToken(providedServiceToken); |
| | | Long userId = SecurityFrameworkUtils.getLoginUserId(); |
| | | if (userId == null) { |
| | | throw new AgentBridgeException(HttpStatus.UNAUTHORIZED, "UNAUTHORIZED", "æªç»å½æç»å½å·²è¿æ"); |
| | | } |
| | | if (!securityFrameworkService.hasAnyPermissions(IMPORT_PERMISSIONS.toArray(String[]::new))) { |
| | | throw new AgentBridgeException(HttpStatus.FORBIDDEN, "FORBIDDEN", "å½åç¨æ·ç¼ºå°æ°æ®å¯¼å
¥æé"); |
| | | } |
| | | return principalOf(userId); |
| | | } |
| | | |
| | | |
| | | public AgentPrincipal requireAny(String providedServiceToken, String delegationToken, String taskId, |
| | | String operation, String moduleId, Long attachmentId) { |
| | | if (delegationToken == null || delegationToken.isBlank()) { |
| | | return requireAny(providedServiceToken); |
| | | } |
| | | verifyServiceToken(providedServiceToken); |
| | | AgentBridgeException last = null; |
| | | for (String permission : IMPORT_PERMISSIONS) { |
| | | try { |
| | | return delegationService.require(delegationToken, taskId, operation, permission, moduleId, attachmentId); |
| | | } catch (AgentBridgeException ex) { |
| | | last = ex; |
| | | if (!"DELEGATION_SCOPE_MISMATCH".equals(ex.getErrorCode()) |
| | | && !"DELEGATION_PERMISSION_REVOKED".equals(ex.getErrorCode())) { |
| | | throw ex; |
| | | } |
| | | } |
| | | } |
| | | throw last == null ? new AgentBridgeException(HttpStatus.FORBIDDEN, "FORBIDDEN", "å§æç¥¨æ®æéä¸è¶³") : last; |
| | | } |
| | | |
| | | private AgentPrincipal principalOf(Long userId) { |
| | | return new AgentPrincipal(userId, "live"); |
| | | } |
| | | } |