Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/interceptor/tokenProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const { tokenPost } = require('../oauth');
async function getToken () {
const environment = config.getEnvironment();
let accessToken = config.get(`${environment}.tokens.accessToken`) || process.env.PHC_ACCESS_TOKEN;
const defaults = config.get(`${environment}.defaults`);
const defaults = config.get(`${environment}.defaults`) || {};

if (!accessToken && !defaults.useClientCredentials && !defaults.useApiKey) {
throw new Error(`Need to run 'lo auth' to get an access token for this command, or 'lo setup' to set API key or client credentials`);
Expand Down
19 changes: 19 additions & 0 deletions test/unit/interceptor/tokenProvider.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,25 @@ test.serial(`tokenProvider should set the request Authorization header with a to
t.false(setSpy.called);
});

test.serial(`tokenProvider should set the request Authorization header with a token from the PHC_ACCESS_TOKEN env var if not configured`, async t => {
const token = jwt.sign({
sub: '1234',
iss: 'cognito',
token_use: 'access',
exp: 2000
}, 'secret');
getStub.withArgs('dev.tokens.accessToken').returns(null);
// defaults will not be present if `lo setup` has not been run
getStub.withArgs('dev.defaults').returns(null);
process.env.PHC_ACCESS_TOKEN = token;

const config = { headers: {} };
await tokenProvider(config);
t.is(config.headers.Authorization, `Bearer ${token}`);
t.false(postStub.called);
t.false(setSpy.called);
});

test.serial(`tokenProvider should set the request Authorization header with a token from config`, async t => {
const token = jwt.sign({
sub: '1234',
Expand Down
Loading