From e89cd94f8717ff5bab78691156eb6549555efce0 Mon Sep 17 00:00:00 2001 From: zerj9 <96551236+zerj9@users.noreply.github.com> Date: Mon, 21 Oct 2024 16:04:00 +0100 Subject: [PATCH] feat: add os-token endpoint --- backend/Cargo.toml | 1 + backend/src/routes/mod.rs | 2 ++ backend/src/routes/os_token.rs | 55 ++++++++++++++++++++++++++++++++++ backend/src/server.rs | 5 ++-- 4 files changed, 61 insertions(+), 2 deletions(-) create mode 100644 backend/src/routes/os_token.rs diff --git a/backend/Cargo.toml b/backend/Cargo.toml index 934eabb..dd4df71 100644 --- a/backend/Cargo.toml +++ b/backend/Cargo.toml @@ -18,6 +18,7 @@ martin = { git = "https://github.com/enmeshed-analytics/martin.git", features = martin-tile-utils = { git = "https://github.com/enmeshed-analytics/martin.git" } rand = "0.8.5" rand_core = { version = "0.6", features = ["std"] } +reqwest = { version = "0.12", features = ["json"] } rustls = { version = "0.23.13", features = ["std"] } serde = "1.0" serde_json = "1.0" diff --git a/backend/src/routes/mod.rs b/backend/src/routes/mod.rs index f509db1..85af12a 100644 --- a/backend/src/routes/mod.rs +++ b/backend/src/routes/mod.rs @@ -1,8 +1,10 @@ mod connector; +mod os_token; mod tiles; mod user; mod workspace; pub use connector::*; +pub use os_token::*; pub use user::*; pub use workspace::*; diff --git a/backend/src/routes/os_token.rs b/backend/src/routes/os_token.rs new file mode 100644 index 0000000..105550b --- /dev/null +++ b/backend/src/routes/os_token.rs @@ -0,0 +1,55 @@ +use axum::{http::StatusCode, response::IntoResponse, Json}; +use base64::{engine::general_purpose, Engine as _}; +use reqwest::Client; +use serde_json::json; +use std::env; + +pub async fn generate_os_token() -> impl IntoResponse { + let project_api_key = env::var("OS_PROJECT_API_KEY").ok(); + let project_api_secret = env::var("OS_PROJECT_API_SECRET").ok(); + + if project_api_key.is_none() || project_api_secret.is_none() { + return ( + StatusCode::INTERNAL_SERVER_ERROR, + Json(json!({"message": "API key or secret not configured"})), + ) + .into_response(); + } + + let auth_string = general_purpose::STANDARD.encode(format!( + "{}:{}", + project_api_key.unwrap(), + project_api_secret.unwrap() + )); + + let client = Client::new(); + let res = client + .post("https://api.os.uk/oauth2/token/v1") + .header("Authorization", format!("Basic {}", auth_string)) + .header("Content-Type", "application/x-www-form-urlencoded") + .body("grant_type=client_credentials") + .send() + .await; + + match res { + Ok(response) => { + if response.status().is_success() { + let data = response.json::().await.unwrap(); + (StatusCode::OK, Json(data)).into_response() + } else { + ( + StatusCode::INTERNAL_SERVER_ERROR, + Json(json!({"message": "Error generating token", "error": format!("HTTP error! status: {}", response.status())})) + ).into_response() + } + } + Err(e) => { + eprintln!("Error generating token: {}", e); + ( + StatusCode::INTERNAL_SERVER_ERROR, + Json(json!({"message": "Error generating token", "error": e.to_string()})), + ) + .into_response() + } + } +} diff --git a/backend/src/server.rs b/backend/src/server.rs index 4fe5176..b24a48f 100644 --- a/backend/src/server.rs +++ b/backend/src/server.rs @@ -1,8 +1,8 @@ use crate::app_state::AppState; use crate::auth::auth_middleware; use crate::routes::{ - add_workspace_member, create_connection, create_workspace, health_check, list_sources, login, - logout, profile, register, remove_workspace_member, + add_workspace_member, create_connection, create_workspace, generate_os_token, health_check, + list_sources, login, logout, profile, register, remove_workspace_member, }; use axum::{ middleware, @@ -38,6 +38,7 @@ pub fn create_app(app_state: AppState) -> Router { )) .route("/register", post(register)) .route("/login", post(login)) + .route("/os-token", get(generate_os_token)) .with_state(shared_state) .layer( TraceLayer::new_for_http()