From 83fdd59b13d4bf45bd35d9693ae361ff896636ab Mon Sep 17 00:00:00 2001 From: mrw1593 Date: Tue, 6 Jun 2023 19:10:46 -0400 Subject: Add new endpoints for allowed and default scopes --- src/api/clients.rs | 85 +++++++++++++++++++++++++++++++++++++++++++++++ src/services/db/client.rs | 50 ++++++++++++++++++++++++++++ 2 files changed, 135 insertions(+) (limited to 'src') diff --git a/src/api/clients.rs b/src/api/clients.rs index 327a0a5..7b6ec94 100644 --- a/src/api/clients.rs +++ b/src/api/clients.rs @@ -125,6 +125,45 @@ async fn get_client_redirect_uris( Ok(HttpResponse::Ok().json(redirect_uris)) } +#[get("/{client_id}/allowed-scopes")] +async fn get_client_allowed_scopes( + client_id: web::Path, + db: web::Data, +) -> Result { + let db = db.as_ref(); + let id = *client_id; + + let Some(allowed_scopes) = db::get_client_allowed_scopes(db, id).await.unwrap() else { + yeet!(ClientNotFound::new(id)) + }; + + let allowed_scopes = allowed_scopes.split_whitespace().collect::>(); + + Ok(HttpResponse::Ok().json(allowed_scopes)) +} + +#[get("/{client_id}/default-scopes")] +async fn get_client_default_scopes( + client_id: web::Path, + db: web::Data, +) -> Result { + let db = db.as_ref(); + let id = *client_id; + + let Some(default_scopes) = db::get_client_default_scopes(db, id).await.unwrap() else { + yeet!(ClientNotFound::new(id)) + }; + + let default_scopes = default_scopes.map(|scopes| { + scopes + .split_whitespace() + .map(Box::from) + .collect::]>>() + }); + + Ok(HttpResponse::Ok().json(default_scopes)) +} + #[derive(Clone, Deserialize)] #[serde(rename_all = "camelCase")] struct ClientRequest { @@ -289,6 +328,48 @@ async fn update_client_type( Ok(HttpResponse::NoContent().finish()) } +#[put("/{id}/allowed-scopes")] +async fn update_client_allowed_scopes( + id: web::Path, + body: web::Json]>>, + db: web::Data, +) -> Result { + let db = db.get_ref(); + let id = *id; + let allowed_scopes = body.0.join(" "); + + if !db::client_id_exists(db, id).await.unwrap() { + yeet!(ClientNotFound::new(id).into()); + } + + db::update_client_allowed_scopes(db, id, &allowed_scopes) + .await + .unwrap(); + + Ok(HttpResponse::NoContent().finish()) +} + +#[put("/{id}/default-scopes")] +async fn update_client_default_scopes( + id: web::Path, + body: web::Json]>>>, + db: web::Data, +) -> Result { + let db = db.get_ref(); + let id = *id; + let default_scopes = body.0.map(|s| s.join(" ")); + + if !db::client_id_exists(db, id).await.unwrap() { + yeet!(ClientNotFound::new(id).into()); + } + + db::update_client_default_scopes(db, id, default_scopes) + .await + .unwrap(); + + Ok(HttpResponse::NoContent().finish()) +} + #[put("/{id}/redirect-uris")] async fn update_client_redirect_uris( id: web::Path, @@ -338,11 +419,15 @@ pub fn service() -> Scope { .service(get_client) .service(get_client_alias) .service(get_client_type) + .service(get_client_allowed_scopes) + .service(get_client_default_scopes) .service(get_client_redirect_uris) .service(create_client) .service(update_client) .service(update_client_alias) .service(update_client_type) + .service(update_client_allowed_scopes) + .service(update_client_default_scopes) .service(update_client_redirect_uris) .service(update_client_secret) } diff --git a/src/services/db/client.rs b/src/services/db/client.rs index ecf98a3..c25ad0d 100644 --- a/src/services/db/client.rs +++ b/src/services/db/client.rs @@ -94,6 +94,28 @@ pub async fn get_client_type<'c>( Ok(ty) } +pub async fn get_client_allowed_scopes<'c>( + executor: impl Executor<'c, Database = MySql>, + id: Uuid, +) -> Result>, RawUnexpected> { + let scopes = query_scalar!("SELECT allowed_scopes FROM clients WHERE id = ?", id) + .fetch_optional(executor) + .await?; + + Ok(scopes.map(Box::from)) +} + +pub async fn get_client_default_scopes<'c>( + executor: impl Executor<'c, Database = MySql>, + id: Uuid, +) -> Result>>, RawUnexpected> { + let scopes = query_scalar!("SELECT default_scopes FROM clients WHERE id = ?", id) + .fetch_optional(executor) + .await?; + + Ok(scopes.map(|s| s.map(Box::from))) +} + pub async fn get_client_redirect_uris<'c>( executor: impl Executor<'c, Database = MySql>, id: Uuid, @@ -236,6 +258,34 @@ pub async fn update_client_type<'c>( .await } +pub async fn update_client_allowed_scopes<'c>( + executor: impl Executor<'c, Database = MySql>, + id: Uuid, + allowed_scopes: &str, +) -> Result { + query!( + "UPDATE clients SET allowed_scopes = ? WHERE id = ?", + allowed_scopes, + id + ) + .execute(executor) + .await +} + +pub async fn update_client_default_scopes<'c>( + executor: impl Executor<'c, Database = MySql>, + id: Uuid, + default_scopes: Option, +) -> Result { + query!( + "UPDATE clients SET default_scopes = ? WHERE id = ?", + default_scopes, + id + ) + .execute(executor) + .await +} + pub async fn update_client_redirect_uris<'c>( mut transaction: Transaction<'c, MySql>, id: Uuid, -- cgit v1.2.3