securitydept-basic-auth-context 0.3.0-beta.3

Basic Auth Context of SecurityDept, a layered authentication and authorization toolkit built as reusable Rust crates.
Documentation
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
use http::StatusCode;
use securitydept_creds::{
    BasicAuthCred, BasicAuthCredsValidator, CredsError, MapBasicAuthCredsValidator,
    parse_basic_auth_header_opt,
};
use securitydept_realip::ResolvedClientIp;
use securitydept_utils::{
    error::{ErrorPresentation, ToErrorPresentation, UserRecovery},
    http::{HttpResponse, ToHttpStatus},
    observability::{
        AuthFlowDiagnosis, AuthFlowDiagnosisField, AuthFlowDiagnosisOutcome, AuthFlowOperation,
        DiagnosedResult,
    },
};
use serde::{Deserialize, Serialize};
use snafu::Snafu;

use crate::{BasicAuthContext, BasicAuthContextError, BasicAuthZone};

fn basic_auth_diagnosis(operation: &str) -> AuthFlowDiagnosis {
    AuthFlowDiagnosis::started(operation).field(AuthFlowDiagnosisField::AUTH_FAMILY, "basic-auth")
}

/// Errors produced by [`BasicAuthContextService`] operations.
#[derive(Debug, Snafu)]
pub enum BasicAuthContextServiceError {
    #[snafu(transparent)]
    BasicAuthContext { source: BasicAuthContextError },
    #[snafu(transparent)]
    Creds { source: CredsError },
}

impl ToHttpStatus for BasicAuthContextServiceError {
    fn to_http_status(&self) -> StatusCode {
        match self {
            Self::BasicAuthContext { .. } => StatusCode::INTERNAL_SERVER_ERROR,
            Self::Creds { source } => source.to_http_status(),
        }
    }
}

impl ToErrorPresentation for BasicAuthContextServiceError {
    fn to_error_presentation(&self) -> ErrorPresentation {
        match self {
            Self::BasicAuthContext { .. } => ErrorPresentation::new(
                "basic_auth_context_invalid",
                "Basic-auth context is misconfigured.",
                UserRecovery::ContactSupport,
            ),
            Self::Creds { source } => source.to_error_presentation(),
        }
    }
}

/// Route-facing service for basic-auth context operations.
///
/// Provides login, logout, and request authorization based on HTTP Basic
/// credentials.
pub struct BasicAuthContextService<'a, Creds>
where
    Creds: BasicAuthCred + Serialize + for<'de> Deserialize<'de>,
{
    basic_auth_context: &'a BasicAuthContext<Creds>,
}

impl<'a, Creds> BasicAuthContextService<'a, Creds>
where
    Creds: BasicAuthCred + Serialize + for<'de> Deserialize<'de>,
{
    pub fn new(
        basic_auth_context: &'a BasicAuthContext<Creds>,
    ) -> Result<Self, BasicAuthContextServiceError> {
        MapBasicAuthCredsValidator::from_config(&basic_auth_context.creds)
            .map_err(|source| BasicAuthContextServiceError::Creds { source })?;

        Ok(Self { basic_auth_context })
    }

    pub fn context(&self) -> &BasicAuthContext<Creds> {
        self.basic_auth_context
    }

    pub fn login(
        &self,
        request_path: &str,
        authorization_header: Option<&str>,
        requested_post_auth_redirect_uri: Option<&str>,
        resolved_client_ip: Option<&ResolvedClientIp>,
    ) -> Result<HttpResponse, BasicAuthContextServiceError> {
        self.login_diagnosed(
            request_path,
            authorization_header,
            requested_post_auth_redirect_uri,
            resolved_client_ip,
        )
        .into_result()
    }

    pub fn login_diagnosed(
        &self,
        request_path: &str,
        authorization_header: Option<&str>,
        requested_post_auth_redirect_uri: Option<&str>,
        resolved_client_ip: Option<&ResolvedClientIp>,
    ) -> DiagnosedResult<HttpResponse, BasicAuthContextServiceError> {
        let diagnosis = basic_auth_diagnosis(AuthFlowOperation::BASIC_AUTH_LOGIN)
            .field(AuthFlowDiagnosisField::REQUEST_PATH, request_path)
            .field("authorization_present", authorization_header.is_some())
            .field(
                "has_requested_post_auth_redirect_uri",
                requested_post_auth_redirect_uri.is_some(),
            )
            .field(
                AuthFlowDiagnosisField::RESOLVED_CLIENT_IP_PRESENT,
                resolved_client_ip.is_some(),
            );
        let Some(zone) = self.basic_auth_context.zone_for_request_path(request_path) else {
            return DiagnosedResult::success(
                diagnosis
                    .with_outcome(AuthFlowDiagnosisOutcome::Rejected)
                    .field(AuthFlowDiagnosisField::REASON, "zone_not_found")
                    .field(
                        AuthFlowDiagnosisField::HTTP_STATUS,
                        StatusCode::NOT_FOUND.as_u16(),
                    ),
                HttpResponse::new(StatusCode::NOT_FOUND),
            );
        };

        let real_ip_allowed = match self.real_ip_allowed(resolved_client_ip) {
            Ok(allowed) => allowed,
            Err(source) => {
                return DiagnosedResult::failure(
                    diagnosis
                        .clone()
                        .with_outcome(AuthFlowDiagnosisOutcome::Failed)
                        .field(AuthFlowDiagnosisField::REASON, "real_ip_resolution_failed"),
                    source,
                );
            }
        };
        if !real_ip_allowed {
            return DiagnosedResult::success(
                diagnosis
                    .with_outcome(AuthFlowDiagnosisOutcome::Rejected)
                    .field(AuthFlowDiagnosisField::REASON, "real_ip_forbidden")
                    .field(
                        AuthFlowDiagnosisField::HTTP_STATUS,
                        StatusCode::FORBIDDEN.as_u16(),
                    ),
                HttpResponse::new(StatusCode::FORBIDDEN),
            );
        }

        let authenticated = match self.verify_basic_auth(authorization_header) {
            Ok(authenticated) => authenticated,
            Err(source) => {
                return DiagnosedResult::failure(
                    diagnosis
                        .clone()
                        .with_outcome(AuthFlowDiagnosisOutcome::Failed)
                        .field(
                            AuthFlowDiagnosisField::REASON,
                            "credential_validation_failed",
                        ),
                    source,
                );
            }
        };

        if authenticated {
            match zone.login_success_response(requested_post_auth_redirect_uri) {
                Ok(response) => DiagnosedResult::success(
                    diagnosis
                        .with_outcome(AuthFlowDiagnosisOutcome::Succeeded)
                        .field("authenticated", true)
                        .field(
                            AuthFlowDiagnosisField::HTTP_STATUS,
                            response.status.as_u16(),
                        ),
                    response,
                ),
                Err(source) => DiagnosedResult::failure(
                    diagnosis
                        .with_outcome(AuthFlowDiagnosisOutcome::Failed)
                        .field("authenticated", true)
                        .field(AuthFlowDiagnosisField::REASON, "post_auth_redirect_invalid"),
                    BasicAuthContextServiceError::BasicAuthContext { source },
                ),
            }
        } else {
            let response = zone.login_challenge_response();
            DiagnosedResult::success(
                diagnosis
                    .with_outcome(AuthFlowDiagnosisOutcome::Rejected)
                    .field("authenticated", false)
                    .field(AuthFlowDiagnosisField::REASON, "challenge_required")
                    .field(
                        AuthFlowDiagnosisField::HTTP_STATUS,
                        response.status.as_u16(),
                    ),
                response,
            )
        }
    }

    pub fn logout(&self, request_path: &str) -> HttpResponse {
        self.logout_diagnosed(request_path)
            .into_result()
            .expect("basic-auth logout diagnosis should not fail")
    }

    pub fn logout_diagnosed(
        &self,
        request_path: &str,
    ) -> DiagnosedResult<HttpResponse, BasicAuthContextServiceError> {
        let diagnosis = basic_auth_diagnosis(AuthFlowOperation::BASIC_AUTH_LOGOUT)
            .field(AuthFlowDiagnosisField::REQUEST_PATH, request_path);
        if let Some(protocol_response) = self.logout_protocol_response(request_path) {
            let response = protocol_response.into_http_response();
            DiagnosedResult::success(
                diagnosis
                    .with_outcome(AuthFlowDiagnosisOutcome::Succeeded)
                    .field("response_kind", "logout_poison")
                    .field(
                        AuthFlowDiagnosisField::HTTP_STATUS,
                        response.status.as_u16(),
                    ),
                response,
            )
        } else {
            DiagnosedResult::success(
                diagnosis
                    .with_outcome(AuthFlowDiagnosisOutcome::Rejected)
                    .field("response_kind", "not_found")
                    .field(
                        AuthFlowDiagnosisField::HTTP_STATUS,
                        StatusCode::NOT_FOUND.as_u16(),
                    ),
                HttpResponse::new(StatusCode::NOT_FOUND),
            )
        }
    }

    pub fn logout_protocol_response(
        &self,
        request_path: &str,
    ) -> Option<crate::BasicAuthProtocolResponse> {
        self.basic_auth_context
            .zone_for_request_path(request_path)
            .map(BasicAuthZone::logout_poison_protocol_response)
    }

    pub fn authorize_request(
        &self,
        authorization_header: Option<&str>,
        resolved_client_ip: Option<&ResolvedClientIp>,
    ) -> Result<bool, BasicAuthContextServiceError> {
        self.authorize_request_diagnosed(authorization_header, resolved_client_ip)
            .into_result()
    }

    pub fn authorize_request_diagnosed(
        &self,
        authorization_header: Option<&str>,
        resolved_client_ip: Option<&ResolvedClientIp>,
    ) -> DiagnosedResult<bool, BasicAuthContextServiceError> {
        let diagnosis = basic_auth_diagnosis(AuthFlowOperation::BASIC_AUTH_AUTHORIZE)
            .field("authorization_present", authorization_header.is_some())
            .field(
                AuthFlowDiagnosisField::RESOLVED_CLIENT_IP_PRESENT,
                resolved_client_ip.is_some(),
            );

        let real_ip_allowed = match self.real_ip_allowed(resolved_client_ip) {
            Ok(allowed) => allowed,
            Err(source) => {
                return DiagnosedResult::failure(
                    diagnosis
                        .clone()
                        .with_outcome(AuthFlowDiagnosisOutcome::Failed)
                        .field(AuthFlowDiagnosisField::REASON, "real_ip_resolution_failed"),
                    source,
                );
            }
        };
        if !real_ip_allowed {
            return DiagnosedResult::success(
                diagnosis
                    .with_outcome(AuthFlowDiagnosisOutcome::Rejected)
                    .field("authorized", false)
                    .field(AuthFlowDiagnosisField::REASON, "real_ip_forbidden"),
                false,
            );
        }

        match self.verify_basic_auth(authorization_header) {
            Ok(authorized) => {
                let outcome = if authorized {
                    AuthFlowDiagnosisOutcome::Succeeded
                } else {
                    AuthFlowDiagnosisOutcome::Rejected
                };
                let reason = if authorized {
                    "credentials_verified"
                } else {
                    "credentials_missing_or_invalid"
                };
                DiagnosedResult::success(
                    diagnosis
                        .with_outcome(outcome)
                        .field("authorized", authorized)
                        .field(AuthFlowDiagnosisField::REASON, reason),
                    authorized,
                )
            }
            Err(source) => DiagnosedResult::failure(
                diagnosis
                    .with_outcome(AuthFlowDiagnosisOutcome::Failed)
                    .field(
                        AuthFlowDiagnosisField::REASON,
                        "credential_validation_failed",
                    ),
                source,
            ),
        }
    }

    fn verify_basic_auth(
        &self,
        authorization_header: Option<&str>,
    ) -> Result<bool, BasicAuthContextServiceError> {
        let Some(authorization_header) = authorization_header else {
            return Ok(false);
        };
        let Some((username, password)) = parse_basic_auth_header_opt(authorization_header) else {
            return Ok(false);
        };
        let validator = MapBasicAuthCredsValidator::from_config(&self.basic_auth_context.creds)
            .map_err(|source| BasicAuthContextServiceError::Creds { source })?;

        validator
            .verify_cred(&username, &password)
            .map(|result| result.is_some())
            .map_err(|source| BasicAuthContextServiceError::Creds { source })
    }

    fn real_ip_allowed(
        &self,
        resolved_client_ip: Option<&ResolvedClientIp>,
    ) -> Result<bool, BasicAuthContextServiceError> {
        match (
            self.basic_auth_context.real_ip_access.is_some(),
            resolved_client_ip,
        ) {
            (false, _) => Ok(true),
            (true, None) => Ok(false),
            (true, Some(resolved_client_ip)) => self
                .basic_auth_context
                .ensure_real_ip_allowed(resolved_client_ip)
                .map(|_| true)
                .or_else(|error| match error {
                    BasicAuthContextError::RealIp { .. } => Ok(false),
                    source => Err(BasicAuthContextServiceError::BasicAuthContext { source }),
                }),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{
        BasicAuthContext, BasicAuthContextConfig, BasicAuthContextConfigSource, BasicAuthZoneConfig,
    };

    #[derive(Debug, Clone, Serialize, Deserialize, Default)]
    struct TestCred {
        username: String,
        password: String,
    }

    impl BasicAuthCred for TestCred {
        fn username(&self) -> &str {
            &self.username
        }

        fn verify_password(&self, password: &str) -> securitydept_creds::CredsResult<bool> {
            Ok(password == self.password)
        }
    }

    fn test_context() -> BasicAuthContext<TestCred> {
        let config = BasicAuthContextConfig::builder()
            .creds(securitydept_creds::BasicAuthCredsConfig {
                users: vec![TestCred {
                    username: "admin".to_string(),
                    password: "secret".to_string(),
                }],
            })
            .zones(vec![BasicAuthZoneConfig::default()])
            .build();

        BasicAuthContext::from_resolved_config(
            BasicAuthContextConfigSource::resolve_all(&config)
                .expect("basic auth config should resolve"),
        )
        .expect("context should build")
    }

    fn test_context_with_dynamic_redirect() -> BasicAuthContext<TestCred> {
        let config = BasicAuthContextConfig::builder()
            .creds(securitydept_creds::BasicAuthCredsConfig {
                users: vec![TestCred {
                    username: "admin".to_string(),
                    password: "secret".to_string(),
                }],
            })
            .zones(vec![BasicAuthZoneConfig::builder()
                .post_auth_redirect(
                    securitydept_utils::redirect::RedirectTargetConfig::dynamic_default_and_dynamic_targets(
                        "/",
                        [securitydept_utils::redirect::RedirectTargetRule::Strict {
                            value: "/console".to_string(),
                        }],
                    ),
                )
                .build()])
            .build();

        BasicAuthContext::from_resolved_config(
            BasicAuthContextConfigSource::resolve_all(&config)
                .expect("basic auth config should resolve"),
        )
        .expect("context should build")
    }

    #[test]
    fn login_without_credentials_returns_challenge() {
        let context = test_context();
        let service = BasicAuthContextService::new(&context).expect("service should build");
        let diagnosed = service.login_diagnosed("/basic/login", None, None, None);
        let response = diagnosed
            .result()
            .as_ref()
            .expect("login should return response");

        assert_eq!(response.status, StatusCode::UNAUTHORIZED);
        assert_eq!(
            diagnosed.diagnosis().operation,
            AuthFlowOperation::BASIC_AUTH_LOGIN
        );
        assert_eq!(
            diagnosed.diagnosis().outcome,
            AuthFlowDiagnosisOutcome::Rejected
        );
        assert_eq!(diagnosed.diagnosis().fields["reason"], "challenge_required");
    }

    #[test]
    fn login_with_valid_credentials_redirects() {
        let context = test_context_with_dynamic_redirect();
        let service = BasicAuthContextService::new(&context).expect("service should build");
        let diagnosed = service.login_diagnosed(
            "/basic/login",
            Some("Basic YWRtaW46c2VjcmV0"),
            Some("/console"),
            None,
        );
        let response = diagnosed
            .result()
            .as_ref()
            .expect("login should return response");

        assert_eq!(response.status, StatusCode::FOUND);
        assert_eq!(
            response.headers.get(http::header::LOCATION).unwrap(),
            "/console"
        );
        assert_eq!(
            diagnosed.diagnosis().operation,
            AuthFlowOperation::BASIC_AUTH_LOGIN
        );
        assert_eq!(
            diagnosed.diagnosis().outcome,
            AuthFlowDiagnosisOutcome::Succeeded
        );
        assert_eq!(diagnosed.diagnosis().fields["authenticated"], true);
    }

    #[test]
    fn authorize_request_diagnosed_reports_verified_credentials() {
        let context = test_context();
        let service = BasicAuthContextService::new(&context).expect("service should build");
        let diagnosed = service.authorize_request_diagnosed(Some("Basic YWRtaW46c2VjcmV0"), None);

        assert!(
            diagnosed
                .result()
                .as_ref()
                .is_ok_and(|authorized| *authorized)
        );
        assert_eq!(
            diagnosed.diagnosis().operation,
            AuthFlowOperation::BASIC_AUTH_AUTHORIZE
        );
        assert_eq!(
            diagnosed.diagnosis().outcome,
            AuthFlowDiagnosisOutcome::Succeeded
        );
        assert_eq!(diagnosed.diagnosis().fields["authorized"], true);
    }

    #[test]
    fn logout_diagnosed_reports_logout_poison_protocol_response() {
        let context = test_context();
        let service = BasicAuthContextService::new(&context).expect("service should build");
        let diagnosed = service.logout_diagnosed("/basic/logout");
        let response = diagnosed
            .result()
            .as_ref()
            .expect("logout should produce response");

        assert_eq!(response.status, StatusCode::UNAUTHORIZED);
        assert_eq!(
            diagnosed.diagnosis().operation,
            AuthFlowOperation::BASIC_AUTH_LOGOUT
        );
        assert_eq!(
            diagnosed.diagnosis().outcome,
            AuthFlowDiagnosisOutcome::Succeeded
        );
        assert_eq!(
            diagnosed.diagnosis().fields["response_kind"],
            "logout_poison"
        );
    }
}