Skip to content

Commit

Permalink
fix(auth): Throw some reasonable error message in basic authenticatio…
Browse files Browse the repository at this point in the history
…n when we can't obtain an access token. (#174)
  • Loading branch information
tremes authored and lholmquist committed Nov 19, 2019
1 parent 02d1427 commit 53614e2
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 5 deletions.
13 changes: 8 additions & 5 deletions lib/basic-auth-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,14 @@ async function getTokenFromBasicAuth (settings) {
}

const hash = resp.request.uri.hash;
const startIndex = hash.indexOf('=') + 1;
const stopIndex = hash.indexOf('&');
const accessToken = hash.slice(startIndex, stopIndex);

return resolve(accessToken);
if (hash) {
const startIndex = hash.indexOf('=') + 1;
const stopIndex = hash.indexOf('&');
const accessToken = hash.slice(startIndex, stopIndex);
return resolve(accessToken);
} else {
return reject(new Error(`Unable to authenticate user ${settings.user} to ${resp.request.uri.host}. Cannot obtain access token from response.`));
}
});
});
}
Expand Down
34 changes: 34 additions & 0 deletions test/basic-auth-request-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,37 @@ test('basic auth request with defined auth url', (t) => {
t.end();
});
});

test('basic auth request with missing hash', (t) => {
const basicAuthRequest = proxyquire('../lib/basic-auth-request', {
request: (requestObject, cb) => {
t.true(requestObject.url.includes('https://test'), 'Unexpected auth url value');
return cb(null, {
statusCode: 200,
request: {
uri: {
hash: undefined,
host: 'testhost'
}
}
});
}
});

const settings = {
url: 'http://',
authUrl: 'https://test',
user: 'username',
password: 'password',
insecureSkipTlsVerify: true
};

const p = basicAuthRequest.getTokenFromBasicAuth(settings);

t.equal(p instanceof Promise, true, 'is an Promise');

p.catch((error) => {
t.equal(error.message, 'Unable to authenticate user username to testhost. Cannot obtain access token from response.', 'should be equal');
t.end();
});
});

0 comments on commit 53614e2

Please sign in to comment.