npm ERR! Unable to authenticate, need: Basic realm="Artifactory Realm"

Viewed 50133

I tried to run the command:

npm install -g @angular/cli@9.1.0

but I have got the following error:

npm ERR! code E401
npm ERR! Unable to authenticate, need: Basic realm="Artifactory Realm"

npm ERR! A complete log of this run can be found in:
npm ERR! /Users/xxxx/.npm/_logs/2021-08-10T19_33_12_063Z-debug.log

note: node js and npm worked fine.

I have followed the instructions in Artifactory to solve this issue using the command:

npm config set registry https://artifactory.......com/artifactory/api/npm/xxxx/

as I have paste the following into the ~/.npmrc file :

_auth = fhgf......ghgj==
email = xxx@xxx.com
always-auth = true

I have also tried using npm login, but I have got the below err and couldn’t continue:

npm login
Username: xxx@xxx.com
npm WARN Name may not contain non-url-safe chars 
Username: (xxx@xxx.com)
Username: (xxx@xxx.com)
Username: (xxx@xxx.com)
Username: (xxx@xxx.com)

How can i solve this issue?

8 Answers

Update Dec, 2021

Artifactory moved to support APIKEY only. If your old _auth was base64 encoding of username:password or username:encrypted_password then both are unacceptable now. You must use APIKEY in place of these.

So, the supported _auth now becomes:

_auth: APIKEY

But ironically, even this didn't work in some cases.

Following seemed to be more reliable:

  1. You need to get base64 string of your username:APIKEY. You can get base64 by running following command in PowerShell

[System.Convert]::ToBase64String([Text.Encoding]::UTF8.GetBytes("username:APIKEY"))

  1. Whatever is the output, use that in _auth variable of your .npmrc file (located in %userprofile%\.npmrc)

So, the final file looks like following:

registry=<URL>
_auth = <Base64 of username:APIKEY>
email = myemail@email.com
always-auth = true

The issue is due to the '@' special character in the username, I assume the user is SAML based. Follow the below steps to resolve the issue,

  1. Generate the auth token using the /api/npm/auth endpoint with “curl -u :<API_KEY> https://url/artifactory/api/npm/auth/”
  2. Add the generated block directly into .npmrc file as

_auth = Auth-token-generated-from-1st-point

always-auth = true

Try something like this.

.npmrc file content like this:

//npm.corp_id.com/:_authToken=TOKEN_VALUE_HERE

Removing _ from authToken in .npmrc, It's just for me

//npm.corp_id.com/:authToken=TOKEN_VALUE_HERE

Login to https://url/artifactory/api/npm/auth/ and copy the _auth value from here to the .npmrc file

This issue can also occur when you try to install the package you are publishing as a dependency. I.e. publishing package name @core with a dependency also named @core

For me, the issue was calling USER myusername too early in the Dockerfile, it caused an authentication issue with my artifactory even though the .npmrc file was in place.

Simply moving the USER call down, after npm i fixed the issue for me.

vim .npmrc(to edit the configuration file) i - to insert registry=<URL> _auth = <Base64 of username:APIKEY> email = myemail@email.com always-auth = true

to get the Base64, you can do in your terminal

$echo -n cb4b84e9-b861-443c-9ed3-958086d97208 | base64

wq - to save document

For me, I already had .npmrc file with someone else's expired token. Hence, when i was doing npm login, it was giving this authentication error.

To fix this, i deleted the .npmrc, executed npm config set registry ... command and then npm login was executed.

Related