Skip to content

Commit 3dc7bb1

Browse files
sakka2catto
authored andcommitted
fix(1030): Not to exchange token if it is already the build token (#44)
1 parent 7f99896 commit 3dc7bb1

File tree

3 files changed

+32
-1
lines changed

3 files changed

+32
-1
lines changed

index.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const Joi = require('joi');
55
const dataSchema = require('screwdriver-data-schema');
66
const executorSchema = dataSchema.plugins.executor;
77
const request = require('requestretry');
8+
const jwt = require('jsonwebtoken');
89
const DEFAULT_BUILD_TIMEOUT = 90; // in minutes
910

1011
/**
@@ -24,6 +25,18 @@ async function validate(config, schema) {
2425
return config;
2526
}
2627

28+
/**
29+
* Check if the scope of jwt is temporal or not
30+
* @async isTemporalJwt
31+
* @param {String} token Jwt
32+
* @return {Boolean}
33+
*/
34+
function isTemporalJwt(token) {
35+
const decoded = jwt.decode(token);
36+
37+
return decoded.scope.includes('temporal');
38+
}
39+
2740
class Executor {
2841
/**
2942
* Constructor for Executor
@@ -135,6 +148,11 @@ class Executor {
135148
* @return {Promise}
136149
*/
137150
async exchangeTokenForBuild(config, buildTimeout = DEFAULT_BUILD_TIMEOUT) {
151+
// Use token directly if the scope is already 'build' (#1030)
152+
if (!isTemporalJwt(config.token)) {
153+
return config.token;
154+
}
155+
138156
if (isFinite(buildTimeout) === false) {
139157
throw new Error(`Invalid buildTimeout value: ${buildTimeout}`);
140158
}

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
},
4747
"dependencies": {
4848
"joi": "^13.0.0",
49+
"jsonwebtoken": "^8.2.2",
4950
"requestretry": "^1.13.0",
5051
"screwdriver-data-schema": "^18.20.0"
5152
}

test/index.test.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const { assert } = require('chai');
44
const sinon = require('sinon');
55
const mockery = require('mockery');
66
const Joi = require('joi');
7+
const jwt = require('jsonwebtoken');
78
const DEFAULT_BUILD_TIMEOUT = 90; // in minutes
89

910
describe('index test', () => {
@@ -164,12 +165,14 @@ describe('index test', () => {
164165
let options;
165166
let buildTimeout;
166167
let fakeResponse;
168+
let token;
167169

168170
beforeEach(() => {
171+
token = jwt.sign({ scope: ['temporal'] }, 'dummyPrivateKey');
169172
postConfig = {
170173
buildId: 111,
171174
apiUri: 'https://dummy.com',
172-
token: 'dummyTemporalToken'
175+
token
173176
};
174177
buildTimeout = 150;
175178
options = {
@@ -205,6 +208,15 @@ describe('index test', () => {
205208
});
206209
});
207210

211+
it('does not exchange and return token as it is if it is already build JWT', async () => {
212+
token = jwt.sign({ scope: ['build'] }, 'dummyPrivateKey');
213+
postConfig.token = token;
214+
215+
await instance.exchangeTokenForBuild(postConfig).then((buildToken) => {
216+
assert.equal(postConfig.token, buildToken);
217+
});
218+
});
219+
208220
it('returns error if buildTimeout value is invalid', async () => {
209221
buildTimeout = 'aaa';
210222
const returnMessage = `Invalid buildTimeout value: ${buildTimeout}`;

0 commit comments

Comments
 (0)