headless_lms_server/controllers/main_frontend/oauth/
authorized_clients.rs

1use crate::prelude::*;
2use actix_web::{HttpResponse, web};
3use models::oauth_user_client_scopes::{AuthorizedClientInfo, OAuthUserClientScopes};
4use sqlx::PgPool;
5use uuid::Uuid;
6
7use serde::{Deserialize, Serialize};
8#[cfg(feature = "ts_rs")]
9use ts_rs::TS;
10
11#[derive(Debug, Clone, Serialize, Deserialize)]
12#[cfg_attr(feature = "ts_rs", derive(TS))]
13pub struct AuthorizedClient {
14    pub client_id: Uuid,     // DB uuid (oauth_clients.id)
15    pub client_name: String, // human-readable name from oauth_clients.client_id
16    pub scopes: Vec<String>,
17}
18
19#[instrument(skip(pool, auth_user))]
20pub async fn get_authorized_clients(
21    pool: web::Data<PgPool>,
22    auth_user: AuthUser,
23) -> ControllerResult<HttpResponse> {
24    let mut conn = pool.acquire().await?;
25    let token = skip_authorize();
26
27    let rows: Vec<AuthorizedClientInfo> =
28        OAuthUserClientScopes::list_authorized_clients_for_user(&mut conn, auth_user.id).await?;
29
30    token.authorized_ok(HttpResponse::Ok().json(rows))
31}
32
33#[instrument(skip(pool, auth_user))]
34pub async fn delete_authorized_client(
35    pool: web::Data<PgPool>,
36    auth_user: AuthUser,
37    path: web::Path<Uuid>, // client_id (DB uuid)
38) -> ControllerResult<HttpResponse> {
39    let client_id = path.into_inner();
40    let mut conn = pool.acquire().await?;
41    let token = skip_authorize();
42
43    OAuthUserClientScopes::revoke_user_client_everything(&mut conn, auth_user.id, client_id)
44        .await?;
45
46    token.authorized_ok(HttpResponse::NoContent().finish())
47}
48
49pub fn _add_routes(cfg: &mut web::ServiceConfig) {
50    cfg.route("/authorized-clients", web::get().to(get_authorized_clients))
51        .route(
52            "/authorized-clients/{client_id}",
53            web::delete().to(delete_authorized_client),
54        );
55}