Détail du package

registry-auth-token

rexxars55mMIT5.1.0

Get the auth token set for an npm registry (if any)

npm, conf, config, npmconf

readme

registry-auth-token

npm versionnpm

Get the auth token set for an npm registry from .npmrc. Also allows fetching the configured registry URL for a given npm scope.

Installing

npm install --save registry-auth-token

Usage

Returns an object containing token and type, or undefined if no token can be found. type can be either Bearer or Basic.

const getAuthToken = require('registry-auth-token')
const getRegistryUrl = require('registry-auth-token/registry-url')

// Get auth token and type for default `registry` set in `.npmrc`
console.log(getAuthToken()) // {token: 'someToken', type: 'Bearer'}

// Get auth token for a specific registry URL
console.log(getAuthToken('//registry.foo.bar'))

// Find the registry auth token for a given URL (with deep path):
// If registry is at `//some.host/registry`
// URL passed is `//some.host/registry/deep/path`
// Will find token the closest matching path; `//some.host/registry`
console.log(getAuthToken('//some.host/registry/deep/path', {recursive: true}))

// Use the npm config that is passed in
console.log(getAuthToken('//registry.foo.bar', {
  npmrc: {
    'registry': 'http://registry.foo.bar',
    '//registry.foo.bar/:_authToken': 'qar'
  }
}))

// Find the configured registry url for scope `@foobar`.
// Falls back to the global registry if not defined.
console.log(getRegistryUrl('@foobar'))

// Use the npm config that is passed in
console.log(getRegistryUrl('http://registry.foobar.eu/', {
  'registry': 'http://registry.foobar.eu/',
  '//registry.foobar.eu/:_authToken': 'qar'
}))

Return value

// If auth info can be found:
{token: 'someToken', type: 'Bearer'}

// Or:
{token: 'someOtherToken', type: 'Basic'}

// Or, if nothing is found:
undefined

Security

Please be careful when using this. Leaking your auth token is dangerous.

License

MIT © Espen Hovlandsdal

changelog

Change Log

All notable changes will be documented in this file.

[5.1.0] - 2025-02-07

Changes

  • Use WHATWG URL instead of legacy APIs, silencing errors in Bun etc (fisker Cheung)

[5.0.3] - 2024-11-27

Changes

  • Make all arguments optional in type definitions (Mattias Leino)

[5.0.2] - 2023-03-05

Changes

  • Prevent crashing on invalid npmrc files (Espen Hovlandsdal)

[5.0.0] - 2022-06-16

BREAKING

  • Require node version 14 or above (Espen Hovlandsdal)

Added

  • Add typescript definitions (Espen Hovlandsdal)

Changes

  • Replace outdated rc dependency with @pnpm/npm-conf (Kyler Nelson)
  • Fix incorrect usage information in readme (Kyler Nelson)

[4.2.2] - 2022-06-16

Changes

[4.2.1] - 2020-11-10

Changes

  • Exclude tests from published npm files (Garrit Franke)

[4.2.0] - 2020-07-13

Changes

  • Add support for NPM_CONFIG_USERCONFIG environment variable (Ben Sorohan)

[4.1.0] - 2020-01-17

Changes

  • Add support for legacy auth token on the registry url (Gustav Blomér)

[4.0.0] - 2019-06-17

BREAKING

  • Minimum node.js version requirement is now v6

Changes

  • Upgraded dependencies (Espen Hovlandsdal)

[3.4.0] - 2019-03-20

Changes

  • Enabled legacy auth token to be read from environment variable (Martin Flodin)

[3.3.2] - 2018-01-26

Changes

  • Support password with ENV variable tokens (Nowell Strite)

[3.3.1] - 2017-05-02

Fixes

  • Auth legacy token is basic auth (Hutson Betts)

3.3.0 - 2017-04-24

Changes

  • Support legacy auth token config key (Zoltan Kochan)
  • Use safe-buffer module for backwards-compatible base64 encoding/decoding (Espen Hovlandsdal)
  • Change to standard.js coding style (Espen Hovlandsdal)

3.2.0 - 2017-04-20

Changes

  • Allow passing parsed npmrc from outside (Zoltan Kochan)

3.1.2 - 2017-04-07

Changes

  • Avoid infinite loop on invalid URL (Zoltan Kochan)

3.1.1 - 2017-04-06

Changes

  • Nerf-dart URLs even if recursive is set to false (Espen Hovlandsdal)

3.1.0 - 2016-10-19

Changes

  • Return the password and username for Basic authorization (Zoltan Kochan)

3.0.1 - 2016-08-07

Changes

  • Fix recursion bug (Lukas Eipert)
  • Implement alternative base64 encoding/decoding implementation for Node 6 (Lukas Eipert)

3.0.0 - 2016-08-04

Added

  • Support for Basic Authentication (username/password) (Lukas Eipert)

Changes

  • The result format of the output changed from a simple string to an object which contains the token type
// before: returns 'tokenString'
// after: returns {token: 'tokenString', type: 'Bearer'}
getAuthToken();

2.1.1 - 2016-07-10

Changes

  • Fix infinite loop when recursively resolving registry URLs on Windows (Espen Hovlandsdal)

2.1.0 - 2016-07-07

Added

  • Add feature to find configured registry URL for a scope (Espen Hovlandsdal)

2.0.0 - 2016-06-17

Changes

  • Fix tokens defined by reference to environment variables (Dan MacTough)

1.1.1 - 2016-04-26

Changes

  • Fix for registries with port number in URL (Ryan Day)