Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
Expand Up @@ -197,6 +197,7 @@
<Compile Include="Web\IotFriendlyWebDialog.xaml.cs">
<DependentUpon>IotFriendlyWebDialog.xaml</DependentUpon>
</Compile>
<Compile Include="OnlineIdAuthenticationByAccountSettingsPaneProvider.cs" />
<EmbeddedResource Include="Properties\Microsoft.OneDrive.Sdk.Authentication.UWP.rd.xml" />
</ItemGroup>
<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
// ------------------------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License. See License in the project root for license information.
// ------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.OneDrive.Sdk.Authentication;
using Windows.Security.Authentication.OnlineId;
using Microsoft.Graph;
using Windows.Security.Credentials;
using Windows.Security.Authentication.Web.Core;
using Windows.UI.ApplicationSettings;
using Yinyue200.OperationDeferral;
using Windows.Storage;

namespace Microsoft.OneDrive.Sdk
{
public class OnlineIdAuthenticationByAccountSettingsPaneProvider : OnlineIdAuthenticationProvider
{
const string containerName = "OneDriveSDK_AuthAdapter_AccountSettingsPane";
public OnlineIdAuthenticationByAccountSettingsPaneProvider(string[] scopes, PromptType promptType = PromptType.PromptIfNeeded) : base(scopes, promptType)
{
}
public async override Task SignOutAsync()
{
await base.SignOutAsync();
ApplicationData.Current.LocalSettings.DeleteContainer(containerName);
await Account?.SignOutAsync();
}

WebAccount Account { get; set; }
private async Task<string> GetTokenSilentlyAsync(string providerId, string accountId)
{
if (null == providerId || null == accountId)
{
return null;
}

WebAccountProvider provider = await WebAuthenticationCoreManager.FindAccountProviderAsync(providerId);
Account = await WebAuthenticationCoreManager.FindAccountAsync(provider, accountId);

WebTokenRequest request = new WebTokenRequest(provider, string.Join(" ", this.scopes));

WebTokenRequestResult result = await WebAuthenticationCoreManager.GetTokenSilentlyAsync(request, Account);
if (result.ResponseStatus == WebTokenRequestStatus.UserInteractionRequired)
{
// Unable to get a token silently - you'll need to show the UI
return null;
}
else if (result.ResponseStatus == WebTokenRequestStatus.Success)
{
// Success
return result.ResponseData[0].Token;
}
else
{
// Other error
return null;
}
}
OperationDeferral<string> od;
private async Task<string> GetTokenByUIAsync()
{
try
{
od = new OperationDeferral<string>();
AccountsSettingsPane.GetForCurrentView().AccountCommandsRequested += BuildPaneAsync;
AccountsSettingsPane.Show();
return await od.WaitOneAsync();
}
finally
{
AccountsSettingsPane.GetForCurrentView().AccountCommandsRequested -= BuildPaneAsync;
}


}

private async void BuildPaneAsync(AccountsSettingsPane sender, AccountsSettingsPaneCommandsRequestedEventArgs args)
{
var deferral = args.GetDeferral();

var msaProvider = await WebAuthenticationCoreManager.FindAccountProviderAsync(
"https://login.microsoft.com", "consumers");

var command = new WebAccountProviderCommand(msaProvider, GetMsaTokenAsync);

args.WebAccountProviderCommands.Add(command);

deferral.Complete();
}
private async void GetMsaTokenAsync(WebAccountProviderCommand command)
{
WebTokenRequest request = new WebTokenRequest(command.WebAccountProvider, string.Join(" ", this.scopes));
WebTokenRequestResult result = await WebAuthenticationCoreManager.RequestTokenAsync(request);

if (result.ResponseStatus == WebTokenRequestStatus.Success)
{
Account = result.ResponseData[0].WebAccount;
string token = result.ResponseData[0].Token;
od.Complete(token);
}
else
{
od.Complete(null);
}
}

protected async override Task<AccountSession> GetAccountSessionAsync()
{
const string useridkey = "userid";
const string proivderidkey = "proid";
try
{
object proid;
object userid = null;
string key = null;
if(ApplicationData.Current.LocalSettings.CreateContainer(containerName, ApplicationDataCreateDisposition.Always).Values.TryGetValue(proivderidkey, out proid))
{
if (ApplicationData.Current.LocalSettings.Containers[containerName].Values.TryGetValue(useridkey, out userid))
key = await GetTokenSilentlyAsync(proid?.ToString(), userid?.ToString());
}
if (key == null)
{
key = await GetTokenByUIAsync();
if(key!=null)
{
try
{
ApplicationData.Current.LocalSettings.Containers[containerName].Values[proivderidkey] = Account.WebAccountProvider.Id;
ApplicationData.Current.LocalSettings.Containers[containerName].Values[useridkey] = Account.Id;
userid = Account.Id;
}
catch { }
}
}
if(key==null)
{
throw new Exception("failed to get token");
}
var accountSession = new AccountSession
{
AccessToken = key,
ExpiresOnUtc = DateTimeOffset.UtcNow.AddMinutes(this.ticketExpirationTimeInMinutes),
ClientId = this.authenticator.ApplicationId.ToString(),
UserId = userid.ToString()
};
return accountSession;
}
catch (TaskCanceledException taskCanceledException)
{
throw new ServiceException(new Error { Code = OAuthConstants.ErrorCodes.AuthenticationCancelled, Message = "Authentication was canceled." }, taskCanceledException);
}
catch (Exception exception)
{
throw new ServiceException(new Error { Code = OAuthConstants.ErrorCodes.AuthenticationFailure, Message = exception.Message }, exception);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ namespace Microsoft.OneDrive.Sdk

public class OnlineIdAuthenticationProvider : MsaAuthenticationProvider
{
private const string onlineIdServiceTicketRequestType = "DELEGATION";
private readonly int ticketExpirationTimeInMinutes = 60;
private readonly OnlineIdAuthenticator authenticator;
private readonly CredentialPromptType credentialPromptType;
protected const string onlineIdServiceTicketRequestType = "DELEGATION";
protected readonly int ticketExpirationTimeInMinutes = 60;
protected readonly OnlineIdAuthenticator authenticator;
protected readonly CredentialPromptType credentialPromptType;

public enum PromptType
{
Expand All @@ -47,7 +47,7 @@ public enum PromptType

public OnlineIdAuthenticationProvider(
string[] scopes, PromptType promptType = PromptType.PromptIfNeeded)
:base(null, null, scopes)
: base(null, null, scopes)
{
this.authenticator = new OnlineIdAuthenticator();
this.credentialPromptType = (CredentialPromptType)promptType;
Expand All @@ -71,7 +71,7 @@ public override async Task AuthenticateUserAsync(IHttpProvider httpProvider, str
});
}
}

this.CacheAuthResult(authResult);
}

Expand All @@ -85,14 +85,14 @@ public override async Task SignOutAsync()
if (this.authenticator.CanSignOut)
{
await this.authenticator.SignOutUserAsync();
}
}

this.DeleteUserCredentialsFromCache(this.CurrentAccountSession);
this.CurrentAccountSession = null;
}
}

internal async Task<AccountSession> GetAccountSessionAsync()
protected virtual async Task<AccountSession> GetAccountSessionAsync()
{
try
{
Expand Down Expand Up @@ -141,7 +141,7 @@ internal override async Task<AccountSession> ProcessCachedAccountSessionAsync(Ac
if (accountSession.ShouldRefresh) // Don't check 'CanRefresh' because this type can always refresh
{
accountSession = await this.GetAccountSessionAsync();

if (!string.IsNullOrEmpty(accountSession?.AccessToken))
{
return accountSession;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
This file contains Runtime Directives, specifications about types your application accesses
through reflection and other dynamic code patterns. Runtime Directives are used to control the
.NET Native optimizer and ensure that it does not remove code accessed by your library. If your
library does not do any reflection, then you generally do not need to edit this file. However,
if your library reflects over types, especially types passed to it or derived from its types,
then you should write Runtime Directives.

The most common use of reflection in libraries is to discover information about types passed
to the library. Runtime Directives have three ways to express requirements on types passed to
your library.

1. Parameter, GenericParameter, TypeParameter, TypeEnumerableParameter
Use these directives to reflect over types passed as a parameter.

2. SubTypes
Use a SubTypes directive to reflect over types derived from another type.

3. AttributeImplies
Use an AttributeImplies directive to indicate that your library needs to reflect over
types or methods decorated with an attribute.

For more information on writing Runtime Directives for libraries, please visit
http://go.microsoft.com/fwlink/?LinkID=391919
-->
<Directives xmlns="http://schemas.microsoft.com/netfx/2013/01/metadata">
<Library Name="OneDrive.Sdk.Authentication.UWP">

<!-- add directives for your library here -->

</Library>
</Directives>
3 changes: 2 additions & 1 deletion src/OneDrive.Sdk.Authentication.UWP/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
"dependencies": {
"Microsoft.Graph.Core": "1.3.1",
"Microsoft.IdentityModel.Clients.ActiveDirectory": "2.28.3",
"Microsoft.NETCore.UniversalWindowsPlatform": "5.1.0"
"Microsoft.NETCore.UniversalWindowsPlatform": "5.1.0",
"OperationDeferral": "1.0.0"
},
"frameworks": {
"uap10.0": {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,17 +79,13 @@
<ItemGroup>
<Reference Include="Microsoft.Graph.Core, Version=1.3.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\..\packages\Microsoft.Graph.Core.1.3.1\lib\portable45-net45+win8+wpa81\Microsoft.Graph.Core.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Newtonsoft.Json, Version=9.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\..\packages\Newtonsoft.Json.9.0.1\lib\portable-net45+wp80+win8+wpa81\Newtonsoft.Json.dll</HintPath>
<Private>True</Private>
</Reference>
</ItemGroup>
<ItemGroup>
<None Include="packages.config">
<SubType>Designer</SubType>
</None>
<None Include="packages.config" />
</ItemGroup>
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\Portable\$(TargetFrameworkVersion)\Microsoft.Portable.CSharp.targets" />
<PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,24 +38,19 @@
<TreatWarningsAsErrors>false</TreatWarningsAsErrors>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)' == 'Release'">
<SignAssembly>true</SignAssembly>
<DelaySign>true</DelaySign>
<AssemblyOriginatorKeyFile>..\..\build\35MSSharedLib1024.snk</AssemblyOriginatorKeyFile>
</PropertyGroup>
<ItemGroup>
<Reference Include="Microsoft.Graph.Core, Version=1.3.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\..\packages\Microsoft.Graph.Core.1.3.1\lib\MonoAndroid10\Microsoft.Graph.Core.dll</HintPath>
<Private>True</Private>
<Reference Include="Microsoft.CSharp" />
<Reference Include="Microsoft.Graph.Core, Version=1.4.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\..\packages\Microsoft.Graph.Core.1.4.0\lib\netstandard1.1\Microsoft.Graph.Core.dll</HintPath>
</Reference>
<Reference Include="Mono.Android" />
<Reference Include="mscorlib" />
<Reference Include="Newtonsoft.Json, Version=9.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\..\packages\Newtonsoft.Json.9.0.1\lib\portable-net45+wp80+win8+wpa81\Newtonsoft.Json.dll</HintPath>
<Private>True</Private>
<Reference Include="Newtonsoft.Json, Version=10.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\..\packages\Newtonsoft.Json.10.0.2\lib\netstandard1.3\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.IO.Compression" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Security" />
<Reference Include="System.Xml.Linq" />
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,8 @@
<HintPath>..\..\packages\Castle.Core.3.3.3\lib\net45\Castle.Core.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.Graph.Core, Version=1.3.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\..\packages\Microsoft.Graph.Core.1.3.1\lib\portable45-net45+win8+wpa81\Microsoft.Graph.Core.dll</HintPath>
<Private>True</Private>
<Reference Include="Microsoft.Graph.Core, Version=1.4.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\..\packages\Microsoft.Graph.Core.1.4.0\lib\net45\Microsoft.Graph.Core.dll</HintPath>
</Reference>
<Reference Include="Microsoft.IdentityModel.Clients.ActiveDirectory, Version=2.21.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\..\packages\Microsoft.IdentityModel.Clients.ActiveDirectory.2.21.301221612\lib\net45\Microsoft.IdentityModel.Clients.ActiveDirectory.dll</HintPath>
Expand All @@ -60,9 +59,8 @@
<HintPath>..\..\packages\Moq.4.5.10\lib\net45\Moq.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Newtonsoft.Json, Version=9.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\..\packages\Newtonsoft.Json.9.0.1\lib\net45\Newtonsoft.Json.dll</HintPath>
<Private>True</Private>
<Reference Include="Newtonsoft.Json, Version=10.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\..\packages\Newtonsoft.Json.10.0.2\lib\net45\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Net" />
Expand Down
3 changes: 1 addition & 2 deletions tests/Test.OneDrive.Sdk.Authentication.UWP/project.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
{
"dependencies": {
"Microsoft.Graph.Core": "1.3.1",
"Microsoft.NETCore.UniversalWindowsPlatform": "5.1.0",
"Newtonsoft.Json": "9.0.1"
"Microsoft.NETCore.UniversalWindowsPlatform": "5.1.0"
},
"frameworks": {
"uap10.0": {}
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading