-
Notifications
You must be signed in to change notification settings - Fork 102
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
38 changed files
with
489 additions
and
243 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
samples/ChatSample/ChatSample.ManagementPublisher/ManagementPublisher.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net6.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\..\src\Microsoft.Azure.SignalR.Management\Microsoft.Azure.SignalR.Management.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
83 changes: 83 additions & 0 deletions
83
samples/ChatSample/ChatSample.ManagementPublisher/MessagePublisher.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using Microsoft.Azure.SignalR.Management; | ||
|
||
// A simple library file to test cross project reference issue: https://github.com/Azure/azure-signalr/issues/1720 | ||
namespace ManagementPublisher | ||
{ | ||
internal class MessagePublisher | ||
{ | ||
private const string Target = "Target"; | ||
private const string HubName = "Chat"; | ||
private ServiceHubContext? _hubContext; | ||
|
||
public async Task InitAsync(string connectionString, ServiceTransportType transportType = ServiceTransportType.Transient) | ||
{ | ||
var serviceManager = new ServiceManagerBuilder().WithOptions(option => | ||
{ | ||
option.ConnectionString = connectionString; | ||
option.ServiceTransportType = transportType; | ||
}) | ||
// Uncomment the following line to get more logs | ||
//.WithLoggerFactory(LoggerFactory.Create(builder => builder.AddConsole())) | ||
.BuildServiceManager(); | ||
|
||
_hubContext = await serviceManager.CreateHubContextAsync(HubName, default); | ||
} | ||
|
||
public Task SendMessages(string command, string? receiver, string message) | ||
{ | ||
if (_hubContext == null) | ||
{ | ||
throw new ArgumentNullException(nameof(_hubContext)); | ||
} | ||
switch (command) | ||
{ | ||
case "broadcast": | ||
return _hubContext.Clients.All.SendCoreAsync(Target, new[] { message }); | ||
case "user": | ||
var userId = receiver ?? throw new ArgumentNullException(nameof(receiver)); | ||
return _hubContext.Clients.User(userId).SendCoreAsync(Target, new[] { message }); | ||
case "group": | ||
var groupName = receiver ?? throw new ArgumentNullException(nameof(receiver)); | ||
return _hubContext.Clients.Group(groupName).SendCoreAsync(Target, new[] { message }); | ||
default: | ||
Console.WriteLine($"Can't recognize command {command}"); | ||
return Task.CompletedTask; | ||
} | ||
} | ||
|
||
public Task CloseConnection(string connectionId, string reason) | ||
{ | ||
if (_hubContext == null) | ||
{ | ||
throw new ArgumentNullException(nameof(_hubContext)); | ||
} | ||
return _hubContext.ClientManager.CloseConnectionAsync(connectionId, reason); | ||
} | ||
|
||
public Task<bool> CheckExist(string type, string id) | ||
{ | ||
if (_hubContext == null) | ||
{ | ||
throw new ArgumentNullException(nameof(_hubContext)); | ||
} | ||
return type switch | ||
{ | ||
"connection" => _hubContext.ClientManager.ConnectionExistsAsync(id), | ||
"user" => _hubContext.ClientManager.UserExistsAsync(id), | ||
"group" => _hubContext.ClientManager.UserExistsAsync(id), | ||
_ => throw new NotSupportedException(), | ||
}; | ||
} | ||
|
||
public async Task DisposeAsync() | ||
{ | ||
if (_hubContext != null) | ||
{ | ||
await _hubContext.DisposeAsync(); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
src/Microsoft.Azure.SignalR.Common/Auth/AadTokenProvider.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using System; | ||
using System.Threading.Tasks; | ||
|
||
namespace Microsoft.Azure.SignalR | ||
{ | ||
internal class AadTokenProvider : IAccessTokenProvider | ||
{ | ||
private readonly AadAccessKey _accessKey; | ||
|
||
public AadTokenProvider(AadAccessKey accessKey) | ||
{ | ||
_accessKey = accessKey ?? throw new ArgumentNullException(nameof(accessKey)); | ||
} | ||
|
||
public Task<string> ProvideAsync() => _accessKey.GenerateAadTokenAsync(); | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
src/Microsoft.Azure.SignalR.Common/Auth/LocalTokenProvider.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Security.Claims; | ||
using System.Threading.Tasks; | ||
|
||
namespace Microsoft.Azure.SignalR | ||
{ | ||
internal class LocalTokenProvider : IAccessTokenProvider | ||
{ | ||
private readonly AccessKey _accessKey; | ||
|
||
private readonly AccessTokenAlgorithm _algorithm; | ||
|
||
private readonly string _audience; | ||
|
||
private readonly TimeSpan _tokenLifetime; | ||
|
||
private readonly IEnumerable<Claim> _claims; | ||
|
||
public LocalTokenProvider( | ||
AccessKey accessKey, | ||
string audience, | ||
IEnumerable<Claim> claims, | ||
AccessTokenAlgorithm algorithm = AccessTokenAlgorithm.HS256, | ||
TimeSpan? tokenLifetime = null) | ||
{ | ||
_accessKey = accessKey ?? throw new ArgumentNullException(nameof(accessKey)); | ||
_algorithm = algorithm; | ||
_audience = audience; | ||
_claims = claims; | ||
_tokenLifetime = tokenLifetime ?? Constants.Periods.DefaultAccessTokenLifetime; | ||
} | ||
|
||
public Task<string> ProvideAsync() => _accessKey.GenerateAccessTokenAsync(_audience, _claims, _tokenLifetime, _algorithm); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
src/Microsoft.Azure.SignalR.Common/Interfaces/IAccessTokenProvider.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using System.Threading.Tasks; | ||
|
||
namespace Microsoft.Azure.SignalR | ||
{ | ||
internal interface IAccessTokenProvider | ||
{ | ||
Task<string> ProvideAsync(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.