Skip to content

Commit

Permalink
refactor(service-client): Pass the correct amqp audience based on aut…
Browse files Browse the repository at this point in the history
…h type (#1835)
  • Loading branch information
vinagesh committed Mar 22, 2021
1 parent 7dd0917 commit e1bc4e1
Show file tree
Hide file tree
Showing 5 changed files with 7 additions and 23 deletions.
2 changes: 1 addition & 1 deletion iothub/service/src/AmqpServiceClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public AmqpServiceClient(
ServiceClientTransportSettings transportSettings,
ServiceClientOptions options)
{
var iotHubConnection = new IotHubConnection(connectionProperties, AccessRights.ServiceConnect, useWebSocketOnly, transportSettings);
var iotHubConnection = new IotHubConnection(connectionProperties, useWebSocketOnly, transportSettings);
Connection = iotHubConnection;
OpenTimeout = IotHubConnection.DefaultOpenTimeout;
OperationTimeout = IotHubConnection.DefaultOperationTimeout;
Expand Down
17 changes: 0 additions & 17 deletions iothub/service/src/Common/Data/AccessRights.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,21 +45,4 @@ public enum AccessRights
/// </summary>
DeviceConnect = 8
}

internal static class AccessRightsHelper
{
public static string[] AccessRightsToStringArray(AccessRights accessRights)
{
var values = new List<string>(2);
foreach (AccessRights right in Enum.GetValues(typeof(AccessRights)))
{
if (accessRights.HasFlag(right))
{
values.Add(right.ToString());
}
}

return values.ToArray();
}
}
}
7 changes: 2 additions & 5 deletions iothub/service/src/IotHubConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ internal sealed class IotHubConnection : IDisposable
internal static readonly TimeSpan DefaultOperationTimeout = TimeSpan.FromMinutes(1);
internal static readonly TimeSpan DefaultOpenTimeout = TimeSpan.FromMinutes(1);

private readonly AccessRights _accessRights;

private readonly bool _useWebSocketOnly;
private readonly ServiceClientTransportSettings _transportSettings;

Expand All @@ -51,7 +49,7 @@ internal sealed class IotHubConnection : IDisposable
private IOThreadTimer _refreshTokenTimer;
#endif

public IotHubConnection(IotHubConnectionProperties credential, AccessRights accessRights, bool useWebSocketOnly, ServiceClientTransportSettings transportSettings)
public IotHubConnection(IotHubConnectionProperties credential, bool useWebSocketOnly, ServiceClientTransportSettings transportSettings)
{
#if !NET451
_refreshTokenTimer = new IOThreadTimerSlim(s => ((IotHubConnection)s).OnRefreshTokenAsync(), this);
Expand All @@ -60,7 +58,6 @@ public IotHubConnection(IotHubConnectionProperties credential, AccessRights acce
#endif

Credential = credential;
_accessRights = accessRights;
_faultTolerantSession = new FaultTolerantAmqpObject<AmqpSession>(CreateSessionAsync, CloseConnection);
_useWebSocketOnly = useWebSocketOnly;
_transportSettings = transportSettings;
Expand Down Expand Up @@ -553,7 +550,7 @@ private async Task SendCbsTokenAsync(AmqpCbsLink cbsLink, TimeSpan timeout)
Credential.AmqpEndpoint,
audience,
resource,
AccessRightsHelper.AccessRightsToStringArray(_accessRights),
Credential.AmqpAudience.ToArray(),
timeout)
.ConfigureAwait(false);
ScheduleTokenRefresh(expiresAtUtc);
Expand Down
2 changes: 2 additions & 0 deletions iothub/service/src/IotHubConnectionProperties.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ protected IotHubConnectionProperties(string hostName)

public Uri AmqpEndpoint { get; protected set; }

public List<string> AmqpAudience { get; protected set; } = new List<string>();

public abstract string GetAuthorizationHeader();

public abstract Task<CbsToken> GetTokenAsync(Uri namespaceAddress, string appliesTo, string[] requiredClaims);
Expand Down
2 changes: 2 additions & 0 deletions iothub/service/src/IotHubSasCredentialProperties.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Microsoft.Azure.Amqp;
using System.Globalization;
using System.Linq;
using Microsoft.Azure.Devices.Common.Data;

#if !NET451

Expand All @@ -30,6 +31,7 @@ public IotHubSasCredentialProperties()
public IotHubSasCredentialProperties(string hostName, AzureSasCredential credential) : base(hostName)
{
_credential = credential;
AmqpAudience = new List<string> { AccessRights.ServiceConnect.ToString() };
}

#endif
Expand Down

0 comments on commit e1bc4e1

Please sign in to comment.