1use super::*;
3
4#[repr(u8)]
7#[derive(Clone, Copy)]
8enum CancelReason {
9 None = 0,
11 Expired = 1,
12 Revoked = 2,
13 Rotated = 3,
14}
15
16impl CancelReason {
17 fn from_u8(raw: u8) -> Self {
18 match raw {
19 1 => Self::Expired,
20 2 => Self::Revoked,
21 3 => Self::Rotated,
22 _ => Self::None,
23 }
24 }
25}
26
27#[derive(Debug)]
28pub struct AuthLease {
29 key_id: KeyId,
30 namespace: u64,
31 expires_at: AtomicU64,
32 cancellation: CancellationToken,
33 cancel_reason: AtomicU8,
35}
36
37impl AuthLease {
38 pub(crate) fn new(key_id: KeyId, issued_into: Option<u64>, expires_at: u64) -> Self {
39 Self {
40 key_id,
41 namespace: key_id.namespace(issued_into),
42 expires_at: AtomicU64::new(expires_at),
43 cancellation: CancellationToken::new(),
44 cancel_reason: AtomicU8::new(CancelReason::None as u8),
45 }
46 }
47
48 pub fn key_id(&self) -> KeyId {
49 self.key_id
50 }
51
52 pub fn expires_at(&self) -> u64 {
53 self.expires_at.load(Ordering::Acquire)
54 }
55
56 pub fn cancellation_token(&self) -> CancellationToken {
57 self.cancellation.clone()
58 }
59
60 fn record_cancel(&self, reason: CancelReason) {
61 let _ = self.cancel_reason.compare_exchange(
62 CancelReason::None as u8,
63 reason as u8,
64 Ordering::AcqRel,
65 Ordering::Acquire,
66 );
67 self.cancellation.cancel();
68 }
69
70 fn cancel_reason(&self) -> CancelReason {
71 CancelReason::from_u8(self.cancel_reason.load(Ordering::Acquire))
72 }
73
74 pub(crate) fn is_cancelled(&self) -> bool {
75 self.cancellation.is_cancelled()
76 }
77
78 pub(crate) fn ran_out(&self) -> bool {
81 match self.cancel_reason() {
82 CancelReason::Expired => true,
83 CancelReason::None | CancelReason::Revoked | CancelReason::Rotated => false,
84 }
85 }
86
87 pub(crate) fn set_expires_at(&self, expires_at: u64) {
89 self.expires_at.store(expires_at, Ordering::Release);
90 }
91
92 pub(crate) fn cancel_expired(&self) {
93 self.record_cancel(CancelReason::Expired);
94 }
95
96 pub(crate) fn cancel_revoked(&self) {
97 self.record_cancel(CancelReason::Revoked);
98 }
99
100 pub(crate) fn cancel_rotated(&self) {
101 self.record_cancel(CancelReason::Rotated);
102 }
103}
104
105#[derive(Clone, Debug)]
106pub struct AuthContext {
107 pub key_id: KeyId,
108 pub namespace: u64,
109 pub is_admin: bool,
110 lease: Weak<AuthLease>,
111}
112
113impl AuthContext {
114 pub(crate) fn from_lease(key_id: KeyId, is_admin: bool, lease: &Arc<AuthLease>) -> Self {
115 Self {
116 key_id,
117 namespace: lease.namespace,
118 is_admin,
119 lease: Arc::downgrade(lease),
120 }
121 }
122
123 pub fn ensure_active(&self) -> Result<Arc<AuthLease>, AuthFailure> {
124 let lease = self.lease.upgrade().ok_or_else(|| {
125 AuthFailure::new(
126 if self.is_admin {
127 codes::ADMINISTRATOR_KEY_ROTATED
128 } else {
129 codes::TEMPORARY_KEY_INACTIVE
130 },
131 "credential lease is no longer active",
132 false,
133 )
134 })?;
135 if lease.is_cancelled() {
136 return Err(cancelled_lease_failure(self.is_admin, &lease));
137 }
138 if !self.is_admin && lease.expires_at() <= unix_seconds() {
139 lease.cancel_expired();
140 return Err(AuthFailure::key_expired());
141 }
142 Ok(lease)
143 }
144
145 pub fn cancellation_token(&self) -> Result<CancellationToken, AuthFailure> {
146 Ok(self.ensure_active()?.cancellation_token())
147 }
148
149 pub fn admin_cancellation_token(&self) -> Result<CancellationToken, AuthFailure> {
150 self.require_admin()?;
151 self.cancellation_token()
152 }
153
154 pub(crate) fn admin_authority(&self) -> Result<Weak<AuthLease>, AuthFailure> {
155 self.require_admin()?;
156 self.ensure_active()?;
157 Ok(self.lease.clone())
158 }
159
160 fn require_admin(&self) -> Result<(), AuthFailure> {
161 if self.is_admin {
162 Ok(())
163 } else {
164 Err(AuthFailure::new(
165 codes::ADMIN_PERMISSION_REQUIRED,
166 "administrator credential is required for this operation",
167 false,
168 ))
169 }
170 }
171}
172
173fn cancelled_lease_failure(is_admin: bool, lease: &AuthLease) -> AuthFailure {
174 if is_admin {
175 return AuthFailure::new(
176 codes::ADMINISTRATOR_KEY_ROTATED,
177 "credential lease has been cancelled",
178 false,
179 );
180 }
181 match lease.cancel_reason() {
182 CancelReason::Expired => AuthFailure::key_expired(),
183 CancelReason::Rotated => AuthFailure::key_rotated(),
184 CancelReason::Revoked => AuthFailure::key_revoked(),
185 CancelReason::None => AuthFailure::new(
186 codes::TEMPORARY_KEY_INACTIVE,
187 "credential lease has been cancelled",
188 false,
189 ),
190 }
191}