Skip to content

Commit 128ba48

Browse files
authored
Merge pull request #235182 from vicancy/patch-6
Add doc for client access url
2 parents 23c155f + bfa5ac3 commit 128ba48

6 files changed

+171
-9
lines changed
Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
---
2+
title: How to generate client access URL for Azure Web PubSub clients
3+
description: How to generate client access URL for Azure Web PubSub clients.
4+
author: vicancy
5+
ms.author: lianwei
6+
ms.date: 04/25/2023
7+
ms.service: azure-web-pubsub
8+
ms.topic: how-to
9+
---
10+
11+
# How to generate client access URL for the clients
12+
13+
A client, be it a browser 💻, a mobile app 📱, or an IoT device 💡, uses a **Client Access URL** to connect and authenticate with your resource. This URL follows a pattern of `wss://<service_name>.webpubsub.azure.com/client/hubs/<hub_name>?access_token=<token>`. This article shows you several ways to get the Client Access URL.
14+
15+
- For quick start, copy one from the Azure portal
16+
- For development, generate the value using [Web PubSub server SDK](./reference-server-sdk-js.md)
17+
- If you're using Azure AD, you can also invoke the [Generate Client Token REST API](/rest/api/webpubsub/dataplane/web-pub-sub/generate-client-token)
18+
19+
## Copy from the Azure portal
20+
In the Keys tab in Azure portal, there's a Client URL Generator tool to quickly generate a Client Access URL for you, as shown in the following diagram. Values input here aren't stored.
21+
22+
:::image type="content" source="./media/howto-websocket-connect/generate-client-url.png" alt-text="Screenshot of the Web PubSub Client URL Generator.":::
23+
24+
## Generate from service SDK
25+
The same Client Access URL can be generated by using the Web PubSub server SDK.
26+
27+
# [JavaScript](#tab/javascript)
28+
29+
1. Follow [Getting started with server SDK](./reference-server-sdk-js.md#getting-started) to create a `WebPubSubServiceClient` object `service`
30+
31+
2. Generate Client Access URL by calling `WebPubSubServiceClient.getClientAccessToken`:
32+
* Configure user ID
33+
```js
34+
let token = await serviceClient.getClientAccessToken({ userId: "user1" });
35+
```
36+
* Configure the lifetime of the token
37+
```js
38+
let token = await serviceClient.getClientAccessToken({ expirationTimeInMinutes: 5 });
39+
```
40+
* Configure a role that can join group `group1` directly when it connects using this Client Access URL
41+
```js
42+
let token = await serviceClient.getClientAccessToken({ roles: ["webpubsub.joinLeaveGroup.group1"] });
43+
```
44+
* Configure a role that the client can send messages to group `group1` directly when it connects using this Client Access URL
45+
```js
46+
let token = await serviceClient.getClientAccessToken({ roles: ["webpubsub.sendToGroup.group1"] });
47+
```
48+
* Configure a group `group1` that the client joins once it connects using this Client Access URL
49+
```js
50+
let token = await serviceClient.getClientAccessToken({ groups: ["group1"] });
51+
```
52+
53+
# [C#](#tab/csharp)
54+
55+
1. Follow [Getting started with server SDK](./reference-server-sdk-csharp.md#getting-started) to create a `WebPubSubServiceClient` object `service`
56+
57+
2. Generate Client Access URL by calling `WebPubSubServiceClient.GetClientAccessUri`:
58+
* Configure user ID
59+
```csharp
60+
var url = service.GetClientAccessUri(userId: "user1");
61+
```
62+
* Configure the lifetime of the token
63+
```csharp
64+
var url = service.GetClientAccessUri(expiresAfter: TimeSpan.FromMinutes(5));
65+
```
66+
* Configure a role that can join group `group1` directly when it connects using this Client Access URL
67+
```csharp
68+
var url = service.GetClientAccessUri(roles: new string[] { "webpubsub.joinLeaveGroup.group1" });
69+
```
70+
* Configure a role that the client can send messages to group `group1` directly when it connects using this Client Access URL
71+
```csharp
72+
var url = service.GetClientAccessUri(roles: new string[] { "webpubsub.sendToGroup.group1" });
73+
```
74+
* Configure a group `group1` that the client joins once it connects using this Client Access URL
75+
```csharp
76+
var url = service.GetClientAccessUri(groups: new string[] { "group1" });
77+
```
78+
79+
# [Python](#tab/python)
80+
81+
1. Follow [Getting started with server SDK](./reference-server-sdk-python.md#install-the-package) to create a `WebPubSubServiceClient` object `service`
82+
83+
2. Generate Client Access URL by calling `WebPubSubServiceClient.get_client_access_token`:
84+
* Configure user ID
85+
```python
86+
token = service.get_client_access_token(user_id="user1")
87+
```
88+
* Configure the lifetime of the token
89+
```python
90+
token = service.get_client_access_token(minutes_to_expire=5)
91+
```
92+
* Configure a role that can join group `group1` directly when it connects using this Client Access URL
93+
```python
94+
token = service.get_client_access_token(roles=["webpubsub.joinLeaveGroup.group1"])
95+
```
96+
* Configure a role that the client can send messages to group `group1` directly when it connects using this Client Access URL
97+
```python
98+
token = service.get_client_access_token(roles=["webpubsub.sendToGroup.group1"])
99+
```
100+
* Configure a group `group1` that the client joins once it connects using this Client Access URL
101+
```python
102+
token = service.get_client_access_token(groups=["group1"])
103+
```
104+
105+
# [Java](#tab/java)
106+
107+
1. Follow [Getting started with server SDK](./reference-server-sdk-java.md#getting-started) to create a `WebPubSubServiceClient` object `service`
108+
109+
2. Generate Client Access URL by calling `WebPubSubServiceClient.getClientAccessToken`:
110+
* Configure user ID
111+
```java
112+
GetClientAccessTokenOptions option = new GetClientAccessTokenOptions();
113+
option.setUserId(id);
114+
WebPubSubClientAccessToken token = service.getClientAccessToken(option);
115+
```
116+
* Configure the lifetime of the token
117+
```java
118+
GetClientAccessTokenOptions option = new GetClientAccessTokenOptions();
119+
option.setExpiresAfter(Duration.ofDays(1));
120+
WebPubSubClientAccessToken token = service.getClientAccessToken(option);
121+
```
122+
* Configure a role that can join group `group1` directly when it connects using this Client Access URL
123+
```java
124+
GetClientAccessTokenOptions option = new GetClientAccessTokenOptions();
125+
option.addRole("webpubsub.joinLeaveGroup.group1");
126+
WebPubSubClientAccessToken token = service.getClientAccessToken(option);
127+
```
128+
* Configure a role that the client can send messages to group `group1` directly when it connects using this Client Access URL
129+
```java
130+
GetClientAccessTokenOptions option = new GetClientAccessTokenOptions();
131+
option.addRole("webpubsub.sendToGroup.group1");
132+
WebPubSubClientAccessToken token = service.getClientAccessToken(option);
133+
```
134+
* Configure a group `group1` that the client joins once it connects using this Client Access URL
135+
```java
136+
GetClientAccessTokenOptions option = new GetClientAccessTokenOptions();
137+
option.setGroups(Arrays.asList("group1")),
138+
WebPubSubClientAccessToken token = service.getClientAccessToken(option);
139+
```
140+
---
141+
142+
In real-world code, we usually have a server side to host the logic generating the Client Access URL. When a client request comes in, the server side can use the general authentication/authorization workflow to validate the client request. Only valid client requests can get the Client Access URL back.
143+
144+
## Invoke the Generate Client Token REST API
145+
146+
You can enable Azure AD in your service and use the Azure AD token to invoke [Generate Client Token rest API](/rest/api/webpubsub/dataplane/web-pub-sub/generate-client-token) to get the token for the client to use.
147+
148+
1. Follow [Authorize from application](./howto-authorize-from-application.md) to enable Azure AD.
149+
2. Follow [Get Azure AD token](./howto-authorize-from-application.md#use-postman-to-get-the-azure-ad-token) to get the Azure AD token with Postman.
150+
3. Use the Azure AD token to invoke `:generateToken` with Postman:
151+
1. For the URI, enter `https://{Endpoint}/api/hubs/{hub}/:generateToken?api-version=2022-11-01`
152+
2. On the **Auth** tab, select **Bearer Token** and paste the Azure AD token fetched in the previous step
153+
3. Select **Send** and you see the Client Access Token in the response:
154+
```json
155+
{
156+
"token": "ABCDEFG.ABC.ABC"
157+
}
158+
```
159+
4. The Client Access URI is in the format of `wss://<endpoint>/client/hubs/<hub_name>?access_token=<token>`
160+

articles/azure-web-pubsub/quickstart-use-client-sdk.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ Note that the SDK is available as a [NuGet packet](https://www.nuget.org/package
128128

129129
### Create and connect to the Web PubSub service
130130

131-
This code example creates a Web PubSub client that connects to the Web PubSub service instance. A client uses a Client Access URL to connect and authenticate with the service. It's best practice to not hard code the Client Access URL in your code. In the production world, we usually set up an app server to return this URL on demand.
131+
This code example creates a Web PubSub client that connects to the Web PubSub service instance. A client uses a Client Access URL to connect and authenticate with the service. It's best practice to not hard code the Client Access URL in your code. In the production world, we usually set up an app server to return this URL on demand. [Generate Client Access URL](./howto-generate-client-access-url.md) describes the practice in detail.
132132

133133
For this example, you can use the Client Access URL you generated in the portal.
134134

articles/azure-web-pubsub/quickstarts-event-notifications-from-clients.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ npm install @azure/web-pubsub-client
4848
#### 2. Connect to Web PubSub
4949
A client, be it a browser, a mobile app, or an IoT device, uses a **Client Access URL** to connect and authenticate with your resource.
5050
This URL follows a pattern of `wss://<service_name>.webpubsub.azure.com/client/hubs/<hub_name>?access_token=<token>`.
51-
A client can have a few ways to obtain the Client Access URL. For this quick start, you can copy and paste one from Azure portal shown in the following diagram.
51+
A client can have a few ways to obtain the Client Access URL. For this quick start, you can copy and paste one from Azure portal shown in the following diagram. It's best practice to not hard code the Client Access URL in your code. In the production world, we usually set up an app server to return this URL on demand. [Generate Client Access URL](./howto-generate-client-access-url.md) describes the practice in detail.
5252

5353
![The diagram shows how to get **Client Access Url**.](./media/quickstarts-event-notifications-from-clients/generate-client-url-no-group.png)
5454

articles/azure-web-pubsub/quickstarts-pubsub-among-clients.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ dotnet add package Azure.Messaging.WebPubSub.Client --prerelease
5555

5656
## Connect to Web PubSub
5757

58-
A client, be it a browser 💻, a mobile app 📱, or an IoT device 💡, uses a **Client Access URL** to connect and authenticate with your resource. This URL follows a pattern of `wss://<service_name>.webpubsub.azure.com/client/hubs/<hub_name>?access_token=<token>`. A client can have a few ways to obtain the Client Access URL. For this quick start, you can copy and paste one from Azure portal shown in the following diagram.
58+
A client, be it a browser 💻, a mobile app 📱, or an IoT device 💡, uses a **Client Access URL** to connect and authenticate with your resource. This URL follows a pattern of `wss://<service_name>.webpubsub.azure.com/client/hubs/<hub_name>?access_token=<token>`. A client can have a few ways to obtain the Client Access URL. For this quick start, you can copy and paste one from Azure portal shown in the following diagram. It's best practice to not hard code the Client Access URL in your code. In the production world, we usually set up an app server to return this URL on demand. [Generate Client Access URL](./howto-generate-client-access-url.md) describes the practice in detail.
5959

6060
![The diagram shows how to get client access url.](./media/howto-websocket-connect/generate-client-url.png)
6161

articles/azure-web-pubsub/quickstarts-push-messages-from-server.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ npm install @azure/web-pubsub-client
6363

6464
#### Connect to your Web PubSub resource and register a listener for the `server-message` event
6565
A client uses a ***Client Access URL*** to connect and authenticate with your resource.
66-
This URL follows a pattern of `wss://<service_name>.webpubsub.azure.com/client/hubs/<hub_name>?access_token=<token>`. A client can have a few ways to obtain the Client Access URL. For this quick start, you can copy and paste one from Azure portal shown in the following diagram.
66+
This URL follows a pattern of `wss://<service_name>.webpubsub.azure.com/client/hubs/<hub_name>?access_token=<token>`. A client can have a few ways to obtain the Client Access URL. For this quick start, you can copy and paste one from Azure portal shown in the following diagram. It's best practice to not hard code the Client Access URL in your code. In the production world, we usually set up an app server to return this URL on demand. [Generate Client Access URL](./howto-generate-client-access-url.md) describes the practice in detail.
6767

6868
![The diagram shows how to get client access url.](./media/quickstarts-push-messages-from-server/push-messages-from-server.png)
6969

articles/azure-web-pubsub/toc.yml

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,19 +49,21 @@
4949
href: howto-websocket-connect.md
5050
- name: Create reliable Websocket clients
5151
href: howto-develop-reliable-clients.md
52+
- name: Generate client access URL
53+
href: howto-generate-client-access-url.md
5254
- name: Configure event handler
5355
href: howto-develop-eventhandler.md
5456
- name: Send client events to Event Hubs
5557
href: howto-develop-event-listener.md
56-
- name: Create `WebPubSubServiceClient` with Azure Identity
58+
- name: Use server SDK with Azure Identity
5759
items:
58-
- name: Create `WebPubSubServiceClient` with Azure Identity and .NET
60+
- name: Use server SDK with Azure Identity and .NET
5961
href: howto-create-serviceclient-with-net-and-azure-identity.md
60-
- name: Create `WebPubSubServiceClient` with Azure Identity and Java
62+
- name: Use server SDK with Azure Identity and Java
6163
href: howto-create-serviceclient-with-java-and-azure-identity.md
62-
- name: Create `WebPubSubServiceClient` with Azure Identity and JavaScript
64+
- name: Use server SDK with Azure Identity and JavaScript
6365
href: howto-create-serviceclient-with-javascript-and-azure-identity.md
64-
- name: Create `WebPubSubServiceClient` with Azure Identity and Python
66+
- name: Use server SDK with Azure Identity and Python
6567
href: howto-create-serviceclient-with-python-and-azure-identity.md
6668
- name: Monitor
6769
items:

0 commit comments

Comments
 (0)