-
Notifications
You must be signed in to change notification settings - Fork 493
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Revert "Revert "Adding RBAC support for provisioning SDK (#2262)"" This reverts commit 1a0b9de. * Revert "Revert "Adding RBAC support for provisioning SDK (#2262)"" This reverts commit 1a0b9de. * Basic test layout * API update * Style updates * Fix namespace issue * Misc cleanup * Added new field * Reverted e2e script changes * Changing <code> to <c> * Replaced the rest * Updating ArgumentException arguments * Removed digital twin comments * Revert "Added new field" This reverts commit 60a3475. * Added URL encoding * Added a few comments * Fixed syntax error Co-authored-by: Azad Abbasi <[email protected]> Co-authored-by: David R. Williamson <[email protected]>
- Loading branch information
1 parent
b1caf77
commit ed49d32
Showing
13 changed files
with
619 additions
and
174 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
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,61 @@ | ||
using System; | ||
|
||
namespace Microsoft.Azure.Devices.E2ETests.Provisioning | ||
{ | ||
internal class ConnectionStringParser | ||
{ | ||
public string ProvisioningHostName { get; private set; } | ||
|
||
public string DeviceId { get; private set; } | ||
|
||
public string SharedAccessKey { get; private set; } | ||
|
||
public string SharedAccessKeyName { get; private set; } | ||
|
||
public ConnectionStringParser(string connectionString) | ||
{ | ||
if (string.IsNullOrWhiteSpace(connectionString)) | ||
{ | ||
throw new ArgumentException(nameof(connectionString), "Parameter cannot be null, empty, or whitespace."); | ||
} | ||
|
||
// Connection string sections are demarcated with semicolon | ||
string[] parts = connectionString.Split(';'); | ||
|
||
foreach (string part in parts) | ||
{ | ||
int separatorIndex = part.IndexOf('='); | ||
if (separatorIndex < 0) | ||
{ | ||
throw new ArgumentException($"Improperly formatted key/value pair: {part}."); | ||
} | ||
|
||
string key = part.Substring(0, separatorIndex); | ||
string value = part.Substring(separatorIndex + 1); | ||
|
||
switch (key.ToUpperInvariant()) | ||
{ | ||
case "HOSTNAME": | ||
// Gives the correct Host Name to send requests to | ||
ProvisioningHostName = value; | ||
break; | ||
|
||
case "SHAREDACCESSKEY": | ||
SharedAccessKey = value; | ||
break; | ||
|
||
case "DEVICEID": | ||
DeviceId = value; | ||
break; | ||
|
||
case "SHAREDACCESSKEYNAME": | ||
SharedAccessKeyName = value; | ||
break; | ||
|
||
default: | ||
throw new NotSupportedException($"Unrecognized tag found in parameter {nameof(connectionString)}."); | ||
} | ||
} | ||
} | ||
} | ||
} |
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
25 changes: 25 additions & 0 deletions
25
provisioning/service/src/Auth/ProvisioningSasCredential.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,25 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
using Azure; | ||
using Microsoft.Azure.Devices.Common.Service.Auth; | ||
|
||
namespace Microsoft.Azure.Devices.Provisioning.Service.Auth | ||
{ | ||
/// <summary> | ||
/// Allows authentication to the API using a Shared Access Key provided by custom implementation. | ||
/// </summary> | ||
internal class ProvisioningSasCredential : IAuthorizationHeaderProvider | ||
{ | ||
private readonly AzureSasCredential _azureSasCredential; | ||
|
||
public ProvisioningSasCredential(AzureSasCredential azureSasCredential) | ||
{ | ||
_azureSasCredential = azureSasCredential; | ||
} | ||
|
||
public string GetAuthorizationHeader() | ||
{ | ||
return _azureSasCredential.Signature; | ||
} | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
provisioning/service/src/Auth/ProvisioningTokenCredential.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,41 @@ | ||
// 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; | ||
using Azure.Core; | ||
using Microsoft.Azure.Devices.Common.Service.Auth; | ||
|
||
namespace Microsoft.Azure.Devices.Provisioning.Service.Auth | ||
{ | ||
/// <summary> | ||
/// Allows authentication to the API using a JWT token generated for Azure active directory. | ||
/// </summary> | ||
internal class ProvisioningTokenCredential : IAuthorizationHeaderProvider | ||
{ | ||
private readonly TokenCredential _credential; | ||
private readonly object _tokenLock = new object(); | ||
private AccessToken? _cachedAccessToken; | ||
|
||
public ProvisioningTokenCredential(TokenCredential credential) | ||
{ | ||
_credential = credential; | ||
} | ||
|
||
// The HTTP protocol uses this method to get the bearer token for authentication. | ||
public string GetAuthorizationHeader() | ||
{ | ||
lock (_tokenLock) | ||
{ | ||
// A new token is generated if it is the first time or the cached token is close to expiry. | ||
if (!_cachedAccessToken.HasValue | ||
|| TokenHelper.IsCloseToExpiry(_cachedAccessToken.Value.ExpiresOn)) | ||
{ | ||
_cachedAccessToken = _credential.GetToken( | ||
new TokenRequestContext(new string[] { "https://azure-devices-provisioning.net/.default" }), | ||
new CancellationToken()); | ||
} | ||
} | ||
|
||
return $"Bearer {_cachedAccessToken.Value.Token}"; | ||
} | ||
} | ||
} |
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,21 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
using System; | ||
|
||
namespace Microsoft.Azure.Devices.Provisioning.Service.Auth | ||
{ | ||
internal static class TokenHelper | ||
{ | ||
/// <summary> | ||
/// Determines if the given token expiry date time is close to expiry. The date and time is | ||
/// considered close to expiry if it has less than 10 minutes relative to the current time. | ||
/// </summary> | ||
/// <param name="expiry">The token expiration date and time.</param> | ||
/// <returns>True if the token expiry has less than 10 minutes relative to the current time, otherwise false.</returns> | ||
public static bool IsCloseToExpiry(DateTimeOffset expiry) | ||
{ | ||
TimeSpan timeToExpiry = expiry - DateTimeOffset.UtcNow; | ||
return timeToExpiry.TotalMinutes < 10; | ||
} | ||
} | ||
} |
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
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.