Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix unwanted scopes collection modification in AzureIdentityAccessTokenProvider #95

Merged
merged 4 commits into from
Jun 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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