Skip to content

Commit

Permalink
feat: Replace jwt expiration with a config and environment variable (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
yakanechi authored Jul 18, 2024
1 parent bed1fac commit 18fe16d
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 4 deletions.
1 change: 1 addition & 0 deletions config/custom-environment-variables.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
auth:
# A public key for verifying JWTs signed by api.screwdriver.cd
jwtPublicKey: SECRET_JWT_PUBLIC_KEY
jwtMaxAge: JWT_MAX_AGE

httpd:
# Port to listen on
Expand Down
1 change: 1 addition & 0 deletions config/default.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ auth:
-----BEGIN PUBLIC KEY-----
INSERT STUFF HERE
-----END PUBLIC KEY-----
jwtMaxAge: 13h

httpd:
# Port to listen on
Expand Down
5 changes: 3 additions & 2 deletions plugins/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ exports.plugin = {
const pluginOptions = joi.attempt(
options,
joi.object().keys({
jwtPublicKey: joi.string().required()
jwtPublicKey: joi.string().required(),
jwtMaxAge: joi.string().required()
}),
'Invalid config for auth plugin'
);
Expand All @@ -40,7 +41,7 @@ exports.plugin = {
key: pluginOptions.jwtPublicKey,
verifyOptions: {
algorithms: ['RS256'],
maxAge: '13h'
maxAge: pluginOptions.jwtMaxAge
},
// This function is run once the Token has been decoded with signature
validate
Expand Down
3 changes: 2 additions & 1 deletion test/lib/server.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ describe('server case', function () {
engine: new Catbox()
},
auth: {
jwtPublicKey: '12345'
jwtPublicKey: '12345',
jwtMaxAge: '1h'
},
commands: {},
ecosystem,
Expand Down
43 changes: 42 additions & 1 deletion test/plugins/auth.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ describe('auth plugin test', () => {
plugin = require('../../plugins/auth');

options = {
jwtPublicKey: fs.readFileSync(path.join(__dirname, './data/auth.test.crt'), 'utf8')
jwtPublicKey: fs.readFileSync(path.join(__dirname, './data/auth.test.crt'), 'utf8'),
jwtMaxAge: '1h'
};

server = Hapi.server({
Expand Down Expand Up @@ -90,4 +91,44 @@ describe('auth plugin test', () => {
assert.equal(200, response.statusCode);
assert.equal('success', response.result);
});

it('jwt expiration', async () => {
const privateKey = fs.readFileSync(path.join(__dirname, './data/auth.test.key'), 'utf8');
const token = jwt.sign({ data: 'some data' }, privateKey, {
algorithm: 'RS256'
});

let response;

await server.register({
plugin,
options: { ...options, jwtMaxAge: '0h' }
});

server.route({
method: 'GET',
path: '/',
options: {
auth: 'token'
},
handler() {
return 'success';
}
});

try {
response = await server.inject({
method: 'GET',
url: '/',
headers: {
Authorization: token
}
});
} catch (err) {
assert.fail(err);
}

assert.equal(401, response.statusCode);
assert.equal('Unauthorized', response.statusMessage);
});
});

0 comments on commit 18fe16d

Please sign in to comment.