Skip to content

Commit

Permalink
Avoid setting DefaultRequestHeaders when request is in flight. (#1604)
Browse files Browse the repository at this point in the history
* Avoid setting DefaultRequestHeaders when request is in flight.

* Bump version number.
  • Loading branch information
peombwa authored Nov 1, 2022
1 parent 217262b commit a45920b
Show file tree
Hide file tree
Showing 45 changed files with 81 additions and 59 deletions.
2 changes: 1 addition & 1 deletion config/ModuleMetadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,5 @@
],
"releaseNotes": "See https://aka.ms/GraphPowerShell-Release.",
"assemblyOriginatorKeyFile": "35MSSharedLib1024.snk",
"version": "1.14.0"
"version": "1.14.1"
}
2 changes: 1 addition & 1 deletion src/Applications/Applications/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,6 @@ directive:
### Versioning
``` yaml
module-version: 1.14.0
module-version: 1.14.1
release-notes: See https://aka.ms/GraphPowerShell-Release.
```
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@
namespace Microsoft.Graph.Authentication.Test.Helpers
{
using System;
using System.Collections.Generic;
using System.Management.Automation;
using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.Graph.Authentication.Core;
using Microsoft.Graph.PowerShell.Authentication;
using Microsoft.Graph.PowerShell.Authentication.Helpers;
using Microsoft.Graph.PowerShell.Authentication.Models;
using Xunit;

public class HttpHelpersTests
{
[Fact]
Expand Down Expand Up @@ -184,5 +186,30 @@ public void GetGraphHttpClientShouldReturnNewHttpClientSignOutThenSignIn()
// reset static instance.
GraphSession.Reset();
}

[Fact]
public async void GetGraphHttpClientShouldBeThreadSafeAsync()
{
GraphSession.Initialize(() => new GraphSession());
GraphSession.Instance.RequestContext = new RequestContext();
var authContext = new AuthContext
{
AuthType = AuthenticationType.UserProvidedAccessToken,
ContextScope = ContextScope.Process,
PSHostVersion = new Version("7.2.7")
};

var tasks = new List<Task<HttpClient>>();
for (int i = 0; i < 100; i++)
{
tasks.Add(Task.Factory.StartNew(() => HttpHelpers.GetGraphHttpClient(authContext)));
}
var exception = await Record.ExceptionAsync(() => Task.WhenAll(tasks));
Assert.Null(exception);

// reset static instance.
GraphSession.Reset();
}

}
}
13 changes: 2 additions & 11 deletions src/Authentication/Authentication/Helpers/HttpHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ namespace Microsoft.Graph.PowerShell.Authentication.Helpers
using System.Security.Authentication;
using Microsoft.Graph.PowerShell.Authentication.Handlers;
using System.Management.Automation;
using System;
using Microsoft.Graph.PowerShell.Authentication.Core.Interfaces;

/// <summary>
Expand Down Expand Up @@ -54,16 +53,6 @@ private static void ReplaceSDKHeader(HttpClient httpClient, string headerName, s
}
}

public static HttpClient GetGraphHttpClient(InvocationInfo invocationInfo, IAuthContext authContext = null)
{
authContext = authContext ?? GraphSession.Instance.AuthContext;
if (authContext is null) { throw new AuthenticationException(Core.ErrorConstants.Message.MissingAuthContext); }
var httpClient = GetGraphHttpClient(authContext);
var requestUserAgent = new RequestUserAgent(authContext.PSHostVersion, invocationInfo);
ReplaceSDKHeader(httpClient, HttpKnownHeaderNames.UserAgent, requestUserAgent.UserAgent);
return httpClient;
}

/// <summary>
/// Creates a pre-configured Microsoft Graph <see cref="HttpClient"/>.
/// </summary>
Expand All @@ -81,6 +70,8 @@ public static HttpClient GetGraphHttpClient(IAuthContext authContext = null)

IAuthenticationProvider authProvider = AuthenticationHelpers.GetAuthProvider(authContext);
var newHttpClient = GetGraphHttpClient(authProvider, GraphSession.Instance.RequestContext);
var requestUserAgent = new RequestUserAgent(authContext.PSHostVersion, null);
ReplaceSDKHeader(newHttpClient, HttpKnownHeaderNames.UserAgent, requestUserAgent.UserAgent);
GraphSession.Instance.GraphHttpClient = newHttpClient;
return newHttpClient;
}
Expand Down
5 changes: 3 additions & 2 deletions src/Authentication/Authentication/Helpers/RequestUserAgent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,9 @@ internal string App
{
get
{
var app = string.Format(CultureInfo.InvariantCulture,
"PowerShell/{0} {1}", _psHostVersion, _invocationInfo.MyCommand.Name);
var app = string.Empty;
if (_psHostVersion != null)
app = string.Format(CultureInfo.InvariantCulture, "PowerShell/{0} {1}", _psHostVersion, _invocationInfo?.MyCommand?.Name);
return app;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#
# Generated by: Microsoft
#
# Generated on: 10/25/2022
# Generated on: 10/31/2022
#

@{
Expand All @@ -12,7 +12,7 @@
RootModule = './Microsoft.Graph.Authentication.psm1'

# Version number of this module.
ModuleVersion = '1.14.0'
ModuleVersion = '1.14.1'

# Supported PSEditions
CompatiblePSEditions = 'Core', 'Desktop'
Expand Down Expand Up @@ -99,7 +99,9 @@ PrivateData = @{
PSData = @{

# Tags applied to this module. These help with module discovery in online galleries.
Tags = 'Microsoft','Office365','Graph','PowerShell','Teams','Outlook','OneDrive','AzureAD','GraphAPI','Productivity','SharePoint','Intune','SDK'
Tags = 'Microsoft', 'Office365', 'Graph', 'PowerShell', 'Teams', 'Outlook',
'OneDrive', 'AzureAD', 'GraphAPI', 'Productivity', 'SharePoint', 'Intune',
'SDK'

# A URL to the license for this module.
LicenseUri = 'https://aka.ms/devservicesagreement'
Expand All @@ -124,7 +126,8 @@ PrivateData = @{

} # End of PSData hashtable

} # End of PrivateData hashtable

} # End of PrivateData hashtable

# HelpInfo URI of this module
# HelpInfoURI = ''
Expand Down
2 changes: 1 addition & 1 deletion src/Bookings/Bookings/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,6 @@ directive:
### Versioning
``` yaml
module-version: 1.14.0
module-version: 1.14.1
release-notes: See https://aka.ms/GraphPowerShell-Release.
```
2 changes: 1 addition & 1 deletion src/Calendar/Calendar/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,6 @@ directive:
### Versioning

``` yaml
module-version: 1.14.0
module-version: 1.14.1
release-notes: See https://aka.ms/GraphPowerShell-Release.
```
2 changes: 1 addition & 1 deletion src/ChangeNotifications/ChangeNotifications/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,6 @@ subject-prefix: ''
### Versioning
``` yaml
module-version: 1.14.0
module-version: 1.14.1
release-notes: See https://aka.ms/GraphPowerShell-Release.
```
2 changes: 1 addition & 1 deletion src/CloudCommunications/CloudCommunications/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,6 @@ directive:
### Versioning
``` yaml
module-version: 1.14.0
module-version: 1.14.1
release-notes: See https://aka.ms/GraphPowerShell-Release.
```
2 changes: 1 addition & 1 deletion src/Compliance/Compliance/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,6 @@ directive:
### Versioning
``` yaml
module-version: 1.14.0
module-version: 1.14.1
release-notes: See https://aka.ms/GraphPowerShell-Release.
```
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,6 @@ subject-prefix: ''
### Versioning
``` yaml
module-version: 1.14.0
module-version: 1.14.1
release-notes: See https://aka.ms/GraphPowerShell-Release.
```
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,6 @@ directive:
### Versioning
``` yaml
module-version: 1.14.0
module-version: 1.14.1
release-notes: See https://aka.ms/GraphPowerShell-Release.
```
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,6 @@ directive:
### Versioning
``` yaml
module-version: 1.14.0
module-version: 1.14.1
release-notes: See https://aka.ms/GraphPowerShell-Release.
```
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,6 @@ directive:
### Versioning
``` yaml
module-version: 1.14.0
module-version: 1.14.1
release-notes: See https://aka.ms/GraphPowerShell-Release.
```
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,6 @@ directive:
### Versioning
``` yaml
module-version: 1.14.0
module-version: 1.14.1
release-notes: See https://aka.ms/GraphPowerShell-Release.
```
2 changes: 1 addition & 1 deletion src/DeviceManagement/DeviceManagement/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,6 @@ directive:
### Versioning
``` yaml
module-version: 1.14.0
module-version: 1.14.1
release-notes: See https://aka.ms/GraphPowerShell-Release.
```
2 changes: 1 addition & 1 deletion src/Devices.CloudPrint/Devices.CloudPrint/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,6 @@ directive:
### Versioning
``` yaml
module-version: 1.14.0
module-version: 1.14.1
release-notes: See https://aka.ms/GraphPowerShell-Release.
```
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,6 @@ directive:
### Versioning
``` yaml
module-version: 1.14.0
module-version: 1.14.1
release-notes: See https://aka.ms/GraphPowerShell-Release.
```
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,6 @@ directive:
### Versioning
``` yaml
module-version: 1.14.0
module-version: 1.14.1
release-notes: See https://aka.ms/GraphPowerShell-Release.
```
2 changes: 1 addition & 1 deletion src/DirectoryObjects/DirectoryObjects/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,6 @@ directive:
### Versioning
``` yaml
module-version: 1.14.0
module-version: 1.14.1
release-notes: See https://aka.ms/GraphPowerShell-Release.
```
2 changes: 1 addition & 1 deletion src/Education/Education/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,6 @@ subject-prefix: ''
### Versioning
``` yaml
module-version: 1.14.0
module-version: 1.14.1
release-notes: See https://aka.ms/GraphPowerShell-Release.
```
2 changes: 1 addition & 1 deletion src/Files/Files/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,6 @@ directive:
### Versioning
``` yaml
module-version: 1.14.0
module-version: 1.14.1
release-notes: See https://aka.ms/GraphPowerShell-Release.
```
2 changes: 1 addition & 1 deletion src/Financials/Financials/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,6 @@ subject-prefix: ''
### Versioning

``` yaml
module-version: 1.14.0
module-version: 1.14.1
release-notes: See https://aka.ms/GraphPowerShell-Release.
```
2 changes: 1 addition & 1 deletion src/Groups/Groups/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,6 @@ directive:
### Versioning
``` yaml
module-version: 1.14.0
module-version: 1.14.1
release-notes: See https://aka.ms/GraphPowerShell-Release.
```
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,6 @@ directive:
### Versioning
``` yaml
module-version: 1.14.0
module-version: 1.14.1
release-notes: See https://aka.ms/GraphPowerShell-Release.
```
2 changes: 1 addition & 1 deletion src/Identity.Governance/Identity.Governance/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,6 @@ directive:
### Versioning
``` yaml
module-version: 1.14.0
module-version: 1.14.1
release-notes: See https://aka.ms/GraphPowerShell-Release.
```
2 changes: 1 addition & 1 deletion src/Identity.SignIns/Identity.SignIns/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,6 @@ directive:
### Versioning
``` yaml
module-version: 1.14.0
module-version: 1.14.1
release-notes: See https://aka.ms/GraphPowerShell-Release.
```
2 changes: 1 addition & 1 deletion src/Mail/Mail/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,6 @@ subject-prefix: ''
### Versioning
``` yaml
module-version: 1.14.0
module-version: 1.14.1
release-notes: See https://aka.ms/GraphPowerShell-Release.
```
2 changes: 1 addition & 1 deletion src/ManagedTenants/ManagedTenants/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,6 @@ directive:
### Versioning
``` yaml
module-version: 1.14.0
module-version: 1.14.1
release-notes: See https://aka.ms/GraphPowerShell-Release.
```
2 changes: 1 addition & 1 deletion src/Notes/Notes/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,6 @@ directive:
### Versioning
``` yaml
module-version: 1.14.0
module-version: 1.14.1
release-notes: See https://aka.ms/GraphPowerShell-Release.
```
2 changes: 1 addition & 1 deletion src/People/People/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,6 @@ directive:
### Versioning
``` yaml
module-version: 1.14.0
module-version: 1.14.1
release-notes: See https://aka.ms/GraphPowerShell-Release.
```
2 changes: 1 addition & 1 deletion src/PersonalContacts/PersonalContacts/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,6 @@ subject-prefix: ''
### Versioning
``` yaml
module-version: 1.14.0
module-version: 1.14.1
release-notes: See https://aka.ms/GraphPowerShell-Release.
```
2 changes: 1 addition & 1 deletion src/Planner/Planner/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,6 @@ directive:
### Versioning
``` yaml
module-version: 1.14.0
module-version: 1.14.1
release-notes: See https://aka.ms/GraphPowerShell-Release.
```
2 changes: 1 addition & 1 deletion src/Reports/Reports/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,6 @@ directive:
### Versioning
``` yaml
module-version: 1.14.0
module-version: 1.14.1
release-notes: See https://aka.ms/GraphPowerShell-Release.
```
2 changes: 1 addition & 1 deletion src/SchemaExtensions/SchemaExtensions/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,6 @@ subject-prefix: ''
### Versioning
``` yaml
module-version: 1.14.0
module-version: 1.14.1
release-notes: See https://aka.ms/GraphPowerShell-Release.
```
2 changes: 1 addition & 1 deletion src/Search/Search/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,6 @@ subject-prefix: ''
### Versioning
``` yaml
module-version: 1.14.0
module-version: 1.14.1
release-notes: See https://aka.ms/GraphPowerShell-Release.
```
Loading

0 comments on commit a45920b

Please sign in to comment.