| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | using System; |
| | 5 | | using System.Threading.Tasks; |
| | 6 | | using Azure.Core; |
| | 7 | | using Azure.Core.Pipeline; |
| | 8 | |
|
| | 9 | | namespace Azure.Iot.Hub.Service.Authentication |
| | 10 | | { |
| | 11 | | /// <summary> |
| | 12 | | /// The shared access signature based HTTP pipeline policy. |
| | 13 | | /// This authentication policy injects the sas token into the HTTP authentication header for each HTTP request made. |
| | 14 | | /// </summary> |
| | 15 | | internal class SasTokenAuthenticationPolicy : HttpPipelinePolicy |
| | 16 | | { |
| | 17 | | private readonly ISasTokenProvider _sasTokenProvider; |
| | 18 | |
|
| 76 | 19 | | internal SasTokenAuthenticationPolicy(ISasTokenProvider sasTokenProvider) |
| | 20 | | { |
| 76 | 21 | | _sasTokenProvider = sasTokenProvider; |
| 76 | 22 | | } |
| | 23 | |
|
| | 24 | | public override void Process(HttpMessage message, ReadOnlyMemory<HttpPipelinePolicy> pipeline) |
| | 25 | | { |
| 196 | 26 | | AddHeaders(message); |
| 196 | 27 | | ProcessNext(message, pipeline); |
| 196 | 28 | | } |
| | 29 | |
|
| | 30 | | public override async ValueTask ProcessAsync(HttpMessage message, ReadOnlyMemory<HttpPipelinePolicy> pipeline) |
| | 31 | | { |
| 194 | 32 | | AddHeaders(message); |
| 194 | 33 | | await ProcessNextAsync(message, pipeline).ConfigureAwait(false); |
| 194 | 34 | | } |
| | 35 | |
|
| | 36 | | private void AddHeaders(HttpMessage message) |
| | 37 | | { |
| 390 | 38 | | message.Request.Headers.Add(HttpHeader.Names.Authorization, _sasTokenProvider.GetSasToken()); |
| 390 | 39 | | } |
| | 40 | | } |
| | 41 | | } |