From 08e2361b07021827dd81cb088457e1fd52ae9b4f Mon Sep 17 00:00:00 2001 From: mjr2595 Date: Wed, 19 Jun 2024 11:58:14 -0500 Subject: [PATCH] Add pre-request script for LM API authentication in Postman --- Scripting Other/prefetch-postman.js | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 Scripting Other/prefetch-postman.js diff --git a/Scripting Other/prefetch-postman.js b/Scripting Other/prefetch-postman.js new file mode 100644 index 0000000..6b339c7 --- /dev/null +++ b/Scripting Other/prefetch-postman.js @@ -0,0 +1,25 @@ +/* +Script to contruct the auth header for the LM API +Should be added within Postman as a pre-request script +LM community post with steps: +https://community.logicmonitor.com/discussions/product-discussions/accessing-the-logicmonitor-rest-api-with-postman-and-lmv1-api-token-authenticati/5143?topicRepliesSort=postTimeDesc + +Note: this script is not needed if using bearer token auth +*/ + +var api_id = pm.environment.get("api_id"); +var api_key = pm.environment.get("api_key"); + +var http_verb = request.method; +var resource_path = request.url.replace(/(^{{url}})([^\?]+)(\?.*)?/, "$2"); +var epoch = new Date().getTime(); + +var request_vars = + http_verb == "GET" || http_verb == "DELETE" + ? http_verb + epoch + resource_path + : http_verb + epoch + request.data + resource_path; + +var signature = btoa(CryptoJS.HmacSHA256(request_vars, api_key).toString()); +var auth = "LMv1 " + api_id + ":" + signature + ":" + epoch; + +pm.environment.set("auth", auth);