Skip to content

Commit

Permalink
feat: add api to retrieve registry name and update token configuratio…
Browse files Browse the repository at this point in the history
…n instruction

issue #18
  • Loading branch information
MarcAntoine-Arnaud authored and woutersl committed Aug 27, 2024
1 parent 1b8431c commit 838d551
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 6 deletions.
9 changes: 8 additions & 1 deletion src/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -150,6 +150,13 @@ impl Application {
.await
}

/// Gets the registry configuration
pub async fn get_registry_information(&self) -> Result<RegistryInformation, ApiError> {
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<RegistryUser, ApiError> {
let mut connection = self.service_db_pool.acquire().await?;
Expand Down
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ async fn main_serve_app(application: Arc<Application>, 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
Expand Down
4 changes: 2 additions & 2 deletions src/model/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions src/model/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
7 changes: 6 additions & 1 deletion src/routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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<Arc<AxumState>>) -> ApiResult<RegistryInformation> {
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<Arc<AxumState>>) -> ApiResult<RegistryUser> {
response(state.application.get_current_user(&auth_data).await)
Expand Down
7 changes: 5 additions & 2 deletions src/webapp/account.html
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,10 @@ <h3 class="mb-3 text-2xl font-bold text-gray-900 dark:text-white">Revoke this to
table.appendChild(renderToken(token));
}
});

apiGetRegistryInformation().then((registryInformation) => {
window.localStorage.setItem("cratery-registry-name", registryInformation.registry_name);
})
});
}

Expand Down Expand Up @@ -289,8 +293,7 @@ <h3 class="mb-3 text-2xl font-bold text-gray-900 dark:text-white">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");
Expand Down
10 changes: 10 additions & 0 deletions src/webapp/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 838d551

Please sign in to comment.