Skip to content
This repository has been archived by the owner on Jul 9, 2024. It is now read-only.

Commit

Permalink
Merge pull request #95 from schuettecarsten/patch-1
Browse files Browse the repository at this point in the history
Fix unwanted scopes collection modification in AzureIdentityAccessTokenProvider
  • Loading branch information
andrueastman authored Jun 26, 2023
2 parents ed4e292 + 7fa09f6 commit 53dc1df
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 11 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added

## [1.0.3] - 2023-06-26

### Changed

- Fix unwanted scopes collection modification in AzureIdentityAccessTokenProvider ([#73]([https://github.com/microsoft/kiota-authentication-azure-dotnet/issues/93])).
- Add missing ConfigureAwait(false) to GetTokenAsync call.
- Replaced true/false values in SetTag method calls with pre-initialized values to prevent boxing.

## [1.0.2] - 2023-03-24

### Changed
Expand Down
27 changes: 17 additions & 10 deletions src/AzureIdentityAccessTokenProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ namespace Microsoft.Kiota.Authentication.Azure;
/// </summary>
public class AzureIdentityAccessTokenProvider : IAccessTokenProvider, IDisposable
{
private static readonly object BoxedTrue = true;
private static readonly object BoxedFalse = false;

private readonly TokenCredential _credential;
private readonly ActivitySource _activitySource;
private readonly HashSet<string> _scopes;
Expand Down Expand Up @@ -52,33 +55,37 @@ public async Task<string> GetAuthorizationTokenAsync(Uri uri, Dictionary<string,
{
using var span = _activitySource?.StartActivity(nameof(GetAuthorizationTokenAsync));
if(!AllowedHostsValidator.IsUrlHostValid(uri)) {
span?.SetTag("com.microsoft.kiota.authentication.is_url_valid", false);
span?.SetTag("com.microsoft.kiota.authentication.is_url_valid", BoxedFalse);
return string.Empty;
}

if(!uri.Scheme.Equals("https", StringComparison.OrdinalIgnoreCase)) {
span?.SetTag("com.microsoft.kiota.authentication.is_url_valid", false);
span?.SetTag("com.microsoft.kiota.authentication.is_url_valid", BoxedFalse);
throw new ArgumentException("Only https is supported");
}

span?.SetTag("com.microsoft.kiota.authentication.is_url_valid", true);
span?.SetTag("com.microsoft.kiota.authentication.is_url_valid", BoxedTrue);

string? decodedClaim = null;
if (additionalAuthenticationContext is not null &&
additionalAuthenticationContext.ContainsKey(ClaimsKey) &&
additionalAuthenticationContext[ClaimsKey] is string claims) {
span?.SetTag("com.microsoft.kiota.authentication.additional_claims_provided", true);
span?.SetTag("com.microsoft.kiota.authentication.additional_claims_provided", BoxedTrue);
var decodedBase64Bytes = Convert.FromBase64String(claims);
decodedClaim = Encoding.UTF8.GetString(decodedBase64Bytes);
} else
span?.SetTag("com.microsoft.kiota.authentication.additional_claims_provided", false);
span?.SetTag("com.microsoft.kiota.authentication.additional_claims_provided", BoxedFalse);

if(!_scopes.Any())
_scopes.Add($"{uri.Scheme}://{uri.Host}/.default");
span?.SetTag("com.microsoft.kiota.authentication.scopes", string.Join(",", _scopes));
var result = await this._credential.GetTokenAsync(new TokenRequestContext(_scopes.ToArray(), claims: decodedClaim), cancellationToken);
string[] scopes;
if (_scopes.Any()) {
scopes = _scopes.ToArray();
} else
scopes = new string[] { $"{uri.Scheme}://{uri.Host}/.default" };
span?.SetTag("com.microsoft.kiota.authentication.scopes", string.Join(",", scopes));

var result = await this._credential.GetTokenAsync(new TokenRequestContext(scopes, claims: decodedClaim), cancellationToken).ConfigureAwait(false);
return result.Token;
}

/// <inheritdoc/>
public void Dispose()
{
Expand Down
2 changes: 1 addition & 1 deletion src/Microsoft.Kiota.Authentication.Azure.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<PackageProjectUrl>https://microsoft.github.io/kiota/</PackageProjectUrl>
<EmbedUntrackedSources>true</EmbedUntrackedSources>
<Deterministic>true</Deterministic>
<VersionPrefix>1.0.2</VersionPrefix>
<VersionPrefix>1.0.3</VersionPrefix>
<VersionSuffix></VersionSuffix>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
Expand Down

0 comments on commit 53dc1df

Please sign in to comment.