Skip to main content

manta_server/backend_dispatcher/
authentication.rs

1//! [`AuthenticationTrait`] impl for [`StaticBackendDispatcher`].
2//!
3//! - `exchange_api_token` is called on every request (from the
4//!   `RequestCtx` extractor): it exchanges the caller's CSCS Keycloak
5//!   token for one the site accepts, at the site's Keycloak on CSM
6//!   (RFC 8693) or the site's Hydra on OpenCHAMI (RFC 7523).
7//! - `get_api_token` (username/password grant) is no longer called by
8//!   manta: the CLI obtains its token from the CSCS Keycloak directly.
9//!   The forwarding is kept so `tests/dispatcher_coverage.rs` keeps
10//!   holding every trait method to an override.
11//!
12//! The wrapper adds structured `tracing::debug` on entry and
13//! `tracing::warn` on the error path so an auth failure surfaces the
14//! backend at the dispatcher boundary.
15
16use super::*;
17
18impl AuthenticationTrait for StaticBackendDispatcher {
19  /// Exchange `username` / `password` for an API bearer token.
20  ///
21  /// # Errors
22  ///
23  /// Returns [`Error::NetError`] / [`Error::RequestError`] when the
24  /// IdP is unreachable, [`Error::CsmError`] / [`Error::BadRequest`]
25  /// on rejection (bad credentials, MFA challenge, locked account),
26  /// and [`Error::Message`] for backend-specific failures.
27  async fn get_api_token(
28    &self,
29    username: &str,
30    password: &str,
31  ) -> Result<String, Error> {
32    let backend = self.backend_kind();
33    tracing::debug!(backend, user = %username, "dispatch: get_api_token");
34    let result = dispatch!(self, get_api_token, username, password);
35    if let Err(ref e) = result {
36      tracing::warn!(
37        backend,
38        user = %username,
39        error = %e,
40        "dispatch: get_api_token returned error from backend client"
41      );
42    }
43    result
44  }
45
46  /// Exchange the caller's CSCS Keycloak token for the token this
47  /// site's backend accepts.
48  ///
49  /// Called once per request by the `RequestCtx` extractor; the
50  /// result replaces the caller's token for every backend, Vault and
51  /// role check made on its behalf.
52  async fn exchange_api_token(
53    &self,
54    subject_token: &str,
55  ) -> Result<String, Error> {
56    let backend = self.backend_kind();
57    tracing::debug!(backend, "dispatch: exchange_api_token");
58    let result = dispatch!(self, exchange_api_token, subject_token);
59    match &result {
60      // A refused token is routine (a user the site does not know, an
61      // ended session): no warning for each one.
62      Err(e @ (Error::Unauthorized(_) | Error::Forbidden(_))) => {
63        tracing::info!(backend, error = %e, "dispatch: token exchange refused");
64      }
65      Err(e) => tracing::warn!(
66        backend,
67        error = %e,
68        "dispatch: exchange_api_token returned error from backend client"
69      ),
70      Ok(_) => {}
71    }
72    result
73  }
74}