node-auth 0.2.0

Authenticate npm, yarn, pnpm, and Bun to Google Artifact Registry using Google Cloud ADC
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
//! # `node-auth`
//!
//! A fast, lightweight, standalone Rust CLI and library that authenticates JavaScript package managers
//! (**npm**, **yarn**, **pnpm**, and **Bun**) to private **Google Artifact Registry (GAR)** npm repositories
//! using **Google Cloud Application Default Credentials (ADC)**.
//!
//! ## Overview
//!
//! `node-auth` replicates and improves upon Google's `@google-cloud/artifact-registry-npm-tools`
//! without requiring a JavaScript runtime or package manager to be installed beforehand.
//!
//! ### Key Features
//! - **Automatic Credential Discovery**: Searches for explicit tokens, Application Default Credentials (ADC),
//!   and falls back to `gcloud auth print-access-token`.
//! - **Credential Isolation**: Reads registry endpoints from project-level `.npmrc` and writes secrets to
//!   user-level `~/.npmrc` by default, preventing sensitive tokens from being checked into source control.
//! - **Git Safety Checks**: When `--local-credential` is used, checks `.gitignore` and emits warnings if `.npmrc`
//!   is not properly ignored.
//! - **Yarn Modern Support**: Detects and updates `npmScopes` authentication in `.yarnrc.yml`.
//! - **Bun Support**: Discovers registries in `bunfig.toml` and optionally manages a local
//!   `.env.local` token reference.
//!
//! The `cli` feature is enabled by default and provides both executables and the CLI modules.
//! Library consumers can disable it with `default-features = false` to omit CLI dependencies.
//! The example below requires Tokio with the `macros` and `rt-multi-thread` features in the
//! consuming application.
//!
//! ### Library Usage Example
//!
//! ```no_run
//! use node_auth::{run, Options};
//!
//! #[tokio::main]
//! async fn main() -> Result<(), node_auth::AuthError> {
//!     let options = Options {
//!         token: Some("custom-token".to_string()),
//!         ..Default::default()
//!     };
//!     run(&options).await?;
//!     Ok(())
//! }
//! ```

#![warn(missing_docs)]

pub mod auth;
pub mod bunfig;
#[cfg(feature = "cli")]
pub mod cli;
pub mod error;
pub mod fs;
#[cfg(feature = "cli")]
pub mod logger;
pub mod npmrc;
pub mod registry;
pub mod token;
pub mod vcs;
pub mod yarnrc;

pub use error::AuthError;
use std::path::{Path, PathBuf};

/// Useful result of a library authentication run, with presentation left to callers.
pub enum RunOutcome {
  /// The requested token, with no configuration files modified.
  Token(String),
  /// Paths replaced, and the Git safety status for local credentials if applicable.
  Updated {
    /// Absolute paths successfully replaced.
    paths: Vec<PathBuf>,
    /// Git status of the local `.npmrc` when `local_credential` was requested.
    git_status: Option<vcs::GitStatus>,
    /// Git safety status for `.env.local` when `bun_env` was requested.
    bun_env_git_status: Option<vcs::GitStatus>,
  },
}

impl std::fmt::Debug for RunOutcome {
  fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
    match self {
      Self::Token(_) => formatter.write_str("Token([redacted])"),
      Self::Updated {
        paths,
        git_status,
        bun_env_git_status,
      } => formatter
        .debug_struct("Updated")
        .field("paths", paths)
        .field("git_status", git_status)
        .field("bun_env_git_status", bun_env_git_status)
        .finish(),
    }
  }
}

/// Configuration options for configuring Node authentication.
#[derive(Clone, Default)]
pub struct Options {
  /// Path to the `.npmrc` file to read registry configs from.
  /// Defaults to project-level `.npmrc` if present, otherwise user-level `~/.npmrc`.
  pub repo_config: Option<PathBuf>,

  /// Path to the `.npmrc` file to write credentials to.
  /// Defaults to user-level `~/.npmrc`.
  pub credential_config: Option<PathBuf>,

  /// Path to the `.yarnrc.yml` file to read registry configs from.
  /// Defaults to project-level `.yarnrc.yml` if present, otherwise user-level `~/.yarnrc.yml`.
  pub repo_config_yarn: Option<PathBuf>,

  /// Path to the `.yarnrc.yml` file to write credentials to.
  /// Defaults to user-level `~/.yarnrc.yml`.
  pub credential_config_yarn: Option<PathBuf>,

  /// Path to the project `bunfig.toml` used to discover Bun registry configuration.
  /// Defaults to `./bunfig.toml`.
  pub repo_config_bun: Option<PathBuf>,

  /// Forces writing credentials to the current directory's `.npmrc`.
  pub local_credential: bool,

  /// Explicit OAuth2 access token to use instead of querying ADC or `gcloud`.
  pub token: Option<String>,

  /// Allow all HTTPS registry domains to receive the auth token (not only `*-npm.pkg.dev`).
  pub allow_all_domains: bool,

  /// Explicitly enable (`Some(true)`) or disable (`Some(false)`) updating `.yarnrc.yml`.
  /// When `None`, Yarn updating is auto-detected based on `.yarnrc.yml` presence.
  pub yarn: Option<bool>,

  /// Explicitly enable (`Some(true)`) or disable (`Some(false)`) reading `bunfig.toml`.
  /// When `None`, Bun support is auto-detected from `./bunfig.toml`.
  pub bun: Option<bool>,

  /// Write Bun's token to `.env.local` and reference it from the project `bunfig.toml`.
  pub bun_env: bool,

  /// Only retrieve and print the access token to standard output without modifying files.
  pub print_token: bool,
}

impl std::fmt::Debug for Options {
  fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
    formatter
      .debug_struct("Options")
      .field("repo_config", &self.repo_config)
      .field("credential_config", &self.credential_config)
      .field("repo_config_yarn", &self.repo_config_yarn)
      .field("credential_config_yarn", &self.credential_config_yarn)
      .field("repo_config_bun", &self.repo_config_bun)
      .field("local_credential", &self.local_credential)
      .field("token", &self.token.as_ref().map(|_| "[redacted]"))
      .field("allow_all_domains", &self.allow_all_domains)
      .field("yarn", &self.yarn)
      .field("bun", &self.bun)
      .field("bun_env", &self.bun_env)
      .field("print_token", &self.print_token)
      .finish()
  }
}

/// Executes authentication and updates `.npmrc` and `.yarnrc.yml` configuration files.
///
/// # Arguments
///
/// * `options` - Parameters controlling file paths, tokens, and domain restrictions.
///
/// # Errors
///
/// Returns [`AuthError`] if authentication fails, files cannot be read/written,
/// or no Artifact Registry configuration is discovered.
pub async fn run(options: &Options) -> Result<RunOutcome, AuthError> {
  if options.bun_env && options.local_credential {
    return Err(AuthError::Config(
      "--bun-env cannot be used with --local-credential".to_string(),
    ));
  }
  if options.bun_env && options.bun == Some(false) {
    return Err(AuthError::Config(
      "--bun-env cannot be used with --bun false".to_string(),
    ));
  }
  let token = auth::get_credentials(options.token.as_deref()).await?;
  token::validate_token(&token)?;
  if options.print_token {
    return Ok(RunOutcome::Token(token));
  }

  let home_dir = dirs::home_dir()
    .ok_or_else(|| AuthError::Config("Unable to determine user home directory".to_string()))?;

  // Determine npmrc paths
  let (repo_npmrc, cred_npmrc, git_status) = if options.local_credential {
    let cwd = std::env::current_dir()?;
    let git_status = vcs::check_git_status(&cwd);

    let local_npmrc = cwd.join(".npmrc");

    let repo = if let Some(ref r) = options.repo_config {
      r.clone()
    } else if local_npmrc.exists() {
      local_npmrc.clone()
    } else {
      home_dir.join(".npmrc")
    };

    (repo, local_npmrc, Some(git_status))
  } else {
    let repo = options.repo_config.clone().unwrap_or_else(|| {
      let local = Path::new(".npmrc");
      if local.exists() {
        local.to_path_buf()
      } else {
        home_dir.join(".npmrc")
      }
    });

    let cred = options
      .credential_config
      .clone()
      .unwrap_or_else(|| home_dir.join(".npmrc"));

    (repo, cred, None)
  };

  // Determine yarnrc paths
  let repo_yarn = options.repo_config_yarn.clone().unwrap_or_else(|| {
    let local = Path::new(".yarnrc.yml");
    if local.exists() {
      local.to_path_buf()
    } else {
      home_dir.join(".yarnrc.yml")
    }
  });

  let cred_yarn = options
    .credential_config_yarn
    .clone()
    .unwrap_or_else(|| home_dir.join(".yarnrc.yml"));

  let should_update_yarn = match options.yarn {
    Some(explicit) => explicit,
    None => {
      options.repo_config_yarn.is_some()
        || options.credential_config_yarn.is_some()
        || Path::new(".yarnrc.yml").exists()
    }
  };

  let repo_bun = options
    .repo_config_bun
    .clone()
    .unwrap_or_else(|| PathBuf::from("bunfig.toml"));
  let should_update_bun = options.bun_env
    || match options.bun {
      Some(explicit) => explicit,
      None => options.repo_config_bun.is_some() || Path::new("bunfig.toml").exists(),
    };

  let repo_npmrc = fs::resolve(&repo_npmrc)?;
  let cred_npmrc = fs::resolve(&cred_npmrc)?;
  let repo_yarn = if should_update_yarn {
    Some(fs::resolve(&repo_yarn)?)
  } else {
    None
  };
  let cred_yarn = if should_update_yarn {
    Some(fs::resolve(&cred_yarn)?)
  } else {
    None
  };
  let repo_bun = if should_update_bun {
    Some(fs::resolve(&repo_bun)?)
  } else {
    None
  };
  let bun_env_path = if options.bun_env {
    let parent = repo_bun
      .as_ref()
      .and_then(|path| path.parent())
      .ok_or_else(|| AuthError::Config("bunfig.toml has no parent directory".to_string()))?;
    Some(fs::resolve(&parent.join(".env.local"))?)
  } else {
    None
  };
  let bun_env_git_status = bun_env_path
    .as_ref()
    .map(|path| vcs::check_path_status(path.parent().unwrap(), Path::new(".env.local")));

  let mut paths = vec![repo_npmrc.clone(), cred_npmrc.clone()];
  if let Some(path) = &repo_yarn {
    paths.push(path.clone());
  }
  if let Some(path) = &cred_yarn {
    paths.push(path.clone());
  }
  if let Some(path) = &repo_bun {
    paths.push(path.clone());
  }
  if let Some(path) = &bun_env_path {
    paths.push(path.clone());
  }
  let mut transaction = fs::FileTransaction::new(&paths)?;

  let bun_registries = if let Some(path) = &repo_bun {
    if !transaction.existed(path)? {
      return Err(AuthError::Config(format!(
        "Bun configuration does not exist: {}",
        path.display()
      )));
    }
    bunfig::registry_urls(transaction.contents(path)?, options.allow_all_domains)?
  } else {
    Vec::new()
  };

  let npm_source = transaction.contents(&repo_npmrc)?.to_owned();
  let npm_target = transaction.contents(&cred_npmrc)?.to_owned();
  let npm_has_registry = npmrc::has_registry(&npm_source, &npm_target, options.allow_all_domains)?;
  let bun_npm_registries = if options.local_credential || options.bun_env {
    &[][..]
  } else {
    bun_registries.as_slice()
  };
  if npm_has_registry || !bun_npm_registries.is_empty() {
    let same_file = repo_npmrc == cred_npmrc;
    let (cleaned, updated) = npmrc::transform_npmrc_contents_with_registries(
      &npm_source,
      &npm_target,
      &token,
      options.allow_all_domains,
      same_file,
      bun_npm_registries,
    )?;
    transaction.stage(&cred_npmrc, &updated)?;
    if !same_file && transaction.existed(&repo_npmrc)? && cleaned != npm_source {
      transaction.stage(&repo_npmrc, &cleaned)?;
    }
  }

  if let (Some(source), Some(target)) = (repo_yarn, cred_yarn) {
    let source_content = transaction.contents(&source)?.to_owned();
    let target_content = transaction.contents(&target)?.to_owned();
    if let Some(updated) = yarnrc::transform_yarnrc_contents_with_policy(
      &source_content,
      &target_content,
      &token,
      options.allow_all_domains,
    )? {
      transaction.stage(&target, &updated)?;
    }
  }

  if options.bun_env {
    let bun_path = repo_bun.as_ref().unwrap();
    let bun_content = transaction.contents(bun_path)?.to_owned();
    let updated_bun = bunfig::transform_bunfig_for_env(&bun_content, options.allow_all_domains)?
      .ok_or_else(|| {
        AuthError::Config("No eligible registry configuration found in bunfig.toml".to_string())
      })?;
    if updated_bun != bun_content {
      transaction.stage(bun_path, &updated_bun)?;
    }

    let env_path = bun_env_path.as_ref().unwrap();
    let env_content = transaction.contents(env_path)?.to_owned();
    let updated_env = bunfig::transform_env_local(&env_content, &token)?;
    if updated_env != env_content {
      transaction.stage(env_path, &updated_env)?;
    }
  }

  let paths = transaction.commit()?;
  if paths.is_empty() {
    return Err(AuthError::Config(
      "No eligible Artifact Registry configuration found".to_string(),
    ));
  }
  Ok(RunOutcome::Updated {
    paths,
    git_status,
    bun_env_git_status,
  })
}

/// Executes authentication synchronously using a temporary Tokio runtime.
///
/// This helper works without the `cli` feature or any runtime setup by the caller.
/// Each call creates a current-thread runtime with I/O and timers enabled, runs
/// [`run`], and drops the runtime before returning. It does not initialize logging
/// or print the outcome.
///
/// Call this from synchronous code. Within an async Tokio context, use [`run`]
/// directly. Threads entered into a Tokio runtime, including `spawn_blocking`
/// threads, are rejected.
///
/// # Errors
///
/// Returns [`AuthError::Config`] when called within an active Tokio runtime,
/// [`AuthError::Io`] if runtime creation fails, or any error returned by [`run`].
///
/// # Example
///
/// ```no_run
/// use node_auth::{run_blocking, Options};
///
/// fn main() -> Result<(), node_auth::AuthError> {
///     let outcome = run_blocking(&Options::default())?;
///     // Handle the returned token or updated configuration paths.
///     Ok(())
/// }
/// ```
pub fn run_blocking(options: &Options) -> Result<RunOutcome, AuthError> {
  if tokio::runtime::Handle::try_current().is_ok() {
    return Err(AuthError::Config(
      "run_blocking cannot be called within an active Tokio runtime; use run().await".to_string(),
    ));
  }
  let runtime = tokio::runtime::Builder::new_current_thread()
    .enable_all()
    .build()?;
  runtime.block_on(run(options))
}

#[cfg(test)]
mod tests {
  use super::*;

  #[test]
  fn blocking_run_returns_token_and_propagates_errors() {
    let mut options = Options {
      token: Some("valid-token".to_string()),
      print_token: true,
      ..Default::default()
    };
    assert!(
      matches!(run_blocking(&options).unwrap(), RunOutcome::Token(token) if token == "valid-token")
    );
    options.token = Some("invalid\ntoken".to_string());
    assert!(run_blocking(&options).is_err());
  }

  #[test]
  fn blocking_run_updates_configuration_without_cli() {
    let dir = tempfile::tempdir().unwrap();
    let source = dir.path().join("project.npmrc");
    let target = dir.path().join("user.npmrc");
    std::fs::write(&source, "registry=https://us-npm.pkg.dev/project/repo/\n").unwrap();
    let options = Options {
      token: Some("valid-token".to_string()),
      repo_config: Some(source),
      credential_config: Some(target.clone()),
      yarn: Some(false),
      bun: Some(false),
      ..Default::default()
    };
    assert!(
      matches!(run_blocking(&options).unwrap(), RunOutcome::Updated { paths, .. } if paths.contains(&fs::resolve(&target).unwrap()))
    );
    assert!(
      std::fs::read_to_string(target)
        .unwrap()
        .contains("//us-npm.pkg.dev/project/repo/:_authToken=\"valid-token\"")
    );
  }

  #[tokio::test]
  async fn blocking_run_rejects_nested_runtime_without_panicking() {
    let options = Options {
      token: Some("valid-token".to_string()),
      print_token: true,
      ..Default::default()
    };
    assert!(
      matches!(run_blocking(&options), Err(AuthError::Config(message)) if message.contains("active Tokio runtime"))
    );
    assert!(matches!(run(&options).await.unwrap(), RunOutcome::Token(_)));
  }
}