Skip to content
This repository was archived by the owner on Jun 14, 2023. It is now read-only.

Commit dee4e15

Browse files
committed
Improved login by auth token
1 parent 3238fe1 commit dee4e15

File tree

10 files changed

+68
-16
lines changed

10 files changed

+68
-16
lines changed

Cargo.lock

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ tar = { version = "0.4" }
5050

5151
[target.'cfg(target_os = "wasi")'.dependencies]
5252
whoami = "0.5"
53-
wasm-bus-reqwest = { version = "1.0", path = "../ate/wasm-bus-reqwest" }
54-
wasm-bus-process = { version = "1.0", path = "../ate/wasm-bus-process" }
53+
wasm-bus-reqwest = "1.0"
54+
wasm-bus-process = "1.0"
5555
getrandom = "0.2.3"
5656
tar = { package = "tar-wasi", version = "0.4" }
5757
serde_yaml = { version = "^0.8" }

src/bin/wapm.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ enum Command {
1515

1616
#[structopt(name = "login")]
1717
/// Logins into wapm, saving the token locally for future commands
18-
Login,
18+
Login(commands::LoginOpt),
1919

2020
#[structopt(name = "logout")]
2121
/// Remove the token for the registry
@@ -110,7 +110,7 @@ fn main() {
110110
}
111111
}
112112

113-
//#[cfg(target_os = "wasi")]
113+
#[cfg(target_os = "wasi")]
114114
{
115115
if let Err(e) = logging::set_up_logging(true) {
116116
eprintln!("Error: {}", e);
@@ -174,7 +174,7 @@ fn main() {
174174

175175
let result = match args {
176176
Command::WhoAmI => commands::whoami(),
177-
Command::Login => commands::login(),
177+
Command::Login(login_options) => commands::login(login_options),
178178
Command::Logout => commands::logout(),
179179
Command::Config(config_options) => commands::config(config_options),
180180
Command::Install(install_options) => commands::install(install_options),

src/commands/list.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use crate::config;
44
use crate::data::lock::lockfile::{CommandMap, ModuleMap};
55
use crate::dataflow::lockfile_packages::LockfileResult;
66
use prettytable::{format, Table};
7-
use std::{fmt::Write as _};
7+
use std::fmt::Write as _;
88
use structopt::StructOpt;
99

1010
#[derive(StructOpt, Debug)]

src/commands/login.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,16 @@ use crate::graphql::execute_query;
33
use rpassword_wasi as rpassword;
44
use std::io::prelude::*;
55
use std::io::{stdin, stdout};
6+
use structopt::StructOpt;
67

78
use graphql_client::*;
89

10+
#[derive(StructOpt, Debug)]
11+
pub struct LoginOpt {
12+
/// Provide the token
13+
token: Option<String>,
14+
}
15+
916
#[derive(GraphQLQuery)]
1017
#[graphql(
1118
schema_path = "graphql/schema.graphql",
@@ -14,7 +21,15 @@ use graphql_client::*;
1421
)]
1522
struct LoginMutation;
1623

17-
pub fn login() -> anyhow::Result<()> {
24+
pub fn login(login_options: LoginOpt) -> anyhow::Result<()> {
25+
if let Some(token) = login_options.token {
26+
let mut config = Config::from_file()?;
27+
config.registry.token = Some(token);
28+
config.save()?;
29+
println!("Login for WAPM saved");
30+
return Ok(());
31+
}
32+
1833
print!("Username: ");
1934
stdout().flush().ok().expect("Could not flush stdout");
2035

src/commands/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ pub use self::install::{install, InstallOpt};
3434
pub use self::keys::{keys, KeyOpt};
3535
#[cfg(feature = "full")]
3636
pub use self::list::{list, ListOpt};
37-
pub use self::login::login;
37+
pub use self::login::{login, LoginOpt};
3838
pub use self::logout::logout;
3939
#[cfg(feature = "full")]
4040
pub use self::publish::{publish, PublishOpt};

src/dataflow/bin_script.rs

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,22 @@ pub fn save_bin_script<P: AsRef<Path>>(
3434
package_path: String,
3535
module_path: String,
3636
) -> Result<(), Error> {
37-
let current_dir = crate::config::Config::get_current_dir().ok().unwrap_or_else(|| std::path::PathBuf::from("/".to_string()));
37+
let current_dir = crate::config::Config::get_current_dir()
38+
.ok()
39+
.unwrap_or_else(|| std::path::PathBuf::from("/".to_string()));
3840
let command_path = format!("/bin/{}.alias", command_name);
39-
let package_path = current_dir.clone().join("wapm_packages").join(package_path).to_string_lossy().to_string();
40-
let module_path = current_dir.clone().join("wapm_packages").join(module_path).to_string_lossy().to_string();
41+
let package_path = current_dir
42+
.clone()
43+
.join("wapm_packages")
44+
.join(package_path)
45+
.to_string_lossy()
46+
.to_string();
47+
let module_path = current_dir
48+
.clone()
49+
.join("wapm_packages")
50+
.join(module_path)
51+
.to_string_lossy()
52+
.to_string();
4153
let mut file = fs::OpenOptions::new()
4254
.create(true)
4355
.truncate(true)
@@ -50,7 +62,12 @@ pub fn save_bin_script<P: AsRef<Path>>(
5062
crate::dataflow::ManifestResult::Manifest(manifest) => {
5163
if let Some(ref fs) = manifest.fs {
5264
for (guest_path, host_path) in fs.iter() {
53-
mappings.push(format!("{}:{}/{}", guest_path, package_path, host_path.to_string_lossy()));
65+
mappings.push(format!(
66+
"{}:{}/{}",
67+
guest_path,
68+
package_path,
69+
host_path.to_string_lossy()
70+
));
5471
}
5572
}
5673
}

src/dataflow/merged_lockfile_packages.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,13 @@ impl<'a> MergedLockfilePackages<'a> {
7373
let module_path = format!("{}/{}", module.package_path, module.source);
7474
commands.insert(name, command);
7575
// save the bin script to execute this command from the terminal
76-
save_bin_script(directory, script_name, module.package_path.clone(), module_path)
77-
.map_err(|e| Error::FailedToSaveLockfile(e.to_string()))?;
76+
save_bin_script(
77+
directory,
78+
script_name,
79+
module.package_path.clone(),
80+
module_path,
81+
)
82+
.map_err(|e| Error::FailedToSaveLockfile(e.to_string()))?;
7883
}
7984
}
8085
}

src/graphql.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use graphql_client::{QueryBody, Response};
22
use serde;
3+
use std::env;
34
use std::string::ToString;
45
use thiserror::Error;
56
#[cfg(not(target_os = "wasi"))]
@@ -65,7 +66,10 @@ where
6566
let res = client
6667
.post(registry_url)
6768
.multipart(form)
68-
.bearer_auth(&config.registry.token.unwrap_or_else(|| "".to_string()))
69+
.bearer_auth(
70+
env::var("WAPM_REGISTRY_TOKEN")
71+
.unwrap_or(config.registry.token.unwrap_or_else(|| "".to_string())),
72+
)
6973
.header(USER_AGENT, user_agent)
7074
.send()?;
7175

src/update_notifier.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,8 @@ pub fn run_async_check_base() {
153153
}
154154

155155
let current_wapm = std::env::current_exe().expect("Can't get current wapm executable");
156-
let current_dir = crate::config::Config::get_current_dir().expect("Can't get current wapm dir");
156+
let current_dir =
157+
crate::config::Config::get_current_dir().expect("Can't get current wapm dir");
157158
std::process::Command::new(current_wapm)
158159
.arg("run-background-update-check")
159160
.current_dir(current_dir)

0 commit comments

Comments
 (0)