Skip to content

Commit

Permalink
Cleanup code in Hub RTAC (#2268)
Browse files Browse the repository at this point in the history
  • Loading branch information
David R. Williamson authored Jan 7, 2022
1 parent 0582733 commit 4de5519
Show file tree
Hide file tree
Showing 11 changed files with 48 additions and 52 deletions.
5 changes: 4 additions & 1 deletion common/src/service/IAuthorizationHeaderProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@

namespace Microsoft.Azure.Devices
{
interface IAuthorizationHeaderProvider
/// <summary>
/// Gets the authorization header for authenticated requests, regardless of choice of authentication.
/// </summary>
internal interface IAuthorizationHeaderProvider
{
string GetAuthorizationHeader();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Microsoft.Azure.Devices.Authentication
/// <summary>
/// Allows authentication to the API using a Shared Access Key generated from the connection string provided.
/// The PnP client is auto generated from swagger and needs to implement a specific class to pass to the protocol layer
/// unlike the rest of the clients which are hand-written. So, this implementation for authentication is specific to digital twin (Pnp).
/// unlike the rest of the clients which are hand-written. So, this implementation for authentication is specific to digital twin (PnP).
/// </summary>
internal class DigitalTwinConnectionStringCredential : DigitalTwinServiceClientCredentials
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// 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.Text;

using Azure;
using Microsoft.Azure.Devices.Authentication;

Expand All @@ -11,11 +9,11 @@ namespace Microsoft.Azure.Devices.DigitalTwin.Authentication
/// <summary>
/// Allows authentication to the API using a Shared Access Key provided by custom implementation.
/// The PnP client is auto generated from swagger and needs to implement a specific class to pass to the protocol layer
/// unlike the rest of the clients which are hand-written. So, this implementation for authentication is specific to digital twin (Pnp).
/// unlike the rest of the clients which are hand-written. So, this implementation for authentication is specific to digital twin (PnP).
/// </summary>
internal class DigitalTwinSasCredential : DigitalTwinServiceClientCredentials
{
private AzureSasCredential _credential;
private readonly AzureSasCredential _credential;

public DigitalTwinSasCredential(AzureSasCredential credential)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// 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.Text;

using System.Threading;
using Azure.Core;
using Microsoft.Azure.Devices.Authentication;
Expand All @@ -19,7 +17,7 @@ internal class DigitalTwinTokenCredential : DigitalTwinServiceClientCredentials
{
private readonly object _tokenLock = new object();
private AccessToken? _cachedAccessToken;
private TokenCredential _credential;
private readonly TokenCredential _credential;

public DigitalTwinTokenCredential(TokenCredential credential)
{
Expand Down
2 changes: 1 addition & 1 deletion iothub/service/src/IAuthenticationMethod.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
namespace Microsoft.Azure.Devices
{
/// <summary>
/// Authentication interface to use for IoTHub communications.
/// Authentication interface to use for IoT hub communications.
/// </summary>
public interface IAuthenticationMethod
{
Expand Down
2 changes: 1 addition & 1 deletion iothub/service/src/IotHubConnectionProperties.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// 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.Text;
using System.Threading.Tasks;
using Microsoft.Azure.Amqp;
using Microsoft.Azure.Devices.Common;
Expand Down
9 changes: 4 additions & 5 deletions iothub/service/src/IotHubSasCredentialProperties.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
// 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.Text;
using System.Threading.Tasks;
using Microsoft.Azure.Amqp;
using System.Globalization;
using System.Linq;
using Microsoft.Azure.Devices.Common.Data;

#if !NET451

using System.Collections.Generic;
using System.Linq;
using Azure;
using Microsoft.Azure.Devices.Common.Data;

#endif

Expand Down
16 changes: 7 additions & 9 deletions iothub/service/src/IotHubTokenCredentialProperties.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
// 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.Text;
using System.Threading.Tasks;
using Microsoft.Azure.Amqp;
using System.Threading;
using Microsoft.Azure.Devices.Common;

#if !NET451

using System.Threading;
using Azure.Core;
using Microsoft.Azure.Devices.Common;

#endif

Expand All @@ -23,7 +22,7 @@ internal class IotHubTokenCrendentialProperties
: IotHubConnectionProperties
{
#if !NET451
private const string _tokenType = "Bearer";
private const string TokenType = "Bearer";
private readonly TokenCredential _credential;
private readonly object _tokenLock = new object();
private AccessToken? _cachedAccessToken;
Expand Down Expand Up @@ -63,8 +62,7 @@ public override string GetAuthorizationHeader()
}
}

return $"{_tokenType} {_cachedAccessToken.Value.Token}";

return $"{TokenType} {_cachedAccessToken.Value.Token}";
#endif
}

Expand All @@ -82,8 +80,8 @@ public async override Task<CbsToken> GetTokenAsync(Uri namespaceAddress, string
new TokenRequestContext(CommonConstants.IotHubAadTokenScopes),
new CancellationToken()).ConfigureAwait(false);
return new CbsToken(
$"{_tokenType} {token.Token}",
_tokenType,
$"{TokenType} {token.Token}",
TokenType,
token.ExpiresOn.UtcDateTime);
#endif
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,16 @@ public ServiceAuthenticationWithDeviceSharedAccessPolicyKey(string deviceId, str
Key = sharedAccessKey;
}

/// <summary>
/// Shared access key of the device
/// </summary>
public string Key { get; set; }

/// <summary>
/// Name of device
/// </summary>
public string DeviceId { get; set; }

/// <summary>
/// Populates the builder with values needed to authenticate with device's shared access key.
/// </summary>
Expand All @@ -33,21 +43,11 @@ public IotHubConnectionStringBuilder Populate(IotHubConnectionStringBuilder iotH
throw new ArgumentNullException(nameof(iotHubConnectionStringBuilder));
}

iotHubConnectionStringBuilder.SharedAccessKey = this.Key;
iotHubConnectionStringBuilder.DeviceId = this.DeviceId;
iotHubConnectionStringBuilder.SharedAccessKey = Key;
iotHubConnectionStringBuilder.DeviceId = DeviceId;
iotHubConnectionStringBuilder.SharedAccessSignature = null;

return iotHubConnectionStringBuilder;
}

/// <summary>
/// Shared access key of the device
/// </summary>
public string Key { get; set; }

/// <summary>
/// Name of device
/// </summary>
public string DeviceId { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,16 @@ public ServiceAuthenticationWithDeviceSharedAccessPolicyToken(string deviceId, s
Token = sharedAccessSignature;
}

/// <summary>
/// Name of device
/// </summary>
public string DeviceId { get; set; }

/// <summary>
/// Shared access signature generated using device's shared access key
/// </summary>
public string Token { get; set; }

/// <summary>
/// Populates the builder with values needed to authenticate with device's shared access signature.
/// </summary>
Expand All @@ -34,20 +44,10 @@ public IotHubConnectionStringBuilder Populate(IotHubConnectionStringBuilder iotH
}

iotHubConnectionStringBuilder.SharedAccessKey = null;
iotHubConnectionStringBuilder.DeviceId = this.DeviceId;
iotHubConnectionStringBuilder.SharedAccessSignature = this.Token;
iotHubConnectionStringBuilder.DeviceId = DeviceId;
iotHubConnectionStringBuilder.SharedAccessSignature = Token;

return iotHubConnectionStringBuilder;
}

/// <summary>
/// Name of device
/// </summary>
public string DeviceId { get; set; }

/// <summary>
/// Shared access signature generated using device's shared access key
/// </summary>
public string Token { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public ServiceAuthenticationWithSharedAccessPolicyKey(string policyName, string
public string PolicyName
{
get => _policyName;
set { SetPolicyName(value); }
set => SetPolicyName(value);
}

/// <summary>
Expand Down

0 comments on commit 4de5519

Please sign in to comment.