From 838d551554186cd564dcb77ad9d018b54ad18c0f Mon Sep 17 00:00:00 2001 From: Marc-Antoine Arnaud Date: Wed, 21 Aug 2024 10:59:45 +0200 Subject: [PATCH] feat: add api to retrieve registry name and update token configuration instruction issue #18 --- src/application.rs | 9 ++++++++- src/main.rs | 1 + src/model/config.rs | 4 ++-- src/model/mod.rs | 5 +++++ src/routes.rs | 7 ++++++- src/webapp/account.html | 7 +++++-- src/webapp/api.js | 10 ++++++++++ 7 files changed, 37 insertions(+), 6 deletions(-) diff --git a/src/application.rs b/src/application.rs index 4cb1203..0aff73b 100644 --- a/src/application.rs +++ b/src/application.rs @@ -19,7 +19,7 @@ use crate::model::config::Configuration; use crate::model::deps::DepsAnalysis; use crate::model::packages::CrateInfo; use crate::model::stats::{DownloadStats, GlobalStats}; -use crate::model::{CrateAndVersion, JobCrate}; +use crate::model::{CrateAndVersion, JobCrate, RegistryInformation}; use crate::services::database::Database; use crate::services::deps::DepsChecker; use crate::services::docs::DocsGenerator; @@ -150,6 +150,13 @@ impl Application { .await } + /// Gets the registry configuration + pub async fn get_registry_information(&self) -> Result { + Ok(RegistryInformation { + registry_name: self.configuration.self_local_name.to_owned(), + }) + } + /// Gets the data about the current user pub async fn get_current_user(&self, auth_data: &AuthData) -> Result { let mut connection = self.service_db_pool.acquire().await?; diff --git a/src/main.rs b/src/main.rs index d00ca97..a36aba7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -64,6 +64,7 @@ async fn main_serve_app(application: Arc, cookie_key: Key) -> Resul .route("/webapp/*path", get(routes::get_webapp_resource)) // api version .route("/version", get(routes::get_version)) + .route("/registry-information", get(routes::api_v1_get_registry_information)) // special handling for cargo login .route("/me", get(routes::webapp_me)) // serve the documentation diff --git a/src/model/config.rs b/src/model/config.rs index ad57a7c..25c5a7d 100644 --- a/src/model/config.rs +++ b/src/model/config.rs @@ -417,8 +417,8 @@ impl Configuration { oauth_client_secret: get_var("REGISTRY_OAUTH_CLIENT_SECRET")?, oauth_client_scope: get_var("REGISTRY_OAUTH_CLIENT_SCOPE")?, deps_check_period: get_var("REGISTRY_DEPS_CHECK_PERIOD") - .map(|s| s.parse().expect("invalid REGISTRY_DEPS_CHECK_PERIOD")) - .unwrap_or(60), // 1 minute + .map(|s| s.parse().expect("invalid REGISTRY_DEPS_CHECK_PERIOD")) + .unwrap_or(60), // 1 minute deps_stale_registry: get_var("REGISTRY_DEPS_STALE_REGISTRY") .map(|s| s.parse().expect("invalid REGISTRY_DEPS_STALE_REGISTRY")) .unwrap_or(60 * 1000), // 1 minute diff --git a/src/model/mod.rs b/src/model/mod.rs index 85a5e0a..e9a33f7 100644 --- a/src/model/mod.rs +++ b/src/model/mod.rs @@ -27,6 +27,11 @@ pub struct AppVersion { pub tag: String, } +#[derive(Debug, Serialize, Deserialize, Clone)] +pub struct RegistryInformation { + pub registry_name: String, +} + /// Generates a token pub fn generate_token(length: usize) -> String { let rng = thread_rng(); diff --git a/src/routes.rs b/src/routes.rs index 86e7a8f..20adac5 100644 --- a/src/routes.rs +++ b/src/routes.rs @@ -28,7 +28,7 @@ use crate::model::cargo::{ use crate::model::deps::DepsAnalysis; use crate::model::packages::CrateInfo; use crate::model::stats::{DownloadStats, GlobalStats}; -use crate::model::{generate_token, AppVersion, CrateAndVersion}; +use crate::model::{generate_token, AppVersion, CrateAndVersion, RegistryInformation}; use crate::services::index::Index; use crate::utils::apierror::{error_invalid_request, error_not_found, specialize, ApiError}; use crate::utils::axum::auth::{AuthData, AxumStateForCookies}; @@ -260,6 +260,11 @@ fn get_content_type(name: &str) -> &'static str { } } +/// Get server configuration +pub async fn api_v1_get_registry_information(State(state): State>) -> ApiResult { + response(state.application.get_registry_information().await) +} + /// Get the current user pub async fn api_v1_get_current_user(auth_data: AuthData, State(state): State>) -> ApiResult { response(state.application.get_current_user(&auth_data).await) diff --git a/src/webapp/account.html b/src/webapp/account.html index 67830e3..9ab2e60 100644 --- a/src/webapp/account.html +++ b/src/webapp/account.html @@ -200,6 +200,10 @@

Revoke this to table.appendChild(renderToken(token)); } }); + + apiGetRegistryInformation().then((registryInformation) => { + window.localStorage.setItem("cratery-registry-name", registryInformation.registry_name); + }) }); } @@ -289,8 +293,7 @@

Revoke this to function openTokenCreatedModal(secret) { const user = JSON.parse(window.localStorage.getItem("cratery-user")); - const domainParts = window.location.hostname.split("."); - const regName = domainParts.length >= 2 ? domainParts[domainParts.length - 2] : domainParts[0]; + const regName = window.localStorage.getItem("cratery-registry-name"); const gitCredEl = document.getElementById("modal-token-created-git-cred"); gitCredEl.value = `${window.location.protocol}//${user.login}:${secret}@${window.location.hostname}`; const cargoCredEl = document.getElementById("modal-token-created-cargo-cred"); diff --git a/src/webapp/api.js b/src/webapp/api.js index c4d73e9..ccde0b0 100644 --- a/src/webapp/api.js +++ b/src/webapp/api.js @@ -8,6 +8,16 @@ function apiGetVersion() { }); } +function apiGetRegistryInformation() { + return fetch("/registry-information").then((response) => { + if (response.status !== 200) { + throw response.text(); + } else { + return response.json(); + } + }); +} + function apiMe() { return fetch("/api/v1/me").then((response) => { if (response.status !== 200) {