mirror of
https://github.com/actix/actix-extras.git
synced 2024-11-23 23:51:06 +01:00
get_identity from HttpMessage (#908)
* get_identity from HttpMessage * more doc for RequestIdentity
This commit is contained in:
parent
c4b7980b4f
commit
ee769832cf
11
CHANGES.md
11
CHANGES.md
@ -1,5 +1,16 @@
|
|||||||
# Changes
|
# Changes
|
||||||
|
|
||||||
|
## [1.0.x] - 2019-xx-xx
|
||||||
|
|
||||||
|
### Add
|
||||||
|
|
||||||
|
* Add `middleware::identity::RequestIdentity` trait to `get_identity` from `HttpMessage`.
|
||||||
|
|
||||||
|
### Changes
|
||||||
|
|
||||||
|
### Fixed
|
||||||
|
|
||||||
|
|
||||||
## [1.0.0] - 2019-06-05
|
## [1.0.0] - 2019-06-05
|
||||||
|
|
||||||
### Add
|
### Add
|
||||||
|
@ -61,7 +61,10 @@ use crate::cookie::{Cookie, CookieJar, Key, SameSite};
|
|||||||
use crate::error::{Error, Result};
|
use crate::error::{Error, Result};
|
||||||
use crate::http::header::{self, HeaderValue};
|
use crate::http::header::{self, HeaderValue};
|
||||||
use crate::service::{ServiceRequest, ServiceResponse};
|
use crate::service::{ServiceRequest, ServiceResponse};
|
||||||
use crate::{dev::Payload, FromRequest, HttpMessage, HttpRequest};
|
use crate::{
|
||||||
|
dev::{Extensions, Payload},
|
||||||
|
FromRequest, HttpMessage, HttpRequest,
|
||||||
|
};
|
||||||
|
|
||||||
/// The extractor type to obtain your identity from a request.
|
/// The extractor type to obtain your identity from a request.
|
||||||
///
|
///
|
||||||
@ -96,11 +99,7 @@ impl Identity {
|
|||||||
/// Return the claimed identity of the user associated request or
|
/// Return the claimed identity of the user associated request or
|
||||||
/// ``None`` if no identity can be found associated with the request.
|
/// ``None`` if no identity can be found associated with the request.
|
||||||
pub fn identity(&self) -> Option<String> {
|
pub fn identity(&self) -> Option<String> {
|
||||||
if let Some(id) = self.0.extensions().get::<IdentityItem>() {
|
Identity::get_identity(&self.0.extensions())
|
||||||
id.id.clone()
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Remember identity.
|
/// Remember identity.
|
||||||
@ -119,6 +118,14 @@ impl Identity {
|
|||||||
id.changed = true;
|
id.changed = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn get_identity(extensions: &Extensions) -> Option<String> {
|
||||||
|
if let Some(id) = extensions.get::<IdentityItem>() {
|
||||||
|
id.id.clone()
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
struct IdentityItem {
|
struct IdentityItem {
|
||||||
@ -126,6 +133,21 @@ struct IdentityItem {
|
|||||||
changed: bool,
|
changed: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Helper trait that allows to get Identity.
|
||||||
|
/// It could be used in middleware but identity policy must be set before any other middleware that needs identity
|
||||||
|
pub trait RequestIdentity {
|
||||||
|
fn get_identity(&self) -> Option<String>;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T> RequestIdentity for T
|
||||||
|
where
|
||||||
|
T: HttpMessage,
|
||||||
|
{
|
||||||
|
fn get_identity(&self) -> Option<String> {
|
||||||
|
Identity::get_identity(&self.extensions())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Extractor implementation for Identity type.
|
/// Extractor implementation for Identity type.
|
||||||
///
|
///
|
||||||
/// ```rust
|
/// ```rust
|
||||||
|
Loading…
Reference in New Issue
Block a user