4

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
Micha Mazaheri
  • 3,481
  • 1
  • 21
  • 26
silent
  • 3,843
  • 23
  • 29

1 Answers1

10

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

Micha Mazaheri
  • 3,481
  • 1
  • 21
  • 26
  • params list is always different and it is not comfortable to list them all in "input" field, it is easier to write own dynamic value, dependent on other (not checksum) field values. – silent Dec 08 '14 at 20:14
  • Oh, it is working, warning is just for notification. Thank you – silent Dec 17 '14 at 07:33
  • @MichaMazaheri Is there a way to do the same with request body params? For example, I want to send two params and MD5([firstParam][secondParam]), as on this screenshot: http://take.ms/dIi43 – Spail Oct 03 '15 at 11:46
  • It actually calculated correct hash if I use "Request Parsed Body" dynamic value (I get warnings about empty body and self-dependency), but sends wrong hash when request is sent. – Spail Oct 03 '15 at 12:01
  • @Spail any chance you can share the request with Pawprint (hit Cmd + / it will give you a sharing URL)? (replace any private credential by something random) this way I can see what's wrong. Will be happy to help. You can also share your Paw file sending to support@luckymarmot.com thanks! – Micha Mazaheri Oct 04 '15 at 03:39
  • 1
    @MichaMazaheri I have posted a question with much more details, could you please look at it? http://stackoverflow.com/questions/32922511/calculating-dynamic-value-from-request-body-parameters-in-paw – Spail Oct 05 '15 at 10:08