Skip to content

Commit c4bc017

Browse files
authored
simplify relaylib.py
1 parent e9e930d commit c4bc017

File tree

1 file changed

+57
-99
lines changed

1 file changed

+57
-99
lines changed

articles/azure-relay/includes/relay-python-helper-functions.md

Lines changed: 57 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -14,107 +14,65 @@ connections for secure communication.
1414

1515
### Dependencies
1616

17-
1. Install the following Python libraries using pip before running the helper function script
17+
Install the following Python libraries using pip before generating the helper function script: `base64`, `hashlib`, `hmac`, `math`, `time`, `urllib`
1818

19-
`base64`, `hashlib`, `hmac`, `math`, `time`, `urllib`
19+
These libraries can be installed using the following command:
2020

21-
These libraries can be installed using the following command:
22-
23-
```bash
24-
pip install <package name>
25-
```
21+
```bash
22+
pip install <package name>
23+
```
2624

2725
### Write the helper function script
2826

29-
1. Import the dependencies into your `relaylib.py` script.
30-
31-
```python
32-
import base64
33-
import hashlib
34-
import hmac
35-
import math
36-
import time
37-
import urllib
38-
```
39-
40-
3. Add the following code to the `relaylib.py` file. The main script should look like the following code:
41-
42-
```python
43-
def hmac_sha256(key, msg):
44-
hash_obj = hmac.new(key=key, msg=msg, digestmod=hashlib._hashlib.openssl_sha256)
45-
return hash_obj.digest()
46-
47-
def createListenUrl(serviceNamespace, entityPath, token = None):
48-
url = 'wss://' + serviceNamespace + '/$hc/' + entityPath + '?sb-hc-action=listen&sb-hc-id=123456'
49-
if token is not None:
50-
url = url + '&sb-hc-token=' + urllib.parse.quote(token)
51-
return url
52-
53-
def createSendUrl(serviceNamespace, entityPath, token = None):
54-
url = 'wss://' + serviceNamespace + '/$hc/' + entityPath + '?sb-hc-action=connect&sb-hc-id=123456'
55-
if token is not None:
56-
url = url + '&sb-hc-token=' + urllib.parse.quote(token)
57-
return url
58-
59-
# Function which creates the Service Bus SAS token.
60-
def createSasToken(serviceNamespace, entityPath, sasKeyName, sasKey):
61-
uri = "http://" + serviceNamespace + "/" + entityPath
62-
encodedResourceUri = urllib.parse.quote(uri, safe = '')
63-
64-
tokenValidTimeInSeconds = 60 * 60 * 48 # One Hour
65-
unixSeconds = math.floor(time.time())
66-
expiryInSeconds = unixSeconds + tokenValidTimeInSeconds
67-
68-
plainSignature = encodedResourceUri + "\n" + str(expiryInSeconds)
69-
sasKeyBytes = sasKey.encode("utf-8")
70-
plainSignatureBytes = plainSignature.encode("utf-8")
71-
hashBytes = hmac_sha256(sasKeyBytes, plainSignatureBytes)
72-
base64HashValue = base64.b64encode(hashBytes)
73-
74-
token = "SharedAccessSignature sr=" + encodedResourceUri + "&sig=" + urllib.parse.quote(base64HashValue) + "&se=" + str(expiryInSeconds) + "&skn=" + sasKeyName
75-
return token
76-
```
77-
Here's what your `relaylib.py` file should look like:
78-
79-
```python
80-
import base64
81-
import hashlib
82-
import hmac
83-
import math
84-
import time
85-
import urllib
86-
87-
def hmac_sha256(key, msg):
88-
hash_obj = hmac.new(key=key, msg=msg, digestmod=hashlib._hashlib.openssl_sha256)
89-
return hash_obj.digest()
90-
91-
def createListenUrl(serviceNamespace, entityPath, token = None):
92-
url = 'wss://' + serviceNamespace + '/$hc/' + entityPath + '?sb-hc-action=listen&sb-hc-id=123456'
93-
if token is not None:
94-
url = url + '&sb-hc-token=' + urllib.parse.quote(token)
95-
return url
96-
97-
def createSendUrl(serviceNamespace, entityPath, token = None):
98-
url = 'wss://' + serviceNamespace + '/$hc/' + entityPath + '?sb-hc-action=connect&sb-hc-id=123456'
99-
if token is not None:
100-
url = url + '&sb-hc-token=' + urllib.parse.quote(token)
101-
return url
102-
103-
# Function which creates the Service Bus SAS token.
104-
def createSasToken(serviceNamespace, entityPath, sasKeyName, sasKey):
105-
uri = "http://" + serviceNamespace + "/" + entityPath
106-
encodedResourceUri = urllib.parse.quote(uri, safe = '')
107-
108-
tokenValidTimeInSeconds = 60 * 60 * 48 # One Hour
109-
unixSeconds = math.floor(time.time())
110-
expiryInSeconds = unixSeconds + tokenValidTimeInSeconds
111-
112-
plainSignature = encodedResourceUri + "\n" + str(expiryInSeconds)
113-
sasKeyBytes = sasKey.encode("utf-8")
114-
plainSignatureBytes = plainSignature.encode("utf-8")
115-
hashBytes = hmac_sha256(sasKeyBytes, plainSignatureBytes)
116-
base64HashValue = base64.b64encode(hashBytes)
117-
118-
token = "SharedAccessSignature sr=" + encodedResourceUri + "&sig=" + urllib.parse.quote(base64HashValue) + "&se=" + str(expiryInSeconds) + "&skn=" + sasKeyName
119-
return token
120-
```
27+
Here's what your `relaylib.py` file should look like:
28+
29+
```python
30+
import base64
31+
import hashlib
32+
import hmac
33+
import math
34+
import time
35+
import urllib
36+
37+
# Function which generates the HMAC-SHA256 of a given message
38+
def hmac_sha256(key, msg):
39+
hash_obj = hmac.new(key=key, msg=msg, digestmod=hashlib._hashlib.openssl_sha256)
40+
return hash_obj.digest()
41+
42+
# Function to create a WebSocket URL for listening for a server application
43+
def createListenUrl(serviceNamespace, entityPath, token = None):
44+
url = 'wss://' + serviceNamespace + '/$hc/' + entityPath + '?sb-hc-action=listen&sb-hc-id=123456'
45+
if token is not None:
46+
url = url + '&sb-hc-token=' + urllib.parse.quote(token)
47+
return url
48+
49+
# Function which creates the url for the client application
50+
def createSendUrl(serviceNamespace, entityPath, token = None):
51+
url = 'wss://' + serviceNamespace + '/$hc/' + entityPath + '?sb-hc-action=connect&sb-hc-id=123456'
52+
if token is not None:
53+
url = url + '&sb-hc-token=' + urllib.parse.quote(token)
54+
return url
55+
56+
# Function which creates the Service Bus SAS token.
57+
def createSasToken(serviceNamespace, entityPath, sasKeyName, sasKey):
58+
uri = "http://" + serviceNamespace + "/" + entityPath
59+
encodedResourceUri = urllib.parse.quote(uri, safe = '')
60+
61+
# Define the token validity period in seconds (48 hours in this case)
62+
tokenValidTimeInSeconds = 60 * 60 * 48
63+
unixSeconds = math.floor(time.time())
64+
expiryInSeconds = unixSeconds + tokenValidTimeInSeconds
65+
66+
# Create the plain signature string by combining the encoded URI and the expiry time
67+
plainSignature = encodedResourceUri + "\n" + str(expiryInSeconds)
68+
69+
# Encode the SAS key and the plain signature as bytes
70+
sasKeyBytes = sasKey.encode("utf-8")
71+
plainSignatureBytes = plainSignature.encode("utf-8")
72+
hashBytes = hmac_sha256(sasKeyBytes, plainSignatureBytes)
73+
base64HashValue = base64.b64encode(hashBytes)
74+
75+
# Construct the SAS token string
76+
token = "SharedAccessSignature sr=" + encodedResourceUri + "&sig=" + urllib.parse.quote(base64HashValue) + "&se=" + str(expiryInSeconds) + "&skn=" + sasKeyName
77+
return token
78+
```

0 commit comments

Comments
 (0)