forked from modcrafter77/hyperspot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontext.rs
More file actions
312 lines (268 loc) · 10.1 KB
/
context.rs
File metadata and controls
312 lines (268 loc) · 10.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
use secrecy::SecretString;
use uuid::Uuid;
/// Error returned when `SecurityContextBuilder::build()` is called without
/// required fields.
#[derive(Debug, thiserror::Error)]
pub enum SecurityContextBuildError {
#[error(
"subject_id is required - use SecurityContext::anonymous() for unauthenticated contexts"
)]
MissingSubjectId,
#[error(
"subject_tenant_id is required - use SecurityContext::anonymous() for unauthenticated contexts"
)]
MissingSubjectTenantId,
}
/// `SecurityContext` encapsulates the security-related information for a request or operation.
///
/// Built by the `AuthN` Resolver during authentication and passed through the request lifecycle.
/// Modules use this context together with the `AuthZ` Resolver to obtain access scopes.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct SecurityContext {
/// Subject ID — the authenticated user, service, or system making the request.
subject_id: Uuid,
/// Subject type classification (e.g., "user", "service").
subject_type: Option<String>,
/// Subject's home tenant (from `AuthN`). Required — every authenticated
/// subject belongs to a tenant.
subject_tenant_id: Uuid,
/// Token capability restrictions. `["*"]` means first-party / unrestricted.
/// Empty means no scopes were asserted (treat as unrestricted for backward compatibility).
#[serde(default)]
token_scopes: Vec<String>,
/// Original bearer token for PDP forwarding. Never serialized/persisted.
/// Wrapped in `SecretString` so `Debug` redacts the value automatically.
#[serde(skip)]
bearer_token: Option<SecretString>,
}
impl SecurityContext {
/// Create a new `SecurityContext` builder
#[must_use]
pub fn builder() -> SecurityContextBuilder {
SecurityContextBuilder::default()
}
/// Create an anonymous `SecurityContext` with no tenant, subject, or permissions.
///
/// Use this for unauthenticated / dev / auth-disabled contexts where no
/// authenticated subject exists.
#[must_use]
pub fn anonymous() -> Self {
Self {
subject_id: Uuid::default(),
subject_type: None,
subject_tenant_id: Uuid::default(),
token_scopes: Vec::new(),
bearer_token: None,
}
}
/// Get the subject ID (user, service, or system) associated with the security context
#[must_use]
pub fn subject_id(&self) -> Uuid {
self.subject_id
}
/// Get the subject type classification (e.g., "user", "service").
#[must_use]
pub fn subject_type(&self) -> Option<&str> {
self.subject_type.as_deref()
}
/// Get the subject's home tenant ID (from `AuthN` token).
#[must_use]
pub fn subject_tenant_id(&self) -> Uuid {
self.subject_tenant_id
}
/// Get the token scopes. `["*"]` means first-party / unrestricted.
#[must_use]
pub fn token_scopes(&self) -> &[String] {
&self.token_scopes
}
/// Get the original bearer token (for PDP forwarding).
#[must_use]
pub fn bearer_token(&self) -> Option<&SecretString> {
self.bearer_token.as_ref()
}
}
#[derive(Default)]
pub struct SecurityContextBuilder {
subject_id: Option<Uuid>,
subject_type: Option<String>,
subject_tenant_id: Option<Uuid>,
token_scopes: Vec<String>,
bearer_token: Option<SecretString>,
}
impl SecurityContextBuilder {
#[must_use]
pub fn subject_id(mut self, subject_id: Uuid) -> Self {
self.subject_id = Some(subject_id);
self
}
#[must_use]
pub fn subject_type(mut self, subject_type: &str) -> Self {
self.subject_type = Some(subject_type.to_owned());
self
}
#[must_use]
pub fn subject_tenant_id(mut self, subject_tenant_id: Uuid) -> Self {
self.subject_tenant_id = Some(subject_tenant_id);
self
}
#[must_use]
pub fn token_scopes(mut self, scopes: Vec<String>) -> Self {
self.token_scopes = scopes;
self
}
#[must_use]
pub fn bearer_token(mut self, token: impl Into<SecretString>) -> Self {
self.bearer_token = Some(token.into());
self
}
/// Build the `SecurityContext`.
///
/// # Errors
///
/// Returns `SecurityContextBuildError` if `subject_id` or
/// `subject_tenant_id` was not set. Use `SecurityContext::anonymous()`
/// for contexts that intentionally have no authenticated subject.
pub fn build(self) -> Result<SecurityContext, SecurityContextBuildError> {
let subject_id = self
.subject_id
.ok_or(SecurityContextBuildError::MissingSubjectId)?;
let subject_tenant_id = self
.subject_tenant_id
.ok_or(SecurityContextBuildError::MissingSubjectTenantId)?;
Ok(SecurityContext {
subject_id,
subject_type: self.subject_type,
subject_tenant_id,
token_scopes: self.token_scopes,
bearer_token: self.bearer_token,
})
}
}
#[cfg(test)]
#[cfg_attr(coverage_nightly, coverage(off))]
mod tests {
use secrecy::ExposeSecret;
use super::*;
#[test]
fn test_security_context_builder_full() {
let subject_id = Uuid::parse_str("550e8400-e29b-41d4-a716-446655440001").unwrap();
let subject_tenant_id = Uuid::parse_str("550e8400-e29b-41d4-a716-446655440002").unwrap();
let ctx = SecurityContext::builder()
.subject_id(subject_id)
.subject_type("user")
.subject_tenant_id(subject_tenant_id)
.token_scopes(vec!["read:events".to_owned(), "write:events".to_owned()])
.bearer_token("test-token-123".to_owned())
.build()
.unwrap();
assert_eq!(ctx.subject_id(), subject_id);
assert_eq!(ctx.subject_tenant_id(), subject_tenant_id);
assert_eq!(ctx.token_scopes(), &["read:events", "write:events"]);
assert_eq!(
ctx.bearer_token().map(ExposeSecret::expose_secret),
Some("test-token-123"),
);
}
#[test]
fn test_security_context_builder_missing_subject_id() {
let err = SecurityContext::builder()
.subject_tenant_id(Uuid::parse_str("550e8400-e29b-41d4-a716-446655440002").unwrap())
.build();
assert!(matches!(
err,
Err(SecurityContextBuildError::MissingSubjectId)
));
}
#[test]
fn test_security_context_builder_missing_tenant_id() {
let err = SecurityContext::builder()
.subject_id(Uuid::parse_str("550e8400-e29b-41d4-a716-446655440001").unwrap())
.build();
assert!(matches!(
err,
Err(SecurityContextBuildError::MissingSubjectTenantId)
));
}
#[test]
fn test_security_context_builder_missing_both() {
let err = SecurityContext::builder().build();
assert!(matches!(
err,
Err(SecurityContextBuildError::MissingSubjectId)
));
}
#[test]
fn test_security_context_anonymous() {
let ctx = SecurityContext::anonymous();
assert_eq!(ctx.subject_id(), Uuid::default());
assert_eq!(ctx.subject_tenant_id(), Uuid::default());
assert!(ctx.token_scopes().is_empty());
assert!(ctx.bearer_token().is_none());
}
#[test]
fn test_security_context_builder_chaining() {
let subject_id = Uuid::parse_str("550e8400-e29b-41d4-a716-446655440001").unwrap();
let subject_tenant_id = Uuid::parse_str("550e8400-e29b-41d4-a716-446655440002").unwrap();
let ctx = SecurityContext::builder()
.subject_id(subject_id)
.subject_type("user")
.subject_tenant_id(subject_tenant_id)
.build()
.unwrap();
assert_eq!(ctx.subject_id(), subject_id);
}
#[test]
fn test_security_context_clone() {
let subject_id = Uuid::parse_str("550e8400-e29b-41d4-a716-446655440001").unwrap();
let subject_tenant_id = Uuid::parse_str("550e8400-e29b-41d4-a716-446655440002").unwrap();
let ctx1 = SecurityContext::builder()
.subject_id(subject_id)
.subject_tenant_id(subject_tenant_id)
.token_scopes(vec!["*".to_owned()])
.bearer_token("secret".to_owned())
.build()
.unwrap();
let ctx2 = ctx1.clone();
assert_eq!(ctx2.subject_id(), ctx1.subject_id());
assert_eq!(ctx2.subject_tenant_id(), ctx1.subject_tenant_id());
assert_eq!(ctx2.token_scopes(), ctx1.token_scopes());
assert_eq!(
ctx2.bearer_token().map(ExposeSecret::expose_secret),
ctx1.bearer_token().map(ExposeSecret::expose_secret),
);
}
#[test]
fn test_security_context_serialize_deserialize() {
let subject_id = Uuid::parse_str("550e8400-e29b-41d4-a716-446655440001").unwrap();
let subject_tenant_id = Uuid::parse_str("550e8400-e29b-41d4-a716-446655440002").unwrap();
let original = SecurityContext::builder()
.subject_id(subject_id)
.subject_type("user")
.subject_tenant_id(subject_tenant_id)
.token_scopes(vec!["admin".to_owned()])
.bearer_token("secret-token".to_owned())
.build()
.unwrap();
let serialized = serde_json::to_string(&original).unwrap();
let deserialized: SecurityContext = serde_json::from_str(&serialized).unwrap();
assert_eq!(deserialized.subject_id(), original.subject_id());
assert_eq!(
deserialized.subject_tenant_id(),
original.subject_tenant_id()
);
assert_eq!(deserialized.token_scopes(), original.token_scopes());
// bearer_token is skipped during serialization
assert!(deserialized.bearer_token().is_none());
}
#[test]
fn test_security_context_bearer_token_not_serialized() {
let ctx = SecurityContext::anonymous();
let serialized = serde_json::to_string(&ctx).unwrap();
assert!(!serialized.contains("bearer_token"));
}
#[test]
fn test_security_context_empty_scopes() {
let ctx = SecurityContext::anonymous();
assert!(ctx.token_scopes().is_empty());
}
}