-
Notifications
You must be signed in to change notification settings - Fork 4.5k
/
WebPubSubServiceClient_helpers.cs
358 lines (316 loc) · 16.6 KB
/
WebPubSubServiceClient_helpers.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using Azure.Core;
using Azure.Core.Pipeline;
namespace Azure.Messaging.WebPubSub
{
/// <summary>
/// Azure Web PubSub Service Client.
/// </summary>
public partial class WebPubSubServiceClient
{
private const string EndpointPropertyName = "Endpoint";
private const string AccessKeyPropertyName = "AccessKey";
private const string PortPropertyName = "Port";
private const string ClientTokenResponseTokenPropertyName = "token";
private static readonly char[] KeyValueSeparator = { '=' };
private static readonly char[] PropertySeparator = { ';' };
internal static byte[] s_role = Encoding.UTF8.GetBytes("role");
internal static byte[] s_group = Encoding.UTF8.GetBytes("webpubsub.group");
/// <summary>
/// Creates a URI with authentication token.
/// </summary>
/// <param name="expiresAt">UTC time when the token expires.</param>
/// <param name="userId">User Id.</param>
/// <param name="cancellationToken">Cancellation token.</param>
/// <param name="roles">Roles that the connection with the generated token will have.</param>
/// <returns></returns>
#pragma warning disable AZC0015 // Unexpected client method return type.
public virtual Uri GetClientAccessUri(DateTimeOffset expiresAt, string userId, IEnumerable<string> roles, CancellationToken cancellationToken)
#pragma warning restore AZC0015 // Unexpected client method return type.
=> GetClientAccessUriInternal(expiresAt, userId, roles, null, async: false, cancellationToken).EnsureCompleted();
/// <summary>
/// Creates a URI with authentication token.
/// </summary>
/// <param name="expiresAt">UTC time when the token expires.</param>
/// <param name="userId">User Id.</param>
/// <param name="cancellationToken">Cancellation token.</param>
/// <param name="roles">Roles that the connection with the generated token will have.</param>
/// <param name="groups">Groups that the connection with the generated token will join when it connects.</param>
/// <returns></returns>
#pragma warning disable AZC0015 // Unexpected client method return type.
public virtual Uri GetClientAccessUri(DateTimeOffset expiresAt, string userId = default, IEnumerable<string> roles = default, IEnumerable<string> groups = default, CancellationToken cancellationToken = default)
#pragma warning restore AZC0015 // Unexpected client method return type.
=> GetClientAccessUriInternal(expiresAt, userId, roles, groups, async: false, cancellationToken).EnsureCompleted();
/// <summary>
/// Creates a URI with authentication token.
/// </summary>
/// <param name="expiresAt">UTC time when the token expires.</param>
/// <param name="userId">User Id.</param>
/// <param name="cancellationToken">Cancellation token.</param>
/// <param name="roles">Roles that the connection with the generated token will have.</param>
/// <returns></returns>
#pragma warning disable AZC0015 // Unexpected client method return type.
public virtual async Task<Uri> GetClientAccessUriAsync(DateTimeOffset expiresAt, string userId, IEnumerable<string> roles, CancellationToken cancellationToken)
=> await GetClientAccessUriInternal(expiresAt, userId, roles, null, async: true, cancellationToken).ConfigureAwait(false);
#pragma warning restore AZC0015 // Unexpected client method return type.
/// <summary>
/// Creates a URI with authentication token.
/// </summary>
/// <param name="expiresAt">UTC time when the token expires.</param>
/// <param name="userId">User Id.</param>
/// <param name="cancellationToken">Cancellation token.</param>
/// <param name="roles">Roles that the connection with the generated token will have.</param>
/// <param name="groups">Groups that the connection with the generated token will join when it connects.</param>
/// <returns></returns>
#pragma warning disable AZC0015 // Unexpected client method return type.
public virtual async Task<Uri> GetClientAccessUriAsync(DateTimeOffset expiresAt, string userId = default, IEnumerable<string> roles = default, IEnumerable<string> groups = default, CancellationToken cancellationToken = default)
=> await GetClientAccessUriInternal(expiresAt, userId, roles, groups, async: true, cancellationToken).ConfigureAwait(false);
#pragma warning restore AZC0015 // Unexpected client method return type.
/// <summary>
/// Creates a URI with authentication token.
/// </summary>
/// <param name="expiresAfter">Defaults to one hour, if not specified. Must be greater or equal zero.</param>
/// <param name="userId">User Id.</param>
/// <param name="roles">Roles that the connection with the generated token will have.</param>
/// <param name="cancellationToken">Cancellation token.</param>
/// <returns></returns>
#pragma warning disable AZC0015 // Unexpected client method return type.
public virtual Uri GetClientAccessUri(
TimeSpan expiresAfter,
string userId,
IEnumerable<string> roles,
CancellationToken cancellationToken)
#pragma warning restore AZC0015 // Unexpected client method return type.
{
return GetClientAccessUriInternal(expiresAfter, userId, roles, null, false, cancellationToken).EnsureCompleted();
}
/// <summary>
/// Creates a URI with authentication token.
/// </summary>
/// <param name="expiresAfter">Defaults to one hour, if not specified. Must be greater or equal zero.</param>
/// <param name="userId">User Id.</param>
/// <param name="roles">Roles that the connection with the generated token will have.</param>
/// <param name="groups">Groups that the connection with the generated token will join when it connects.</param>
/// <param name="cancellationToken">Cancellation token.</param>
/// <returns></returns>
#pragma warning disable AZC0015 // Unexpected client method return type.
public virtual Uri GetClientAccessUri(
TimeSpan expiresAfter = default,
string userId = default,
IEnumerable<string> roles = default,
IEnumerable<string> groups = default,
CancellationToken cancellationToken = default)
#pragma warning restore AZC0015 // Unexpected client method return type.
{
return GetClientAccessUriInternal(expiresAfter, userId, roles, groups, false, cancellationToken).EnsureCompleted();
}
/// <summary>
/// Creates a URI with authentication token.
/// </summary>
/// <param name="expiresAfter">Defaults to one hour, if not specified. Must be greater or equal zero.</param>
/// <param name="userId">User Id.</param>
/// <param name="roles">Roles that the connection with the generated token will have.</param>
/// <param name="cancellationToken">Cancellation token.</param>
/// <returns></returns>
#pragma warning disable AZC0015 // Unexpected client method return type.
public virtual async Task<Uri> GetClientAccessUriAsync(
TimeSpan expiresAfter,
string userId,
IEnumerable<string> roles,
CancellationToken cancellationToken)
#pragma warning restore AZC0015 // Unexpected client method return type.
{
return await GetClientAccessUriInternal(expiresAfter, userId, roles, null, true, cancellationToken).ConfigureAwait(false);
}
/// <summary>
/// Creates a URI with authentication token.
/// </summary>
/// <param name="expiresAfter">Defaults to one hour, if not specified. Must be greater or equal zero.</param>
/// <param name="userId">User Id.</param>
/// <param name="roles">Roles that the connection with the generated token will have.</param>
/// <param name="groups">Groups that the connection with the generated token will join when it connects.</param>
/// <param name="cancellationToken">Cancellation token.</param>
/// <returns></returns>
#pragma warning disable AZC0015 // Unexpected client method return type.
public virtual async Task<Uri> GetClientAccessUriAsync(
TimeSpan expiresAfter = default,
string userId = default,
IEnumerable<string> roles = default,
IEnumerable<string> groups = default,
CancellationToken cancellationToken = default)
#pragma warning restore AZC0015 // Unexpected client method return type.
{
return await GetClientAccessUriInternal(expiresAfter, userId, roles, groups, true, cancellationToken).ConfigureAwait(false);
}
internal static int GetMinutesToExpire(TimeSpan expiresAfter) => Math.Max((int)expiresAfter.TotalMinutes, 1);
internal static int GetMinutesToExpire(DateTimeOffset expiresAt) => Math.Max((int)expiresAt.Subtract(DateTimeOffset.UtcNow).TotalMinutes, 1);
private async Task<Uri> GetClientAccessUriInternal(
DateTimeOffset expiresAt,
string userId = default,
IEnumerable<string> roles = default,
IEnumerable<string> groups = default,
bool async = true,
CancellationToken cancellationToken = default)
{
string token;
if (_tokenCredential != null)
{
RequestContext context = new() { CancellationToken = cancellationToken };
var minutesToExpire = GetMinutesToExpire(expiresAt);
Response clientTokenResponse = async ?
await GenerateClientTokenImplAsync(userId, roles, minutesToExpire, groups, context).ConfigureAwait(false) :
GenerateClientTokenImpl(userId, roles, minutesToExpire, null, context);
using var jsonDocument = JsonDocument.Parse(clientTokenResponse.Content);
token = jsonDocument.RootElement.GetProperty(ClientTokenResponseTokenPropertyName).GetString();
}
else if (_credential != null)
{
token = GenerateTokenFromAzureKeyCredential(expiresAt, userId, roles, groups);
}
else
{
throw new InvalidOperationException($"{nameof(WebPubSubServiceClient)} must be constructed with either a {typeof(TokenCredential)} or {typeof(AzureKeyCredential)} to generate client access URIs.");
}
UriBuilder clientEndpoint = new(Endpoint)
{
Scheme = Endpoint.Scheme == "http" ? "ws" : "wss"
};
return new Uri($"{clientEndpoint}client/hubs/{_hub}?access_token={token}");
}
private async Task<Uri> GetClientAccessUriInternal(
TimeSpan expireAfter,
string userId = default,
IEnumerable<string> roles = default,
IEnumerable<string> groups = default,
bool async = true,
CancellationToken cancellationToken = default)
{
string token;
if (_tokenCredential != null)
{
RequestContext context = new() { CancellationToken = cancellationToken };
var minutesToExpire = GetMinutesToExpire(expireAfter);
Response clientTokenResponse = async ?
await GenerateClientTokenImplAsync(userId, roles, minutesToExpire, groups, context).ConfigureAwait(false) :
GenerateClientTokenImpl(userId, roles, minutesToExpire, null, context);
using var jsonDocument = JsonDocument.Parse(clientTokenResponse.Content);
token = jsonDocument.RootElement.GetProperty(ClientTokenResponseTokenPropertyName).GetString();
}
else if (_credential != null)
{
if (expireAfter == default)
{
expireAfter = TimeSpan.FromHours(1);
}
token = GenerateTokenFromAzureKeyCredential(DateTimeOffset.UtcNow.Add(expireAfter), userId, roles, groups);
}
else
{
throw new InvalidOperationException($"{nameof(WebPubSubServiceClient)} must be constructed with either a {typeof(TokenCredential)} or {typeof(AzureKeyCredential)} to generate client access URIs.");
}
UriBuilder clientEndpoint = new(Endpoint)
{
Scheme = Endpoint.Scheme == "http" ? "ws" : "wss"
};
return new Uri($"{clientEndpoint}client/hubs/{_hub}?access_token={token}");
}
/// <summary>
/// Parse connection string to endpoint and credential.
/// </summary>
/// <returns></returns>
internal static (Uri Endpoint, AzureKeyCredential Credential) ParseConnectionString(string connectionString)
{
Argument.AssertNotNull(connectionString, nameof(connectionString));
var properties = connectionString.Split(PropertySeparator, StringSplitOptions.RemoveEmptyEntries);
var dict = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
foreach (var property in properties)
{
var kvp = property.Split(KeyValueSeparator, 2);
if (kvp.Length != 2)
continue;
var key = kvp[0].Trim();
if (dict.ContainsKey(key))
{
throw new ArgumentException($"Duplicate properties found in connection string: {key}.");
}
dict.Add(key, kvp[1].Trim());
}
if (!dict.TryGetValue(EndpointPropertyName, out var endpoint))
{
throw new ArgumentException($"Required property not found in connection string: {EndpointPropertyName}.");
}
endpoint = endpoint.TrimEnd('/');
if (!dict.TryGetValue(AccessKeyPropertyName, out var accessKey))
{
throw new ArgumentException($"Required property not found in connection string: {AccessKeyPropertyName}.");
}
int? port = null;
if (dict.TryGetValue(PortPropertyName, out var rawPort))
{
if (int.TryParse(rawPort, out var portValue) && portValue > 0 && portValue <= 0xFFFF)
{
port = portValue;
}
else
{
throw new ArgumentException($"Invalid Port value: {rawPort}");
}
}
var uriBuilder = new UriBuilder(endpoint);
if (port.HasValue)
{
uriBuilder.Port = port.Value;
}
return (uriBuilder.Uri, new AzureKeyCredential(accessKey));
}
internal static string PermissionToString(WebPubSubPermission permission)
{
switch (permission)
{
case WebPubSubPermission.SendToGroup:
return "sendToGroup";
case WebPubSubPermission.JoinLeaveGroup:
return "joinLeaveGroup";
default:
throw new ArgumentOutOfRangeException(nameof(permission));
}
}
private string GenerateTokenFromAzureKeyCredential(DateTimeOffset expiresAt, string userId = default, IEnumerable<string> roles = default, IEnumerable<string> groups = default)
{
var keyBytes = Encoding.UTF8.GetBytes(_credential.Key);
var jwt = new JwtBuilder(keyBytes);
var now = DateTimeOffset.UtcNow;
string endpoint = Endpoint.AbsoluteUri;
if (!endpoint.EndsWith("/", StringComparison.Ordinal))
{
endpoint += "/";
}
var audience = $"{endpoint}client/hubs/{_hub}";
if (userId != default)
{
jwt.AddClaim(JwtBuilder.Sub, userId);
}
if (roles != default && roles.Any())
{
jwt.AddClaim(s_role, roles);
}
if (groups != default && groups.Any())
{
jwt.AddClaim(s_group, groups);
}
jwt.AddClaim(JwtBuilder.Nbf, now);
jwt.AddClaim(JwtBuilder.Exp, expiresAt);
jwt.AddClaim(JwtBuilder.Iat, now);
jwt.AddClaim(JwtBuilder.Aud, audience);
return jwt.BuildString();
}
}
}