diff --git a/CHANGELOG.md b/CHANGELOG.md index 8672b5b..41ad754 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Changed +## [1.0.0-rc.2] - 2023-01-16 + +### Changed + +- Removed microsoft graph specific constants to make usage easier for other MIP protected APIs. + ## [1.0.0-rc.1] - 2022-12-15 ### Changed diff --git a/Microsoft.Kiota.Authentication.Azure.Tests/AzureIdentityAuthenticationProviderTests.cs b/Microsoft.Kiota.Authentication.Azure.Tests/AzureIdentityAuthenticationProviderTests.cs index a4fed65..f74835b 100644 --- a/Microsoft.Kiota.Authentication.Azure.Tests/AzureIdentityAuthenticationProviderTests.cs +++ b/Microsoft.Kiota.Authentication.Azure.Tests/AzureIdentityAuthenticationProviderTests.cs @@ -27,15 +27,18 @@ public void ConstructorThrowsArgumentNullExceptionOnNullTokenCredential() public async Task GetAuthorizationTokenAsyncGetsToken(string url, string expectedToken) { // Arrange + var uri = new Uri(url); var mockTokenCredential = new Mock(); mockTokenCredential.Setup(credential => credential.GetTokenAsync(It.IsAny(), It.IsAny())).Returns(new ValueTask(new AccessToken(expectedToken, DateTimeOffset.Now))); - var azureIdentityAuthenticationProvider = new AzureIdentityAccessTokenProvider(mockTokenCredential.Object, null); + var azureIdentityAuthenticationProvider = new AzureIdentityAccessTokenProvider(mockTokenCredential.Object); // Act - var token = await azureIdentityAuthenticationProvider.GetAuthorizationTokenAsync(new Uri(url)); + var token = await azureIdentityAuthenticationProvider.GetAuthorizationTokenAsync(uri); // Assert Assert.Equal(expectedToken, token); + mockTokenCredential.Verify(x => x.GetTokenAsync(It.Is(t => + t.Scopes.Any(s => $"{uri.Scheme}://{uri.Host}/.default".Equals(s, StringComparison.OrdinalIgnoreCase))), It.IsAny())); } [Theory] diff --git a/src/AzureIdentityAccessTokenProvider.cs b/src/AzureIdentityAccessTokenProvider.cs index 0a8e818..11aa3d2 100644 --- a/src/AzureIdentityAccessTokenProvider.cs +++ b/src/AzureIdentityAccessTokenProvider.cs @@ -19,9 +19,8 @@ namespace Microsoft.Kiota.Authentication.Azure; public class AzureIdentityAccessTokenProvider : IAccessTokenProvider, IDisposable { private readonly TokenCredential _credential; - private readonly ObservabilityOptions _obsOptions; private readonly ActivitySource _activitySource; - private readonly List _scopes; + private readonly HashSet _scopes; /// public AllowedHostsValidator AllowedHostsValidator { get; private set; } @@ -36,21 +35,14 @@ public AzureIdentityAccessTokenProvider(TokenCredential credential, string [] al { _credential = credential ?? throw new ArgumentNullException(nameof(credential)); - if(!allowedHosts?.Any() ?? true) - AllowedHostsValidator = new AllowedHostsValidator(new string[] { "graph.microsoft.com", "graph.microsoft.us", "dod-graph.microsoft.us", "graph.microsoft.de", "microsoftgraph.chinacloudapi.cn", "canary.graph.microsoft.com" }); - else - AllowedHostsValidator = new AllowedHostsValidator(allowedHosts); + AllowedHostsValidator = new AllowedHostsValidator(allowedHosts); if(scopes == null) _scopes = new(); else - _scopes = scopes.ToList(); - - if(!_scopes.Any()) - _scopes.Add("https://graph.microsoft.com/.default"); //TODO: init from the request hostname instead so it doesn't block national clouds? + _scopes = new(scopes, StringComparer.OrdinalIgnoreCase); - _obsOptions = observabilityOptions ?? new(); - _activitySource = new(_obsOptions.TracerInstrumentationName); + _activitySource = new((observabilityOptions ?? new()).TracerInstrumentationName); } private const string ClaimsKey = "claims"; @@ -81,8 +73,10 @@ public async Task GetAuthorizationTokenAsync(Uri uri, Dictionary diff --git a/src/Microsoft.Kiota.Authentication.Azure.csproj b/src/Microsoft.Kiota.Authentication.Azure.csproj index 7e4dde6..0a83c60 100644 --- a/src/Microsoft.Kiota.Authentication.Azure.csproj +++ b/src/Microsoft.Kiota.Authentication.Azure.csproj @@ -14,7 +14,7 @@ true true 1.0.0 - rc.1 + rc.2 true true @@ -23,7 +23,7 @@ false 35MSSharedLib1024.snk - - Release candidate 1 + https://github.com/microsoft/kiota-authentication-azure-dotnet/releases true LICENSE