Skip to content

Commit aa17b61

Browse files
committed
Revert "Credentials for multiple hosts."
This reverts commit 29960d2.
1 parent 29960d2 commit aa17b61

File tree

6 files changed

+62
-290
lines changed

6 files changed

+62
-290
lines changed

src/bin/login.rs

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -40,30 +40,26 @@ pub fn execute(options: Options, config: &Config) -> CliResult {
4040
&options.flag_color,
4141
options.flag_frozen,
4242
options.flag_locked)?;
43-
44-
let host = match options.flag_host {
45-
Some(host) => host,
43+
let token = match options.arg_token.clone() {
44+
Some(token) => token,
4645
None => {
4746
let src = SourceId::crates_io(config)?;
4847
let mut src = RegistrySource::remote(&src, config);
4948
src.update()?;
50-
src.config()?.unwrap().api
51-
}
52-
};
53-
54-
let token = match options.arg_token {
55-
Some(token) => token,
56-
None => {
57-
println!("please visit {}me and paste the API Token below", &host);
49+
let config = src.config()?.unwrap();
50+
let host = options.flag_host.clone().unwrap_or(config.api);
51+
println!("please visit {}me and paste the API Token below", host);
5852
let mut line = String::new();
5953
let input = io::stdin();
6054
input.lock().read_line(&mut line).chain_err(|| {
6155
"failed to read stdin"
6256
})?;
63-
line.trim().to_string()
57+
line
6458
}
6559
};
6660

67-
ops::registry_login(config, token, host)?;
61+
let token = token.trim().to_string();
62+
ops::registry_login(config, token)?;
6863
Ok(())
6964
}
65+

src/cargo/core/source.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ impl SourceId {
232232
/// This is the main cargo registry by default, but it can be overridden in
233233
/// a `.cargo/config`.
234234
pub fn crates_io(config: &Config) -> CargoResult<SourceId> {
235-
let cfg = ops::registry_configuration(config, "https://crates.io")?;
235+
let cfg = ops::registry_configuration(config)?;
236236
let url = if let Some(ref index) = cfg.index {
237237
static WARNED: AtomicBool = ATOMIC_BOOL_INIT;
238238
if !WARNED.swap(true, SeqCst) {

src/cargo/ops/registry.rs

Lines changed: 12 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -177,52 +177,32 @@ fn transmit(config: &Config,
177177
}
178178
}
179179

180-
pub fn registry_configuration(config: &Config,
181-
host: &str) -> CargoResult<RegistryConfig> {
182-
let mut index = None;
183-
let mut token = None;
184-
185-
if !host.is_empty() {
186-
index = config.get_string(&format!("registry.{}.index", host))?;
187-
token = config.get_string(&format!("registry.{}.token", host))?;
188-
}
189-
190-
// FIXME: Checking out for the values which were picked up from
191-
// $CARGO_HOME/config. This section should be removed after all the users
192-
// start to use $CARGO_HOME/credentials for token configuration.
193-
if index.is_none() && token.is_none() {
194-
index = config.get_string("registry.index")?;
195-
token = config.get_string("registry.token")?;
196-
}
197-
198-
Ok(RegistryConfig {
199-
index: index.map(|p| p.val),
200-
token: token.map(|p| p.val)
201-
})
180+
pub fn registry_configuration(config: &Config) -> CargoResult<RegistryConfig> {
181+
let index = config.get_string("registry.index")?.map(|p| p.val);
182+
let token = config.get_string("registry.token")?.map(|p| p.val);
183+
Ok(RegistryConfig { index: index, token: token })
202184
}
203185

204186
pub fn registry(config: &Config,
205187
token: Option<String>,
206188
index: Option<String>) -> CargoResult<(Registry, SourceId)> {
207189
// Parse all configuration options
190+
let RegistryConfig {
191+
token: token_config,
192+
index: _index_config,
193+
} = registry_configuration(config)?;
194+
let token = token.or(token_config);
208195
let sid = match index {
209196
Some(index) => SourceId::for_registry(&index.to_url()?),
210197
None => SourceId::crates_io(config)?,
211198
};
212-
213199
let api_host = {
214200
let mut src = RegistrySource::remote(&sid, config);
215201
src.update().chain_err(|| {
216202
format!("failed to update {}", sid)
217203
})?;
218204
(src.config()?).unwrap().api
219205
};
220-
221-
let RegistryConfig {
222-
token: token_config,
223-
index: _index_config,
224-
} = registry_configuration(config, &api_host)?;
225-
let token = token.or(token_config);
226206
let handle = http_handle(config)?;
227207
Ok((Registry::new_handle(api_host, token, handle), sid))
228208
}
@@ -301,31 +281,15 @@ pub fn http_timeout(config: &Config) -> CargoResult<Option<i64>> {
301281
Ok(env::var("HTTP_TIMEOUT").ok().and_then(|s| s.parse().ok()))
302282
}
303283

304-
pub fn registry_login(config: &Config,
305-
token: String,
306-
host: String) -> CargoResult<()> {
307-
let host = match host.to_url()?.host_str() {
308-
Some(h) => h.to_string(),
309-
None => host,
310-
};
311-
let host: String = host.chars()
312-
.map(|x| match x {
313-
'\\'|'/'|':'|'.'|'-' => '_',
314-
_ => x,
315-
}).collect();
316-
317-
let RegistryConfig {
318-
index: _,
319-
token: old_token
320-
} = registry_configuration(config, &host)?;
321-
284+
pub fn registry_login(config: &Config, token: String) -> CargoResult<()> {
285+
let RegistryConfig { index: _, token: old_token } = registry_configuration(config)?;
322286
if let Some(old_token) = old_token {
323287
if old_token == token {
324288
return Ok(());
325289
}
326290
}
327291

328-
config::save_credentials(config, token, host)
292+
config::save_credentials(config, token)
329293
}
330294

331295
pub struct OwnersOptions {

src/cargo/util/config.rs

Lines changed: 36 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ use std::collections::HashSet;
55
use std::env;
66
use std::fmt;
77
use std::fs::{self, File};
8-
use std::io::SeekFrom;
98
use std::io::prelude::*;
109
use std::mem;
1110
use std::path::{Path, PathBuf};
@@ -430,52 +429,28 @@ impl Config {
430429
Ok(())
431430
}).chain_err(|| "Couldn't load Cargo configuration")?;
432431

433-
self.load_credentials(&mut cfg)?;
434-
match cfg {
435-
CV::Table(map, _) => Ok(map),
432+
let mut map = match cfg {
433+
CV::Table(map, _) => map,
436434
_ => unreachable!(),
437-
}
438-
}
435+
};
439436

440-
fn load_credentials(&self, cfg: &mut ConfigValue) -> CargoResult<()> {
441437
let home_path = self.home_path.clone().into_path_unlocked();
442-
let credentials = home_path.join("credentials");
443-
if !fs::metadata(&credentials).is_ok() {
444-
return Ok(());
438+
let token = load_credentials(&home_path)?;
439+
if let Some(t) = token {
440+
if !t.is_empty() {
441+
let mut registry = map.entry("registry".into())
442+
.or_insert(CV::Table(HashMap::new(), PathBuf::from(".")));
443+
match *registry {
444+
CV::Table(ref mut m, _) => {
445+
m.insert("token".into(),
446+
CV::String(t, home_path.join("credentials")));
447+
}
448+
_ => unreachable!(),
449+
}
450+
}
445451
}
446452

447-
let mut contents = String::new();
448-
let mut file = File::open(&credentials)?;
449-
file.read_to_string(&mut contents).chain_err(|| {
450-
format!("failed to read configuration file `{}`",
451-
credentials.display())
452-
})?;
453-
454-
let toml = cargo_toml::parse(&contents,
455-
&credentials,
456-
self).chain_err(|| {
457-
format!("could not parse TOML configuration in `{}`",
458-
credentials.display())
459-
})?;
460-
let value = CV::from_toml(&credentials, toml).chain_err(|| {
461-
format!("failed to load TOML configuration from `{}`",
462-
credentials.display())
463-
})?;
464-
465-
let mut cfg = match *cfg {
466-
CV::Table(ref mut map, _) => map,
467-
_ => unreachable!(),
468-
};
469-
470-
let mut registry = cfg.entry("registry".into())
471-
.or_insert(CV::Table(HashMap::new(),
472-
PathBuf::from(".")));
473-
registry.merge(value).chain_err(|| {
474-
format!("failed to merge configuration at `{}`",
475-
credentials.display())
476-
})?;
477-
478-
Ok(())
453+
Ok(map)
479454
}
480455

481456
/// Look for a path for `tool` in an environment variable or config path, but return `None`
@@ -592,21 +567,6 @@ impl ConfigValue {
592567
}
593568
}
594569

595-
fn into_toml(self) -> toml::Value {
596-
match self {
597-
CV::Boolean(s, _) => toml::Value::Boolean(s),
598-
CV::String(s, _) => toml::Value::String(s),
599-
CV::Integer(i, _) => toml::Value::Integer(i),
600-
CV::List(l, _) => toml::Value::Array(l
601-
.into_iter()
602-
.map(|(s, _)| toml::Value::String(s))
603-
.collect()),
604-
CV::Table(l, _) => toml::Value::Table(l.into_iter()
605-
.map(|(k, v)| (k, v.into_toml()))
606-
.collect()),
607-
}
608-
}
609-
610570
fn merge(&mut self, from: ConfigValue) -> CargoResult<()> {
611571
match (self, from) {
612572
(&mut CV::String(..), CV::String(..)) |
@@ -807,32 +767,15 @@ fn walk_tree<F>(pwd: &Path, mut walk: F) -> CargoResult<()>
807767
}
808768

809769
pub fn save_credentials(cfg: &Config,
810-
token: String,
811-
host: String) -> CargoResult<()> {
770+
token: String) -> CargoResult<()> {
812771
let mut file = {
813772
cfg.home_path.create_dir()?;
814773
cfg.home_path.open_rw(Path::new("credentials"), cfg,
815774
"credentials' config file")?
816775
};
817776

818-
let mut map = HashMap::new();
819-
map.insert("token".to_string(),
820-
ConfigValue::String(token, file.path().to_path_buf()));
821-
822-
let mut contents = String::new();
823-
file.read_to_string(&mut contents).chain_err(|| {
824-
format!("failed to read configuration file `{}`",
825-
file.path().display())
826-
})?;
827-
let mut toml = cargo_toml::parse(&contents, file.path(), cfg)?;
828-
toml.as_table_mut()
829-
.unwrap()
830-
.insert(host, CV::Table(map, file.path().to_path_buf()).into_toml());
831-
832-
let contents = toml.to_string();
833-
file.seek(SeekFrom::Start(0))?;
834-
file.write_all(contents.as_bytes())?;
835-
file.file().set_len(contents.len() as u64)?;
777+
file.write_all(token.as_bytes())?;
778+
file.file().set_len(token.len() as u64)?;
836779
set_permissions(file.file(), 0o600)?;
837780

838781
return Ok(());
@@ -853,3 +796,19 @@ pub fn save_credentials(cfg: &Config,
853796
Ok(())
854797
}
855798
}
799+
800+
fn load_credentials(home: &PathBuf) -> CargoResult<Option<String>> {
801+
let credentials = home.join("credentials");
802+
if !fs::metadata(&credentials).is_ok() {
803+
return Ok(None);
804+
}
805+
806+
let mut token = String::new();
807+
let mut file = File::open(&credentials)?;
808+
file.read_to_string(&mut token).chain_err(|| {
809+
format!("failed to read configuration file `{}`",
810+
credentials.display())
811+
})?;
812+
813+
Ok(Some(token.trim().into()))
814+
}

0 commit comments

Comments
 (0)