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]
thiserror = "1"
chrono = "0.4.23"
sqlx = "0.5.13"
url = "2.3.1"
chrono = "0.4"
sqlx = "0.6"
url = "2.3"
serde = "1"
serde_json = "1"

View file

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

View file

@ -17,7 +17,6 @@ pub use user::JwtUser;
#[derive(Debug)]
pub enum Error {
GetDatabase,
GetIpAddress,
GetCookies,
StartTransaction,
GetRefreshToken(refresh_tokens::Error),
@ -31,6 +30,7 @@ pub enum Error {
MarkRefreshTokenUsed(refresh_tokens::Error),
GetSettings(settings::Error),
ServerUrlNotSet,
UnknownIp,
SaveRefreshToken(refresh_tokens::Error),
GetKey(jwt::Error),
MostRecentKeyNotFound,
@ -150,7 +150,6 @@ pub async fn use_refresh_token(
use refresh_tokens::RefreshToken;
use rocket::http::{Cookie, CookieJar, SameSite};
use rocket::time::Duration;
use rocket_client_addr::ClientRealAddr;
use settings::Settings;
use users::User;
@ -161,13 +160,6 @@ pub async fn use_refresh_token(
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
let cookie_jar = match request.guard::<&CookieJar>().await {
Outcome::Success(cookie_jar) => cookie_jar,
@ -272,11 +264,19 @@ pub async fn use_refresh_token(
// Refresh token duration in days
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
if let Err(e) = RefreshToken::insert(
&mut transaction,
new_refresh_token.as_ref(),
ip_address.get_ipv6_string().as_str(),
ip_address,
user.id(),
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
#[main]
async fn main() -> Result<(), Error> {
// Rocket with default settings
let rocket_builder = rocket::build();
// Custom config
// - 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
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::time::Duration;
use rocket::{get, UriDisplayQuery};
use rocket_client_addr::ClientRealAddr;
use settings::Settings;
use std::net::IpAddr;
use users::User;
#[derive(Debug, FromForm, UriDisplayQuery)]
@ -23,7 +23,7 @@ pub async fn redirect_page(
mut db: Connection<Database>,
jwt_user: Option<JwtUser>,
redirect_request: RedirectRequest<'_>,
ip_address: &ClientRealAddr,
ip_address: IpAddr,
cookie_jar: &CookieJar<'_>,
) -> Result<Page> {
let mut transaction = db.begin().await?;
@ -89,7 +89,7 @@ pub async fn redirect_page(
RefreshToken::insert(
&mut transaction,
refresh_token.as_ref(),
ip_address.get_ipv6_string().as_str(),
ip_address.to_string(),
user.id(),
refresh_token_duration,
)

View file

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