Skip to content

Commit 87da31c

Browse files
elrrrrrrrclaude
andauthored
feat(pm): utoo auth cmd (#2604)
* feat(pm): add auth and npmrc infrastructure - Add util/auth.rs for resolving registry authentication tokens - Add util/npmrc.rs for parsing .npmrc configuration files - Add service/auth.rs for web-based and legacy npm login flows - Add config delete method for logout support Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * fix: cmd * fix: ci --------- Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 7589869 commit 87da31c

File tree

12 files changed

+423
-4
lines changed

12 files changed

+423
-4
lines changed

Cargo.lock

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

crates/pm/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ indicatif = "0.17.8"
2828
libc = "0.2"
2929
libdeflater = "1.25"
3030
once_cell = "1.19"
31+
open = "5"
3132
owo-colors = { workspace = true }
3233
petgraph = "0.6"
3334
rayon = { workspace = true }

crates/pm/src/cmd/login.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
use anyhow::Result;
2+
use colored::Colorize;
3+
4+
use crate::service::auth;
5+
use crate::util::user_config::get_registry;
6+
7+
pub async fn login() -> Result<()> {
8+
let registry = get_registry();
9+
10+
println!("Login to {}", registry.cyan());
11+
12+
let token = auth::web_login(&registry, |url| {
13+
println!("Login at: {}", url);
14+
if let Err(e) = open::that(url) {
15+
tracing::warn!("Failed to open browser: {e}");
16+
}
17+
})
18+
.await?;
19+
20+
auth::save_token(&registry, token).await?;
21+
println!("{}", "Logged in successfully.".green());
22+
Ok(())
23+
}

crates/pm/src/cmd/logout.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
use anyhow::Result;
2+
use colored::Colorize;
3+
4+
use crate::service::auth;
5+
use crate::util::user_config::get_registry;
6+
7+
pub async fn logout() -> Result<()> {
8+
let registry = get_registry();
9+
let token = auth::require_token(&registry).await?;
10+
11+
auth::logout(&registry, &token).await?;
12+
13+
println!("Logged out from {}.", registry.green());
14+
Ok(())
15+
}

crates/pm/src/cmd/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,11 @@ pub mod init;
66
pub mod install;
77
pub mod link;
88
pub mod list;
9+
pub mod login;
10+
pub mod logout;
911
pub mod ping;
1012
pub mod rebuild;
1113
pub mod run;
1214
pub mod update;
1315
pub mod view;
16+
pub mod whoami;

crates/pm/src/cmd/whoami.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
use anyhow::Result;
2+
3+
use crate::service::auth;
4+
use crate::util::user_config::get_registry;
5+
6+
pub async fn whoami() -> Result<()> {
7+
let registry = get_registry();
8+
let token = auth::require_token(&registry).await?;
9+
10+
let username = auth::whoami(&registry, &token).await?;
11+
println!("{}", username);
12+
Ok(())
13+
}

crates/pm/src/constants.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,18 @@ pub mod cmd {
6262
pub const PING_ALIAS: &str = "pg";
6363
pub const PING_ABOUT: &str = "Ping npm registry";
6464

65+
pub const LOGIN_NAME: &str = "login";
66+
pub const LOGIN_ALIAS: &str = "lg";
67+
pub const LOGIN_ABOUT: &str = "Login to npm registry";
68+
69+
pub const WHOAMI_NAME: &str = "whoami";
70+
pub const WHOAMI_ALIAS: &str = "who";
71+
pub const WHOAMI_ABOUT: &str = "Display npm username";
72+
73+
pub const LOGOUT_NAME: &str = "logout";
74+
pub const LOGOUT_ALIAS: &str = "lo";
75+
pub const LOGOUT_ABOUT: &str = "Logout from npm registry";
76+
6577
pub const COMPLETIONS_NAME: &str = "completions";
6678
pub const COMPLETIONS_ABOUT: &str = "Generate shell completion scripts\n\nAdd to your shell config:\n bash: echo 'eval \"$(utoo completions bash)\"' >> ~/.bashrc\n zsh: echo 'eval \"$(utoo completions zsh)\"' >> ~/.zshrc\n fish: utoo completions fish > ~/.config/fish/completions/utoo.fish";
6779
}

crates/pm/src/main.rs

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,11 @@ use crate::constants::cmd::{
3333
CLEAN_ABOUT, CLEAN_ALIAS, CLEAN_NAME, COMPLETIONS_ABOUT, COMPLETIONS_NAME, CONFIG_ABOUT,
3434
CONFIG_ALIAS, CONFIG_NAME, DEPS_ABOUT, DEPS_ALIAS, DEPS_NAME, EXECUTE_ABOUT, EXECUTE_ALIAS,
3535
EXECUTE_NAME, INIT_ABOUT, INIT_ALIAS, INIT_NAME, INSTALL_ABOUT, INSTALL_ALIAS, INSTALL_NAME,
36-
LINK_ABOUT, LINK_ALIAS, LINK_NAME, LIST_ALIAS, LIST_NAME, PING_ABOUT, PING_ALIAS, PING_NAME,
37-
REBUILD_ABOUT, REBUILD_ALIAS, REBUILD_NAME, RUN_ALIAS, RUN_NAME, UNINSTALL_ABOUT,
38-
UNINSTALL_ALIAS, UNINSTALL_NAME, UPDATE_ABOUT, UPDATE_ALIAS, UPDATE_NAME, VIEW_ABOUT,
39-
VIEW_ALIAS, VIEW_ALIAS_INFO, VIEW_ALIAS_SHOW, VIEW_NAME,
36+
LINK_ABOUT, LINK_ALIAS, LINK_NAME, LIST_ALIAS, LIST_NAME, LOGIN_ABOUT, LOGIN_ALIAS, LOGIN_NAME,
37+
LOGOUT_ABOUT, LOGOUT_ALIAS, LOGOUT_NAME, PING_ABOUT, PING_ALIAS, PING_NAME, REBUILD_ABOUT,
38+
REBUILD_ALIAS, REBUILD_NAME, RUN_ALIAS, RUN_NAME, UNINSTALL_ABOUT, UNINSTALL_ALIAS,
39+
UNINSTALL_NAME, UPDATE_ABOUT, UPDATE_ALIAS, UPDATE_NAME, VIEW_ABOUT, VIEW_ALIAS,
40+
VIEW_ALIAS_INFO, VIEW_ALIAS_SHOW, VIEW_NAME, WHOAMI_ABOUT, WHOAMI_ALIAS, WHOAMI_NAME,
4041
};
4142
use crate::constants::{APP_ABOUT, APP_NAME, APP_VERSION};
4243
use crate::helper::workspace::update_cwd_to_root;
@@ -265,6 +266,15 @@ enum Commands {
265266
registry: Option<String>,
266267
},
267268

269+
#[command(name = LOGIN_NAME, alias = LOGIN_ALIAS, about = LOGIN_ABOUT)]
270+
Login,
271+
272+
#[command(name = WHOAMI_NAME, alias = WHOAMI_ALIAS, about = WHOAMI_ABOUT)]
273+
Whoami,
274+
275+
#[command(name = LOGOUT_NAME, alias = LOGOUT_ALIAS, about = LOGOUT_ABOUT)]
276+
Logout,
277+
268278
#[command(name = CONFIG_NAME, alias = CONFIG_ALIAS, about = CONFIG_ABOUT)]
269279
Config {
270280
#[command(subcommand)]
@@ -544,6 +554,15 @@ async fn async_main() -> Result<()> {
544554
Some(Commands::Ping { registry }) => {
545555
cmd::ping::ping(registry.as_deref()).await?;
546556
}
557+
Some(Commands::Login) => {
558+
cmd::login::login().await?;
559+
}
560+
Some(Commands::Whoami) => {
561+
cmd::whoami::whoami().await?;
562+
}
563+
Some(Commands::Logout) => {
564+
cmd::logout::logout().await?;
565+
}
547566
Some(Commands::Config { command }) => match command {
548567
ConfigCommands::Set { key, value, global } => {
549568
handle_config_set(key, value, global).await?;

0 commit comments

Comments
 (0)