Skip to content

Commit 8b8b027

Browse files
authored
Merge pull request #2186 from microsoftgraph/enhancements/AddEnvironmentToContext
Add environment to `Get-MgContext` output
2 parents 94b660d + b00a619 commit 8b8b027

File tree

6 files changed

+46
-2
lines changed

6 files changed

+46
-2
lines changed

src/Authentication/Authentication.Core/Interfaces/IAuthContext.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ public interface IAuthContext
4242
string[] Scopes { get; set; }
4343
AuthenticationType AuthType { get; set; }
4444
TokenCredentialType TokenCredentialType { get; set; }
45+
string Environment { get; set; }
4546
string AppName { get; set; }
4647
string Account { get; set; }
4748
string CertificateThumbprint { get; set; }

src/Authentication/Authentication.Test/Helpers/AuthenticationHelpersTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ public async Task ShouldUseDelegateAuthProviderWhenUserAccessTokenIsProvidedAsyn
5050
// Assert
5151
Assert.IsType<AzureIdentityAccessTokenProvider>(authProvider);
5252
Assert.Equal(dummyAccessToken, accessToken);
53+
Assert.Equal(GraphEnvironmentConstants.EnvironmentName.Global, userProvidedAuthContext.Environment);
5354

5455
// reset static instance.
5556
GraphSession.Reset();

src/Authentication/Authentication/Cmdlets/ConnectMgGraph.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ private async Task ProcessRecordAsync()
158158
{
159159
using (NoSynchronizationContext)
160160
{
161-
IAuthContext authContext = new AuthContext { TenantId = TenantId, PSHostVersion = this.Host.Version };
161+
IAuthContext authContext = new AuthContext { TenantId = TenantId, PSHostVersion = this.Host.Version, Environment = environment?.Name };
162162
if (MyInvocation.BoundParameters.ContainsKey(nameof(ClientTimeout)))
163163
GraphSession.Instance.RequestContext.ClientTimeout = TimeSpan.FromSeconds(ClientTimeout);
164164

src/Authentication/Authentication/Cmdlets/GetMGContext.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ protected override void ProcessRecord()
1919
{
2020
base.ProcessRecord();
2121
IAuthContext authConfig = GraphSession.Instance.AuthContext;
22-
WriteObject(authConfig);
22+
if (authConfig != null)
23+
WriteObject(authConfig);
2324
}
2425

2526
protected override void EndProcessing()

src/Authentication/Authentication/Models/AuthContext.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License. See License in the project root for license information.
33
// ------------------------------------------------------------------------------
44

5+
using Microsoft.Graph.PowerShell.Authentication.Interfaces;
56
using System;
67
using System.Security;
78
using System.Security.Cryptography.X509Certificates;
@@ -25,6 +26,7 @@ public class AuthContext : IAuthContext
2526
public Version PSHostVersion { get; set; }
2627
public string ManagedIdentityId { get; set; }
2728
public SecureString ClientSecret { get; set; }
29+
public string Environment { get; set; } = GraphEnvironmentConstants.EnvironmentName.Global;
2830

2931
public AuthContext()
3032
{
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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+
5+
BeforeAll {
6+
$ModuleName = "Microsoft.Graph.Authentication"
7+
$ModulePath = Join-Path $PSScriptRoot "..\artifacts\$ModuleName.psd1"
8+
Import-Module $ModulePath -Force
9+
# Test JWT access token. Holds dummy values.
10+
$DummyToken = "eyJhbGciOiJIUzI1NiJ9.eyJSb2xlIjoiVGVzdCIsIklzc3VlciI6Iklzc3VlciIsIlVzZXJuYW1lIjoiVGVzdCIsImV4cCI6MTkxMTIzMDM1NiwiaWF0IjoxNjc4NDg4ODE2fQ.yjRvogDyxlQrrQV3EaEsZJKhpYuNzaCyrh5Ip9WvdjU"
11+
}
12+
13+
Describe 'Get-MgContext' {
14+
It 'Should throw return null when no auth context exists for the session' {
15+
$Context = Get-MgContext
16+
$Context | Should -Be $null
17+
}
18+
19+
It 'Should default to Global environment when no environment is specified' {
20+
Connect-MgGraph -AccessToken (ConvertTo-SecureString -AsPlainText -String $DummyToken)
21+
$Context = Get-MgContext
22+
$Context | Should -Not -Be $null
23+
$Context.Environment | Should -Be "Global"
24+
$Context.AuthType | Should -Be "UserProvidedAccessToken"
25+
}
26+
27+
28+
It 'Should return the correct environment when specified via Connect-MgGraph -Environment' {
29+
Connect-MgGraph -AccessToken (ConvertTo-SecureString -AsPlainText -String $DummyToken) -Environment Germany
30+
$Context = Get-MgContext
31+
$Context | Should -Not -Be $null
32+
$Context.Environment | Should -Be "Germany"
33+
$Context.AuthType | Should -Be "UserProvidedAccessToken"
34+
}
35+
}
36+
37+
AfterAll {
38+
Disconnect-MgGraph
39+
}

0 commit comments

Comments
 (0)