Skip to content
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
daaa43d
Add new api to gather all wwwAuthenticateParameters
Jun 30, 2022
7108c00
refactor
Jun 30, 2022
4ef405b
Request uri fix
Jul 15, 2022
51f71f1
Add additional tests.
Jul 20, 2022
ff29f70
Rename scheme to authScheme
Jul 20, 2022
e790052
Refactoring api
Aug 2, 2022
bd59b3d
Refactoring.
Aug 3, 2022
cab97ae
Refactoring to use Dictionary return type
Aug 5, 2022
6677c1f
Merge remote-tracking branch 'origin/main' into trwalke/wwwAuthentica…
Sep 26, 2022
37d9c43
Adding Authentication header parser and support for authentication info
Sep 27, 2022
bae9643
Refactoring
Sep 27, 2022
af2f19e
Merge remote-tracking branch 'origin/main' into trwalke/wwwAuthentica…
Sep 30, 2022
eb8032f
Apply suggestions from code review
trwalke Oct 13, 2022
8949f55
Addressing PR Feedback.
Oct 18, 2022
8c46a01
Refactoring, more tests
Oct 18, 2022
e87abff
Build fix
Oct 18, 2022
4f4cc68
Adding additional tests
Oct 19, 2022
264921f
Adding NTLM support
Oct 20, 2022
8fbd0e4
Apply suggestions from code review
trwalke Nov 1, 2022
6a58dac
Update src/client/Microsoft.Identity.Client/WwwAuthenticateParameters.cs
trwalke Nov 1, 2022
9b31775
Update src/client/Microsoft.Identity.Client/WwwAuthenticateParameters.cs
trwalke Nov 1, 2022
b068acc
Recfactoring
Nov 2, 2022
384cd11
Merge remote-tracking branch 'origin/main' into trwalke/wwwAuthentica…
Nov 2, 2022
4b56fb2
resolving build errors
Nov 2, 2022
ddbadf8
resolving tests
Nov 2, 2022
0867daa
Updating parameter parsing
Nov 16, 2022
b55f3c2
Updating parsing logic
Nov 30, 2022
1a08dda
Clean up
Nov 30, 2022
fd6733a
Pr Feedback
Dec 1, 2022
9626ec3
test update
Dec 1, 2022
a1cdedf
Update src/client/Microsoft.Identity.Client/WwwAuthenticateParameters.cs
trwalke Dec 1, 2022
37b1ab7
Adding additional tests
Dec 7, 2022
d6a1e1b
Refactoring tests.
Dec 13, 2022
1e9ce55
Merge remote-tracking branch 'origin/pmaytak/net6-only' into trwalke/…
Dec 13, 2022
c3addbe
Refactoring
Dec 13, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Identity.Client.PlatformsCommon.Factories;
using Microsoft.Identity.Client.Utils;

namespace Microsoft.Identity.Client
{
/// <summary>
/// Parsed authentication headers to retreve header values from HttpResponseHeaders.
/// </summary>
public class AuthenticationHeaderParser
Comment thread
trwalke marked this conversation as resolved.
{
/// <summary>
/// Parameters returned by the WWW-Authenticate header. This allows for dynamic
/// scenarios such as claim challenge, CAE, CA auth context.
/// See https://aka.ms/msal-net/wwwAuthenticate.
/// </summary>
public IReadOnlyList<WwwAuthenticateParameters> ParsedWwwAuthenticateParameters { get; private set; }
Comment thread
trwalke marked this conversation as resolved.
Outdated

/// <summary>
/// Parameters returned by the Authenticatin-Info header.
Comment thread
trwalke marked this conversation as resolved.
Outdated
/// This allows for authentication scenarios such as Proof-Of-Posession.
/// </summary>
public AuthenticationInfoParameters ParsedAuthenticationInfoParameters { get; private set; }

/// <summary>
/// Nonce parsed from HttpResponseHeaders
/// </summary>
public string Nonce { get; private set; }

/// <summary>
/// Creates the authenticate parameters by attempting to call the resource unauthenticated, and analyzing the response.
/// </summary>
/// <param name="resourceUri">URI of the resource.</param>
/// <returns></returns>
public static async Task<AuthenticationHeaderParser> ParseAuthenticationHeadersAsync(string resourceUri)
{
return await ParseAuthenticationHeadersAsync(resourceUri, new CancellationToken()).ConfigureAwait(false);
Comment thread
trwalke marked this conversation as resolved.
Outdated
}

/// <summary>
/// Creates the authenticate parameters by attempting to call the resource unauthenticated, and analyzing the response.
/// </summary>
/// <param name="resourceUri">URI of the resource.</param>
/// <param name="cancellationToken">The cancellation token to cancel operation.</param>
/// <returns></returns>
public static async Task<AuthenticationHeaderParser> ParseAuthenticationHeadersAsync(string resourceUri, CancellationToken cancellationToken)
{
return await ParseAuthenticationHeadersAsync(resourceUri, cancellationToken, GetHttpClient()).ConfigureAwait(false);
}

/// <summary>
/// Creates the authenticate parameters by attempting to call the resource unauthenticated, and analyzing the response.
/// </summary>
/// <param name="resourceUri">URI of the resource.</param>
/// <param name="cancellationToken">The cancellation token to cancel operation.</param>
/// <param name="httpClient">Instance of <see cref="HttpClient"/> to make the request with.</param>
/// <returns></returns>
/// <exception cref="ArgumentNullException"></exception>
public static async Task<AuthenticationHeaderParser> ParseAuthenticationHeadersAsync(string resourceUri, CancellationToken cancellationToken, HttpClient httpClient)
Comment thread
trwalke marked this conversation as resolved.
Outdated
{
if (httpClient is null)
{
throw new ArgumentNullException(nameof(httpClient));
}
if (string.IsNullOrWhiteSpace(resourceUri))
{
throw new ArgumentNullException(nameof(resourceUri));
}

// call this endpoint and see what the header says and return that
HttpResponseMessage httpResponseMessage = await httpClient.GetAsync(resourceUri, cancellationToken).ConfigureAwait(false);

return ParseAuthenticationHeaders(httpResponseMessage.Headers);
}

/// <summary>
/// Creates a parsed set of parameters from the provided HttpResponseHeaders.
/// </summary>
/// <param name="httpResponseHeaders"></param>
/// <remarks>For known values, such as the nonce used for Proof-of-Possession, the parser will first chack for in the WWW-Authenticate headers
Comment thread
trwalke marked this conversation as resolved.
Outdated
/// If it cannot find it, it will then check the Authentication-Info parameters for the value.</remarks>
/// <returns></returns>
public static AuthenticationHeaderParser ParseAuthenticationHeaders(HttpResponseHeaders httpResponseHeaders)
{
AuthenticationHeaderParser authenticationHeaderParser = new AuthenticationHeaderParser();
string serverNonce = null;

//Check for WWW-AuthenticateHeaders
if (httpResponseHeaders.WwwAuthenticate.Count != 0)
{
var WwwParameters = WwwAuthenticateParameters.CreateFromAuthenticateHeaders(httpResponseHeaders);
Comment thread
trwalke marked this conversation as resolved.
Outdated

if (WwwParameters.Any(parameter => parameter.AuthScheme == "PoP"))
{
serverNonce = WwwParameters.Where(parameter => parameter.AuthScheme == "PoP").Single().ServerNonce;
Comment thread
trwalke marked this conversation as resolved.
Outdated
}

authenticationHeaderParser.ParsedWwwAuthenticateParameters = WwwParameters;
}
else
{
authenticationHeaderParser.ParsedWwwAuthenticateParameters = new List<WwwAuthenticateParameters>();
}

//If no WWW-AuthenticateHeaders exist, attempt to parse AuthenticationInfo headers instead
AuthenticationInfoParameters authenticationInfoParameters = AuthenticationInfoParameters.CreateFromHeaders(httpResponseHeaders);
Comment thread
trwalke marked this conversation as resolved.
Outdated
authenticationHeaderParser.ParsedAuthenticationInfoParameters = authenticationInfoParameters;

//If server nonce is not acquired from WWW-Authenticate headers, use next nonce from Authentication-Info parameters.
authenticationHeaderParser.Nonce = serverNonce?? authenticationInfoParameters.NextNonce;

return authenticationHeaderParser;
}

/// <summary>
/// Created an HttpClient
/// </summary>
internal static HttpClient GetHttpClient()
{
var httpClientFactory = PlatformProxyFactory.CreatePlatformProxy(null).CreateDefaultHttpClientFactory();
return httpClientFactory.GetHttpClient();
}

/// <summary>
/// Extracts a key value pair from an expression of the form a=b
/// </summary>
/// <param name="assignment">assignment</param>
/// <returns>Key Value pair</returns>
internal static KeyValuePair<string, string> ExtractKeyValuePair(string assignment)
{
string[] segments = CoreHelpers.SplitWithQuotes(assignment, '=')
Comment thread
trwalke marked this conversation as resolved.
Outdated
.Select(s => s.Trim().Trim('"'))
.ToArray();

if (segments.Length != 2)
{
throw new ArgumentException(nameof(assignment), $"{assignment} isn't of the form a=b");
}

return new KeyValuePair<string, string>(segments[0], segments[1]);
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Identity.Client.Utils;
using Microsoft.Identity.Json.Linq;

namespace Microsoft.Identity.Client
{
/// <summary>
Comment thread
pmaytak marked this conversation as resolved.
Outdated
///
/// </summary>
public class AuthenticationInfoParameters
{
private const string _authenticationInfoKey = "Authentication-Info";
Comment thread
trwalke marked this conversation as resolved.
Outdated
/// <summary>
///
/// </summary>
public string NextNonce { get; private set; }
Comment thread
trwalke marked this conversation as resolved.
Outdated

/// <summary>
///
/// </summary>
/// <param name="respnseHeaders"></param>
/// <returns></returns>
public static AuthenticationInfoParameters CreateFromHeaders(HttpResponseHeaders respnseHeaders)
Comment thread
trwalke marked this conversation as resolved.
Outdated
{
AuthenticationInfoParameters parameters = new AuthenticationInfoParameters();

if (respnseHeaders.Any(header => header.Key == _authenticationInfoKey))
Comment thread
trwalke marked this conversation as resolved.
Outdated
{
var authInfoValue = respnseHeaders.Where(header => header.Key == _authenticationInfoKey).Single().Value.FirstOrDefault();
Comment thread
trwalke marked this conversation as resolved.
Outdated
Comment thread
trwalke marked this conversation as resolved.
Outdated

var AuthValuesSplit = authInfoValue.Split(new char[] { ' ' }, 2);
Comment thread
trwalke marked this conversation as resolved.
Outdated
Comment thread
trwalke marked this conversation as resolved.
Outdated

var paramValues = CoreHelpers.SplitWithQuotes(AuthValuesSplit[1], ',')
.Select(v => AuthenticationHeaderParser.ExtractKeyValuePair(v.Trim()))
.ToDictionary(pair => pair.Key, pair => pair.Value, StringComparer.OrdinalIgnoreCase);

string value;

if (paramValues.TryGetValue("nextnonce", out value))
Comment thread
trwalke marked this conversation as resolved.
Outdated
Comment thread
trwalke marked this conversation as resolved.
Outdated
Comment thread
trwalke marked this conversation as resolved.
Outdated
{
parameters.NextNonce = value;
}

}

return parameters;
}
}
}
Loading