Skip to content

Commit

Permalink
Add more AdalAuthenticationProvider tests for WinRT, create WindowsFo…
Browse files Browse the repository at this point in the history
…rms test project
  • Loading branch information
ginach committed Nov 3, 2015
1 parent 74e1375 commit d70677e
Show file tree
Hide file tree
Showing 40 changed files with 1,264 additions and 112 deletions.
3 changes: 2 additions & 1 deletion Common/Microsoft.OneDriveSDK.nuspec
Expand Up @@ -2,7 +2,7 @@
<package xmlns="http://schemas.microsoft.com/packaging/2011/10/nuspec.xsd">
<metadata>
<id>Microsoft.OneDriveSDK</id>
<version>1.0.0</version>
<version>1.0.2</version>
<title>OneDrive C# SDK</title>
<authors>Microsoft</authors>
<owners>Microsoft</owners>
Expand Down Expand Up @@ -32,6 +32,7 @@

<!-- Windows 8.1 -->
<file src="..\src\OneDriveSdk\bin\Release\OneDriveSdk.dll" target="lib\netcore451\OneDriveSdk.dll" />
<file src="..\src\OneDriveSdk.WinStore\bin\Release\OneDriveSdk.WinRT.dll" target="lib\netcore451\OneDriveSdk.WinRT.dll" />
<file src="..\src\OneDriveSdk.WinStore\bin\Release\OneDriveSdk.WinStore.dll" target="lib\netcore451\OneDriveSdk.WinStore.dll" />
</files>
</package>
19 changes: 19 additions & 0 deletions OneDriveSdk.sln
Expand Up @@ -28,6 +28,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OneDriveSdk.WinRT", "src\On
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Test.OneDriveSdk.WinRT", "tests\Test.OneDriveSdk.WinRT\Test.OneDriveSdk.WinRT.csproj", "{61BCEAD9-36D4-49B6-9B0F-1999CDCD149F}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Test.OneDriveSdk.WindowsForms", "Test.OneDriveSdk.WindowsForms\Test.OneDriveSdk.WindowsForms.csproj", "{6C0D57DC-89C6-4313-BB2C-2CCB662AFDFB}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -181,6 +183,22 @@ Global
{61BCEAD9-36D4-49B6-9B0F-1999CDCD149F}.Release|x86.ActiveCfg = Release|x86
{61BCEAD9-36D4-49B6-9B0F-1999CDCD149F}.Release|x86.Build.0 = Release|x86
{61BCEAD9-36D4-49B6-9B0F-1999CDCD149F}.Release|x86.Deploy.0 = Release|x86
{6C0D57DC-89C6-4313-BB2C-2CCB662AFDFB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{6C0D57DC-89C6-4313-BB2C-2CCB662AFDFB}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6C0D57DC-89C6-4313-BB2C-2CCB662AFDFB}.Debug|ARM.ActiveCfg = Debug|Any CPU
{6C0D57DC-89C6-4313-BB2C-2CCB662AFDFB}.Debug|ARM.Build.0 = Debug|Any CPU
{6C0D57DC-89C6-4313-BB2C-2CCB662AFDFB}.Debug|x64.ActiveCfg = Debug|Any CPU
{6C0D57DC-89C6-4313-BB2C-2CCB662AFDFB}.Debug|x64.Build.0 = Debug|Any CPU
{6C0D57DC-89C6-4313-BB2C-2CCB662AFDFB}.Debug|x86.ActiveCfg = Debug|Any CPU
{6C0D57DC-89C6-4313-BB2C-2CCB662AFDFB}.Debug|x86.Build.0 = Debug|Any CPU
{6C0D57DC-89C6-4313-BB2C-2CCB662AFDFB}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6C0D57DC-89C6-4313-BB2C-2CCB662AFDFB}.Release|Any CPU.Build.0 = Release|Any CPU
{6C0D57DC-89C6-4313-BB2C-2CCB662AFDFB}.Release|ARM.ActiveCfg = Release|Any CPU
{6C0D57DC-89C6-4313-BB2C-2CCB662AFDFB}.Release|ARM.Build.0 = Release|Any CPU
{6C0D57DC-89C6-4313-BB2C-2CCB662AFDFB}.Release|x64.ActiveCfg = Release|Any CPU
{6C0D57DC-89C6-4313-BB2C-2CCB662AFDFB}.Release|x64.Build.0 = Release|Any CPU
{6C0D57DC-89C6-4313-BB2C-2CCB662AFDFB}.Release|x86.ActiveCfg = Release|Any CPU
{6C0D57DC-89C6-4313-BB2C-2CCB662AFDFB}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -194,5 +212,6 @@ Global
{E3A990AA-5628-42AA-A8D4-1C599602DD96} = {369050EF-15AF-4B0E-98DF-085B91E1B68D}
{9F6BB55B-3985-42E8-88DE-CE10A39C6389} = {2EFD6E75-C1E0-4600-88D6-BFEDAD24A50A}
{61BCEAD9-36D4-49B6-9B0F-1999CDCD149F} = {369050EF-15AF-4B0E-98DF-085B91E1B68D}
{6C0D57DC-89C6-4313-BB2C-2CCB662AFDFB} = {369050EF-15AF-4B0E-98DF-085B91E1B68D}
EndGlobalSection
EndGlobal
@@ -0,0 +1,31 @@
// ------------------------------------------------------------------------------
// Copyright (c) 2015 Microsoft Corporation
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
// ------------------------------------------------------------------------------

namespace Test.OneDriveSdk.WindowsForms.Authentication
{
using Microsoft.VisualStudio.TestTools.UnitTesting;

[TestClass]
public class AdalAuthenticationProviderTests
{
}
}
36 changes: 36 additions & 0 deletions Test.OneDriveSdk.WindowsForms/Properties/AssemblyInfo.cs
@@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("Test.OneDriveSdk.WindowsForms")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("Test.OneDriveSdk.WindowsForms")]
[assembly: AssemblyCopyright("Copyright © 2015")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("6c0d57dc-89c6-4313-bb2c-2ccb662afdfb")]

// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
100 changes: 100 additions & 0 deletions Test.OneDriveSdk.WindowsForms/Test.OneDriveSdk.WindowsForms.csproj
@@ -0,0 +1,100 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{6C0D57DC-89C6-4313-BB2C-2CCB662AFDFB}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Test.OneDriveSdk.WindowsForms</RootNamespace>
<AssemblyName>Test.OneDriveSdk.WindowsForms</AssemblyName>
<TargetFrameworkVersion>v4.5.1</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<ProjectTypeGuids>{3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">10.0</VisualStudioVersion>
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
<ReferencePath>$(ProgramFiles)\Common Files\microsoft shared\VSTT\$(VisualStudioVersion)\UITestExtensionPackages</ReferencePath>
<IsCodedUITest>False</IsCodedUITest>
<TestProjectType>UnitTest</TestProjectType>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="Microsoft.IdentityModel.Clients.ActiveDirectory, Version=2.19.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.IdentityModel.Clients.ActiveDirectory.2.19.208020213\lib\net45\Microsoft.IdentityModel.Clients.ActiveDirectory.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.IdentityModel.Clients.ActiveDirectory.WindowsForms, Version=2.19.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.IdentityModel.Clients.ActiveDirectory.2.19.208020213\lib\net45\Microsoft.IdentityModel.Clients.ActiveDirectory.WindowsForms.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System" />
</ItemGroup>
<Choose>
<When Condition="('$(VisualStudioVersion)' == '10.0' or '$(VisualStudioVersion)' == '') and '$(TargetFrameworkVersion)' == 'v3.5'">
<ItemGroup>
<Reference Include="Microsoft.VisualStudio.QualityTools.UnitTestFramework, Version=10.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL" />
</ItemGroup>
</When>
<Otherwise>
<ItemGroup>
<Reference Include="Microsoft.VisualStudio.QualityTools.UnitTestFramework" />
</ItemGroup>
</Otherwise>
</Choose>
<ItemGroup>
<Compile Include="Authentication\AdalAuthenticationProviderTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\tests\Test.OneDriveSdk\Test.OneDriveSdk.csproj">
<Project>{e3a990aa-5628-42aa-a8d4-1c599602dd96}</Project>
<Name>Test.OneDriveSdk</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<Choose>
<When Condition="'$(VisualStudioVersion)' == '10.0' And '$(IsCodedUITest)' == 'True'">
<ItemGroup>
<Reference Include="Microsoft.VisualStudio.QualityTools.CodedUITestFramework, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<Private>False</Private>
</Reference>
<Reference Include="Microsoft.VisualStudio.TestTools.UITest.Common, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<Private>False</Private>
</Reference>
<Reference Include="Microsoft.VisualStudio.TestTools.UITest.Extension, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<Private>False</Private>
</Reference>
<Reference Include="Microsoft.VisualStudio.TestTools.UITesting, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<Private>False</Private>
</Reference>
</ItemGroup>
</When>
</Choose>
<Import Project="$(VSToolsPath)\TeamTest\Microsoft.TestTools.targets" Condition="Exists('$(VSToolsPath)\TeamTest\Microsoft.TestTools.targets')" />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- 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">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>
4 changes: 4 additions & 0 deletions Test.OneDriveSdk.WindowsForms/packages.config
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Microsoft.IdentityModel.Clients.ActiveDirectory" version="2.19.208020213" targetFramework="net451" />
</packages>
2 changes: 1 addition & 1 deletion samples/OneDriveApiBrowser/FormBrowser.cs
Expand Up @@ -294,7 +294,7 @@ private async Task SignIn(ClientType clientType)
"https://login.live.com/oauth20_desktop.srf",
FormBrowser.Scopes,
webAuthenticationUi: new FormsWebAuthenticationUi())
: BusinessClientExtensions.GetActiveDirectoryClient("67b8454b-58df-4e6d-a688-c769bd327052", "https://localhost:777", "https://graph.microsoft.com/", "https://graph.microsoft.com/beta");
: BusinessClientExtensions.GetActiveDirectoryClient("67b8454b-58df-4e6d-a688-c769bd327052", "https://localhost:777");
}

try
Expand Down
Expand Up @@ -53,6 +53,7 @@ internal ServiceInfo ServiceInfo
{
return this.serviceInfo;
}

set
{
this.serviceInfo = value;
Expand All @@ -69,6 +70,16 @@ internal ServiceInfo ServiceInfo

var adalCredentialCache = this.serviceInfo.CredentialCache as AdalCredentialCache;

if (adalCredentialCache == null && this.serviceInfo.CredentialCache != null)
{
throw new OneDriveException(
new Error
{
Code = OneDriveErrorCode.AuthenticationFailure.ToString(),
Message = "Invalid credential cache type for authentication using ADAL.",
});
}

this.authenticationContextWrapper = adalCredentialCache == null
? new AuthenticationContextWrapper(serviceInfo.AuthenticationServiceUrl)
: new AuthenticationContextWrapper(serviceInfo.AuthenticationServiceUrl, false, adalCredentialCache.TokenCache);
Expand All @@ -77,7 +88,7 @@ internal ServiceInfo ServiceInfo

public AccountSession CurrentAccountSession { get; private set; }

protected abstract Task<AuthenticationResult> AuthenticateResourceAsync(string resource);
protected abstract Task<IAuthenticationResult> AuthenticateResourceAsync(string resource);

public async Task AppendAuthHeaderAsync(HttpRequestMessage request)
{
Expand Down Expand Up @@ -107,6 +118,12 @@ public async Task<AccountSession> AuthenticateAsync()

var authenticationResult = await this.AuthenticateResourceAsync(this.ServiceInfo.ServiceResource);

if (authenticationResult == null)
{
this.CurrentAccountSession = null;
return this.CurrentAccountSession;
}

this.CurrentAccountSession = new AdalAccountSession
{
AccessToken = authenticationResult.AccessToken,
Expand All @@ -115,7 +132,7 @@ public async Task<AccountSession> AuthenticateAsync()
CanSignOut = true,
ClientId = this.ServiceInfo.AppId,
ExpiresOnUtc = authenticationResult.ExpiresOn,
UserId = authenticationResult.UserInfo.UniqueId,
UserId = authenticationResult.UserInfo == null ? null : authenticationResult.UserInfo.UniqueId,
};

return this.CurrentAccountSession;
Expand Down Expand Up @@ -156,7 +173,12 @@ private async Task<string> GetAuthenticationTokenForResourceAsync(string resourc
return authenticationResult.AccessToken;
}

private async Task RetrieveMyFilesServiceResourceAsync(string discoveryServiceToken)
private Task RetrieveMyFilesServiceResourceAsync(string discoveryServiceToken)
{
return this.RetrieveServiceResourceAsync(discoveryServiceToken, Constants.Authentication.MyFilesCapability);
}

private async Task RetrieveServiceResourceAsync(string discoveryServiceToken, string capability)
{
using (var httpRequestMessage = new HttpRequestMessage(HttpMethod.Get, this.ServiceInfo.DiscoveryServiceUrl))
{
Expand All @@ -167,25 +189,27 @@ private async Task RetrieveMyFilesServiceResourceAsync(string discoveryServiceTo
{
var responseValues = this.ServiceInfo.HttpProvider.Serializer.DeserializeObject<DiscoveryServiceResponse>(responseStream);

if (responseValues == null)
if (responseValues == null || responseValues.Value == null)
{
throw new OneDriveException(
new Error
{
Code = OneDriveErrorCode.ItemNotFound.ToString(),
Message = "MyFiles service not found for the current user."
Code = OneDriveErrorCode.MyFilesCapabilityNotFound.ToString(),
Message = "MyFiles capability not found for the current user."
});
}

var service = responseValues.Value.FirstOrDefault(value => value.ServiceApiVersion.Equals(this.ServiceInfo.OneDriveServiceEndpointVersion));
var service = responseValues.Value.FirstOrDefault(value =>
string.Equals(value.ServiceApiVersion, this.ServiceInfo.OneDriveServiceEndpointVersion, StringComparison.OrdinalIgnoreCase) &&
string.Equals(value.Capability, capability, StringComparison.OrdinalIgnoreCase));

if (service == null)
{
throw new OneDriveException(
new Error
{
Code = OneDriveErrorCode.ItemNotFound.ToString(),
Message = "MyFiles service not found for the current user."
Code = OneDriveErrorCode.MyFilesCapabilityNotFound.ToString(),
Message = string.Format("{0} capability with version {1} not found for the current user.", capability, this.ServiceInfo.OneDriveServiceEndpointVersion),
});
}

Expand Down
5 changes: 5 additions & 0 deletions src/OneDriveSdk.Common/Authentication/AdalCredentialCache.cs
Expand Up @@ -57,6 +57,11 @@ internal TokenCache TokenCache

return this.tokenCache;
}

set
{
this.tokenCache = value;
}
}

public override bool HasStateChanged
Expand Down

0 comments on commit d70677e

Please sign in to comment.