-
-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Using Bearer Token for Kudu REST API calls (#444)
* Using Bearer Token for Kudu REST API calls * Complete prefer use Bearer Token
- Loading branch information
Showing
3 changed files
with
41 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,36 @@ | ||
using System; | ||
using System.Net.Http; | ||
using System.Net.Http.Headers; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
using Azure.Core; | ||
|
||
namespace AppService.Acmebot.Internal; | ||
|
||
public class KuduClientFactory | ||
{ | ||
public KuduClientFactory(IHttpClientFactory httpClientFactory) | ||
public KuduClientFactory(IHttpClientFactory httpClientFactory, TokenCredential tokenCredential, AzureEnvironment environment) | ||
{ | ||
_httpClientFactory = httpClientFactory; | ||
_tokenCredential = tokenCredential; | ||
_environment = environment; | ||
} | ||
|
||
private readonly IHttpClientFactory _httpClientFactory; | ||
private readonly TokenCredential _tokenCredential; | ||
private readonly AzureEnvironment _environment; | ||
|
||
public KuduClient CreateClient(Uri scmUri) | ||
public async Task<KuduClient> CreateClientAsync(string scmHost) | ||
{ | ||
var httpClient = _httpClientFactory.CreateClient(); | ||
|
||
return new KuduClient(httpClient, scmUri); | ||
var context = new TokenRequestContext(new[] { _environment.ResourceManager.DefaultScope }, null); | ||
var accessToken = await _tokenCredential.GetTokenAsync(context, CancellationToken.None); | ||
|
||
httpClient.BaseAddress = new Uri($"https://{scmHost}"); | ||
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken.Token); | ||
|
||
return new KuduClient(httpClient); | ||
} | ||
} |