pub struct KeyRotation { /* private fields */ }Expand description
JWT 密钥轮换管理器
支持多密钥并存验证:当前密钥签发/验证,旧密钥在 grace period 内仍可验证。 轮换任务定期生成新密钥,旧密钥移入 previous 列表,超期后删除。
Implementations§
Source§impl KeyRotation
impl KeyRotation
Sourcepub fn from_env() -> Result<KeyRotation, KeyRotationError>
pub fn from_env() -> Result<KeyRotation, KeyRotationError>
从环境变量创建密钥轮换管理器
SZ300_JWT_SECRET(必填):当前密钥SZ300_JWT_ROTATION_INTERVAL:轮换间隔秒数(默认 86400 = 24h)SZ300_JWT_GRACE_PERIOD:宽限期秒数(默认 3600 = 1h)
Sourcepub fn new(
current: String,
rotation_interval: Duration,
grace_period: Duration,
max_previous: usize,
) -> KeyRotation
pub fn new( current: String, rotation_interval: Duration, grace_period: Duration, max_previous: usize, ) -> KeyRotation
直接构造(用于测试或显式配置)
Sourcepub fn sign_token(&self, claims: &JwtClaims) -> Result<String, KeyRotationError>
pub fn sign_token(&self, claims: &JwtClaims) -> Result<String, KeyRotationError>
用当前密钥签发 token
Sourcepub fn verify_token(&self, token: &str) -> Result<JwtClaims, KeyRotationError>
pub fn verify_token(&self, token: &str) -> Result<JwtClaims, KeyRotationError>
验证 token:先尝试当前密钥,失败则遍历旧密钥(grace period 内)
Sourcepub fn spawn_rotation_task(self: Arc<KeyRotation>) -> JoinHandle<()> ⓘ
pub fn spawn_rotation_task(self: Arc<KeyRotation>) -> JoinHandle<()> ⓘ
启动密钥轮换定时任务
Sourcepub async fn do_rotation(
&self,
grace_period: Duration,
max_previous: usize,
) -> Result<(), String>
pub async fn do_rotation( &self, grace_period: Duration, max_previous: usize, ) -> Result<(), String>
执行一次密钥轮换
Sourcepub fn fingerprint(key: &str) -> String
pub fn fingerprint(key: &str) -> String
计算密钥指纹(SHA256 前 8 位十六进制)
Sourcepub fn current_fingerprint(&self) -> String
pub fn current_fingerprint(&self) -> String
获取当前密钥指纹(用于审计日志)
Trait Implementations§
Auto Trait Implementations§
impl !Freeze for KeyRotation
impl !RefUnwindSafe for KeyRotation
impl Send for KeyRotation
impl Sync for KeyRotation
impl Unpin for KeyRotation
impl UnsafeUnpin for KeyRotation
impl UnwindSafe for KeyRotation
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> Pointable for T
impl<T> Pointable for T
impl<T> Read<Exclusive, BecauseExclusive> for Twhere
T: ?Sized,
Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
The inverse inclusion map: attempts to construct
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
Checks if
self is actually part of its subset T (and can be converted to it).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
Use with care! Same as
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
The inclusion map: converts
self to the equivalent element of its superset.