Skip to main content

rig_core/client/
verify.rs

1//! Credential verification reads a 401 or 403 reply as a verdict on the
2//! credential rather than as a provider failure.
3
4use crate::driver::{Model, Transport};
5use crate::error::ProviderError;
6use crate::wire::Wire;
7
8impl<W, T> Model<W, T>
9where
10    W: Wire<Op = crate::operation::Verify>,
11    T: Transport<W>,
12{
13    /// Check that the provider accepts the configured credentials. A 401 or
14    /// 403 reply is [`ProviderError::InvalidAuthentication`].
15    pub async fn verify(&self) -> Result<(), ProviderError> {
16        self.call(()).await.map_err(authentication)
17    }
18}
19
20/// Reclassifies a 401 or 403 reply as [`ProviderError::InvalidAuthentication`],
21/// keeping the reply. Other failures are unchanged.
22pub(crate) fn authentication(error: ProviderError) -> ProviderError {
23    match error {
24        ProviderError::ProviderResponse(response)
25            if matches!(
26                response.status,
27                Some(http::StatusCode::UNAUTHORIZED | http::StatusCode::FORBIDDEN)
28            ) =>
29        {
30            ProviderError::InvalidAuthentication(response)
31        }
32        other => other,
33    }
34}
35
36#[cfg(test)]
37mod tests;