Skip to content

Commit

Permalink
Merge pull request AzureAD#120 from AzureAD/dev
Browse files Browse the repository at this point in the history
Merging dev branch to master branch
  • Loading branch information
afshins committed Sep 19, 2014
2 parents 0961571 + 4e22b63 commit 886043a
Show file tree
Hide file tree
Showing 13 changed files with 211 additions and 33 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
Active Directory Authentication Library (ADAL) provides easy to use authentication functionality for your .NET client and Windows Store apps by taking advantage of Windows Server Active Directory and Windows Azure Active Directory.
Here you can find the source code for the library. You can find the corresponding releases (both stable and prerelease) on the NuGet gallery at [http://www.nuget.org/packages/Microsoft.IdentityModel.Clients.ActiveDirectory/](http://www.nuget.org/packages/Microsoft.IdentityModel.Clients.ActiveDirectory/).

- The latest stable release is [2.10.10910.1511](https://www.nuget.org/packages/Microsoft.IdentityModel.Clients.ActiveDirectory/).
- The latest stable release is [2.11.10918.1222](https://www.nuget.org/packages/Microsoft.IdentityModel.Clients.ActiveDirectory/).
-
- There is no prerelease version available at this point.
-
Expand Down
31 changes: 19 additions & 12 deletions src/ADAL.Common/ClientMetrics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ internal class ClientMetrics
private const string ClientMetricsHeaderLastEndpoint = "x-client-last-endpoint";

private static ClientMetrics pendingClientMetrics;
private static readonly object PendingClientMetricsLock = new object();

private Stopwatch metricsTimer;
private string lastError;
Expand All @@ -65,9 +66,12 @@ public void EndClientMetricsRecord(string endpoint, CallState callState)
lastResponseTime = metricsTimer.ElapsedMilliseconds;
lastCorrelationId = callState.CorrelationId;
lastEndpoint = endpoint;
if (pendingClientMetrics == null)
lock (PendingClientMetricsLock)
{
pendingClientMetrics = this;
if (pendingClientMetrics == null)
{
pendingClientMetrics = this;
}
}
}
}
Expand All @@ -79,20 +83,23 @@ public void SetLastError(string[] errorCodes)

private static void AddClientMetricsHeadersToRequest(IHttpWebRequest request)
{
if (pendingClientMetrics != null && NetworkPlugin.RequestCreationHelper.RecordClientMetrics)
lock (PendingClientMetricsLock)
{
Dictionary<string, string> headers = new Dictionary<string, string>();
if (pendingClientMetrics.lastError != null)
if (pendingClientMetrics != null && NetworkPlugin.RequestCreationHelper.RecordClientMetrics)
{
headers[ClientMetricsHeaderLastError] = pendingClientMetrics.lastError;
}
Dictionary<string, string> headers = new Dictionary<string, string>();
if (pendingClientMetrics.lastError != null)
{
headers[ClientMetricsHeaderLastError] = pendingClientMetrics.lastError;
}

headers[ClientMetricsHeaderLastRequest] = pendingClientMetrics.lastCorrelationId.ToString();
headers[ClientMetricsHeaderLastResponseTime] = pendingClientMetrics.lastResponseTime.ToString();
headers[ClientMetricsHeaderLastEndpoint] = pendingClientMetrics.lastEndpoint;
headers[ClientMetricsHeaderLastRequest] = pendingClientMetrics.lastCorrelationId.ToString();
headers[ClientMetricsHeaderLastResponseTime] = pendingClientMetrics.lastResponseTime.ToString();
headers[ClientMetricsHeaderLastEndpoint] = pendingClientMetrics.lastEndpoint;

HttpHelper.AddHeadersToRequest(request, headers);
pendingClientMetrics = null;
HttpHelper.AddHeadersToRequest(request, headers);
pendingClientMetrics = null;
}
}
}
}
Expand Down
8 changes: 6 additions & 2 deletions src/ADAL.Common/CommonAssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
// limitations under the License.
//----------------------------------------------------------------------

using System;
using System.Reflection;

[assembly: AssemblyProduct("Active Directory Authentication Library")]
Expand All @@ -26,11 +27,14 @@
[assembly: AssemblyCopyright("Copyright (c) Microsoft Open Technologies. All rights reserved.")]
[assembly: AssemblyTrademark("")]

[assembly: AssemblyVersion("2.10.0.0")]
[assembly: AssemblyVersion("2.11.0.0")]

// Keep major and minor versions in AssemblyFileVersion in sync with AssemblyVersion.
// Build and revision numbers are replaced on build machine for official builds.
[assembly: AssemblyFileVersion("2.10.00000.0000")]
[assembly: AssemblyFileVersion("2.11.00000.0000")]
// On official build, attribute AssemblyInformationalVersionAttribute is added as well
// with its value equal to the hash of the last commit to the git branch.
// e.g.: [assembly: AssemblyInformationalVersionAttribute("4392c9835a38c27516fc0cd7bad7bccdcaeab161")]

// Assembly marked as compliant.
[assembly: CLSCompliant(true)]
45 changes: 44 additions & 1 deletion src/ADAL.Common/HttpWebRequestWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
// limitations under the License.
//----------------------------------------------------------------------

using System;
using System.IO;
using System.Net;
using System.Threading.Tasks;
Expand All @@ -26,6 +27,8 @@ internal class HttpWebRequestWrapper : IHttpWebRequest
{
private readonly HttpWebRequest request;

private int timeoutInMilliSeconds = 30000;

public HttpWebRequestWrapper(string uri)
{
this.request = (HttpWebRequest)WebRequest.Create(uri);
Expand Down Expand Up @@ -73,6 +76,14 @@ public WebHeaderCollection Headers
}
}

public int TimeoutInMilliSeconds
{
set
{
this.timeoutInMilliSeconds = value;
}
}

public async Task<IHttpWebResponse> GetResponseSyncOrAsync(CallState callState)
{
if (this.BodyParameters != null)
Expand All @@ -86,10 +97,42 @@ public async Task<IHttpWebResponse> GetResponseSyncOrAsync(CallState callState)
#if ADAL_NET
if (callState != null && callState.CallSync)
{
this.request.Timeout = this.timeoutInMilliSeconds;
return NetworkPlugin.HttpWebRequestFactory.CreateResponse(this.request.GetResponse());
}

Task<WebResponse> getResponseTask = this.request.GetResponseAsync();
System.Threading.ThreadPool.RegisterWaitForSingleObject(
((IAsyncResult)getResponseTask).AsyncWaitHandle,
delegate (object state, bool timedOut)
{
if (timedOut)
{
((HttpWebRequest)state).Abort();
}
},
this.request,
this.timeoutInMilliSeconds,
true);

return NetworkPlugin.HttpWebRequestFactory.CreateResponse(await getResponseTask);
#else
var timer = Windows.System.Threading.ThreadPoolTimer.CreateTimer(
delegate
{
this.request.Abort();
},
TimeSpan.FromMilliseconds(this.timeoutInMilliSeconds));

try
{
return NetworkPlugin.HttpWebRequestFactory.CreateResponse(await this.request.GetResponseAsync());
}
finally
{
timer.Cancel();
}
#endif
return NetworkPlugin.HttpWebRequestFactory.CreateResponse(await this.request.GetResponseAsync());
}

public async Task<Stream> GetRequestStreamSyncOrAsync(CallState callState)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public string Result
{
get
{
return this.result;
return this.authenticationResult;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public abstract class WindowsFormsWebAuthenticationDialogBase : Form

private Uri desiredCallbackUri;

protected string result;
protected string authenticationResult;

protected IWin32Window ownerWindow;

Expand Down Expand Up @@ -171,7 +171,7 @@ private bool CheckForClosingUrl(Uri url)
{
if (url.Authority.Equals(this.desiredCallbackUri.Authority, StringComparison.OrdinalIgnoreCase) && url.AbsolutePath.Equals(this.desiredCallbackUri.AbsolutePath))
{
this.result = url.AbsoluteUri;
this.authenticationResult = url.AbsoluteUri;
this.StopWebBrowser();

// in this handler object could be already disposed, so it should be the last method
Expand Down Expand Up @@ -202,7 +202,7 @@ private void StopWebBrowser()
public string AuthenticateAAD(Uri requestUri, Uri callbackUri)
{
this.desiredCallbackUri = callbackUri;
this.result = null;
this.authenticationResult = null;

// The WebBrowser event handlers must not throw exceptions.
// If they do then they may be swallowed by the native
Expand All @@ -214,7 +214,7 @@ public string AuthenticateAAD(Uri requestUri, Uri callbackUri)
this.webBrowser.Navigate(requestUri);
this.OnAuthenticate();

return this.result;
return this.authenticationResult;
}

protected virtual void OnAuthenticate()
Expand Down Expand Up @@ -303,7 +303,7 @@ protected AdalException CreateExceptionForAuthenticationUiFailed(int statusCode)

return new AdalServiceException(
AdalError.AuthenticationUiFailed,
string.Format("The browser based authentication dialog failed to complete for an unkown reason. StatusCode: {0}", statusCode)) { StatusCode = statusCode };
string.Format("The browser based authentication dialog failed to complete for an unknown reason. StatusCode: {0}", statusCode)) { StatusCode = statusCode };
}

protected static class DpiHelper
Expand Down
12 changes: 2 additions & 10 deletions tests/Test.ADAL.NET.Unit/NonInteractiveTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,16 +65,8 @@ public async Task UserRealmDiscoveryTest()
Verify.IsNotNull(ex.ErrorCode, AdalError.UnknownUser);
}

try
{
await UserRealmDiscoveryResponse.CreateByDiscoveryAsync(context.Authenticator.UserRealmUri, "ab@cd@ef", null);
Verify.Fail("Exception expected");
}
catch (AdalException ex)
{
Verify.IsNotNull(ex.ErrorCode, AdalError.UserRealmDiscoveryFailed);
Verify.IsNotNull(ex.InnerException);
}
userRealmResponse = await UserRealmDiscoveryResponse.CreateByDiscoveryAsync(context.Authenticator.UserRealmUri, "ab@cd@ef", null);
Verify.AreEqual("Unknown", userRealmResponse.AccountType);

try
{
Expand Down
26 changes: 26 additions & 0 deletions tests/Test.ADAL.NET.Unit/Test.ADAL.NET.Unit.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
<IsCodedUITest>False</IsCodedUITest>
<TestProjectType>UnitTest</TestProjectType>
<DefineConstants>TEST_ADAL_NET</DefineConstants>
<SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">..\..\</SolutionDir>
<RestorePackages>true</RestorePackages>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
Expand Down Expand Up @@ -45,7 +47,23 @@
<DelaySign>true</DelaySign>
</PropertyGroup>
<ItemGroup>
<Reference Include="Microsoft.Owin, Version=2.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\packages\Microsoft.Owin.2.1.0\lib\net45\Microsoft.Owin.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Owin.Host.HttpListener, Version=2.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\packages\Microsoft.Owin.Host.HttpListener.2.1.0\lib\net45\Microsoft.Owin.Host.HttpListener.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Owin.Hosting, Version=2.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\packages\Microsoft.Owin.Hosting.2.1.0\lib\net45\Microsoft.Owin.Hosting.dll</HintPath>
</Reference>
<Reference Include="Microsoft.VisualStudio.TestTools.UITesting, Version=11.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL" />
<Reference Include="Owin, Version=1.0.0.0, Culture=neutral, PublicKeyToken=f0ebd12fd5e55cc5, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\packages\Owin.1.0\lib\net40\Owin.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Data" />
<Reference Include="System.Runtime.Serialization" />
Expand Down Expand Up @@ -137,6 +155,7 @@
<Link>valid_cert2.pfx</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Include="packages.config" />
</ItemGroup>
<Choose>
<When Condition="'$(VisualStudioVersion)' == '10.0' And '$(IsCodedUITest)' == 'True'">
Expand All @@ -158,6 +177,13 @@
</Choose>
<Import Project="$(VSToolsPath)\TeamTest\Microsoft.TestTools.targets" Condition="Exists('$(VSToolsPath)\TeamTest\Microsoft.TestTools.targets')" />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Import Project="$(SolutionDir)\.nuget\NuGet.targets" Condition="Exists('$(SolutionDir)\.nuget\NuGet.targets')" />
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup>
<ErrorText>This project references NuGet package(s) that are missing on this computer. Enable NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
</PropertyGroup>
<Error Condition="!Exists('$(SolutionDir)\.nuget\NuGet.targets')" Text="$([System.String]::Format('$(ErrorText)', '$(SolutionDir)\.nuget\NuGet.targets'))" />
</Target>
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
Expand Down
Loading

0 comments on commit 886043a

Please sign in to comment.