mirror of
https://github.com/actix/actix-extras.git
synced 2025-03-20 20:05:18 +01:00
[actix-identity] Fix visit deadline (#263)
This commit is contained in:
parent
1cc37c371e
commit
1089faaf93
@ -1,7 +1,9 @@
|
|||||||
# Changes
|
# Changes
|
||||||
|
|
||||||
## Unreleased - 2022-xx-xx
|
## Unreleased - 2022-xx-xx
|
||||||
|
- Fix visit deadline. [#263]
|
||||||
|
|
||||||
|
[#263]: https://github.com/actix/actix-extras/pull/263
|
||||||
|
|
||||||
## 0.5.1 - 2022-07-11
|
## 0.5.1 - 2022-07-11
|
||||||
- Remove unnecessary dependencies. [#259]
|
- Remove unnecessary dependencies. [#259]
|
||||||
|
@ -152,10 +152,13 @@ impl Identity {
|
|||||||
pub fn login(ext: &Extensions, id: String) -> Result<Self, anyhow::Error> {
|
pub fn login(ext: &Extensions, id: String) -> Result<Self, anyhow::Error> {
|
||||||
let inner = IdentityInner::extract(ext);
|
let inner = IdentityInner::extract(ext);
|
||||||
inner.session.insert(ID_KEY, id)?;
|
inner.session.insert(ID_KEY, id)?;
|
||||||
inner.session.insert(
|
let now = OffsetDateTime::now_utc().unix_timestamp();
|
||||||
LOGIN_UNIX_TIMESTAMP_KEY,
|
if inner.is_login_deadline_enabled {
|
||||||
OffsetDateTime::now_utc().unix_timestamp(),
|
inner.session.insert(LOGIN_UNIX_TIMESTAMP_KEY, now)?;
|
||||||
)?;
|
}
|
||||||
|
if inner.is_visit_deadline_enabled {
|
||||||
|
inner.session.insert(LAST_VISIT_UNIX_TIMESTAMP_KEY, now)?;
|
||||||
|
}
|
||||||
inner.session.renew();
|
inner.session.renew();
|
||||||
Ok(Self(inner))
|
Ok(Self(inner))
|
||||||
}
|
}
|
||||||
@ -220,6 +223,12 @@ impl Identity {
|
|||||||
.transpose()
|
.transpose()
|
||||||
.map_err(anyhow::Error::from)
|
.map_err(anyhow::Error::from)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub(crate) fn set_last_visited_at(&self) -> Result<(), anyhow::Error> {
|
||||||
|
let now = OffsetDateTime::now_utc().unix_timestamp();
|
||||||
|
self.0.session.insert(LAST_VISIT_UNIX_TIMESTAMP_KEY, now)?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Extractor implementation for [`Identity`].
|
/// Extractor implementation for [`Identity`].
|
||||||
|
@ -162,6 +162,14 @@ fn enforce_policies(req: &ServiceRequest, configuration: &Configuration) {
|
|||||||
) {
|
) {
|
||||||
identity.logout();
|
identity.logout();
|
||||||
return;
|
return;
|
||||||
|
} else {
|
||||||
|
if let Err(err) = identity.set_last_visited_at() {
|
||||||
|
tracing::warn!(
|
||||||
|
error.display = %err,
|
||||||
|
error.debug = ?err,
|
||||||
|
"Failed to set the last visited timestamp on `Identity` for an incoming request."
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -147,6 +147,23 @@ async fn login_deadline_does_not_log_users_out_before_their_time() {
|
|||||||
assert_eq!(body.user_id, Some(user_id));
|
assert_eq!(body.user_id, Some(user_id));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[actix_web::test]
|
||||||
|
async fn visit_deadline_does_not_log_users_out_before_their_time() {
|
||||||
|
// 1 hour
|
||||||
|
let visit_deadline = Duration::from_secs(60 * 60);
|
||||||
|
let app = TestApp::spawn_with_config(
|
||||||
|
IdentityMiddleware::builder().visit_deadline(Some(visit_deadline)),
|
||||||
|
);
|
||||||
|
let user_id = user_id();
|
||||||
|
|
||||||
|
// Log-in
|
||||||
|
let body = app.post_login(user_id.clone()).await;
|
||||||
|
assert_eq!(body.user_id, Some(user_id.clone()));
|
||||||
|
|
||||||
|
let body = app.get_current().await;
|
||||||
|
assert_eq!(body.user_id, Some(user_id));
|
||||||
|
}
|
||||||
|
|
||||||
#[actix_web::test]
|
#[actix_web::test]
|
||||||
async fn user_is_logged_out_when_visit_deadline_is_elapsed() {
|
async fn user_is_logged_out_when_visit_deadline_is_elapsed() {
|
||||||
let visit_deadline = Duration::from_millis(10);
|
let visit_deadline = Duration::from_millis(10);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user