Skip to content

Commit d9bc8c0

Browse files
author
Alex Helfet
committed
Validate registry token before operations that require it.
1 parent ef0223f commit d9bc8c0

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

src/cargo/ops/registry.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ pub fn publish(ws: &Workspace<'_>, opts: &PublishOpts<'_>) -> CargoResult<()> {
6969
opts.index.clone(),
7070
opts.registry.clone(),
7171
true,
72+
!opts.dry_run
7273
)?;
7374
verify_dependencies(pkg, &registry, reg_id)?;
7475

@@ -334,12 +335,13 @@ pub fn registry_configuration(
334335
Ok(RegistryConfig { index, token })
335336
}
336337

337-
pub fn registry(
338+
fn registry(
338339
config: &Config,
339340
token: Option<String>,
340341
index: Option<String>,
341342
registry: Option<String>,
342343
force_update: bool,
344+
validate_token: bool
343345
) -> CargoResult<(Registry, SourceId)> {
344346
// Parse all configuration options
345347
let RegistryConfig {
@@ -363,6 +365,9 @@ pub fn registry(
363365
.ok_or_else(|| format_err!("{} does not support API commands", sid))?
364366
};
365367
let handle = http_handle(config)?;
368+
if validate_token && token.is_none() {
369+
bail!("no upload token found, please run `cargo login`");
370+
};
366371
Ok((Registry::new_handle(api_host, token, handle), sid))
367372
}
368373

@@ -536,7 +541,7 @@ pub fn registry_login(
536541
token: Option<String>,
537542
reg: Option<String>,
538543
) -> CargoResult<()> {
539-
let (registry, _) = registry(config, token.clone(), None, reg.clone(), false)?;
544+
let (registry, _) = registry(config, token.clone(), None, reg.clone(), false, false)?;
540545

541546
let token = match token {
542547
Some(token) => token,
@@ -604,6 +609,7 @@ pub fn modify_owners(config: &Config, opts: &OwnersOptions) -> CargoResult<()> {
604609
opts.index.clone(),
605610
opts.registry.clone(),
606611
true,
612+
true
607613
)?;
608614

609615
if let Some(ref v) = opts.to_add {
@@ -664,7 +670,7 @@ pub fn yank(
664670
None => bail!("a version must be specified to yank"),
665671
};
666672

667-
let (mut registry, _) = registry(config, token, index, reg, true)?;
673+
let (mut registry, _) = registry(config, token, index, reg, true, true)?;
668674

669675
if undo {
670676
config
@@ -720,7 +726,7 @@ pub fn search(
720726
prefix
721727
}
722728

723-
let (mut registry, source_id) = registry(config, None, index, reg, false)?;
729+
let (mut registry, source_id) = registry(config, None, index, reg, false, false)?;
724730
let (crates, total_crates) = registry
725731
.search(query, limit)
726732
.chain_err(|| "failed to retrieve search results from the registry")?;

tests/testsuite/alt_registry.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,9 @@ fn cannot_publish_to_crates_io_with_registry_dependency() {
289289
)
290290
.build();
291291

292+
// Login so that we have the token available
293+
p.cargo("login --registry fakeio TOKEN").run();
294+
292295
p.cargo("publish --registry fakeio")
293296
.with_status(101)
294297
.with_stderr_contains("[ERROR] crates cannot be published to crates.io[..]")

0 commit comments

Comments
 (0)