Skip to content

Instantly share code, notes, and snippets.

@malys
Last active February 13, 2020 14:54
Show Gist options
  • Select an option

  • Save malys/32dcc9ae7057ca2439ed6eb596102a9c to your computer and use it in GitHub Desktop.

Select an option

Save malys/32dcc9ae7057ca2439ed6eb596102a9c to your computer and use it in GitHub Desktop.
[Artillery] functions #artillery #oauth #openidc
// requestSpec will be the request spec for this response (currently always null)
// response is a Request.js response object
// context is the scenario context containing scenario variables
// ee is an event emitter for this scenario that we can use to add custom stats to the report
// npm install jsonwebtoken
const jwt = require('jsonwebtoken');
const util = require('util');
const tokenConfig = {
scope: 'openid',
};
const httpOptions = {};
let prettyJSONLog = (payload) => {
console.log(util.inspect(payload, {
showHidden: false,
depth: null
}));
}
var richOuput = (payload) => {
payload.iatDate = new Date(payload.iat * 1000);
payload.expDate = new Date(payload.exp * 1000);
prettyJSONLog(payload)
}
/*
let token="eyJhbGciOiJSUzI1NiIsInR5cCIgOiAiSldUIiwia2lkIiA6ICJSZlZSaFlycmxEN3JOUkhJVVVEQXI1Smp4R3pEd29SRng2dTBEQlBkdXJZIn0.eyJqdGkiOiJjMTFiZGRmMC01ZjQ5LTRmOWMtOWYzYy03ZmRjNmE3YWQzMmMiLCJleHAiOjE1ODEwNjQ1NzcsIm5iZiI6MCwiaWF0IjoxNTgxMDY0Mjc3LCJpc3MiOiJodHRwczovL2FwaS1pbnRlMDIubGJnLm9mZmljZS5mci5seXJhL2F1dGgvcmVhbG1zL21hcmtldHBsYWNlIiwiYXVkIjoic29uZGUiLCJzdWIiOiI1MDRlMTQ5Mi1iMDZmLTQ0Y2QtYWE5OS1jM2VmNDY2NWZlM2YiLCJ0eXAiOiJCZWFyZXIiLCJhenAiOiJzb25kZSIsImF1dGhfdGltZSI6MCwic2Vzc2lvbl9zdGF0ZSI6ImQxN2U1MGFlLWJhYTYtNDRiMy04M2U5LWRmMmJhOGRlNDEyYSIsImFjciI6IjEiLCJhbGxvd2VkLW9yaWdpbnMiOlsiKiJdLCJyZXNvdXJjZV9hY2Nlc3MiOnt9LCJhdWQiOiJhcGktc2VydmljZSIsImNsaWVudElkIjoic29uZGUiLCJjbGllbnRIb3N0IjoiMTAuMzMuMTE4LjM5IiwiYXpwIjoiYXBpLXNlcnZpY2UiLCJwcmVmZXJyZWRfdXNlcm5hbWUiOiJzb25kZSIsImNsaWVudEFkZHJlc3MiOiIxMC4zMy4xMTguMzkifQ.Y1SqyD6CTulzZ1mrtr6JEGhBE97bbo42SL_2B8hk4-X0A_rRxLGIhG1I7qwA8GLLFaRSjNDdsSXmVIyuu1foWNsyWE8aTBof1JTuOT1VaE6M0L1-oV5VjJe3pKb_i0VQ5m9RaR33a9gZ--Qr4QTLjLSnZvOhhhePPjY7LR4OxDBP9lurPSoGuy2ZDxFUkeBJOdBxeSqcL8mr7z44ZkciVmbLEQqXpnGUvzto8tnf0QHhy5gOkqUsxUaaWHzySTsHzvJ91SbD7HcLLtZvuA_R5U_fDONpS3YBq7kvhDS24cDpTQ4IoIdqKmWlX5dUFca5t1a-GTJj3KSP0iAAu4ggUQ"
let decoded = jwt.decode(token, {
complete: true
});
console.log(decoded.signature)*/
module.exports = {
clientCredentials: (context, events, done) => {
//console.log(context.vars)
const credentials = {
client: {
id: context.vars.client_id,
secret: context.vars.client_secret
},
auth: {
tokenHost: context.vars.auth_server,
tokenPath: '/auth/realms/' + context.vars.realm + '/protocol/openid-connect/token'
}
};
const oauth2 = require('simple-oauth2').create(credentials);
oauth2.clientCredentials.getToken(tokenConfig, httpOptions)
.then((result) => {
let token = oauth2.accessToken.create(result).token
context.vars.access_token = token.access_token
context.vars.refresh_token = token.refresh_token
context.vars.id_token = token.id_token
return done()
}, (failure) => {
console.error(failure)
return done()
})
},
addDateTime: ((requestParams, context, ee, next) => {
var d = new Date();
d.setHours(d.getHours() - 2);
context.vars.from = d.toISOString().slice(0, 16) + 'Z';
return next();
}),
basicAuthentification: ((requestParams, context, ee, next) => {
requestParams.headers['Authorization'] = 'Basic ' + new Buffer(context.vars.username + ':' + context.vars.password).toString('base64');
return next();
}),
parseJWT: ((requestParams, response, context, ee, next) => {
console.log("------------------------------------------------")
// get the decoded payload and header
let body = JSON.parse(response.body);
//console.log(body)
if (body.access_token) {
let decoded = jwt.decode(body.access_token, {
complete: true
});
richOuput(decoded.payload)
}
if (body.refresh_token) {
let decoded = jwt.decode(body.refresh_token, {
complete: true
});
richOuput(decoded.payload)
}
//console.log('-> Realm role' ,decoded.payload.realm_access.roles)
//console.log('-> testPublic role',decoded.payload.resource_access.testPublic.roles)
return next();
}),
parseJWTAccess: ((requestParams, response, context, ee, next) => {
console.log("------------------------------------------------")
// get the decoded payload and header
let body = JSON.parse(response.body);
//console.log(body)
if (body.access_token) {
let decoded = jwt.decode(body.access_token, {
complete: true
});
richOuput(decoded.payload)
}
//console.log('-> Realm role' ,decoded.payload.realm_access.roles)
//console.log('-> testPublic role',decoded.payload.resource_access.testPublic.roles)
return next();
}),
responseLatency: ((requestParams, response, context, ee, next) => {
for (const key in response.headers) {
if (key.toUpperCase().indexOf("LATENCY") > -1) console.log(key, response.headers[key])
}
return next();
}),
prettyJSONLog: ((requestParams, response, context, ee, next)=>{
prettyJSONLog(JSON.parse(response.body))
}),
};
processor: "./artilleryFunctions.js"
scenarios:
- name: apigateway
beforeScenario: clientCredentials
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment