package com.ruoyi.system.rental; import static com.ruoyi.system.rental.RentalValues.*; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.time.Instant; import java.util.*; /** 外部APP和本地NODE共用凭证认证,登录用户归属仍由sys_user及RBAC决定。 */ @Service public class RentalIdentity { private final RentalStore store; private final RentalCrypto crypto; @Value("${rental.master-key-version:v1}") private String masterVersion; public RentalIdentity(RentalStore store, RentalCrypto crypto) { this.store = store; this.crypto = crypto; } public Map credential(String key) { Map row = store.one("SELECT * FROM rent_credential WHERE api_key=?", key); require( row != null && "ACTIVE".equals(text(row, "status")) && (row.get("expires_at") == null || time(row.get("expires_at")).isAfter(Instant.now())), "租赁凭证无效"); return row; } public String secret(Map credential) { return crypto.decrypt(text(credential, "api_secret")); } @Transactional(rollbackFor = Exception.class) public Map authenticate( String key, String ownerType, String scope, long timestamp, String nonce, String method, String path, String body, String signature) { require( key != null && key.length() <= 64 && nonce != null && nonce.matches("[A-Za-z0-9_-]{16,64}") && timestamp > System.currentTimeMillis() - 60000 && timestamp < System.currentTimeMillis() + 60000, "认证时间或随机数无效"); Map row = credential(key); require( ownerType.equals(text(row, "owner_type")) && RentalCrypto.equal( RentalCrypto.sign( secret(row), scope, key, timestamp, nonce, method, path, body), signature), "租赁签名无效"); store.insert( "nonce", map("api_key", key, "scope", scope, "nonce", nonce, "expires_at", after(180))); return row; } /** 轮换保留历史Secret,仅明确在途任务可以使用VERIFY_ONLY,不能签发新任务。 */ @Transactional(rollbackFor = Exception.class) public Map issue(String type, long owner, long actor) { require("APP".equals(type) || "NODE".equals(type), "凭证主体非法"); Map row = store.get(type.toLowerCase(Locale.ROOT), owner, true); require(number(row, "user_id") == actor, "无主体权限"); String secret = RentalCrypto.random(); String encrypted = crypto.encrypt(secret); String key = id("RK"); store.execute( "UPDATE rent_credential SET status='VERIFY_ONLY',version=version+1,updated_at=?" + " WHERE owner_type=? AND owner_id=? AND status='ACTIVE'", now(), type, owner); long id = store.insert( "credential", map( "owner_type", type, "owner_id", owner, "api_key", key, "api_secret", encrypted, "master_key_version", masterVersion, "status", "ACTIVE")); store.event(type, owner, "ROTATE_CREDENTIAL", actor, "新Secret仅通过本次TLS响应展示,旧凭证仅核对在途"); return map("id", id, "api_key", key, "api_secret", secret); } @Transactional(rollbackFor = Exception.class) public void revoke(long id, long actor) { Map credential = store.get("credential", id, true); Map owner = store.get( text(credential, "owner_type").toLowerCase(Locale.ROOT), number(credential, "owner_id"), false); require(number(owner, "user_id") == actor, "无主体权限"); store.update("credential", id, map("status", "REVOKED", "revoked_at", now())); store.event( "APP".equals(text(credential, "owner_type")) ? "APP" : "NODE", number(credential, "owner_id"), "REVOKE_CREDENTIAL", actor, "主动吊销凭证"); } /** * 仅物理删除未产生业务引用的 APP 凭证。节点凭证必须保留用于原任务验签。 * 先锁应用再锁凭证,与签发、下单及应用删除保持一致锁顺序。 * 订单未保存请求凭证版本,因此存在任意应用订单时保守拒绝,不猜测密钥是否使用过。 */ @Transactional(rollbackFor = Exception.class) public int deleteAppCredential(long id, long actor) { Map snapshot = store.get("credential", id, false); require("APP".equals(text(snapshot, "owner_type")), "节点凭证请使用吊销,不能删除"); long appId = number(snapshot, "owner_id"); Map app = store.get("app", appId, true); require(number(app, "user_id") == actor, "无应用凭证删除权限"); Map credential = store.get("credential", id, true); require("APP".equals(text(credential, "owner_type")) && number(credential, "owner_id") == appId, "凭证归属发生变化,请刷新重试"); require(store.one("SELECT id FROM rent_order WHERE app_id=? LIMIT 1 FOR UPDATE", appId) == null, "应用存在历史或在途订单,请保留凭证或使用吊销"); require(store.one("SELECT id FROM rent_task WHERE credential_id=? LIMIT 1 FOR UPDATE", id) == null, "凭证存在关联任务,不能删除"); require(store.one("SELECT id FROM rent_webhook WHERE credential_id=? LIMIT 1 FOR UPDATE", id) == null, "凭证存在关联回调,不能删除"); require(store.one("SELECT id FROM rent_nonce WHERE api_key=? AND expires_at>? LIMIT 1 FOR UPDATE", text(credential, "api_key"), now()) == null, "凭证近期仍在使用,请停止调用并等待认证记录过期后重试"); require(store.execute("DELETE FROM rent_credential WHERE id=?", id) == 1, "凭证删除失败"); store.event("APP", appId, "DELETE_CREDENTIAL", actor, "删除无业务引用的凭证,凭证ID=" + id); return 1; } }