How should I use artifactory access token for access to npm repo

Viewed 35751

I would like to use an access token to publish and retrieve from an artifactory npm repo from a CI environment. I have created a Bearer token using the artifactory API but when I try and use that for access in the .npmrc with the format:

//mydomain.jrog.io/:_authToken=myveryverylongaccesstoken
registry=https://mydomain.jfrog.io/mydomain/api/npm/npm

I always receive 401 errors back.

In addition, though perhaps a different issue, if I try to use npm login with my actual artifactory credentials I get the response:

adduser Incorrect username or password
npm WARN adduser You can reset your account by visiting:
npm WARN adduser 
npm WARN adduser     https://npmjs.org/forgot
npm WARN adduser 
npm ERR! code E403
npm ERR! forbidden No oauth npm default login configuration found: org.couchdb.user:myusername

The artifactory docs around access tokens explicitly say this is the sort of use case to set up an access token, but the docs around setting up the npm repo alwyas seem to imply you need a real user account and make no mention of access tokens...

5 Answers

To generate the contents for .npmrc, use an existing user like admin with its password

curl -uadmin:<PASSWORD> http://<ARTIFACTORY_SERVER_DOMAIN>/artifactory/api/npm/auth

afterwards you can replace/set the _auth key with any base64 encoded username:ACCESS_TOKEN

for scoped packages, use

curl -uadmin:<PASSWORD> http://<ARTIFACTORY_SERVER_DOMAIN>/artifactory/api/npm/npm-repo/auth/<SCOPE>

and set ..username and .._password (base64) with any user and access token

  1. Using Basic Authentication Using the next described steps you should change your local .nprmc file with the output of the command
  • To retrieve the next fields _auth, email and always-auth automatically generated -> Run the following command to retrieve these strings directly from Artifactory:

curl -u {username}:{API_key} http://{artifactory_server_domain}/{domain}/artifactory/api/npm/auth

Notes:

The _auth line in the other answers breaks some scoped package retrieval from npmjs. I found that I needed to use specific _auth for just the one registry I was using. See my original answer here https://stackoverflow.com/a/57014522/442837

For windows user :-

1/ Add https://registry.npmjs.org to .npmrc to user profile on top:

registry=https://registry.npmjs.org

or 2/ Run below command

npm config set registry  https://registry.npmjs.org
Related