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
Changes from 3 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
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