Skip to content

Commit

Permalink
add managed identity option for authorization
Browse files Browse the repository at this point in the history
  • Loading branch information
Ben-Carpenter committed Jul 17, 2024
1 parent 6a73f1d commit 0eb1e67
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 15 deletions.
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; }
}

0 comments on commit 0eb1e67

Please sign in to comment.