spiral-auth 0.1.10

Spiral credentials: bearer tokens and the providers that obtain them
Documentation
//! The rustls crypto provider for credential requests and private-key signing.
//!
//! An application that installs a process-default provider chooses the crypto. Otherwise this
//! crate installs the one its `aws-lc` or `ring` feature selects. Without either, building an
//! HTTP client panics until the application installs a provider.

#[cfg(any(feature = "private-key-auth", feature = "browser-auth"))]
use std::sync::Arc;

use rustls::crypto::CryptoProvider;
#[cfg(feature = "aws-lc")]
use rustls::crypto::aws_lc_rs;
#[cfg(all(feature = "ring", not(feature = "aws-lc")))]
use rustls::crypto::ring;

#[cfg(any(feature = "private-key-auth", feature = "browser-auth"))]
use crate::{CredentialError, CredentialResult};

/// Install the rustls crypto provider this build selects, unless the process already has one.
///
/// Call it before building a reqwest client with `rustls-no-provider`.
pub fn install_default_crypto_provider() {
    if CryptoProvider::get_default().is_some() {
        return;
    }
    // Another thread may install a provider first; either is then the process default.
    #[cfg(feature = "aws-lc")]
    drop(aws_lc_rs::default_provider().install_default());
    #[cfg(all(feature = "ring", not(feature = "aws-lc")))]
    drop(ring::default_provider().install_default());
}

/// The process's rustls crypto provider, installing this crate's when the application chose none.
#[cfg(any(feature = "private-key-auth", feature = "browser-auth"))]
pub(crate) fn crypto_provider() -> CredentialResult<Arc<CryptoProvider>> {
    install_default_crypto_provider();
    CryptoProvider::get_default().cloned().ok_or_else(|| {
        CredentialError::invalid(
            "no rustls crypto provider: enable the `aws-lc` or `ring` feature, or install a \
             process-default provider first",
        )
    })
}