actix / actix-extras
1
use actix_web::{
2
    dev::{Extensions, Payload},
3
    Error, FromRequest, HttpRequest,
4
};
5
use futures_util::future::{ready, Ready};
6

7
pub(crate) struct IdentityItem {
8
    pub(crate) id: Option<String>,
9
    pub(crate) changed: bool,
10
}
11

12
/// The extractor type to obtain your identity from a request.
13
///
14
/// ```
15
/// use actix_web::*;
16
/// use actix_identity::Identity;
17
///
18
/// #[get("/")]
19
/// async fn index(id: Identity) -> impl Responder {
20
///     // access request identity
21
///     if let Some(id) = id.identity() {
22
///         format!("Welcome! {}", id)
23
///     } else {
24
///         "Welcome Anonymous!".to_owned()
25
///     }
26
/// }
27
///
28
/// #[post("/login")]
29
/// async fn login(id: Identity) -> impl Responder {
30
///     // remember identity
31
///     id.remember("User1".to_owned());
32
///
33
///     HttpResponse::Ok()
34
/// }
35
///
36
/// #[post("/logout")]
37
/// async fn logout(id: Identity) -> impl Responder {
38
///     // remove identity
39
///     id.forget();
40
///
41
///     HttpResponse::Ok()
42
/// }
43
/// ```
44
#[derive(Clone)]
45
pub struct Identity(HttpRequest);
46

47
impl Identity {
48
    /// Return the claimed identity of the user associated request or `None` if no identity can be
49
    /// found associated with the request.
50 1
    pub fn identity(&self) -> Option<String> {
51 1
        Identity::get_identity(&self.0.extensions())
52
    }
53

54
    /// Remember identity.
55 1
    pub fn remember(&self, identity: String) {
56 1
        if let Some(id) = self.0.extensions_mut().get_mut::<IdentityItem>() {
57 1
            id.id = Some(identity);
58 1
            id.changed = true;
59
        }
60
    }
61

62
    /// This method is used to 'forget' the current identity on subsequent requests.
63 1
    pub fn forget(&self) {
64 1
        if let Some(id) = self.0.extensions_mut().get_mut::<IdentityItem>() {
65 1
            id.id = None;
66 1
            id.changed = true;
67
        }
68
    }
69

70 1
    pub(crate) fn get_identity(extensions: &Extensions) -> Option<String> {
71 1
        let id = extensions.get::<IdentityItem>()?;
72 1
        id.id.clone()
73
    }
74
}
75

76
/// Extractor implementation for Identity type.
77
///
78
/// ```
79
/// # use actix_web::*;
80
/// use actix_identity::Identity;
81
///
82
/// #[get("/")]
83
/// async fn index(id: Identity) -> impl Responder {
84
///     // access request identity
85
///     if let Some(id) = id.identity() {
86
///         format!("Welcome! {}", id)
87
///     } else {
88
///         "Welcome Anonymous!".to_owned()
89
///     }
90
/// }
91
/// ```
92
impl FromRequest for Identity {
93
    type Config = ();
94
    type Error = Error;
95
    type Future = Ready<Result<Identity, Error>>;
96

97
    #[inline]
98 1
    fn from_request(req: &HttpRequest, _: &mut Payload) -> Self::Future {
99 1
        ready(Ok(Identity(req.clone())))
100
    }
101
}

Read our documentation on viewing source code .

Loading