1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
pub(crate) mod bearer;
pub(crate) mod cookie;
pub(crate) mod jwt;
pub(crate) mod oauth;
use std::future::Future;
use super::{Context, claim::Claim};
pub trait Handler<Request> {
type Error;
/// The name of the handler
/// This is used to identify the handler in logs and other contexts
const NAME: &'static str;
/// Authenticate the current request
///
/// This method is called to authenticate the current request
fn authenticate(&self, contex: &Context<Request>, claim: &Claim) -> impl Future<Output = Result<(), Self::Error>> + Send;
/// Forbid the current request
///
/// This method is called to forbid the current request
///
/// # Arguments
/// `state` - The current state of the request `S`
fn forbid(&self, contex: &Context<Request>, claim: &Claim) -> impl Future<Output = Result<(), Self::Error>> + Send;
/// Challenge the current request
///
/// This method is called to challenge the current request
///
/// # Arguments
/// `state` - The current state of the request `Self::State`
fn challenge(&self, contex: &Context<Request>, claim: &Claim) -> impl Future<Output = Result<(), Self::Error>> + Send;
}
pub mod handler {
use crate::authentication::{Context, claim::Claim};
pub struct Handler<H> {
handler: H,
}
impl<H> Handler<H> {
async fn sign_out<Request>(&self, req: &Context<Request>, claim: &Claim) -> Result<(), H::Error>
where
H: super::Handler<Request>,
{
// Delegate the Request to the handler
todo!()
}
}
}
pub mod sign_in {
use super::sign_out::SignOutHandler;
use crate::authentication::{Context, claim::Claim};
use std::future::Future;
pub trait SignInHandler<Request>: SignOutHandler<Request> {
type Success;
fn sign_in(&self, contex: &Context<Request>, claim: &Claim) -> impl Future<Output = Result<Self::Success, Self::Error>> + Send;
}
/*
We want the api to be like
// Sign in can create it self from request part via state a state populated when the application starts and shared among the request
// SignIn works with any T as long as T::Option is in State which would be used to create T
async fn get_users(sign_in_ctx: SignIn<Cookie>, claim: Context<Parts>) -> Response {
let claim = Claim::default();
let res = sign_in_ctx.sign_in(context, claim).await?;
Ok(res)
}
*/
#[derive(Clone)]
pub struct SignIn<H> {
handler: H,
// Other Stuff goes in here
}
impl<H> SignIn<H> {
async fn sign_in<Request>(&self, req: &Context<Request>, claim: &Claim) -> Result<H::Success, H::Error>
where
H: SignInHandler<Request>,
{
// Delegate the Request to the handler
todo!()
}
}
}
//
pub mod sign_out {
use crate::authentication::{Context, claim::Claim, handler::Handler};
use std::future::Future;
pub trait SignOutHandler<Request>: Handler<Request> {
fn sign_out(&self, ctx: Context<Request>, claim: &Claim) -> impl Future<Output = Result<(), Self::Error>> + Send;
}
#[derive(Clone)]
pub struct SignOut<H> {
handler: H,
// Other Stuff goes in here
}
impl<H> SignOut<H> {
async fn sign_out<Request>(&self, req: &Context<Request>, claim: &Claim) -> Result<(), H::Error>
where
H: SignOutHandler<Request>,
{
// Delegate the Request to the handler
todo!()
}
}
}