From 1dec56ed14b60a3d774261399238f63327108bfb Mon Sep 17 00:00:00 2001
From: Philippe Loctaux
Date: Sun, 19 Mar 2023 22:52:22 +0100
Subject: [PATCH] ezidam: authorize: don't prompt login if already
authenticated
---
crates/ezidam/src/guards/jwt/admin.rs | 1 +
crates/ezidam/src/guards/jwt/user.rs | 4 +-
crates/ezidam/src/routes/oauth.rs | 2 +
crates/ezidam/src/routes/oauth/authorize.rs | 83 ++++++++++++-------
.../templates/pages/oauth/authorize.html.tera | 20 ++++-
5 files changed, 76 insertions(+), 34 deletions(-)
diff --git a/crates/ezidam/src/guards/jwt/admin.rs b/crates/ezidam/src/guards/jwt/admin.rs
index d5e34c9..d9bb62a 100644
--- a/crates/ezidam/src/guards/jwt/admin.rs
+++ b/crates/ezidam/src/guards/jwt/admin.rs
@@ -4,6 +4,7 @@ use jwt::JwtClaims;
use rocket::request::{FromRequest, Outcome};
use rocket::Request;
+#[derive(Debug)]
pub struct JwtAdmin(pub JwtClaims);
#[rocket::async_trait]
diff --git a/crates/ezidam/src/guards/jwt/user.rs b/crates/ezidam/src/guards/jwt/user.rs
index 685473a..5172afa 100644
--- a/crates/ezidam/src/guards/jwt/user.rs
+++ b/crates/ezidam/src/guards/jwt/user.rs
@@ -1,13 +1,15 @@
use super::use_access_token_or_refresh_token;
+use super::Error;
use jwt::JwtClaims;
use rocket::request::{FromRequest, Outcome};
use rocket::Request;
+#[derive(Debug)]
pub struct JwtUser(pub JwtClaims);
#[rocket::async_trait]
impl<'r> FromRequest<'r> for JwtUser {
- type Error = super::Error;
+ type Error = Error;
async fn from_request(request: &'r Request<'_>) -> Outcome {
let get_admin: Option = None;
diff --git a/crates/ezidam/src/routes/oauth.rs b/crates/ezidam/src/routes/oauth.rs
index d0398ca..8cc7f2b 100644
--- a/crates/ezidam/src/routes/oauth.rs
+++ b/crates/ezidam/src/routes/oauth.rs
@@ -15,6 +15,7 @@ pub fn routes() -> Vec {
}
pub mod content {
+ use jwt::JwtClaims;
use rocket::serde::Serialize;
#[derive(Serialize)]
@@ -23,6 +24,7 @@ pub mod content {
pub struct Authorize {
pub app_name: String,
pub business_name: String,
+ pub user: Option,
}
#[derive(Serialize)]
diff --git a/crates/ezidam/src/routes/oauth/authorize.rs b/crates/ezidam/src/routes/oauth/authorize.rs
index 171be92..d030ecf 100644
--- a/crates/ezidam/src/routes/oauth/authorize.rs
+++ b/crates/ezidam/src/routes/oauth/authorize.rs
@@ -6,11 +6,10 @@ use rocket::{get, post};
use settings::Settings;
use users::User;
-// TODO: When already signed in, pass Result in existing routes directly
-
-#[get("/oauth/authorize?", rank = 2)]
+#[get("/oauth/authorize?", rank = 1)]
pub async fn authorize_page(
mut db: Connection,
+ user: Option,
flash: Option>,
auth_request: AuthenticationRequest<'_>,
) -> Result {
@@ -34,6 +33,7 @@ pub async fn authorize_page(
let content = super::content::Authorize {
app_name: app.label().into(),
business_name: settings.business_name().into(),
+ user: user.map(|user| user.0),
};
Ok(flash
@@ -41,7 +41,7 @@ pub async fn authorize_page(
.unwrap_or_else(|| Page::Authorize(content).into()))
}
-#[get("/oauth/authorize", rank = 3)]
+#[get("/oauth/authorize", rank = 2)]
pub async fn authorize_ezidam(mut db: Connection) -> Result {
let mut transaction = db.begin().await?;
@@ -92,7 +92,8 @@ fn user_archived(login: &str, request: AuthenticationRequest) -> Flash
#[post("/oauth/authorize?", data = "