Skip to main content

protocol_droid/application/client/
bearer.rs

1//! How a client authenticates to an HTTP server.
2//!
3//! The transports ask this port for a token and tell it when the server
4//! refuses one. They know nothing of OAuth: an authorizer that runs the whole
5//! flow implements it (`application::oauth::authorizer`), and so does a
6//! deployment that holds a static key.
7
8use core::future::Future;
9use core::pin::Pin;
10
11/// A server refused authentication and the client could not recover.
12#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
13#[error("{0}")]
14pub struct BearerError(pub String);
15
16/// A token to send, and a way to get another.
17pub trait BearerAuth: Send + Sync {
18    /// The token to send in `Authorization: Bearer …`, if there is one.
19    fn bearer(&self) -> Option<String>;
20
21    /// The server answered `status` (`401` or `403`) with `www_authenticate`.
22    ///
23    /// `Ok` means a new token is in place and the request should be retried;
24    /// `Err` means it should not be.
25    fn challenged<'a>(
26        &'a self,
27        status: u16,
28        www_authenticate: Option<&'a str>,
29    ) -> Pin<Box<dyn Future<Output = Result<(), BearerError>> + Send + 'a>>;
30
31    /// The last request was accepted, so the count of refusals starts over.
32    fn accepted(&self);
33}