-
Notifications
You must be signed in to change notification settings - Fork 225
Expand file tree
/
Copy pathMicrosoft.Graph.Authentication.psm1
More file actions
180 lines (147 loc) · 6.24 KB
/
Microsoft.Graph.Authentication.psm1
File metadata and controls
180 lines (147 loc) · 6.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
function Get-GraphAuthenticationLoadContextName {
param(
[Parameter(Mandatory = $true)]
[string] $ModulePath
)
$sha256 = [System.Security.Cryptography.SHA256]::Create()
try {
$pathBytes = [System.Text.Encoding]::UTF8.GetBytes($ModulePath)
$hash = [System.BitConverter]::ToString($sha256.ComputeHash($pathBytes)).Replace('-', '').Substring(0, 16)
"Microsoft.Graph.Authentication.$hash"
}
finally {
$sha256.Dispose()
}
}
function Initialize-GraphAuthenticationAssemblyResolver {
if ('Microsoft.Graph.PowerShell.Authentication.Loader.GraphAuthenticationAssemblyResolver' -as [type]) {
return
}
Add-Type -TypeDefinition @'
using System;
using System.Collections.Concurrent;
using System.IO;
using System.Reflection;
using System.Runtime.Loader;
namespace Microsoft.Graph.PowerShell.Authentication.Loader
{
public static class GraphAuthenticationAssemblyResolver
{
private static readonly ConcurrentDictionary<string, string[]> DependencyFolders = new ConcurrentDictionary<string, string[]>(StringComparer.Ordinal);
private static readonly ConcurrentDictionary<string, bool> RegisteredContexts = new ConcurrentDictionary<string, bool>(StringComparer.Ordinal);
public static void Register(AssemblyLoadContext context, string[] dependencyFolders)
{
if (context == null)
{
throw new ArgumentNullException(nameof(context));
}
string contextName = context.Name ?? string.Empty;
DependencyFolders[contextName] = dependencyFolders ?? Array.Empty<string>();
if (RegisteredContexts.TryAdd(contextName, true))
{
context.Resolving += Resolve;
}
}
private static Assembly Resolve(AssemblyLoadContext context, AssemblyName assemblyName)
{
if (context == null || assemblyName == null)
{
return null;
}
if (!DependencyFolders.TryGetValue(context.Name ?? string.Empty, out string[] dependencyFolders))
{
return null;
}
foreach (string dependencyFolder in dependencyFolders)
{
if (string.IsNullOrWhiteSpace(dependencyFolder) || !Directory.Exists(dependencyFolder))
{
continue;
}
string dependencyPath = Path.Combine(dependencyFolder, assemblyName.Name + ".dll");
if (File.Exists(dependencyPath))
{
return context.LoadFromAssemblyPath(Path.GetFullPath(dependencyPath));
}
}
return null;
}
}
}
'@
}
function Import-GraphAuthenticationAssembly {
param(
[Parameter(Mandatory = $true)]
[string] $ModulePath
)
if ($PSEdition -ne 'Core' -or -not ('System.Runtime.Loader.AssemblyLoadContext' -as [type])) {
return Import-Module -Name $ModulePath -PassThru
}
$loadContextName = Get-GraphAuthenticationLoadContextName -ModulePath $ModulePath
$loadContext = [System.Runtime.Loader.AssemblyLoadContext]::All |
Where-Object { $_.Name -eq $loadContextName } |
Select-Object -First 1
if ($null -eq $loadContext) {
$loadContext = [System.Runtime.Loader.AssemblyLoadContext]::new($loadContextName, $false)
}
$moduleRoot = $PSScriptRoot
$dependencyFolders = @(
(Join-Path $moduleRoot 'Dependencies\Core'),
(Join-Path $moduleRoot 'Dependencies'),
$moduleRoot
)
Initialize-GraphAuthenticationAssemblyResolver
[Microsoft.Graph.PowerShell.Authentication.Loader.GraphAuthenticationAssemblyResolver]::Register($loadContext, [string[]] $dependencyFolders)
$moduleAssembly = $loadContext.Assemblies |
Where-Object { $_.GetName().Name -eq 'Microsoft.Graph.Authentication' } |
Select-Object -First 1
if ($null -eq $moduleAssembly) {
$moduleAssembly = $loadContext.LoadFromAssemblyPath((Resolve-Path -LiteralPath $ModulePath).Path)
}
Import-Module -Assembly $moduleAssembly -PassThru
}
function Test-GraphAuthenticationDoNotExport {
param(
[Parameter(Mandatory = $true)]
[System.Management.Automation.CommandInfo] $Command
)
$implementingType = $Command.ImplementingType
$null -ne $implementingType -and ($implementingType.GetCustomAttributes($false) |
Where-Object { $_.GetType().FullName -eq 'Microsoft.Graph.PowerShell.Authentication.Utilities.Runtime.DoNotExportAttribute' })
}
function New-GraphAuthenticationCmdletAlias {
param(
[Parameter(Mandatory = $true)]
[System.Management.Automation.CmdletInfo] $Command
)
$aliasNames = $Command.ImplementingType.GetCustomAttributes($false) |
Where-Object { $_.GetType().FullName -eq 'System.Management.Automation.AliasAttribute' } |
ForEach-Object { $_.AliasNames } |
Where-Object { -not [string]::IsNullOrWhiteSpace($_) } |
Select-Object -Unique
foreach ($aliasName in $aliasNames) {
New-Alias -Name $aliasName -Value $Command.Name -Force -Scope Script
$aliasName
}
}
# Load the module DLL before exporting cmdlets. On PowerShell Core, loading the
# binary through a custom AssemblyLoadContext keeps its dependencies isolated
# from other Microsoft 365 modules that may already be loaded in the process.
$ModulePath = (Join-Path $PSScriptRoot 'Microsoft.Graph.Authentication.dll')
$ModuleInfo = Import-GraphAuthenticationAssembly -ModulePath $ModulePath
# Export nothing to clear implicit exports.
Export-ModuleMember
if (Test-Path -Path "$PSScriptRoot\StartupScripts" -ErrorAction Ignore)
{
Get-ChildItem "$PSScriptRoot\StartupScripts" -Filter *.ps1 -ErrorAction Stop | ForEach-Object {
. $_.FullName
}
}
# Export binary module cmdlets.
$CmdletsToExport = $ModuleInfo.ExportedCommands.Values |
Where-Object { $_.CommandType -eq 'Cmdlet' -and -not (Test-GraphAuthenticationDoNotExport -Command $_) }
$AliasesToExport = $CmdletsToExport |
ForEach-Object { New-GraphAuthenticationCmdletAlias -Command $_ } |
Select-Object -Unique
Export-ModuleMember -Cmdlet ($CmdletsToExport | Select-Object -ExpandProperty Name -Unique) -Alias $AliasesToExport