From ec6609d7d003d922a540540eb733f3d2e18524fd Mon Sep 17 00:00:00 2001 From: "liuye.adam" Date: Fri, 7 May 2021 16:37:02 +0800 Subject: [PATCH] fix: get auth token correct when login with sso PR-URL: https://github.com/npm/npm-registry-fetch/pull/50 Credit: @ShangguanQuail Close: #50 Reviewed-by: @isaacs EDIT(@isaacs): updated token in test so that it was more clear which one was being selected. --- auth.js | 2 +- test/auth.js | 30 ++++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/auth.js b/auth.js index 80aed9ca..01a4436a 100644 --- a/auth.js +++ b/auth.js @@ -52,7 +52,7 @@ const getAuth = (uri, opts = {}) => { if (forceAuth && !regKey) { return new Auth({ scopeAuthKey: null, - token: forceAuth._authToken, + token: forceAuth._authToken || forceAuth.token, username: forceAuth.username, password: forceAuth._password || forceAuth.password, auth: forceAuth._auth || forceAuth.auth, diff --git a/test/auth.js b/test/auth.js index a64394e7..b024b9f4 100644 --- a/test/auth.js +++ b/test/auth.js @@ -113,6 +113,36 @@ t.test('forceAuth', t => { .then(res => t.equal(res, 'success', 'used forced auth details')) }) +t.test('forceAuth token', t => { + const config = { + registry: 'https://my.custom.registry/here/', + token: 'deadbeef', + 'always-auth': false, + '//my.custom.registry/here/:_authToken': 'c0ffee', + '//my.custom.registry/here/:token': 'nope', + forceAuth: { + token: 'cafebad', + }, + } + t.same(getAuth(config.registry, config), { + scopeAuthKey: null, + isBasicAuth: false, + token: 'cafebad', + auth: null, + }, 'correct forceAuth token picked out') + + const opts = Object.assign({}, OPTS, config) + tnock(t, opts.registry) + .matchHeader('authorization', auth => { + t.equal(auth[0], 'Bearer cafebad', 'got correct bearer token') + return auth[0] === 'Bearer cafebad' + }) + .get('/hello') + .reply(200, '"success"') + return fetch.json('/hello', opts) + .then(res => t.equal(res, 'success', 'token forceAuth succeeded')) +}) + t.test('_auth auth', t => { const config = { registry: 'https://my.custom.registry/here/',