Skip to content

Commit 6beeb3d

Browse files
committed
updated the JS code
1 parent b00f781 commit 6beeb3d

File tree

1 file changed

+38
-13
lines changed

1 file changed

+38
-13
lines changed

articles/event-hubs/authenticate-shared-access-signature.md

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
title: Authenticate access to Azure Event Hubs with shared access signatures
33
description: This article shows you how to authenticate access to Event Hubs resources using shared access signatures.
44
ms.topic: conceptual
5-
ms.date: 01/05/2022
5+
ms.date: 09/16/2022
66
ms.devlang: csharp, java, javascript, php
77
ms.custom: devx-track-js, devx-track-csharp
88
---
@@ -72,18 +72,43 @@ Following section shows generating a SAS token using shared access signature pol
7272

7373
```javascript
7474
function createSharedAccessToken(uri, saName, saKey) {
75-
if (!uri || !saName || !saKey) {
76-
throw "Missing required parameter";
77-
}
78-
var encoded = encodeURIComponent(uri);
79-
var now = new Date();
80-
var week = 60*60*24*7;
81-
var ttl = Math.round(now.getTime() / 1000) + week;
82-
var signature = encoded + '\n' + ttl;
83-
var signatureUTF8 = utf8.encode(signature);
84-
var hash = crypto.createHmac('sha256', saKey).update(signatureUTF8).digest('base64');
85-
return 'SharedAccessSignature sr=' + encoded + '&sig=' +
86-
encodeURIComponent(hash) + '&se=' + ttl + '&skn=' + saName;
75+
if (!uri || !saName || !saKey) {
76+
throw "Missing required parameter";
77+
}
78+
var encoded = encodeURIComponent(uri);
79+
var now = new Date();
80+
var week = 60*60*24*7;
81+
var ttl = Math.round(now.getTime() / 1000) + week;
82+
var signature = encoded + '\n' + ttl;
83+
var hash = crypto.createHmac('sha256', saKey).update(signature, 'utf8').digest('base64');
84+
return 'SharedAccessSignature sr=' + encoded + '&sig=' +
85+
encodeURIComponent(hash) + '&se=' + ttl + '&skn=' + saName;
86+
}
87+
```
88+
89+
To use a policy name and a key value to connect to an event hub, use the `EventHubProducerClient` constructor that takes the `AzureNamedKeyCredential` parameter.
90+
91+
```javascript
92+
const producer = new EventHubProducerClient("NAMESPACE NAME.servicebus.windows.net", eventHubName, new AzureNamedKeyCredential("POLICYNAME", "KEYVALUE"));
93+
```
94+
95+
You'll need to add a reference to `AzureNamedKeyCredential`.
96+
97+
```javascript
98+
const { AzureNamedKeyCredential } = require("@azure/core-auth");
99+
```
100+
101+
To use a SAS token that you generated using the code above, use the `EventHubProducerClient` constructor that takes the `AzureSASCredential` parameter.
102+
103+
```javascript
104+
var token = createSharedAccessToken("https://NAMESPACENAME.servicebus.windows.net", "POLICYNAME", "KEYVALUE");
105+
const producer = new EventHubProducerClient("NAMESPACENAME.servicebus.windows.net", eventHubName, new AzureSASCredential(token));
106+
```
107+
108+
You'll need to add a reference to `AzureSASCredential`.
109+
110+
```javascript
111+
const { AzureSASCredential } = require("@azure/core-auth");
87112
```
88113

89114
#### JAVA

0 commit comments

Comments
 (0)