Skip to content

Commit

Permalink
feat: add os-token endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
zerj9 committed Oct 21, 2024
1 parent c1c5038 commit e89cd94
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 2 deletions.
1 change: 1 addition & 0 deletions backend/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 2 additions & 0 deletions backend/src/routes/mod.rs
Original file line number Diff line number Diff line change
@@ -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::*;
55 changes: 55 additions & 0 deletions backend/src/routes/os_token.rs
Original file line number Diff line number Diff line change
@@ -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::<serde_json::Value>().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()
}
}
}
5 changes: 3 additions & 2 deletions backend/src/server.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit e89cd94

Please sign in to comment.