Skip to content

Commit

Permalink
PN-2750: change header keys to lower case (#68)
Browse files Browse the repository at this point in the history
* fix: change header keys to lower case

* fix: add test to check that tokenExchange works with uppercase keys in headerfix: add test to check that tokenExchange works with uppercase keys in headerss

(cherry picked from commit 4b01d7d)
  • Loading branch information
LorenzoCocciarelli authored and MTurraPpa committed Dec 5, 2022
1 parent 3d60ef3 commit 8058b16
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
12 changes: 12 additions & 0 deletions tokenExchange/src/app/eventHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const auditLog = require("./log.js");

module.exports = {
async handleEvent(event){
event.headers = makeLower(event.headers);
const eventOrigin = event?.headers?.origin;
if (eventOrigin) {
auditLog('', 'AUD_ACC_LOGIN', eventOrigin).info('info');
Expand Down Expand Up @@ -109,3 +110,14 @@ function generateKoResponse(err, allowedOrigin) {
isBase64Encoded: false
};
}

function makeLower(headers) {
let head = {}
for(const key in headers) {
if (headers.hasOwnProperty(key)) {
head[key.toLowerCase()] = headers[key]
}
}

return head
}
20 changes: 19 additions & 1 deletion tokenExchange/src/test/eventHandler.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ describe('test eventHandler', () => {
// test token exchange
const result = await eventHandler.handleEvent({
httpMethod: 'POST',
headers:{
headers: {
origin: 'https://portale-pa-develop.fe.dev.pn.pagopa.it'
},
body: JSON.stringify({
Expand All @@ -156,4 +156,22 @@ describe('test eventHandler', () => {
const sessionToken = await tokenGen.generateToken(decodedToken);
expect(body).to.be.eql({...decodedToken, sessionToken});
})

it('handle event with uppercase origin in headers', async () => {
const result = await eventHandler.handleEvent({
httpMethod: 'POST',
headers: {
Origin: 'https://portale-pa-develop.fe.dev.pn.pagopa.it'
},
body: JSON.stringify({
authorizationToken: 'eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCIsImtpZCI6Imh1Yi1zcGlkLWxvZ2luLXRlc3QifQ.eyJlbWFpbCI6ImluZm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ZUfCo21lZcXh9kzxAXCrZ_rK09RUew7hZwP3Xpt4X-4DS1YzXfwl4So5ayDv38zsOocT10EJEEKQg8UOCSXzh8_-MgMsukU6fgdXny3epvLKq0aahtP3vqSbl9wZd5aPPEklU08PS-bWifw2Qa8gozzSR-MOPGTdLun5230Z1MQJmyJXy_HJuLIKeKMMfCAinhR5476xBE2bpC_gjvPcr7LGfUYTI6ZRLDFf96Muf48hq0bGWZzT2nxOBs5WpWQcOvPw3XIgQ8Th9wWSOWiSakpyT-AIpbj7K83Z-HkHIUwqzgbtApRPNhnlzaMrRELqF0'
})
});
expect(result.statusCode).to.equal(200);
const body = JSON.parse(result.body);
expect(body.error).to.be.undefined;
// calc sessionToken
const sessionToken = await tokenGen.generateToken(decodedToken);
expect(body).to.be.eql({...decodedToken, sessionToken});
})
});

0 comments on commit 8058b16

Please sign in to comment.