upgraded to rocket v0.5-rc3

This commit is contained in:
Philippe Loctaux 2023-03-24 22:56:01 +01:00
parent eab918d643
commit 7f4862ff30
7 changed files with 239 additions and 434 deletions

619
Cargo.lock generated

File diff suppressed because it is too large Load diff

View file

@ -6,8 +6,8 @@ members = [
[workspace.dependencies] [workspace.dependencies]
thiserror = "1" thiserror = "1"
chrono = "0.4.23" chrono = "0.4"
sqlx = "0.5.13" sqlx = "0.6"
url = "2.3.1" url = "2.3"
serde = "1" serde = "1"
serde_json = "1" serde_json = "1"

View file

@ -4,15 +4,14 @@ version = "0.1.0"
edition = "2021" edition = "2021"
[dependencies] [dependencies]
rocket = { version = "0.5.0-rc.2", features = ["json"] } rocket = { version = "=0.5.0-rc.3", features = ["json"] }
rocket_db_pools = { version = "0.1.0-rc.2", features = ["sqlx_sqlite"] } rocket_db_pools = { version = "=0.1.0-rc.3", features = ["sqlx_sqlite"] }
rocket_dyn_templates = { version = "0.1.0-rc.2", features = ["tera"] } rocket_dyn_templates = { version = "=0.1.0-rc.3", features = ["tera"] }
infer = { version = "0.12.0", default-features = false } infer = { version = "0.12.0", default-features = false }
erased-serde = "0.3" erased-serde = "0.3"
url = { workspace = true } url = { workspace = true }
identicon-rs = "4.0.1" identicon-rs = "4.0.1"
futures = "0.3.26" futures = "0.3.26"
rocket-client-addr = "0.5.2"
# local crates # local crates
database_pool = { path = "../database_pool" } database_pool = { path = "../database_pool" }

View file

@ -17,7 +17,6 @@ pub use user::JwtUser;
#[derive(Debug)] #[derive(Debug)]
pub enum Error { pub enum Error {
GetDatabase, GetDatabase,
GetIpAddress,
GetCookies, GetCookies,
StartTransaction, StartTransaction,
GetRefreshToken(refresh_tokens::Error), GetRefreshToken(refresh_tokens::Error),
@ -31,6 +30,7 @@ pub enum Error {
MarkRefreshTokenUsed(refresh_tokens::Error), MarkRefreshTokenUsed(refresh_tokens::Error),
GetSettings(settings::Error), GetSettings(settings::Error),
ServerUrlNotSet, ServerUrlNotSet,
UnknownIp,
SaveRefreshToken(refresh_tokens::Error), SaveRefreshToken(refresh_tokens::Error),
GetKey(jwt::Error), GetKey(jwt::Error),
MostRecentKeyNotFound, MostRecentKeyNotFound,
@ -150,7 +150,6 @@ pub async fn use_refresh_token(
use refresh_tokens::RefreshToken; use refresh_tokens::RefreshToken;
use rocket::http::{Cookie, CookieJar, SameSite}; use rocket::http::{Cookie, CookieJar, SameSite};
use rocket::time::Duration; use rocket::time::Duration;
use rocket_client_addr::ClientRealAddr;
use settings::Settings; use settings::Settings;
use users::User; use users::User;
@ -161,13 +160,6 @@ pub async fn use_refresh_token(
Outcome::Forward(f) => return Outcome::Forward(f), Outcome::Forward(f) => return Outcome::Forward(f),
}; };
// Get ip address
let ip_address = match request.guard::<&ClientRealAddr>().await {
Outcome::Success(ip_address) => ip_address,
Outcome::Failure(e) => return Outcome::Failure((e.0, Error::GetIpAddress)),
Outcome::Forward(f) => return Outcome::Forward(f),
};
// Get cookies // Get cookies
let cookie_jar = match request.guard::<&CookieJar>().await { let cookie_jar = match request.guard::<&CookieJar>().await {
Outcome::Success(cookie_jar) => cookie_jar, Outcome::Success(cookie_jar) => cookie_jar,
@ -272,11 +264,19 @@ pub async fn use_refresh_token(
// Refresh token duration in days // Refresh token duration in days
let refresh_token_duration = 21; let refresh_token_duration = 21;
// Attempt to get ip address
let ip_address = match request.client_ip() {
Some(ip) => ip.to_string(),
None => {
return Outcome::Failure((Status::BadRequest, Error::UnknownIp));
}
};
// Insert refresh token in database // Insert refresh token in database
if let Err(e) = RefreshToken::insert( if let Err(e) = RefreshToken::insert(
&mut transaction, &mut transaction,
new_refresh_token.as_ref(), new_refresh_token.as_ref(),
ip_address.get_ipv6_string().as_str(), ip_address,
user.id(), user.id(),
refresh_token_duration, refresh_token_duration,
) )

View file

@ -4,8 +4,15 @@ use rocket::{main, Error};
// see for using rocket with main function https://github.com/intellij-rust/intellij-rust/issues/5975#issuecomment-920620289 // see for using rocket with main function https://github.com/intellij-rust/intellij-rust/issues/5975#issuecomment-920620289
#[main] #[main]
async fn main() -> Result<(), Error> { async fn main() -> Result<(), Error> {
// Rocket with default settings // Custom config
let rocket_builder = rocket::build(); // - rocket defaults
// - from `Rocket.toml`
// - from env variables
// - from code below
let config = rocket::Config::figment().merge(("ip_header", "x-forwarded-for"));
// Rocket with custom config
let rocket_builder = rocket::custom(config);
// Setup server // Setup server
let rocket_builder = rocket_setup(rocket_builder); let rocket_builder = rocket_setup(rocket_builder);

View file

@ -8,8 +8,8 @@ use refresh_tokens::RefreshToken;
use rocket::http::{Cookie, CookieJar, SameSite}; use rocket::http::{Cookie, CookieJar, SameSite};
use rocket::time::Duration; use rocket::time::Duration;
use rocket::{get, UriDisplayQuery}; use rocket::{get, UriDisplayQuery};
use rocket_client_addr::ClientRealAddr;
use settings::Settings; use settings::Settings;
use std::net::IpAddr;
use users::User; use users::User;
#[derive(Debug, FromForm, UriDisplayQuery)] #[derive(Debug, FromForm, UriDisplayQuery)]
@ -23,7 +23,7 @@ pub async fn redirect_page(
mut db: Connection<Database>, mut db: Connection<Database>,
jwt_user: Option<JwtUser>, jwt_user: Option<JwtUser>,
redirect_request: RedirectRequest<'_>, redirect_request: RedirectRequest<'_>,
ip_address: &ClientRealAddr, ip_address: IpAddr,
cookie_jar: &CookieJar<'_>, cookie_jar: &CookieJar<'_>,
) -> Result<Page> { ) -> Result<Page> {
let mut transaction = db.begin().await?; let mut transaction = db.begin().await?;
@ -89,7 +89,7 @@ pub async fn redirect_page(
RefreshToken::insert( RefreshToken::insert(
&mut transaction, &mut transaction,
refresh_token.as_ref(), refresh_token.as_ref(),
ip_address.get_ipv6_string().as_str(), ip_address.to_string(),
user.id(), user.id(),
refresh_token_duration, refresh_token_duration,
) )

View file

@ -26,7 +26,7 @@ impl RefreshToken {
pub async fn insert( pub async fn insert(
conn: impl SqliteExecutor<'_>, conn: impl SqliteExecutor<'_>,
token: &str, token: &str,
ip_address: &str, ip_address: String,
user: &UserID, user: &UserID,
duration_days: i64, duration_days: i64,
) -> Result<Option<()>, Error> { ) -> Result<Option<()>, Error> {
@ -35,7 +35,7 @@ impl RefreshToken {
Ok(DatabaseRefreshTokens::insert( Ok(DatabaseRefreshTokens::insert(
conn, conn,
token, token,
ip_address, ip_address.as_str(),
user.as_ref(), user.as_ref(),
expires_at.timestamp(), expires_at.timestamp(),
) )