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

Add managed identity option for authorization handler #30

Merged
merged 1 commit into from
Jul 18, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -125,27 +125,78 @@ protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage
return response;
}

private async Task<bool> ObtainAccessToken()
private async Task<AccessToken> ObtainAccessToken(CancellationToken cancellationToken = default)
{
bool IsSuccess = false;
string DevCenterTokenUrl = string.Format("https://login.microsoftonline.com/{0}/oauth2/token", _authCredentials.TenantId);
if (!string.IsNullOrEmpty(_authCredentials.ManagedIdentityClientId))
{
return await GetClientAssertionTokenAsync(cancellationToken);
}
else
{
return await GetTokenUsingClientSecretAsync(cancellationToken);
}
}

private async Task<AccessToken> GetClientAssertionTokenAsync(CancellationToken cancellationToken)
{
AccessToken token = default;

using (HttpClient client = new())
var clientAssertionCredential = new ClientAssertionCredential(
_authCredentials.TenantId,
_authCredentials.ClientId,
async (token) => await GetTokenUsingManagedIdentityAsync(cancellationToken)
);

token = await clientAssertionCredential.GetTokenAsync(
new TokenRequestContext(new[] { "https://manage.devcenter.microsoft.com/.default" }),
cancellationToken
);

if (!string.IsNullOrEmpty(token.Token))
{
client.Timeout = _httpTimeout;
Uri restApi = new(DevCenterTokenUrl);
_accessToken = token.Token;
}

ClientSecretCredential credential = new(_authCredentials.TenantId, _authCredentials.ClientId, _authCredentials.Key);
AccessToken token = await credential.GetTokenAsync(new TokenRequestContext(scopes: new string[] { "https://manage.devcenter.microsoft.com/.default" }));
return token;
}

if (string.IsNullOrEmpty(token.Token) == false)
{
_accessToken = token.Token;
IsSuccess = true;
}
/// <summary>
/// Callback function for <see cref="GetClientAssertionTokenAsync"/>
/// </summary>
/// <param name="cancellationToken"></param>
/// <returns></returns>
private async Task<string> GetTokenUsingManagedIdentityAsync(CancellationToken cancellationToken)
{
var credential = new ManagedIdentityCredential(_authCredentials.ManagedIdentityClientId);

var token = await credential.GetTokenAsync(
new TokenRequestContext(new[] { _authCredentials.Scope }),
cancellationToken
);

return token.Token;
}

private async Task<AccessToken> GetTokenUsingClientSecretAsync(CancellationToken cancellationToken)
{

var credential = new ClientSecretCredential(
_authCredentials.TenantId,
_authCredentials.ClientId,
_authCredentials.Key
);

var token = await credential.GetTokenAsync(
new TokenRequestContext(new[] { "https://manage.devcenter.microsoft.com/.default" }),
cancellationToken
);

if (!string.IsNullOrEmpty(token.Token))
{
_accessToken = token.Token;
}

return IsSuccess;
return token;
}

//
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*++
Copyright (c) Microsoft Corporation. All rights reserved.

Licensed under the MIT license. See LICENSE file in the project root for full license information.
Licensed under the MIT license. See LICENSE file in the project root for full license information.
--*/

using System.Text.Json.Serialization;
Expand All @@ -24,4 +24,10 @@ public class AuthorizationHandlerCredentials

[JsonPropertyName("urlPrefix")]
public System.Uri UrlPrefix { get; set; }

[JsonPropertyName("managedIdentityClientId")]
public string ManagedIdentityClientId { get; set; }

[JsonPropertyName("scope")]
public string Scope { get; set; }
}
Loading