Skip to content

Commit

Permalink
[actix-identity] Fix visit deadline (#263)
Browse files Browse the repository at this point in the history
  • Loading branch information
Luca Palmieri committed Jul 19, 2022
1 parent 1cc37c3 commit 1089faa
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 5 deletions.
4 changes: 3 additions & 1 deletion actix-identity/CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
# Changes

## Unreleased - 2022-xx-xx

- Fix visit deadline. [#263]

[#263]: https://github.com/actix/actix-extras/pull/263

## 0.5.1 - 2022-07-11
- Remove unnecessary dependencies. [#259]
Expand Down
17 changes: 13 additions & 4 deletions actix-identity/src/identity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,10 +152,13 @@ impl Identity {
pub fn login(ext: &Extensions, id: String) -> Result<Self, anyhow::Error> {
let inner = IdentityInner::extract(ext);
inner.session.insert(ID_KEY, id)?;
inner.session.insert(
LOGIN_UNIX_TIMESTAMP_KEY,
OffsetDateTime::now_utc().unix_timestamp(),
)?;
let now = OffsetDateTime::now_utc().unix_timestamp();
if inner.is_login_deadline_enabled {
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();
Ok(Self(inner))
}
Expand Down Expand Up @@ -220,6 +223,12 @@ impl Identity {
.transpose()
.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`].
Expand Down
8 changes: 8 additions & 0 deletions actix-identity/src/middleware.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,14 @@ fn enforce_policies(req: &ServiceRequest, configuration: &Configuration) {
) {
identity.logout();
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."
);
}
}
}
}
Expand Down
17 changes: 17 additions & 0 deletions actix-identity/tests/integration/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,23 @@ async fn login_deadline_does_not_log_users_out_before_their_time() {
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]
async fn user_is_logged_out_when_visit_deadline_is_elapsed() {
let visit_deadline = Duration::from_millis(10);
Expand Down

0 comments on commit 1089faa

Please sign in to comment.