using System;
namespace Aging.Infrastructure.Contracts
{
///
/// 所有者类型枚举
///
public enum OwnerType
{
///
/// 生产模式(正常老化测试流程)
///
Production,
///
/// 维护模式(调试/维护会话)
///
Maintenance
}
///
/// 所有者令牌,标识资源预约的归属。
/// 用于区分不同会话对硬件资源的所有权。
///
public sealed class ResourceOwnerToken
{
///
/// 令牌唯一标识
///
public string TokenId { get; }
///
/// 所有者类型(生产/维护)
///
public OwnerType OwnerType { get; }
///
/// 所有者标识(RunID 或 DebugSessionID)
///
public string OwnerId { get; }
///
/// 世代号,每次新预约递增,防止重放攻击
///
public long Generation { get; set; }
///
/// 令牌签发时间
///
public DateTime IssuedAt { get; }
///
/// 令牌过期时间(可选)
///
public DateTime? ExpiresAt { get; }
///
/// 初始化所有者令牌实例
///
/// 所有者类型
/// 所有者标识
/// 过期时间(可选)
public ResourceOwnerToken(OwnerType ownerType, string ownerId, DateTime? expiresAt = null)
{
TokenId = Guid.NewGuid().ToString();
OwnerType = ownerType;
OwnerId = ownerId ?? throw new ArgumentNullException(nameof(ownerId));
Generation = 0;
IssuedAt = DateTime.UtcNow;
ExpiresAt = expiresAt;
}
///
/// 检查令牌是否已过期
///
/// 过期返回 true;未设置过期时间或尚未过期返回 false
public bool IsExpired()
{
if (!ExpiresAt.HasValue)
return false;
return DateTime.UtcNow > ExpiresAt.Value;
}
}
}