Skip to content

Commit

Permalink
NewsReader: Workaround for Graph SDK and use MSAL Broker on Windows (#70
Browse files Browse the repository at this point in the history
)

* NR: Adapt MSAL on Windows; update packages

* NR: Rollback unit testing packages to previous version

* NR: Use MSAL Broker on Windows

* NR: Workaround for MS Graph to get the required driveId
microsoftgraph/msgraph-sdk-dotnet#2624
  • Loading branch information
jbe2277 authored Jan 21, 2025
1 parent c70816d commit f73dbf0
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/NewsReader/Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project>
<PropertyGroup>
<MauiVersion>9.0.21</MauiVersion>
<MauiVersion>9.0.22</MauiVersion>

<NeutralLanguage>en</NeutralLanguage>
<Nullable>enable</Nullable>
Expand Down
8 changes: 4 additions & 4 deletions src/NewsReader/Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
<PackageVersion Include="Autofac" Version="8.2.0" />
<PackageVersion Include="Autofac.Extensions.DependencyInjection" Version="10.0.0" />
<PackageVersion Include="NLog" Version="5.3.4" />
<PackageVersion Include="Microsoft.Graph" Version="5.67.0" />
<PackageVersion Include="Microsoft.Identity.Client" Version="4.66.2" />
<PackageVersion Include="Microsoft.Identity.Client.Extensions.Msal" Version="4.66.2" />
<PackageVersion Include="System.ServiceModel.Syndication" Version="9.0.0" />
<PackageVersion Include="Microsoft.Graph" Version="5.68.0" />
<PackageVersion Include="Microsoft.Identity.Client.Broker" Version="4.67.2" />
<PackageVersion Include="Microsoft.Identity.Client.Extensions.Msal" Version="4.67.2" />
<PackageVersion Include="System.ServiceModel.Syndication" Version="9.0.1" />
<PackageVersion Include="System.Waf.Core" Version="8.0.2-alpha.0.81" />

<!-- Unit tests -->
Expand Down
3 changes: 3 additions & 0 deletions src/NewsReader/NewsReader.Presentation/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ public App(ISettingsService settingsService, IAppInfoService appInfoService, Laz

public static string LogFileName { get; } = Path.Combine(FileSystem.CacheDirectory, "Logging", "AppLog.txt");

public static Window? CurrentWindow { get; private set; }

protected override Window CreateWindow(IActivationState? activationState)
{
var window = new Window((Page)appController.MainView)
Expand All @@ -56,6 +58,7 @@ protected override Window CreateWindow(IActivationState? activationState)
window.Stopped += (_, _) => OnStopped();
window.Destroying += (_, _) => OnDestroying();
window.Resumed += (_, _) => OnResumed();
CurrentWindow = window;
return window;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<PackageReference Include="Microsoft.Maui.Controls" VersionOverride="$(MauiVersion)" />

<PackageReference Include="Microsoft.Graph" />
<PackageReference Include="Microsoft.Identity.Client" />
<PackageReference Include="Microsoft.Identity.Client.Broker" />
<PackageReference Include="Microsoft.Identity.Client.Extensions.Msal" />
<PackageReference Include="System.ServiceModel.Syndication" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using Microsoft.Graph.Models;
using Microsoft.Graph.Models.ODataErrors;
using Microsoft.Identity.Client;
using Microsoft.Identity.Client.Extensions.Msal;
using Microsoft.Kiota.Abstractions.Authentication;
using Waf.NewsReader.Applications.Services;

Expand Down Expand Up @@ -41,6 +40,12 @@ public WebStorageService()
builder.WithParentActivityOrWindow(() => Platform.CurrentActivity);
#elif IOS
builder.WithIosKeychainSecurityGroup(Foundation.NSBundle.MainBundle.BundleIdentifier);
#elif WINDOWS
Microsoft.Identity.Client.Broker.BrokerExtension.WithBroker(builder, new BrokerOptions(BrokerOptions.OperatingSystems.Windows)
{
Title = AppInfo.Name
});
builder.WithParentActivityOrWindow(() => WinRT.Interop.WindowNative.GetWindowHandle(App.CurrentWindow!.Handler.PlatformView!));
#endif
publicClient = builder.Build();
}
Expand All @@ -61,8 +66,8 @@ public async Task<bool> TrySilentSignIn()
if (!cacheInitialized && publicClient is not null)
{
cacheInitialized = true;
var storageProperties = new StorageCreationPropertiesBuilder("msal.dat", FileSystem.CacheDirectory).Build();
var cacheHelper = await MsalCacheHelper.CreateAsync(storageProperties);
var storageProperties = new Microsoft.Identity.Client.Extensions.Msal.StorageCreationPropertiesBuilder("msal.dat", FileSystem.CacheDirectory).Build();
var cacheHelper = await Microsoft.Identity.Client.Extensions.Msal.MsalCacheHelper.CreateAsync(storageProperties);
cacheHelper.RegisterCache(publicClient.UserTokenCache);
}
#endif
Expand Down Expand Up @@ -180,11 +185,14 @@ private async Task InitGraphClient()
private async Task<CustomDriveItemItemRequestBuilder> GetItemRequest(string fileName)
{
if (graphClient is null) throw new InvalidOperationException("graphClient is null");
var driveItem = await graphClient.Me.Drive.GetAsync().ConfigureAwait(false);
ArgumentNullException.ThrowIfNull(driveItem);
var appRootFolder = await graphClient.Drives[driveItem.Id].Special["AppRoot"].GetAsync().ConfigureAwait(false);
ArgumentNullException.ThrowIfNull(appRootFolder);
return graphClient.Drives[driveItem.Id].Items[appRootFolder.Id].ItemWithPath(fileName);

// Using workaround to get the driveId because AppFolder scope does not allow to the read Drive directly. https://github.com/microsoftgraph/msgraph-sdk-dotnet/issues/2624
var result = await graphClient.Me.Drive.WithUrl($"{graphClient.RequestAdapter.BaseUrl}/drive/special/approot:/{fileName}").GetAsync().ConfigureAwait(false);
var driveId = result?.Id?.Split("!")[0] ?? throw new InvalidOperationException("graphClient: not able to get the driveId");

var appRootFolder = await graphClient.Drives[driveId].Special["AppRoot"].GetAsync().ConfigureAwait(false)
?? throw new InvalidOperationException("graphClient: not able to get the appRootFolder");
return graphClient.Drives[driveId].Items[appRootFolder.Id].ItemWithPath(fileName);
}

static partial void GetApplicationId(ref string? applicationId);
Expand Down

0 comments on commit f73dbf0

Please sign in to comment.