| | |
| | | |
| | | import cn.iocoder.yudao.framework.security.core.service.SecurityFrameworkService; |
| | | import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils; |
| | | import cn.iocoder.yudao.framework.tenant.core.context.TenantContextHolder; |
| | | import jakarta.annotation.Resource; |
| | | import org.springframework.beans.factory.annotation.Value; |
| | | import org.springframework.http.HttpStatus; |
| | |
| | | |
| | | @Resource |
| | | private SecurityFrameworkService securityFrameworkService; |
| | | @Resource |
| | | private SystemAgentDelegationService delegationService; |
| | | |
| | | public void verifyServiceToken(String provided) { |
| | | if (bridgeServiceToken == null || bridgeServiceToken.isBlank() || provided == null |
| | |
| | | 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) { |
| | |
| | | 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); |
| | |
| | | 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) { |
| | | Long tenantId = TenantContextHolder.getTenantId(); |
| | | return new AgentPrincipal(userId, tenantId == null ? 0L : tenantId, "live"); |
| | | return new AgentPrincipal(userId, "live"); |
| | | } |
| | | } |