ezidam: mount all routes in "/", and use functions directly when redirecting

This commit is contained in:
Philippe Loctaux 2023-03-12 21:28:19 +01:00
parent 36fb1cff52
commit 3d065bbc22
5 changed files with 20 additions and 22 deletions

View file

@ -33,7 +33,9 @@ pub fn routes(rocket_builder: Rocket<Build>) -> Rocket<Build> {
// Root // Root
.mount("/", root::routes()) .mount("/", root::routes())
// Setup // Setup
.mount("/setup", setup::routes()) .mount("/", setup::routes())
.mount("/auth", auth::routes()) // Oauth
.mount("/.well-known", well_known::routes()) .mount("/", oauth::routes())
// Well known
.mount("/", well_known::routes())
} }

View file

@ -82,7 +82,7 @@ pub mod content {
#[get("/")] #[get("/")]
async fn redirect_to_setup(_setup: NeedSetup) -> Redirect { async fn redirect_to_setup(_setup: NeedSetup) -> Redirect {
Redirect::to(uri!("/setup")) Redirect::to(uri!(super::setup::setup))
} }
#[get("/", rank = 2)] #[get("/", rank = 2)]

View file

@ -8,12 +8,12 @@ pub fn routes() -> Vec<Route> {
routes![setup_completed, setup, create_first_account] routes![setup_completed, setup, create_first_account]
} }
#[get("/")] #[get("/setup")]
async fn setup_completed(_setup: CompletedSetup) -> Redirect { async fn setup_completed(_setup: CompletedSetup) -> Redirect {
Redirect::to(uri!("/")) Redirect::to(uri!(super::root::homepage))
} }
#[get("/", rank = 2)] #[get("/setup", rank = 2)]
async fn setup(flash: Option<FlashMessage<'_>>) -> Template { async fn setup(flash: Option<FlashMessage<'_>>) -> Template {
flash flash
.map(|flash| Page::with_flash(Page::Setup, flash)) .map(|flash| Page::with_flash(Page::Setup, flash))
@ -27,7 +27,7 @@ struct CreateFirstAccount<'r> {
pub url: &'r str, pub url: &'r str,
} }
#[post("/", data = "<form>")] #[post("/setup", data = "<form>")]
async fn create_first_account( async fn create_first_account(
form: Form<CreateFirstAccount<'_>>, form: Form<CreateFirstAccount<'_>>,
_setup: NeedSetup, _setup: NeedSetup,
@ -40,7 +40,7 @@ async fn create_first_account(
Ok(url) => url, Ok(url) => url,
Err(e) => { Err(e) => {
return Ok(Either::Right(Flash::new( return Ok(Either::Right(Flash::new(
Redirect::to(uri!("/setup")), Redirect::to(uri!(setup)),
FlashKind::Danger, FlashKind::Danger,
e.to_string(), e.to_string(),
))); )));
@ -76,7 +76,7 @@ async fn create_first_account(
// TODO: login with openid/oauth // TODO: login with openid/oauth
Ok(Either::Left(Redirect::to(uri!("/")))) Ok(Either::Left(Redirect::to(uri!(super::root::homepage))))
} }
#[cfg(test)] #[cfg(test)]
@ -89,7 +89,7 @@ mod test {
let client = setup_rocket_testing(); let client = setup_rocket_testing();
// Make request // Make request
let setup_page = client.get(uri!("/setup")).dispatch(); let setup_page = client.get(uri!(super::setup)).dispatch();
assert_eq!(setup_page.status(), Status::Ok); assert_eq!(setup_page.status(), Status::Ok);
// Create account // Create account
@ -97,7 +97,7 @@ mod test {
assert_eq!(create_account.status(), Status::SeeOther); assert_eq!(create_account.status(), Status::SeeOther);
// Make request again, make sure its not OK // Make request again, make sure its not OK
let setup_page_after_creation = client.get(uri!("/setup")).dispatch(); let setup_page_after_creation = client.get(uri!(super::setup)).dispatch();
assert_ne!(setup_page_after_creation.status(), Status::Ok); assert_ne!(setup_page_after_creation.status(), Status::Ok);
} }
} }

View file

@ -10,7 +10,7 @@ pub fn routes() -> Vec<Route> {
routes![openid_configuration, json_web_keys] routes![openid_configuration, json_web_keys]
} }
#[get("/openid-configuration")] #[get("/.well-known/openid-configuration")]
async fn openid_configuration(mut db: Connection<Database>) -> Result<Json<Value>> { async fn openid_configuration(mut db: Connection<Database>) -> Result<Json<Value>> {
// Get settings // Get settings
let settings = Settings::get(&mut *db).await?; let settings = Settings::get(&mut *db).await?;
@ -27,7 +27,7 @@ async fn openid_configuration(mut db: Connection<Database>) -> Result<Json<Value
Ok(Json(openid_configuration)) Ok(Json(openid_configuration))
} }
#[get("/jwks.json")] #[get("/.well-known/jwks.json")]
async fn json_web_keys(mut db: Connection<Database>) -> Result<Json<Vec<Value>>> { async fn json_web_keys(mut db: Connection<Database>) -> Result<Json<Vec<Value>>> {
// Get keys // Get keys
let keys = Key::get_all(&mut *db, Some(false)).await?; let keys = Key::get_all(&mut *db, Some(false)).await?;
@ -70,9 +70,7 @@ mod test {
run_setup(&client); run_setup(&client);
// Make request // Make request
let response = client let response = client.get(uri!(super::openid_configuration)).dispatch();
.get(uri!("/.well-known/openid-configuration"))
.dispatch();
assert_eq!(response.status(), Status::Ok); assert_eq!(response.status(), Status::Ok);
// Make sure it is valid json // Make sure it is valid json
@ -85,9 +83,7 @@ mod test {
let client = setup_rocket_testing(); let client = setup_rocket_testing();
// Make request // Make request
let response = client let response = client.get(uri!(super::openid_configuration)).dispatch();
.get(uri!("/.well-known/openid-configuration"))
.dispatch();
assert_ne!(response.status(), Status::Ok); assert_ne!(response.status(), Status::Ok);
} }
@ -97,7 +93,7 @@ mod test {
let client = setup_rocket_testing(); let client = setup_rocket_testing();
// Make request // Make request
let response = client.get(uri!("/.well-known/jwks.json")).dispatch(); let response = client.get(uri!(super::json_web_keys)).dispatch();
assert_eq!(response.status(), Status::Ok); assert_eq!(response.status(), Status::Ok);
// Make sure it is valid json // Make sure it is valid json

View file

@ -29,7 +29,7 @@ pub fn setup_rocket_testing() -> Client {
pub fn run_setup(client: &Client) -> LocalResponse { pub fn run_setup(client: &Client) -> LocalResponse {
client client
.post(uri!("/setup")) .post(uri!(crate::routes::setup::setup))
.header(ContentType::Form) .header(ContentType::Form)
.body(r#"username=phil&password=password&url=https://example.com"#) .body(r#"username=phil&password=password&url=https://example.com"#)
.dispatch() .dispatch()