Alok
Alok

Reputation: 10618

Timed Hash Verification based web api call in Lindane Scripting Language

I am trying to call a web api which is secred by Timed Hash Verification system.

I am able to call that API using python

import hmac
import hashlib
import time
import requests
import json

def generate_timed_hash(secret_key, data):
    timestamp = str(int(time.time()))  # Current Unix timestamp
    message = data + timestamp
    hash_value = hmac.new(secret_key.encode(), message.encode(), hashlib.sha256).hexdigest()
    return hash_value, timestamp

def testApi():
    data = json.dumps({'userId': 'c088ab7f-dd04-4836-93cd-7ab2843db971'})
    secret_key = 'mysecret'
    hash_value, timestamp = generate_timed_hash(secret_key, data)

    headers = {
        'X-Timestamp': timestamp,
        'X-Hash': hash_value,
    }

    response = requests.post('https://some-host/api/secured-plan-detail/', data={'data': data}, headers=headers)
    print(response.json())

testApi()

I found llSHA256String for generating SHA256 string. But still not able to figure out way tranaslate that python code.

Here is my attempt to make http call.

default
{
    state_entry()
    {
        llHTTPRequest(
            "https://some-host/api/secured-plan-detail/",
            [
                HTTP_METHOD, "POST",
                HTTP_MIMETYPE, "application/json"
                //HTTP_CUSTOM_HEADER, "X-Timestamp:", timestamp
                //HTTP_CUSTOM_HEADER, "X-Hash:", hash_value
            ],
            llList2Json(JSON_OBJECT, ["userId", "c088ab7f-dd04-4836-93cd-7ab2843db971"])
        ); 
    }
    http_response(key request_id, integer status, list metadata, string body) {
        llOwnerSay((string)status);
        llOwnerSay("response: " + body);
    }
}

How can make such call in Lindane Scripting Language?

Upvotes: 0

Views: 44

Answers (1)

Alok
Alok

Reputation: 10618

This is working for me.

default
{
    state_entry()
    {
        string time = llGetUnixTime();
        string data = llList2Json(JSON_OBJECT, ["userId", "c088ab7f-dd04-4836-93cd-7ab2843db971"]);
        string PASS = "mysecret";
        string message = PASS + data + (string)time;
        string hash = llSHA256String(message);
        string url = "https://some-host/api/secured-plan-detail";
       
        llHTTPRequest(
            url,
            [
                HTTP_METHOD, "POST",
                HTTP_MIMETYPE, "application/json",
                HTTP_CUSTOM_HEADER, "X-Timestamp", (string)time,
                HTTP_CUSTOM_HEADER, "X-Hash", hash
            ],
            llList2Json(JSON_OBJECT, ["data", data])
        );
    }
    http_response(key request_id, integer status, list metadata, string body) {
        llOwnerSay((string)status);
        llOwnerSay("response: " + body);
    }
}

Upvotes: 0

Related Questions