|
| 1 | +// Copyright (c) Microsoft. All rights reserved. |
| 2 | +// Licensed under the MIT license. See LICENSE file in the project root for full license information. |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.Threading.Tasks; |
| 6 | + |
| 7 | +namespace Microsoft.Azure.Devices.Client.Authentication |
| 8 | +{ |
| 9 | + /// <summary> |
| 10 | + /// Authentication method that generates shared access signature (SAS) token with refresh, based on a provided shared access key (SAK). |
| 11 | + /// Build for using $edgeHub in IoT Edge to authenticate on behalf of leaf devices or modules only. |
| 12 | + /// </summary> |
| 13 | + public class ClientAuthenticationForEdgeHubOnBehalfOf : ClientAuthenticationWithSharedAccessKeyRefresh |
| 14 | + { |
| 15 | + /// <summary> |
| 16 | + /// Creates an instance of this class. |
| 17 | + /// </summary> |
| 18 | + /// <param name="sharedAccessKey">Shared access key value for the $edgehub module.</param> |
| 19 | + /// <param name="parentDeviceId">Identifier of the higher-layer parent device that connects directly to IoT Hub.</param> |
| 20 | + /// <param name="deviceId">Device identifier of the lower-layer device that authenticates through the parent IoT device.</param> |
| 21 | + /// <param name="moduleId">Module identifier.</param> |
| 22 | + /// <param name="sasTokenTimeToLive"> |
| 23 | + /// The suggested time to live value for the generated SAS tokens. |
| 24 | + /// The default value is 1 hour. |
| 25 | + /// </param> |
| 26 | + /// <param name="sasTokenRenewalBuffer"> |
| 27 | + /// The time buffer before expiry when the token should be renewed, expressed as a percentage of the time to live. |
| 28 | + /// The default behavior is that the token will be renewed when it has 15% or less of its lifespan left. |
| 29 | + /// </param> |
| 30 | + public ClientAuthenticationForEdgeHubOnBehalfOf( |
| 31 | + string sharedAccessKey, |
| 32 | + string parentDeviceId, |
| 33 | + string deviceId, |
| 34 | + string moduleId = null, |
| 35 | + TimeSpan sasTokenTimeToLive = default, |
| 36 | + int sasTokenRenewalBuffer = default) |
| 37 | + : base( |
| 38 | + sharedAccessKey, |
| 39 | + deviceId, |
| 40 | + moduleId, |
| 41 | + sasTokenTimeToLive, |
| 42 | + sasTokenRenewalBuffer) |
| 43 | + { |
| 44 | + ParentDeviceId = parentDeviceId; |
| 45 | + } |
| 46 | + |
| 47 | + /// <summary> |
| 48 | + /// Gets the shared access key name. |
| 49 | + /// </summary> |
| 50 | + public string ParentDeviceId { get; private set; } |
| 51 | + |
| 52 | + ///<inheritdoc/> |
| 53 | + protected override Task<string> SafeCreateNewTokenAsync(string iotHub, TimeSpan suggestedTimeToLive) |
| 54 | + { |
| 55 | + string audience = SharedAccessSignatureBuilder.BuildAudience(iotHub, ParentDeviceId, "$edgeHub"); |
| 56 | + string sasToken = SharedAccessSignatureBuilder.BuildSignature(null, SharedAccessKey, null, TimeSpan.FromMinutes(60), audience, null, null); |
| 57 | + return Task.FromResult(sasToken); |
| 58 | + } |
| 59 | + } |
| 60 | +} |
0 commit comments