Skip to content

Commit 712cafa

Browse files
authored
code cleanup (#1718)
1 parent 4e1e099 commit 712cafa

File tree

4 files changed

+31
-34
lines changed

4 files changed

+31
-34
lines changed

src/Microsoft.Azure.SignalR.AspNet/EndpointProvider/ServiceEndpointProvider.cs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,26 @@ internal class ServiceEndpointProvider : IServiceEndpointProvider
1818
"or explicitly pass one using IAppBuilder.RunAzureSignalR(connectionString) in Startup.ConfigureServices.";
1919

2020
private const string ClientPath = "aspnetclient";
21+
2122
private const string ServerPath = "aspnetserver";
2223

2324
private readonly string _audienceBaseUrl;
25+
2426
private readonly string _clientEndpoint;
27+
2528
private readonly string _serverEndpoint;
2629

2730
private readonly AccessKey _accessKey;
31+
2832
private readonly string _appName;
33+
2934
private readonly TimeSpan _accessTokenLifetime;
35+
3036
private readonly AccessTokenAlgorithm _algorithm;
3137

3238
public IWebProxy Proxy { get; }
3339

34-
public ServiceEndpointProvider(
35-
ServiceEndpoint endpoint,
36-
ServiceOptions options)
40+
public ServiceEndpointProvider(ServiceEndpoint endpoint, ServiceOptions options)
3741
{
3842
_accessTokenLifetime = options.AccessTokenLifetime;
3943

@@ -48,11 +52,6 @@ public ServiceEndpointProvider(
4852
Proxy = options.Proxy;
4953
}
5054

51-
private string GetPrefixedHubName(string applicationName, string hubName)
52-
{
53-
return string.IsNullOrEmpty(applicationName) ? hubName.ToLower() : $"{applicationName.ToLower()}_{hubName.ToLower()}";
54-
}
55-
5655
public Task<string> GenerateClientAccessTokenAsync(string hubName = null, IEnumerable<Claim> claims = null, TimeSpan? lifetime = null)
5756
{
5857
var audience = $"{_audienceBaseUrl}{ClientPath}";
@@ -67,17 +66,13 @@ public Task<string> GenerateServerAccessTokenAsync(string hubName, string userId
6766
return key.GenerateAadTokenAsync();
6867
}
6968

70-
IEnumerable<Claim> claims = null;
71-
if (userId != null)
69+
if (string.IsNullOrEmpty(hubName))
7270
{
73-
claims = new[]
74-
{
75-
new Claim(ClaimTypes.NameIdentifier, userId)
76-
};
71+
throw new ArgumentNullException(nameof(hubName));
7772
}
7873

7974
var audience = $"{_audienceBaseUrl}{ServerPath}/?hub={GetPrefixedHubName(_appName, hubName)}";
80-
75+
var claims = userId != null ? new[] { new Claim(ClaimTypes.NameIdentifier, userId) } : null;
8176
return _accessKey.GenerateAccessTokenAsync(audience, claims, lifetime ?? _accessTokenLifetime, _algorithm);
8277
}
8378

@@ -114,5 +109,10 @@ public string GetServerEndpoint(string hubName)
114109
{
115110
return $"{_serverEndpoint}{ServerPath}/?hub={GetPrefixedHubName(_appName, hubName)}";
116111
}
112+
113+
private string GetPrefixedHubName(string applicationName, string hubName)
114+
{
115+
return string.IsNullOrEmpty(applicationName) ? hubName.ToLower() : $"{applicationName.ToLower()}_{hubName.ToLower()}";
116+
}
117117
}
118118
}

src/Microsoft.Azure.SignalR.Common/ServiceConnections/ConnectionFactory.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public async Task<ConnectionContext> ConnectAsync(HubServiceEndpoint hubServiceE
3131
{
3232
var provider = hubServiceEndpoint.Provider;
3333
var hubName = hubServiceEndpoint.Hub;
34-
Task<string> accessTokenGenerater() => provider.GenerateServerAccessTokenAsync(hubName, _serverId);
34+
Task<string> accessTokenProvider() => provider.GenerateServerAccessTokenAsync(hubName, _serverId);
3535
var url = GetServiceUrl(provider, hubName, connectionId, target);
3636

3737
headers ??= new Dictionary<string, string>();
@@ -45,7 +45,7 @@ public async Task<ConnectionContext> ConnectAsync(HubServiceEndpoint hubServiceE
4545
Headers = headers,
4646
Proxy = provider.Proxy,
4747
};
48-
var connection = new WebSocketConnectionContext(connectionOptions, _loggerFactory, accessTokenGenerater);
48+
var connection = new WebSocketConnectionContext(connectionOptions, _loggerFactory, accessTokenProvider);
4949
try
5050
{
5151
await connection.StartAsync(url, cancellationToken);

src/Microsoft.Azure.SignalR.Common/ServiceConnections/WebSocketConnectionContext.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ internal class WebSocketConnectionContext : ConnectionContext
3131

3232
public override IDuplexPipe Transport { get; set; }
3333

34-
public WebSocketConnectionContext(WebSocketConnectionOptions httpConnectionOptions, ILoggerFactory loggerFactory, Func<Task<string>> accessTokenProvider)
34+
public WebSocketConnectionContext(WebSocketConnectionOptions httpConnectionOptions,
35+
ILoggerFactory loggerFactory,
36+
Func<Task<string>> accessTokenProvider)
3537
{
3638
Transport = _websocketTransport = new WebSocketsTransport(httpConnectionOptions, loggerFactory, accessTokenProvider);
3739
ConnectionId = "sc_" + Guid.NewGuid();

src/Microsoft.Azure.SignalR/EndpointProvider/ServiceEndpointProvider.cs

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,18 @@ internal class ServiceEndpointProvider : IServiceEndpointProvider
1717
"or explicitly pass one using IServiceCollection.AddAzureSignalR(connectionString) in Startup.ConfigureServices.";
1818

1919
private readonly AccessKey _accessKey;
20+
2021
private readonly string _appName;
22+
2123
private readonly TimeSpan _accessTokenLifetime;
24+
2225
private readonly IServiceEndpointGenerator _generator;
26+
2327
private readonly AccessTokenAlgorithm _algorithm;
2428

2529
public IWebProxy Proxy { get; }
2630

27-
public ServiceEndpointProvider(
28-
ServiceEndpoint endpoint,
29-
ServiceOptions serviceOptions)
31+
public ServiceEndpointProvider(ServiceEndpoint endpoint, ServiceOptions serviceOptions)
3032
{
3133
_accessTokenLifetime = serviceOptions.AccessTokenLifetime;
3234
_accessKey = endpoint.AccessKey;
@@ -64,28 +66,21 @@ public Task<string> GenerateServerAccessTokenAsync(string hubName, string userId
6466

6567
var audience = _generator.GetServerAudience(hubName, _appName);
6668
var claims = userId != null ? new[] { new Claim(ClaimTypes.NameIdentifier, userId) } : null;
67-
6869
return _accessKey.GenerateAccessTokenAsync(audience, claims, lifetime ?? _accessTokenLifetime, _algorithm);
6970
}
7071

7172
public string GetClientEndpoint(string hubName, string originalPath, string queryString)
7273
{
73-
if (string.IsNullOrEmpty(hubName))
74-
{
75-
throw new ArgumentNullException(nameof(hubName));
76-
}
77-
78-
return _generator.GetClientEndpoint(hubName, _appName, originalPath, queryString);
74+
return string.IsNullOrEmpty(hubName)
75+
? throw new ArgumentNullException(nameof(hubName))
76+
: _generator.GetClientEndpoint(hubName, _appName, originalPath, queryString);
7977
}
8078

8179
public string GetServerEndpoint(string hubName)
8280
{
83-
if (string.IsNullOrEmpty(hubName))
84-
{
85-
throw new ArgumentNullException(nameof(hubName));
86-
}
87-
88-
return _generator.GetServerEndpoint(hubName, _appName);
81+
return string.IsNullOrEmpty(hubName)
82+
? throw new ArgumentNullException(nameof(hubName))
83+
: _generator.GetServerEndpoint(hubName, _appName);
8984
}
9085
}
9186
}

0 commit comments

Comments
 (0)