|
2 | 2 | title: Authenticate access to Azure Event Hubs with shared access signatures
|
3 | 3 | description: This article shows you how to authenticate access to Event Hubs resources using shared access signatures.
|
4 | 4 | ms.topic: conceptual
|
5 |
| -ms.date: 01/05/2022 |
| 5 | +ms.date: 09/16/2022 |
6 | 6 | ms.devlang: csharp, java, javascript, php
|
7 | 7 | ms.custom: devx-track-js, devx-track-csharp
|
8 | 8 | ---
|
@@ -72,18 +72,43 @@ Following section shows generating a SAS token using shared access signature pol
|
72 | 72 |
|
73 | 73 | ```javascript
|
74 | 74 | 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"); |
87 | 112 | ```
|
88 | 113 |
|
89 | 114 | #### JAVA
|
|
0 commit comments