Skip to content

Commit da1c2a4

Browse files
authored
Make admin-token optional (#55)
1 parent 4917998 commit da1c2a4

File tree

3 files changed

+12
-10
lines changed

3 files changed

+12
-10
lines changed

config.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,6 @@ uri = "http://127.0.0.1:3000"
1313
# users and create impersonation tokens. `sudo` is used to fetch all the
1414
# packages the user can access, and the impersonation token is returned
1515
# to the user to download packages
16+
#
17+
# May be omitted if clients are using their own personal access tokens.
1618
admin-token = "personal-access-token"

src/config.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ impl FromStr for Config {
3333
#[serde(rename_all = "kebab-case")]
3434
pub struct GitlabConfig {
3535
pub uri: Url,
36-
pub admin_token: String,
36+
/// If absent personal access tokens must be provided.
37+
pub admin_token: Option<String>,
3738
#[serde(default = "GitlabConfig::default_token_expiry")]
3839
pub token_expiry: Duration,
3940
#[serde(default)]

src/providers/gitlab.rs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,10 @@ use futures::{stream::FuturesUnordered, StreamExt, TryStreamExt};
77
use percent_encoding::{utf8_percent_encode, NON_ALPHANUMERIC};
88
use reqwest::{header, Certificate};
99
use serde::{Deserialize, Serialize};
10-
use std::{borrow::Cow, sync::Arc};
10+
use std::{borrow::Cow, str::FromStr, sync::Arc};
1111
use time::{Duration, OffsetDateTime};
1212
use tracing::{info_span, instrument, Instrument};
1313
use url::Url;
14-
use std::str::FromStr;
1514

1615
pub struct Gitlab {
1716
client: reqwest::Client,
@@ -22,13 +21,13 @@ pub struct Gitlab {
2221

2322
impl Gitlab {
2423
pub fn new(config: &GitlabConfig) -> anyhow::Result<Self> {
25-
let mut headers = header::HeaderMap::new();
26-
headers.insert(
27-
"PRIVATE-TOKEN",
28-
header::HeaderValue::from_str(&config.admin_token)?,
29-
);
24+
let mut client_builder = reqwest::ClientBuilder::new();
3025

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

3332
let ssl_cert = match &config.ssl_cert {
3433
Some(cert_path) => {
@@ -286,7 +285,7 @@ impl super::PackageProvider for Gitlab {
286285
let uri = self.base_url.join(&path.metadata_uri(version))?;
287286
let client = match &do_as.token {
288287
None => self.client.clone(),
289-
Some(token) => self.build_client_with_token("PRIVATE-TOKEN", token)?
288+
Some(token) => self.build_client_with_token("PRIVATE-TOKEN", token)?,
290289
};
291290

292291
Ok(handle_error(client.get(uri).send().await?)

0 commit comments

Comments
 (0)