tide_http_auth/storage.rs
1use std::any::Any;
2use tide::Result;
3
4/// A storage provider. Implementors should pick a concrete `Request` type, representing a
5/// struct or parameter sent by a corresponding [`Scheme`].
6///
7/// # Example
8///
9/// ```no_run
10/// # use async_std::task::block_on;
11/// # fn main() -> Result<(), std::io::Error> { block_on(async {
12/// #
13/// use tide_http_auth::{ Storage, BasicAuthRequest, BasicAuthScheme };
14/// #[derive(Clone)]
15/// struct MyState;
16/// struct MyUserType {
17/// username: String
18/// }
19/// let state = MyState { };
20///
21/// // note that we're implementing the concrete "BasicAuthRequest" type here.
22/// #[async_trait::async_trait]
23/// impl Storage<MyUserType, BasicAuthRequest> for MyState {
24/// async fn get_user(&self, request: BasicAuthRequest) -> tide::Result<Option<MyUserType>> {
25/// if request.username == "Basil" && request.password == "meow time now" {
26/// // If the credential request succeeds, return your user type here.
27/// Ok(Some(MyUserType{username: "Basil".to_string()}))
28/// } else {
29/// Ok(None) // Nothing went wrong, but these credentials are invalid.
30/// // you might also return Err here, to indicate a problem talking to the backing store.
31/// }
32/// }
33/// }
34///
35/// let mut app = tide::with_state(state);
36///
37/// // BasicAuthScheme's ::Request associated type is BasicAuthRequest.
38/// app.with(tide_http_auth::Authentication::new(BasicAuthScheme::default()));
39///
40/// # Ok(()) })}
41/// ```
42#[async_trait::async_trait]
43pub trait Storage<User: Send + Sync + 'static, Request: Any + Send + Sync + 'static> {
44 #[doc(hidden)]
45 async fn get_user(&self, _request: Request) -> Result<Option<User>> {
46 Ok(None)
47 }
48}