Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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...

like image 969
Robin Southgate Avatar asked Jul 08 '17 08:07

Robin Southgate


People also ask

How do I connect npm to Artifactory?

For example, if you are using Artifactory standalone or as a local service, you would access your npm repositories using the following URL: http://localhost:8081/artifactory/api/npm/<repository key> Or, if you are using Artifactory Cloud, the URL would be: https://<server name>.

How do I add an npm repository to Artifactory?

Edit your package. json file and add a publishConfig section to a local repository: "publishConfig":{"registry":"http://localhost:8081/artifactory/api/npm/npm-repo/"} Provide a local repository to the npm publish command: npm publish --registry http://localhost:8081/artifactory/api/npm/npm-repo/

How do I use my Artifactory API key?

To use your API key for Basic Authentication, it must be generated using Artifactory 4.4. 3 or later. If generated on a previous version, you must regenerate your API key and use the new key as a password for basic authentication.


2 Answers

What is your artifactory version?

The "npm login" way is only supported since 5.4 (aka. _authToken), so if you are below that, your only chance is to authenticate with BASIC authentication (aka. _auth).

Basic authentication is fairly easy to setup, Artifactory provides an easy entry point to help you set up your .npmrc

Launch the following (and do not forget to replace user and encrypted_password with the Artifactory user account you want to authenticate with)

curl -u user:encrypted_password https://mydomain.jfrog.io/mydomain/api/npm/auth

It will output something like

_auth = YourLongBasicAuthToken always-auth = true email = [email protected]

Copy these 3 lines in your .npmrc, and this should work well...

like image 67
Arnaud Jeansen Avatar answered Oct 11 '22 15:10

Arnaud Jeansen


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

like image 5
pHiL Avatar answered Oct 11 '22 16:10

pHiL