Skip to content

Commit 7278c05

Browse files
authored
Handling additional auth scenarios for Zip Deploy (#611)
1 parent bbd305d commit 7278c05

File tree

3 files changed

+22
-11
lines changed

3 files changed

+22
-11
lines changed

src/Microsoft.NET.Sdk.Functions.MSBuild/Tasks/Http/HttpClientHelpers.cs

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,13 @@ namespace Microsoft.NET.Sdk.Functions.Http
1111
{
1212
internal static class HttpClientHelpers
1313
{
14-
public static async Task<IHttpResponse> PostWithBasicAuthAsync(this IHttpClient client, Uri uri, string username, string password, string contentType, string userAgent, Encoding encoding, Stream messageBody)
14+
internal static readonly string AzureADUserName = Guid.Empty.ToString();
15+
internal static readonly string BearerAuthenticationScheme = "Bearer";
16+
internal static readonly string BasicAuthenticationScheme = "Basic";
17+
18+
public static async Task<IHttpResponse> PostRequestAsync(this IHttpClient client, Uri uri, string username, string password, string contentType, string userAgent, Encoding encoding, Stream messageBody)
1519
{
16-
AddBasicAuthToClient(username, password, client);
20+
AddAuthenticationHeader(username, password, client);
1721
client.DefaultRequestHeaders.Add("User-Agent", userAgent);
1822

1923
StreamContent content = new StreamContent(messageBody ?? new MemoryStream())
@@ -42,9 +46,9 @@ public static async Task<IHttpResponse> PostWithBasicAuthAsync(this IHttpClient
4246
}
4347
}
4448

45-
public static async Task<IHttpResponse> GetWithBasicAuthAsync(this IHttpClient client, Uri uri, string username, string password, string userAgent, CancellationToken cancellationToken)
49+
public static async Task<IHttpResponse> GetRequestAsync(this IHttpClient client, Uri uri, string username, string password, string userAgent, CancellationToken cancellationToken)
4650
{
47-
AddBasicAuthToClient(username, password, client);
51+
AddAuthenticationHeader(username, password, client);
4852
client.DefaultRequestHeaders.Add("User-Agent", userAgent);
4953

5054
try
@@ -58,14 +62,21 @@ public static async Task<IHttpResponse> GetWithBasicAuthAsync(this IHttpClient c
5862
}
5963
}
6064

61-
private static void AddBasicAuthToClient(string username, string password, IHttpClient client)
65+
private static void AddAuthenticationHeader(string username, string password, IHttpClient client)
6266
{
6367
client.DefaultRequestHeaders.Remove("Connection");
6468

65-
string plainAuth = string.Format("{0}:{1}", username, password);
66-
byte[] plainAuthBytes = Encoding.ASCII.GetBytes(plainAuth);
67-
string base64 = Convert.ToBase64String(plainAuthBytes);
68-
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", base64);
69+
if (!string.Equals(username, AzureADUserName, StringComparison.Ordinal))
70+
{
71+
string plainAuth = string.Format("{0}:{1}", username, password);
72+
byte[] plainAuthBytes = Encoding.ASCII.GetBytes(plainAuth);
73+
string base64 = Convert.ToBase64String(plainAuthBytes);
74+
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(BasicAuthenticationScheme, base64);
75+
}
76+
else
77+
{
78+
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(BearerAuthenticationScheme, password);
79+
}
6980
}
7081
}
7182
}

src/Microsoft.NET.Sdk.Functions.MSBuild/Tasks/ZipDeployTask.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ internal async System.Threading.Tasks.Task<bool> ZipDeployAsync(string zipToPubl
8787
Uri uri = new Uri($"{zipDeployPublishUrl}?isAsync=true", UriKind.Absolute);
8888
string userAgent = $"{UserAgentName}/{userAgentVersion}";
8989
FileStream stream = File.OpenRead(zipToPublishPath);
90-
IHttpResponse response = await client.PostWithBasicAuthAsync(uri, userName, password, "application/zip", userAgent, Encoding.UTF8, stream);
90+
IHttpResponse response = await client.PostRequestAsync(uri, userName, password, "application/zip", userAgent, Encoding.UTF8, stream);
9191
if (response.StatusCode != HttpStatusCode.OK && response.StatusCode != HttpStatusCode.Accepted)
9292
{
9393
if (logMessages)

src/Microsoft.NET.Sdk.Functions.MSBuild/Tasks/ZipDeploymentStatus.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ private async Task<T> InvokeGetRequestWithRetryAsync<T>(string url, string userN
8282
IHttpResponse response = null;
8383
await RetryAsync(async () =>
8484
{
85-
response = await _client.GetWithBasicAuthAsync(new Uri(url, UriKind.RelativeOrAbsolute), userName, password, _userAgent, cts.Token);
85+
response = await _client.GetRequestAsync(new Uri(url, UriKind.RelativeOrAbsolute), userName, password, _userAgent, cts.Token);
8686
}, retryCount, retryDelay);
8787

8888
if (response.StatusCode != HttpStatusCode.OK && response.StatusCode != HttpStatusCode.Accepted)

0 commit comments

Comments
 (0)