Skip to content

Commit dcbf5fb

Browse files
committed
Pre load System.Security.Cryptography.ProtectedData assembly on Windows PowerShell.
1 parent 5984cb2 commit dcbf5fb

8 files changed

+79
-6
lines changed

Repo.props

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
2+
<Project>
3+
<PropertyGroup>
4+
<RepoRoot>$(MSBuildThisFileDirectory)</RepoRoot>
5+
<RepoSrc>$(RepoRoot)src/</RepoSrc>
6+
<RepoArtifacts>$(RepoRoot)artifacts/</RepoArtifacts>
7+
<RepoTools>$(RepoRoot)tools/</RepoTools>
8+
</PropertyGroup>
9+
</Project>

src/Authentication/Authentication/Helpers/AuthenticationHelpers.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ private static void ConfigureTokenCache(ITokenCache tokenCache, string tokenCach
6767
lock (FileLock)
6868
{
6969
args.TokenCache.DeserializeMsalV3(File.Exists(tokenCachePath)
70-
? File.ReadAllBytes(tokenCachePath)
70+
? TokenCryptoHelpers.DecryptToken(File.ReadAllBytes(tokenCachePath))
7171
: null,
7272
shouldClearExistingCache: true);
7373
}
@@ -78,7 +78,7 @@ private static void ConfigureTokenCache(ITokenCache tokenCache, string tokenCach
7878
{
7979
if (args.HasStateChanged)
8080
{
81-
File.WriteAllBytes(tokenCachePath, args.TokenCache.SerializeMsalV3());
81+
File.WriteAllBytes(tokenCachePath, TokenCryptoHelpers.EncryptToken(args.TokenCache.SerializeMsalV3()));
8282
}
8383
}
8484
});
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// ------------------------------------------------------------------------------
2+
// Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License. See License in the project root for license information.
3+
// ------------------------------------------------------------------------------
4+
namespace Microsoft.Graph.PowerShell.Authentication.Helpers
5+
{
6+
using System;
7+
using System.Security.Cryptography;
8+
9+
/// <summary>
10+
/// Helper class to handle token encryption and decryption.
11+
/// </summary>
12+
internal static class TokenCryptoHelpers
13+
{
14+
/// <summary>
15+
/// Encrypts the passed buffer based on the host platform.
16+
/// </summary>
17+
/// <param name="buffer">A <see cref="byte[]"/> to encrypt.</param>
18+
/// <returns>An encrypted <see cref="byte[]"/>.</returns>
19+
public static byte[] EncryptToken(byte[] buffer)
20+
{
21+
if (Environment.OSVersion.Platform == PlatformID.Win32NT)
22+
return ProtectedData.Protect(buffer, null, DataProtectionScope.CurrentUser);
23+
return buffer;
24+
}
25+
26+
/// <summary>
27+
/// Decrypts the passed buffer based on the host platform.
28+
/// </summary>
29+
/// <param name="buffer">A <see cref="byte[]"/> to decrypt.</param>
30+
/// <returns>An decrypted <see cref="byte[]"/>.</returns>
31+
public static byte[] DecryptToken(byte[] buffer)
32+
{
33+
if (Environment.OSVersion.Platform == PlatformID.Win32NT)
34+
return ProtectedData.Unprotect(buffer, null, DataProtectionScope.CurrentUser);
35+
return buffer;
36+
}
37+
}
38+
}

src/Authentication/Authentication/Microsoft.Graph.Authentication.csproj

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

3+
<Import Project="$(MSBuildThisFileDirectory)..\..\..\Repo.props" />
4+
35
<PropertyGroup>
46
<Version>0.0.1</Version>
57
<LangVersion>7.1</LangVersion>
@@ -16,11 +18,20 @@
1618
<WarningsAsErrors />
1719
<NuspecFile>Microsoft.Graph.Authentication.nuspec</NuspecFile>
1820
</PropertyGroup>
21+
22+
<ItemGroup>
23+
<PreLoadAssemblies Include="$(RepoTools)lib\System.Security.Cryptography.ProtectedData.dll" />
24+
</ItemGroup>
1925

2026
<ItemGroup>
2127
<PackageReference Include="Microsoft.Graph.Auth" Version="1.0.0-preview.2" />
2228
<PackageReference Include="Microsoft.Graph.Core" Version="1.18.0" />
2329
<PackageReference Include="Microsoft.Identity.Client" Version="4.5.1" />
2430
<PackageReference Include="PowerShellStandard.Library" Version="5.1.0" />
31+
<PackageReference Include="System.Security.Cryptography.ProtectedData" Version="4.7.0" />
2532
</ItemGroup>
33+
34+
<Target Name="CopyFiles" AfterTargets="Build">
35+
<Copy SourceFiles="@(PreLoadAssemblies)" DestinationFolder="$(TargetDir)\PreloadAssemblies" />
36+
</Target>
2637
</Project>

src/Authentication/Authentication/Microsoft.Graph.Authentication.nuspec

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0"?>
22
<package>
33
<metadata>
4-
<version>0.1.4</version>
4+
<version>0.1.6</version>
55
<id>Microsoft.Graph.Authentication</id>
66
<description>Microsoft Graph PowerShell authentication module</description>
77
<authors>Microsoft</authors>
@@ -18,12 +18,14 @@
1818
</metadata>
1919
<files>
2020
<file src="Microsoft.Graph.Authentication.psd1" />
21+
<file src="Microsoft.Graph.Authentication.psm1" />
2122
<!--<file src="Graph.Authentication.psm1" />-->
2223
<!-- https://github.com/NuGet/Home/issues/3584 -->
2324
<file src="bin/Microsoft.Graph.Authentication.dll" target="bin" />
2425
<file src="bin/Microsoft.Graph.Authentication.deps.json" target="bin" />
2526
<file src="bin/Microsoft.Graph.Auth.dll" target="bin" />
2627
<file src="bin/Microsoft.Graph.Core.dll" target="bin" />
2728
<file src="bin/Microsoft.Identity.Client.dll" target="bin" />
29+
<file src="bin/PreloadAssemblies/System.Security.Cryptography.ProtectedData.dll" target="bin/PreloadAssemblies" />
2830
</files>
2931
</package>

src/Authentication/Authentication/Microsoft.Graph.Authentication.psd1

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,16 @@
33
#
44
# Generated by: Microsoft
55
#
6-
# Generated on: 12/12/2019
6+
# Generated on: 2/3/2020
77
#
88

99
@{
1010

1111
# Script module or binary module file associated with this manifest.
12-
RootModule = '.\bin\Microsoft.Graph.Authentication.dll'
12+
RootModule = './Microsoft.Graph.Authentication.psm1'
1313

1414
# Version number of this module.
15-
ModuleVersion = '0.1.4'
15+
ModuleVersion = '0.1.6'
1616

1717
# Supported PSEditions
1818
CompatiblePSEditions = 'Core', 'Desktop'
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Load dependencies
2+
$preloadPath = (Join-Path $PSScriptRoot -ChildPath ".\bin\PreloadAssemblies")
3+
if ($PSEdition -eq 'Desktop' -and (Test-Path $preloadPath -ErrorAction Ignore)) {
4+
try {
5+
Get-ChildItem -ErrorAction Stop -Path $preloadPath -Filter "*.dll" | ForEach-Object {
6+
Add-Type -Path $_.FullName -ErrorAction Ignore | Out-Null
7+
}
8+
}
9+
catch { }
10+
}
11+
12+
# Load the module dll
13+
$null = Import-Module -Name (Join-Path $PSScriptRoot '.\bin\Microsoft.Graph.Authentication.dll')
Binary file not shown.

0 commit comments

Comments
 (0)