# Authenticating before sending requests

**URL:** https://discourse.nodered.org/t/authenticating-before-sending-requests/51162
**Category:** Developing Nodes
**Created:** [17 September 2021 14:34 UTC](https://discourse.nodered.org/t/authenticating-before-sending-requests/51162 "2021-09-17T14:34:22Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![sd\_dracula](https://avatars.discourse-cdn.com/v4/letter/s/2bfe46/32.png) [@sd\_dracula](https://discourse.nodered.org/u/sd_dracula)
#### Post date: [17 September 2021 14:34 UTC](https://discourse.nodered.org/t/authenticating-before-sending-requests/51162/1 "2021-09-17T14:34:22Z")

</div>

I am trying to connect and pull data from a url and I have no idea how to even begin for the login/authentication part. This will pull data from a url via HTTP POST  
I was hoping someone here with the correct know how can help me out.

This is what I have from the docs:  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/8/38ce873a1278bac9ffe4582b15b72a1a93133978.png)

It gives some java methods for calculating time, md5 but not sure what do with them.  
The signature string is:

```auto
"POST\nmBAcWORUK93FBOz6jsAuqw==\napplication/json\nFri, 26 Jul 2019 
06:00:46 GMT\n/v1/api/userStationList"

```

---

<div class="post-metadata">

### Author: ![sd\_dracula](https://avatars.discourse-cdn.com/v4/letter/s/2bfe46/32.png) [@sd\_dracula](https://discourse.nodered.org/u/sd_dracula)
#### Post date: [17 September 2021 15:07 UTC](https://discourse.nodered.org/t/authenticating-before-sending-requests/51162/2 "2021-09-17T15:07:49Z")

</div>

Even if someone can help me convert it to javascript code so that it will just work inside a normal function (if possible)

---

<div class="post-metadata">

### Author: ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)
#### Post date: [17 September 2021 15:32 UTC](https://discourse.nodered.org/t/authenticating-before-sending-requests/51162/3 "2021-09-17T15:32:08Z")

</div>

How do you convert a image to javascript,? You need to paste code, then someone may help. You may find someone who wishes to waste their time coping it from your image, but you will find more will help if you make life easier for them to help.

---

<div class="post-metadata">

### Author: ![sd\_dracula](https://avatars.discourse-cdn.com/v4/letter/s/2bfe46/32.png) [@sd\_dracula](https://discourse.nodered.org/u/sd_dracula)
#### Post date: [17 September 2021 15:37 UTC](https://discourse.nodered.org/t/authenticating-before-sending-requests/51162/4 "2021-09-17T15:37:46Z")

</div>

Good point, let me add it here:

```auto
Authorization = "API " + KeyId + ":" + Sign
Sign = base64(HmacSHA1(KeySecret,
VERB + "\n"
+ Content-MD5 + "\n"
+ Content-Type + "\n"
+ Date + "\n"
+ CanonicalizedResource))

```

Authorization Field calculation example  
If the KeyId is: 2424 and the KeySecret is: 6680182547, the following method can be used to  
calculate the signature sign  
Request:

```auto
POST /v1/api/userStationList
Content-MD5: mBAcWORUK93FBOz6jsAuqw==
Content-Type: application/json
Date: Fri, 26 Jul 2019 06:00:46 GMT
Authorization: API 2424: ForAqMkym0v7R94RSU0rRqpEkf0=
Body：{"userId":"1145611319416590338"}

```

Note: There is no "Body:" in the post content sent

The signature string is:

```auto
" POST\nmBAcWORUK93FBOz6jsAuqw==\napplication/json\nFri, 26 Jul 2019
06:00:46 GMT\n/v1/api/userStationList"

```

Authorization Calculation  
Because Authorization = "API "+ KeyId + ":" + Signa, the final Authorization is “API 2424:  
/2Np3q5h846q9cWEFUNtbL+G7H8=”，  
Then add the Authorization header to form the final message that needs to be sent：

```auto
POST /v1/api/userStationList
Content-MD5: mBAcWORUK93FBOz6jsAuqw==
Content-Type: application/json
Date: Fri, 26 Jul 2019 06:00:46 GMT
Authorization: API 2424:/2Np3q5h846q9cWEFUNtbL+G7H8=
{"userId":"1145611319416590338"}

```

I am guessing I need to pass those headers above to each request but I have no idea how to calculate them.

---

<div class="post-metadata">

### Author: ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)
#### Post date: [18 September 2021 08:02 UTC](https://discourse.nodered.org/t/authenticating-before-sending-requests/51162/5 "2021-09-18T08:02:31Z")

</div>

I am unsure of syntax for the sha1 part of the code.  
But there are node for base64 and crypto.js sha1  
[https://flows.nodered.org/node/node-red-contrib-checksum](https://flows.nodered.org/node/node-red-contrib-checksum)  
[https://flows.nodered.org/node/node-red-node-base64](https://flows.nodered.org/node/node-red-node-base64)

---

<div class="post-metadata">

### Author: ![sd\_dracula](https://avatars.discourse-cdn.com/v4/letter/s/2bfe46/32.png) [@sd\_dracula](https://discourse.nodered.org/u/sd_dracula)
#### Post date: [18 September 2021 16:06 UTC](https://discourse.nodered.org/t/authenticating-before-sending-requests/51162/6 "2021-09-18T16:06:19Z")

</div>

They provide this java method, not sure it can be just plugged into node-red?

```auto
public static String HmacSHA1Encrypt(String encryptText, String 
keySecret) throws Exception 
{ 
 byte[] data= keySecret.getBytes("UTF-8"); 

 SecretKey secretKey = new SecretKeySpec(data, "HmacSHA1"); 

 Mac mac = Mac.getInstance("HmacSHA1"); 

 mac.init(secretKey); 
 byte[] text = encryptText.getBytes("UTF-8"); 

 byte[] result = mac.doFinal(text); 
 return Base64.encodeBase64String(result); 
} 

```

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)
#### Post date: [17 November 2021 16:06 UTC](https://discourse.nodered.org/t/authenticating-before-sending-requests/51162/7 "2021-11-17T16:06:48Z")

</div>

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.
