3 Commits
Author SHA1 Message Date
MightyPork c5a9767881 update deps, new version 2021-01-24 11:30:28 +01:00
MightyPork 89bebc6b03 change rng to OsRng, version bump 2020-02-09 21:51:16 +01:00
MightyPork 4046e7f185 clean up the dogs example 2019-12-31 20:56:28 +01:00
4 changed files with 18 additions and 27 deletions
+7
View File
@@ -0,0 +1,7 @@
# [0.2.2]
- Update dependencies
# [0.2.1]
- change from `thread_rng` to `OsRng` for better session ID entropy
+4 -4
View File
@@ -1,6 +1,6 @@
[package] [package]
name = "rocket_session" name = "rocket_session"
version = "0.2.0" version = "0.2.2"
authors = ["Ondřej Hruška <ondra@ondrovo.com>"] authors = ["Ondřej Hruška <ondra@ondrovo.com>"]
edition = "2018" edition = "2018"
license = "MIT" license = "MIT"
@@ -16,6 +16,6 @@ categories = [
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
rand = "0.7.2" rand = "0.8"
rocket = "0.4.2" rocket = "0.4"
parking_lot = "0.10.0" parking_lot = "0.11"
+4 -21
View File
@@ -2,7 +2,6 @@
#[macro_use] #[macro_use]
extern crate rocket; extern crate rocket;
use rocket::request::Form;
use rocket::response::content::Html; use rocket::response::content::Html;
use rocket::response::Redirect; use rocket::response::Redirect;
@@ -30,38 +29,23 @@ fn index(session: Session) -> Html<String> {
<ul> <ul>
"#, "#,
); );
session.tap(|sess| { session.tap(|sess| {
for (n, dog) in sess.iter().enumerate() { for (n, dog) in sess.iter().enumerate() {
page.push_str(&format!( page.push_str(&format!(
r#" r#"<li>&#x1F436; {} <a href="/remove/{}">Remove</a></li>"#,
<li>&#x1F436; {} <a href="/remove/{}">Remove</a></li>
"#,
dog, n dog, n
)); ));
} }
}); });
page.push_str("</ul>");
page.push_str(
r#"
</ul>
"#,
);
Html(page) Html(page)
} }
#[derive(FromForm)]
struct AddForm {
name: String,
}
#[post("/add", data = "<dog>")] #[post("/add", data = "<dog>")]
fn add(session: Session, dog: Form<AddForm>) -> Redirect { fn add(session: Session, dog: String) -> Redirect {
session.tap(move |sess| { session.tap(move |sess| {
sess.push(dog.into_inner().name); sess.push(dog);
}); });
Redirect::found("/") Redirect::found("/")
} }
@@ -72,6 +56,5 @@ fn remove(session: Session, dog: usize) -> Redirect {
sess.remove(dog); sess.remove(dog);
} }
}); });
Redirect::found("/") Redirect::found("/")
} }
+3 -2
View File
@@ -1,5 +1,5 @@
use parking_lot::{Mutex, RwLock, RwLockUpgradableReadGuard}; use parking_lot::{Mutex, RwLock, RwLockUpgradableReadGuard};
use rand::Rng; use rand::{Rng, rngs::OsRng};
use rocket::{ use rocket::{
fairing::{self, Fairing, Info}, fairing::{self, Fairing, Info},
@@ -176,9 +176,10 @@ where
// Find a new unique ID - we are still safely inside the write guard // Find a new unique ID - we are still safely inside the write guard
let new_id = SessionID(loop { let new_id = SessionID(loop {
let token: String = rand::thread_rng() let token: String = OsRng
.sample_iter(&rand::distributions::Alphanumeric) .sample_iter(&rand::distributions::Alphanumeric)
.take(store.config.cookie_len) .take(store.config.cookie_len)
.map(char::from)
.collect(); .collect();
if !store_wg.sessions.contains_key(&token) { if !store_wg.sessions.contains_key(&token) {