Jwt token on headers

252 views
Skip to first unread message

Aron Contini

unread,
Oct 10, 2023, 11:21:43 AM10/10/23
to ZAP User Group
Hi everyone I'm new to zap and i'm testing it on an application which requires a jwt token as authentication

I already have the jwt token and i would like to feed it onto the headers to allow authentication.
For example i know a simple curl request does the trick adding headers like this

curl -H 'Accept: application/json' -H "Authorization: Bearer ${TOKEN}" localhost:8088/path/to/something/

I tried using a script to add new headers like in the curl message using the following jython script as a HTTPsender:

headers = dict({"Accept": "application/json",
"Authorization": "Bearer HARD_CODED_TOKEN"});

def sendingRequest(msg, initiator, helper):
for x in list(headers):
msg.getRequestHeader().setHeader(x, headers[x]);


def responseReceived(msg, initiator, helper):
pass;

Which is practically the same i found on the examples, still after i try to do the quick scan i am getting 401 response.
the curl instead retrieves a json response and is correctly authenticated.

Is there something I am missing? What am I doin wrong?

NB the HARD_CODED_TOKEN is a placeholder here in reality i just pasted the token

Simon Bennetts

unread,
Oct 10, 2023, 12:25:52 PM10/10/23
to ZAP User Group
It looks like you are doing the right things.
Have you enabled the script?
And checked that no errors are being logged in the Script Console?
Have a look at one of the requests - can you see the headers being added correctly?

Cheers,

Simon

Aron Contini

unread,
Oct 12, 2023, 9:47:22 AM10/12/23
to ZAP User Group
Thanks!
I completely missed the fact i had to activate it xD

Aron Contini

unread,
Oct 12, 2023, 9:52:23 AM10/12/23
to ZAP User Group
Since pasting into the answer made some indentation errors here is the correct code for the future readers

headers = dict({"Accept": "application/json",
    "Authorization": "Bearer HARD_CODED_TOKEN"});

def sendingRequest(msg, initiator, helper):
    for x in list(headers):
        msg.getRequestHeader().setHeader(x, headers[x]);

def responseReceived(msg, initiator, helper):
    pass;

Simon Bennetts

unread,
Oct 12, 2023, 9:53:19 AM10/12/23
to ZAP User Group
Thanks for the update!
Reply all
Reply to author
Forward
0 new messages