Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamic param value calculation depending on params in Paw

Tags:

paw-app

I have some API that must be signed with request params hash. For example I have 2 params - login and password in request params. So I need to add the param checksum that is calculated with login and password fields hash.

How can I implement it? Now when I try to calculate it, I have the self-dependency error.

login = test
password = test
somefield = lalala
checksum = md5([login][password][somefield]) <- here is dynamic evaluation
like image 465
silent Avatar asked Dec 25 '22 01:12

silent


1 Answers

The self-dependency error is shown because it actually tries to evaluate the full URL to get one of the other parameters. That's probably something that needs to be fixed in Paw.

However, you can simply ignore the warning, as it still works. Here is an example:

Calculate a MD5 digest of URL parameters with Paw

In your example, the checksum is 8bc22595f820ff1612fd16294c02359a which is the expected result.

Update: if you want to do that with JavaScript code, here's an example.

function evaluate(context) {
    var url = context.getCurrentRequest().url;
    var query = url.split('?')[1];
    var fragments = query.split('&');
    var login, password, somefield;
    for (var i in fragments) {
        var keyvalue = fragments[i].split('=');
        if (keyvalue[0] == "login") {
            login = keyvalue[1];
        } else if (keyvalue[0] == "password") {
            password = keyvalue[1];
        } else if (keyvalue[0] == "somefield") {
            somefield = keyvalue[1];
        }
    }
    // you can now compute whatever hash you want with these values
    // the self-dependency error will be shown but it should work
    return "" + login + "-" + password + "-" + somefield;
};

Use a JavaScript script to extract request parameters in Paw

To calculate MD5 hashes with JS, you'll need to include a 3rd party library. That can be more easily (and more cleanly) done via npm. See how we are managing dependencies in other Extensions: https://github.com/LuckyMarmot/Paw-PythonRequestsCodeGenerator

like image 73
Micha Mazaheri Avatar answered Jun 19 '23 16:06

Micha Mazaheri