Skip to content

Commit

Permalink
Make admin-token optional (#55)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexheretic authored Nov 10, 2023
1 parent 4917998 commit da1c2a4
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 10 deletions.
2 changes: 2 additions & 0 deletions config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,6 @@ uri = "http://127.0.0.1:3000"
# users and create impersonation tokens. `sudo` is used to fetch all the
# packages the user can access, and the impersonation token is returned
# to the user to download packages
#
# May be omitted if clients are using their own personal access tokens.
admin-token = "personal-access-token"
3 changes: 2 additions & 1 deletion src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ impl FromStr for Config {
#[serde(rename_all = "kebab-case")]
pub struct GitlabConfig {
pub uri: Url,
pub admin_token: String,
/// If absent personal access tokens must be provided.
pub admin_token: Option<String>,
#[serde(default = "GitlabConfig::default_token_expiry")]
pub token_expiry: Duration,
#[serde(default)]
Expand Down
17 changes: 8 additions & 9 deletions src/providers/gitlab.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@ use futures::{stream::FuturesUnordered, StreamExt, TryStreamExt};
use percent_encoding::{utf8_percent_encode, NON_ALPHANUMERIC};
use reqwest::{header, Certificate};
use serde::{Deserialize, Serialize};
use std::{borrow::Cow, sync::Arc};
use std::{borrow::Cow, str::FromStr, sync::Arc};
use time::{Duration, OffsetDateTime};
use tracing::{info_span, instrument, Instrument};
use url::Url;
use std::str::FromStr;

pub struct Gitlab {
client: reqwest::Client,
Expand All @@ -22,13 +21,13 @@ pub struct Gitlab {

impl Gitlab {
pub fn new(config: &GitlabConfig) -> anyhow::Result<Self> {
let mut headers = header::HeaderMap::new();
headers.insert(
"PRIVATE-TOKEN",
header::HeaderValue::from_str(&config.admin_token)?,
);
let mut client_builder = reqwest::ClientBuilder::new();

let mut client_builder = reqwest::ClientBuilder::new().default_headers(headers);
if let Some(token) = &config.admin_token {
let mut headers = header::HeaderMap::new();
headers.insert("PRIVATE-TOKEN", header::HeaderValue::from_str(token)?);
client_builder = client_builder.default_headers(headers);
}

let ssl_cert = match &config.ssl_cert {
Some(cert_path) => {
Expand Down Expand Up @@ -286,7 +285,7 @@ impl super::PackageProvider for Gitlab {
let uri = self.base_url.join(&path.metadata_uri(version))?;
let client = match &do_as.token {
None => self.client.clone(),
Some(token) => self.build_client_with_token("PRIVATE-TOKEN", token)?
Some(token) => self.build_client_with_token("PRIVATE-TOKEN", token)?,
};

Ok(handle_error(client.get(uri).send().await?)
Expand Down

0 comments on commit da1c2a4

Please sign in to comment.