Skip to main content

useceleris_server/
credential_provider.rs

1use std::future::Future;
2use std::sync::Arc;
3
4use useceleris_client::{CredentialProvider, CredentialRequest, ProviderError};
5
6use crate::claims::Claims;
7use crate::signer::Signer;
8
9/// Makes a [`CredentialProvider`] for a trusted server that connects to
10/// realtime itself.
11///
12/// For every connection attempt, the provider calls `claims` again and signs
13/// the result with a new timestamp. The claims are authoritative: nothing in
14/// the request, which the client makes, can make them wider. An error from
15/// `claims` is returned unchanged, and the client reports it as
16/// [`CelerisError::Transport`](useceleris_client::CelerisError::Transport).
17/// Invalid claims give the error of [`Signer::sign`].
18///
19/// When the client drops the provider's future, for example because the
20/// channel closed, nothing is signed.
21pub fn credential_provider<ClaimsFor, Decided>(
22    signer: Signer,
23    claims: ClaimsFor,
24) -> CredentialProvider
25where
26    ClaimsFor: Fn(CredentialRequest) -> Decided + Send + Sync + 'static,
27    Decided: Future<Output = Result<Claims, ProviderError>> + Send + 'static,
28{
29    let signer = Arc::new(signer);
30
31    CredentialProvider::new(move |request| {
32        let decided = claims(request);
33        let signer = Arc::clone(&signer);
34
35        async move {
36            let claims = decided.await?;
37
38            Ok(signer.sign(&claims)?)
39        }
40    })
41} // end fn credential_provider