-
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.
(service-client): Refactor and add sas credential
- Loading branch information
Showing
8 changed files
with
156 additions
and
32 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,47 @@ | ||
// 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; | ||
|
||
#if !NET451 | ||
|
||
using Azure; | ||
|
||
namespace Microsoft.Azure.Devices | ||
{ | ||
/// <summary> | ||
/// Shared access signature credential used to authenticate to an IoT hub. | ||
/// </summary> | ||
public abstract class IoTHubSasCredential : AzureSasCredential | ||
{ | ||
/// <summary> | ||
/// Creates and instance of <see cref="IoTHubSasCredential"/>. | ||
/// </summary> | ||
/// <param name="signature">Shared access signature to use to authenticate with the IoT hub.</param> | ||
/// <param name="expiresOnUtc">The shared access signature expiry in UTC.</param> | ||
public IoTHubSasCredential(string signature, DateTime expiresOnUtc) : base(signature) | ||
{ | ||
ExpiresOnUtc = expiresOnUtc; | ||
} | ||
|
||
/// <summary> | ||
/// Updates the shared access signature. This is intended to be used when you've | ||
/// regenerated your shared access signature and want to update clients. | ||
/// </summary> | ||
/// <param name="signature">Shared access signature to use to authenticate with the IoT hub.</param> | ||
/// <param name="expiresOnUtc">The shared access signature expiry in UTC.</param> | ||
public void Update(string signature, DateTime expiresOnUtc) | ||
{ | ||
Update(signature); | ||
ExpiresOnUtc = expiresOnUtc; | ||
} | ||
|
||
/// <summary> | ||
/// The shared access signature expiry in UTC. | ||
/// </summary> | ||
public DateTime ExpiresOnUtc { get; private set; } | ||
} | ||
} | ||
|
||
#endif |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// 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; | ||
|
||
#if !NET451 | ||
|
||
using Azure; | ||
|
||
#endif | ||
|
||
namespace Microsoft.Azure.Devices | ||
{ | ||
internal class IotHubSasCredentialProperties : IotHubConnectionProperties | ||
{ | ||
#if NET451 | ||
|
||
public IotHubSasCredentialProperties() | ||
{ | ||
throw new InvalidOperationException("nameof(AzureSasCredential) is not supported in NET451"); | ||
} | ||
#else | ||
private readonly IoTHubSasCredential _credential; | ||
|
||
public IotHubSasCredentialProperties(string hostName, IoTHubSasCredential credential) : base(hostName) | ||
{ | ||
_credential = credential; | ||
} | ||
|
||
#endif | ||
|
||
public override string GetAuthorizationHeader() | ||
{ | ||
#if NET451 | ||
throw new InvalidOperationException($"AzureSasCredential is not supported on NET451"); | ||
|
||
#else | ||
return _credential.Signature; | ||
#endif | ||
} | ||
|
||
public override Task<CbsToken> GetTokenAsync(Uri namespaceAddress, string appliesTo, string[] requiredClaims) | ||
{ | ||
#if NET451 | ||
throw new InvalidOperationException($"AzureSasCredential is not supported on NET451"); | ||
|
||
#else | ||
var token = new CbsToken( | ||
_credential.Signature, | ||
CbsConstants.IotHubSasTokenType, | ||
_credential.ExpiresOnUtc); | ||
return Task.FromResult(token); | ||
#endif | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// 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 FluentAssertions; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
|
||
namespace Microsoft.Azure.Devices.Tests | ||
{ | ||
[TestClass] | ||
[TestCategory("Unit")] | ||
public class IotHubConnectionPropertiesTests | ||
{ | ||
[TestMethod] | ||
[DataRow("acme.azure-devices.net", "acme")] | ||
[DataRow("Acme-1.azure-devices.net", "Acme-1")] | ||
[DataRow("acme2.azure-devices.net", "acme2")] | ||
[DataRow("3acme.azure-devices.net", "3acme")] | ||
[DataRow("4-acme.azure-devices.net", "4-acme")] | ||
public void IotHubConnectionPropertiesGetHubNameTest(string hostName, string expectedHubName) | ||
{ | ||
// act | ||
string hubName = IotHubConnectionProperties.GetIotHubName(hostName); | ||
|
||
// assert | ||
hubName.Should().Be(expectedHubName); | ||
} | ||
} | ||
} |