node-auth 0.4.1

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
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
//! `.npmrc` configuration parsing, formatting, and transformation.
//!
//! Handles scoped and unscoped Artifact Registry repository definitions, `_authToken` credential lines,
//! legacy basic auth password and username cleanup, and comment/formatting preservation.

use crate::error::AuthError;
use crate::fs::{FileTransaction, resolve};
use crate::registry::{RegistryPolicy, RegistryUrl};
use crate::token::validate_token;
use regex::Regex;
use std::collections::BTreeMap;
use std::path::Path;

/// Recognized line configurations in `.npmrc` files.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum NpmrcConfigType {
  /// Registry URL specification (e.g. `@scope:registry=https://...` or `registry=https://...`).
  Registry {
    /// Optional npm package scope (e.g. `@my-org`).
    scope: Option<String>,
    /// Normalized registry URL (e.g. `//us-central1-npm.pkg.dev/proj/repo/`).
    registry: String,
    /// Original unmodified raw line content.
    raw: String,
  },

  /// Auth token credential line (e.g. `//...:_authToken="..."`).
  AuthToken {
    /// Normalized registry URL associated with this token.
    registry: String,
    /// Extracted authentication token string.
    token: String,
  },

  /// Legacy basic auth password line (e.g. `//...:_password=...`).
  Password {
    /// Normalized registry URL associated with this password.
    registry: String,
    /// Base64-encoded password value.
    password: String,
  },

  /// Legacy basic auth username line (e.g. `//...:username=oauth2accesstoken`).
  Username {
    /// Normalized registry URL.
    registry: String,
  },

  /// Comments, blank lines, or other unrecognized configuration keys.
  Other(String),
}

impl NpmrcConfigType {
  /// Formats the configuration entry back into an `.npmrc` line.
  pub fn to_line(&self) -> String {
    match self {
      NpmrcConfigType::Registry { raw, .. } => raw.clone(),
      NpmrcConfigType::AuthToken { registry, token } => {
        format!("{registry}:_authToken=\"{token}\"")
      }
      NpmrcConfigType::Password { registry, password } => {
        format!("{registry}:_password={password}")
      }
      NpmrcConfigType::Username { registry } => {
        format!("{registry}:username=oauth2accesstoken")
      }
      NpmrcConfigType::Other(line) => line.clone(),
    }
  }
}

/// Regular-expression-based parser for `.npmrc` lines.
pub struct NpmrcParser {
  policy: RegistryPolicy,
  registry_re: Regex,
  auth_token_re: Regex,
  password_re: Regex,
  username_re: Regex,
}

impl NpmrcParser {
  /// Creates a new parser instance.
  ///
  /// If `allow_all_domains` is false, only `*-npm.pkg.dev` Artifact Registry domains match.
  pub fn new(allow_all_domains: bool) -> Result<Self, AuthError> {
    Ok(Self {
      policy: RegistryPolicy { allow_all_domains },
      registry_re: Regex::new(
        r"^(@[a-zA-Z0-9-*~][a-zA-Z0-9-*._~]*:)?registry=(https://[^ \t\r\n]+)$",
      )?,
      auth_token_re: Regex::new(r"^(//[^ \t\r\n]+):_authToken=.*$")?,
      password_re: Regex::new(r"^(//[^ \t\r\n]+):_password=.*$")?,
      username_re: Regex::new(r"^(//[^ \t\r\n]+):username=oauth2accesstoken$")?,
    })
  }

  /// Parses a single `.npmrc` configuration line into a [`NpmrcConfigType`].
  pub fn parse_line(&self, line: &str) -> Result<NpmrcConfigType, AuthError> {
    let trimmed = line.trim();
    if trimmed.is_empty() || trimmed.starts_with('#') || trimmed.starts_with(';') {
      return Ok(NpmrcConfigType::Other(line.to_string()));
    }

    if let Some(caps) = self.registry_re.captures(trimmed) {
      let scope = caps
        .get(1)
        .map(|m| m.as_str().trim_end_matches(':').to_string());
      let registry_raw = caps.get(2).map(|m| m.as_str()).unwrap_or("");

      if let Some(registry) = self.policy.parse(registry_raw)? {
        return Ok(NpmrcConfigType::Registry {
          scope,
          registry: registry.npm_key().to_string(),
          raw: line.to_string(),
        });
      }
    }

    if let Some(caps) = self.auth_token_re.captures(trimmed) {
      let key = caps.get(1).map(|m| m.as_str()).unwrap_or("");
      if let Some(registry) = self.policy.parse_npm_key(key)? {
        let token_part = trimmed
          .split_once(":_authToken=")
          .map(|(_, t)| t.trim().trim_matches('"').trim_matches('\'').to_string())
          .unwrap_or_default();

        return Ok(NpmrcConfigType::AuthToken {
          registry: registry.npm_key().to_string(),
          token: token_part,
        });
      }
    }

    if let Some(caps) = self.password_re.captures(trimmed) {
      let key = caps.get(1).map(|m| m.as_str()).unwrap_or("");
      if let Some(registry) = self.policy.parse_npm_key(key)? {
        let pass_part = trimmed
          .split_once(":_password=")
          .map(|(_, p)| p.trim().to_string())
          .unwrap_or_default();

        return Ok(NpmrcConfigType::Password {
          registry: registry.npm_key().to_string(),
          password: pass_part,
        });
      }
    }

    if let Some(caps) = self.username_re.captures(trimmed) {
      let key = caps.get(1).map(|m| m.as_str()).unwrap_or("");
      if let Some(registry) = self.policy.parse_npm_key(key)? {
        return Ok(NpmrcConfigType::Username {
          registry: registry.npm_key().to_string(),
        });
      }
    }

    Ok(NpmrcConfigType::Other(line.to_string()))
  }
}

/// Whether either npm configuration contains an eligible registry definition.
pub fn has_registry(
  from_content: &str,
  to_content: &str,
  allow_all_domains: bool,
) -> Result<bool, AuthError> {
  let parser = NpmrcParser::new(allow_all_domains)?;

  for line in from_content.lines().chain(to_content.lines()) {
    if matches!(parser.parse_line(line)?, NpmrcConfigType::Registry { .. }) {
      return Ok(true);
    }
  }

  Ok(false)
}

/// Pure in-memory transformation of `.npmrc` contents.
///
/// Strips out legacy passwords and usernames, strips sensitive tokens from project configuration
/// when writing to user configuration, and inserts the fresh auth token for each Artifact Registry scope.
///
/// # Arguments
///
/// * `from_content` - Raw text of the source repository `.npmrc`.
/// * `to_content` - Raw text of the target credential `.npmrc`.
/// * `creds` - The OAuth2 access token to insert.
/// * `allow_all_domains` - Whether non-GAR domains are allowed for authentication.
/// * `same_file` - True if reading and writing to the exact same file.
///
/// # Returns
///
/// Returns `(cleaned_from_content, updated_to_content)`.
///
/// # Errors
///
/// Returns [`AuthError::NoNpmRegistry`] if no eligible registry configurations were found in the input.
pub fn transform_npmrc_contents(
  from_content: &str,
  to_content: &str,
  creds: &str,
  allow_all_domains: bool,
  same_file: bool,
) -> Result<(String, String), AuthError> {
  transform_npmrc_contents_with_registries(
    from_content,
    to_content,
    creds,
    allow_all_domains,
    same_file,
    &[],
  )
}

/// Transform npm configuration and attach credentials for additional discovered registries.
pub(crate) fn transform_npmrc_contents_with_registries(
  from_content: &str,
  to_content: &str,
  creds: &str,
  allow_all_domains: bool,
  same_file: bool,
  additional_registries: &[RegistryUrl],
) -> Result<(String, String), AuthError> {
  validate_token(creds)?;

  let parser = NpmrcParser::new(allow_all_domains)?;
  let mut registries_found = BTreeMap::new();
  let mut from_lines_out = Vec::new();

  // Parse source config
  for line in from_content.lines() {
    match parser.parse_line(line)? {
      NpmrcConfigType::Registry {
        scope,
        registry,
        raw,
      } => {
        registries_found.insert(registry.clone(), scope);
        from_lines_out.push(raw);
      }

      NpmrcConfigType::AuthToken { registry, .. } => {
        if same_file {
          // Updating same file: will be refreshed below
        } else {
          log::debug!(
            "Moving existing _authToken for {registry} from project .npmrc to credential .npmrc"
          );
          // Strip from project config so secrets aren't checked into git
        }
      }

      NpmrcConfigType::Password { registry, .. } => {
        if !same_file {
          log::debug!("Removing legacy password for {registry} from project .npmrc");
        }
      }

      NpmrcConfigType::Username { .. } => {
        // Strip legacy oauth username line
      }

      NpmrcConfigType::Other(other) => {
        from_lines_out.push(other);
      }
    }
  }

  if !same_file {
    for line in to_content.lines() {
      if let NpmrcConfigType::Registry {
        scope, registry, ..
      } = parser.parse_line(line)?
      {
        registries_found.entry(registry).or_insert(scope);
      }
    }
  }

  for registry in additional_registries {
    registries_found
      .entry(registry.npm_key().to_string())
      .or_insert(None);
  }

  if registries_found.is_empty() {
    return Err(AuthError::NoNpmRegistry);
  }

  // Prepare credentials to write to to_content
  let mut to_lines_out = Vec::new();
  let mut pending_registries = registries_found.clone();

  if !to_content.is_empty() {
    for line in to_content.lines() {
      let parsed = parser.parse_line(line)?;
      match parsed {
        NpmrcConfigType::AuthToken { registry, .. } => {
          if registries_found.contains_key(&registry) {
            if pending_registries.remove(&registry).is_some() {
              to_lines_out.push(format!("{registry}:_authToken=\"{creds}\""));
            }
          } else {
            to_lines_out.push(line.to_string());
          }
        }

        NpmrcConfigType::Password { registry, .. } => {
          if registries_found.contains_key(&registry) {
            if pending_registries.remove(&registry).is_some() {
              to_lines_out.push(format!("{registry}:_authToken=\"{creds}\""));
            }
          } else {
            to_lines_out.push(line.to_string());
          }
        }

        NpmrcConfigType::Username { registry } => {
          if !registries_found.contains_key(&registry) {
            to_lines_out.push(line.to_string());
          }
        }
        _ => {
          to_lines_out.push(line.to_string());
        }
      }
    }
  }

  // Append auth tokens for registries not yet present in to_content
  for (registry, _) in pending_registries {
    log::debug!("Adding auth token for registry {registry}");
    to_lines_out.push(format!("{registry}:_authToken=\"{creds}\""));
  }

  let to_output = to_lines_out.join("\n") + "\n";
  let from_output = from_lines_out.join("\n") + "\n";

  Ok((from_output, to_output))
}

/// Updates `.npmrc` configuration files with the given access token.
///
/// # Arguments
///
/// * `from_path` - Path to `.npmrc` file to read registry settings from (e.g. project `.npmrc`).
/// * `to_path` - Path to `.npmrc` file to write credentials to (e.g. user `~/.npmrc` or local `./.npmrc`).
/// * `creds` - The OAuth2 access token string.
/// * `allow_all_domains` - Whether to allow non-pkg.dev domains.
/// # Errors
///
/// Returns [`AuthError::Io`] if reading or writing files fails, or [`AuthError::NoNpmRegistry`] if no registry was found.
pub fn update_npmrc_configs(
  from_path: &Path,
  to_path: &Path,
  creds: &str,
  allow_all_domains: bool,
) -> Result<(), AuthError> {
  let from_path = resolve(from_path)?;
  let to_path = resolve(to_path)?;
  let same_file = from_path == to_path;
  let mut transaction = FileTransaction::new(&[from_path.clone(), to_path.clone()])?;
  let from_content = transaction.contents(&from_path)?.to_owned();
  let to_content = transaction.contents(&to_path)?.to_owned();

  let (from_output, to_output) = transform_npmrc_contents(
    &from_content,
    if same_file {
      &from_content
    } else {
      &to_content
    },
    creds,
    allow_all_domains,
    same_file,
  )?;

  transaction.stage(&to_path, &to_output)?;

  if !same_file && transaction.existed(&from_path)? && from_output != from_content {
    transaction.stage(&from_path, &from_output)?;
  }

  transaction.commit()?;

  Ok(())
}

#[cfg(test)]
mod tests {
  use super::*;
  use std::fs;
  use tempfile::tempdir;

  #[test]
  fn missing_registry_returns_dedicated_error() {
    assert!(matches!(
      transform_npmrc_contents("", "", "valid-token", false, false),
      Err(AuthError::NoNpmRegistry)
    ));
  }

  #[test]
  fn test_parser_standard_ar() {
    let parser = NpmrcParser::new(false).unwrap();

    let line = "@my-org:registry=https://us-central1-npm.pkg.dev/my-project/my-repo/";
    let parsed = parser.parse_line(line).unwrap();
    match parsed {
      NpmrcConfigType::Registry {
        scope, registry, ..
      } => {
        assert_eq!(scope.as_deref(), Some("@my-org"));
        assert_eq!(registry, "//us-central1-npm.pkg.dev/my-project/my-repo/");
      }
      _ => panic!("Expected Registry variant"),
    }
  }

  #[test]
  fn test_parser_unscoped_ar() {
    let parser = NpmrcParser::new(false).unwrap();

    let line = "registry=https://europe-west1-npm.pkg.dev/google-cloud/demo-repo/";
    let parsed = parser.parse_line(line).unwrap();
    match parsed {
      NpmrcConfigType::Registry {
        scope, registry, ..
      } => {
        assert_eq!(scope, None);
        assert_eq!(
          registry,
          "//europe-west1-npm.pkg.dev/google-cloud/demo-repo/"
        );
      }
      _ => panic!("Expected Registry variant"),
    }
  }

  #[test]
  fn test_parser_existing_auth_token() {
    let parser = NpmrcParser::new(false).unwrap();

    let line = "//us-central1-npm.pkg.dev/my-project/my-repo/:_authToken=\"ya29.sample-token\"";
    let parsed = parser.parse_line(line).unwrap();
    match parsed {
      NpmrcConfigType::AuthToken { registry, token } => {
        assert_eq!(registry, "//us-central1-npm.pkg.dev/my-project/my-repo/");
        assert_eq!(token, "ya29.sample-token");
      }
      _ => panic!("Expected AuthToken variant"),
    }
  }

  #[test]
  fn test_in_memory_transform() {
    let from = "@corp:registry=https://us-central1-npm.pkg.dev/my-corp/my-npm/\nstrict-ssl=true\n";
    let to = "//registry.npmjs.org/:_authToken=\"npm_secret_xyz\"\n";
    let token = "fresh-adc-token-12345";

    let (from_out, to_out) = transform_npmrc_contents(from, to, token, false, false).unwrap();
    assert!(from_out.contains("@corp:registry=https://us-central1-npm.pkg.dev/my-corp/my-npm/"));
    assert!(from_out.contains("strict-ssl=true"));
    assert!(to_out.contains("//registry.npmjs.org/:_authToken=\"npm_secret_xyz\""));
    assert!(
      to_out
        .contains("//us-central1-npm.pkg.dev/my-corp/my-npm/:_authToken=\"fresh-adc-token-12345\"")
    );
  }

  #[test]
  fn duplicate_legacy_credentials_are_removed() {
    let source = "@corp:registry=https://us-npm.pkg.dev/project/repo/\n";
    let target = "//us-npm.pkg.dev/project/repo/:_authToken=old\n//us-npm.pkg.dev/project/repo/:_password=old\n//us-npm.pkg.dev/project/repo/:username=oauth2accesstoken\n";
    let (_, updated) = transform_npmrc_contents(source, target, "new", false, false).unwrap();
    assert_eq!(updated.matches(":_authToken=").count(), 1);
    assert!(!updated.contains("old"));
    assert!(!updated.contains(":username="));
  }

  #[test]
  fn test_update_npmrc_moves_token_to_user_config() {
    let dir = tempdir().unwrap();
    let project_npmrc = dir.path().join("project.npmrc");
    let user_npmrc = dir.path().join("user.npmrc");

    fs::write(
      &project_npmrc,
      "# Project configuration\n\
             @corp:registry=https://us-central1-npm.pkg.dev/my-corp/my-npm/\n\
             //us-central1-npm.pkg.dev/my-corp/my-npm/:_authToken=\"old-expired-token\"\n\
             strict-ssl=true\n",
    )
    .unwrap();

    fs::write(
      &user_npmrc,
      "//registry.npmjs.org/:_authToken=\"npm_secret_xyz\"\n",
    )
    .unwrap();

    let token = "fresh-adc-token-12345";
    update_npmrc_configs(&project_npmrc, &user_npmrc, token, false).unwrap();

    let project_out = fs::read_to_string(&project_npmrc).unwrap();
    let user_out = fs::read_to_string(&user_npmrc).unwrap();

    assert!(project_out.contains("@corp:registry=https://us-central1-npm.pkg.dev/my-corp/my-npm/"));
    assert!(project_out.contains("strict-ssl=true"));
    assert!(!project_out.contains("_authToken"));

    assert!(user_out.contains("//registry.npmjs.org/:_authToken=\"npm_secret_xyz\""));
    assert!(
      user_out
        .contains("//us-central1-npm.pkg.dev/my-corp/my-npm/:_authToken=\"fresh-adc-token-12345\"")
    );
  }

  #[test]
  fn test_update_npmrc_legacy_password_conversion() {
    let dir = tempdir().unwrap();
    let project_npmrc = dir.path().join("project.npmrc");
    let user_npmrc = dir.path().join("user.npmrc");

    fs::write(
      &project_npmrc,
      "@corp:registry=https://us-central1-npm.pkg.dev/my-corp/my-npm/\n\
             //us-central1-npm.pkg.dev/my-corp/my-npm/:_password=base64pass\n\
             //us-central1-npm.pkg.dev/my-corp/my-npm/:username=oauth2accesstoken\n",
    )
    .unwrap();

    update_npmrc_configs(&project_npmrc, &user_npmrc, "new-token", false).unwrap();

    let project_out = fs::read_to_string(&project_npmrc).unwrap();
    let user_out = fs::read_to_string(&user_npmrc).unwrap();

    assert!(!project_out.contains("_password"));
    assert!(!project_out.contains("username=oauth2accesstoken"));
    assert!(
      user_out.contains("//us-central1-npm.pkg.dev/my-corp/my-npm/:_authToken=\"new-token\"")
    );
  }

  #[test]
  fn test_update_npmrc_single_file_local_credential() {
    let dir = tempdir().unwrap();
    let local_npmrc = dir.path().join(".npmrc");

    fs::write(
      &local_npmrc,
      "@corp:registry=https://us-central1-npm.pkg.dev/my-corp/my-npm/\n\
             save-exact=true\n",
    )
    .unwrap();

    let token = "local-token-999";
    update_npmrc_configs(&local_npmrc, &local_npmrc, token, false).unwrap();

    let out = fs::read_to_string(&local_npmrc).unwrap();
    assert!(out.contains("@corp:registry=https://us-central1-npm.pkg.dev/my-corp/my-npm/"));
    assert!(out.contains("save-exact=true"));
    assert!(
      out.contains("//us-central1-npm.pkg.dev/my-corp/my-npm/:_authToken=\"local-token-999\"")
    );
  }
}