Skip to content

Commit ec6609d

Browse files
liuye.adamisaacs
liuye.adam
authored andcommitted
fix: get auth token correct when login with sso
PR-URL: #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.
1 parent 0108123 commit ec6609d

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

auth.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ const getAuth = (uri, opts = {}) => {
5252
if (forceAuth && !regKey) {
5353
return new Auth({
5454
scopeAuthKey: null,
55-
token: forceAuth._authToken,
55+
token: forceAuth._authToken || forceAuth.token,
5656
username: forceAuth.username,
5757
password: forceAuth._password || forceAuth.password,
5858
auth: forceAuth._auth || forceAuth.auth,

test/auth.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,36 @@ t.test('forceAuth', t => {
113113
.then(res => t.equal(res, 'success', 'used forced auth details'))
114114
})
115115

116+
t.test('forceAuth token', t => {
117+
const config = {
118+
registry: 'https://my.custom.registry/here/',
119+
token: 'deadbeef',
120+
'always-auth': false,
121+
'//my.custom.registry/here/:_authToken': 'c0ffee',
122+
'//my.custom.registry/here/:token': 'nope',
123+
forceAuth: {
124+
token: 'cafebad',
125+
},
126+
}
127+
t.same(getAuth(config.registry, config), {
128+
scopeAuthKey: null,
129+
isBasicAuth: false,
130+
token: 'cafebad',
131+
auth: null,
132+
}, 'correct forceAuth token picked out')
133+
134+
const opts = Object.assign({}, OPTS, config)
135+
tnock(t, opts.registry)
136+
.matchHeader('authorization', auth => {
137+
t.equal(auth[0], 'Bearer cafebad', 'got correct bearer token')
138+
return auth[0] === 'Bearer cafebad'
139+
})
140+
.get('/hello')
141+
.reply(200, '"success"')
142+
return fetch.json('/hello', opts)
143+
.then(res => t.equal(res, 'success', 'token forceAuth succeeded'))
144+
})
145+
116146
t.test('_auth auth', t => {
117147
const config = {
118148
registry: 'https://my.custom.registry/here/',

0 commit comments

Comments
 (0)