-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
75464e1
commit f8e1b36
Showing
4 changed files
with
60 additions
and
11 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
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 |
---|---|---|
@@ -1,30 +1,44 @@ | ||
using System.Collections.Specialized; | ||
using System; | ||
using System.Net.Http; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using System.Web; | ||
|
||
namespace Stowage.Impl.Microsoft { | ||
class SasAuthHandler : DelegatingHandler { | ||
private readonly string _signedStart; | ||
private readonly string _signedExpiry; | ||
private readonly string _signedResource; | ||
private readonly string _signedPermissions; | ||
|
||
private readonly string _sasToken; | ||
|
||
/// <summary> | ||
/// Examples: https://docs.microsoft.com/en-us/rest/api/storageservices/service-sas-examples#blob-examples | ||
/// </summary> | ||
/// <param name="sasToken">See https://docs.microsoft.com/en-us/azure/storage/common/storage-sas-overview#sas-token</param> | ||
public SasAuthHandler(string sasToken) { | ||
// parse SAS token into parts to sign later | ||
NameValueCollection qs = HttpUtility.ParseQueryString(sasToken); | ||
public SasAuthHandler(string sasToken) : base(new HttpClientHandler()) { | ||
// make sure they didn't include the leading ?, we should strip it | ||
if(sasToken.StartsWith('?')) { | ||
sasToken = sasToken.Substring(1); | ||
} | ||
|
||
_sasToken = sasToken; | ||
} | ||
|
||
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) { | ||
//?sv=2019-12-12&ss=b&srt=sco&sp=r&se=2021-01-09T19:55:19Z&st=2020-12-07T11:55:19Z&spr=https&sig=ubKzO2g6eWX4pELoKk3XzmptRBi5P34AJj6i42%2Bm5%2FA%3D | ||
|
||
Authenticate(request); | ||
|
||
return base.SendAsync(request, cancellationToken); | ||
} | ||
|
||
#if NET5_0_OR_GREATER | ||
protected override HttpResponseMessage Send(HttpRequestMessage request, CancellationToken cancellationToken) { | ||
Authenticate(request); | ||
|
||
return base.Send(request, cancellationToken); | ||
} | ||
#endif | ||
|
||
private void Authenticate(HttpRequestMessage request) { | ||
// azure just requires us to append the sasToken to the url, we just need to check if we already have query arguments or not | ||
request.RequestUri = new Uri(request.RequestUri + (request.RequestUri.Query.Length > 0 ? "&" : "?") + _sasToken); | ||
} | ||
} | ||
} |