|
| 1 | +--- |
| 2 | +title: Use wildcard group role patterns |
| 3 | +description: Learn how to grant Azure Web PubSub clients permissions to many groups using wildcard role patterns. |
| 4 | +author: kevinguo-ed |
| 5 | +ms.author: kevinguo |
| 6 | +ms.service: azure-web-pubsub |
| 7 | +ms.topic: conceptual |
| 8 | +ms.date: 10/14/2025 |
| 9 | +ms.custom: |
| 10 | +--- |
| 11 | + |
| 12 | +# Use wildcard group role patterns |
| 13 | + |
| 14 | +Azure Web PubSub now supports wildcard pattern matching in client "group" roles so you can authorize a client for many related groups with a single role string. This reduces token size, simplifies permission management, and improves performance versus enumerating many concrete group roles. |
| 15 | + |
| 16 | +You can continue to use the existing literal roles: |
| 17 | + |
| 18 | +- `webpubsub.sendToGroup.{groupName}` |
| 19 | +- `webpubsub.joinLeaveGroup.{groupName}` |
| 20 | + |
| 21 | +But you can now also use the new pattern roles: |
| 22 | + |
| 23 | +- `webpubsub.sendToGroups.{pattern}` |
| 24 | +- `webpubsub.joinLeaveGroups.{pattern}` |
| 25 | + |
| 26 | +Where `{pattern}` follows the wildcard syntax below. |
| 27 | + |
| 28 | +## When to use pattern roles |
| 29 | + |
| 30 | +Use pattern roles when: |
| 31 | + |
| 32 | +- A user or device must access a large but bounded dynamic set of groups (for example: all groups for a specific tenant or project) |
| 33 | +- You want to keep access tokens small (avoid listing dozens or hundreds of explicit group roles) |
| 34 | + |
| 35 | +Avoid over-broad patterns (like `**`) unless absolutely required; follow the principle of least privilege. |
| 36 | + |
| 37 | +## Pattern syntax |
| 38 | + |
| 39 | +| Symbol | Meaning | |
| 40 | +| ------ | ------- | |
| 41 | +| `?` | Matches exactly one character except `/` | |
| 42 | +| `*` | Matches zero or more characters except `/` | |
| 43 | +| `**` | Matches zero or more characters including `/` (crosses path boundaries) | |
| 44 | +| `\` | Escape character for `\`, `*`, `?` | |
| 45 | + |
| 46 | +Additional rules: |
| 47 | + |
| 48 | +- `/` acts as a path separator and is never matched by `?` or `*` (only by `**`). |
| 49 | +- Use `**` sparingly; prefer narrower patterns (`clientA/*/chat`). |
| 50 | +- Up to five total `*` characters (including those forming `**`) are allowed in a single pattern. |
| 51 | + |
| 52 | +### Examples |
| 53 | + |
| 54 | +| Pattern | Matches | Does not match | |
| 55 | +| ------- | ------- | -------------- | |
| 56 | +| `chat-*` | `chat-1`, `chat-room` | `chat/1`, `xchat-1` | |
| 57 | +| `clientA/*` | `clientA/alpha`, `clientA/1` | `clientA/alpha/room1`, `clientB/alpha` | |
| 58 | +| `clientA/**` | `clientA/alpha`, `clientA/alpha/room1` | `clientB/anything` | |
| 59 | +| `clientA/rooms/?1` | `clientA/rooms/a1`, `clientA/rooms/11` | `clientA/rooms/1`, `clientA/rooms/a/1` | |
| 60 | +| `literal\*star` | `literal*star` | `literalXstar` | |
| 61 | + |
| 62 | +### Escaping |
| 63 | + |
| 64 | +Prefix `*`, `?`, or `\` with `\` to match the literal character. Example: `project\*123` matches only `project*123`. |
| 65 | + |
| 66 | +## Using pattern roles in code |
| 67 | + |
| 68 | +Add the pattern role to the `roles` collection when generating a client access token. The client then automatically has the implied permissions for matching groups. |
| 69 | + |
| 70 | +## Code samples |
| 71 | + |
| 72 | +# [JavaScript](#tab/javascript) |
| 73 | + |
| 74 | +```js |
| 75 | +const token = await serviceClient.getClientAccessToken({ |
| 76 | + roles: [ |
| 77 | + // Can send to all groups under clientA/ |
| 78 | + 'webpubsub.sendToGroups.clientA/**', |
| 79 | + // Can join/leave any direct child group under clientA/public/ |
| 80 | + 'webpubsub.joinLeaveGroups.clientA/public/*' |
| 81 | + ] |
| 82 | +}); |
| 83 | +``` |
| 84 | + |
| 85 | +# [C#](#tab/csharp) |
| 86 | + |
| 87 | +```csharp |
| 88 | +var url = service.GetClientAccessUri(roles: new [] { |
| 89 | + "webpubsub.sendToGroups.clientA/**", |
| 90 | + "webpubsub.joinLeaveGroups.clientA/public/*" |
| 91 | +}); |
| 92 | +``` |
| 93 | + |
| 94 | +# [Python](#tab/python) |
| 95 | + |
| 96 | +```python |
| 97 | +token = service.get_client_access_token(roles=[ |
| 98 | + "webpubsub.sendToGroups.clientA/**", |
| 99 | + "webpubsub.joinLeaveGroups.clientA/public/*" |
| 100 | +]) |
| 101 | +``` |
| 102 | + |
| 103 | +# [Java](#tab/java) |
| 104 | + |
| 105 | +```java |
| 106 | +GetClientAccessTokenOptions opt = new GetClientAccessTokenOptions(); |
| 107 | +opt.addRole("webpubsub.sendToGroups.clientA/**"); |
| 108 | +opt.addRole("webpubsub.joinLeaveGroups.clientA/public/*"); |
| 109 | +WebPubSubClientAccessToken token = service.getClientAccessToken(opt); |
| 110 | +``` |
| 111 | + |
| 112 | +--- |
| 113 | + |
| 114 | +## Security guidance |
| 115 | + |
| 116 | +- Prefer the narrowest pattern that satisfies the scenario. |
| 117 | + |
| 118 | +## Frequently asked questions |
| 119 | + |
| 120 | +**Q: Can I mix literal and pattern roles?** |
| 121 | +Yes. A literal role always applies exactly; patterns add broader coverage. |
| 122 | + |
| 123 | + |
| 124 | +## Next steps |
| 125 | + |
| 126 | +> [!div class="nextstepaction"] |
| 127 | +> [Generate client access URL and use roles](howto-generate-client-access-url.md) |
| 128 | +
|
| 129 | +> [!div class="nextstepaction"] |
| 130 | +> [Authorize access with Microsoft Entra ID](concept-azure-ad-authorization.md) |
0 commit comments