Skip to content

Commit ad8d54f

Browse files
committed
Fix API client in QbPackParser
1 parent 0ab8599 commit ad8d54f

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

QbPackParser/APIClient/QbQuestionsClient.cs

+29-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1+
using Microsoft.Azure.KeyVault;
2+
using Microsoft.Azure.KeyVault.Models;
3+
using Microsoft.Azure.Services.AppAuthentication;
14
using System.Net.Http;
5+
using System.Net.Http.Headers;
26
using System.Text;
37
using System.Threading.Tasks;
48

@@ -18,8 +22,32 @@ public QbQuestionsClient() : base()
1822
/// <param name="content">The questions to add in the form of a JSON array</param>
1923
public async Task<HttpResponseMessage> AddQuestionsToDbAsync(string content)
2024
{
25+
HttpResponseMessage authorizationResponse = await GetAuthorization();
26+
string token = await authorizationResponse.Content.ReadAsStringAsync();
27+
DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
2128
StringContent stringContent = new StringContent(content, Encoding.UTF8, "application/json");
22-
return await PostAsync("qbquestions", stringContent);
29+
return await PostAsync("api/qbquestions", stringContent);
30+
}
31+
32+
// TODO: Refactor to use refresh token
33+
/// <summary>Gets the authorization information.</summary>
34+
private async Task<HttpResponseMessage> GetAuthorization()
35+
{
36+
string username = await GetKeyVaultSecret("https://extriviaganza-vault.vault.azure.net/secrets/QbQuestionsUsername");
37+
string password = await GetKeyVaultSecret("https://extriviaganza-vault.vault.azure.net/secrets/QbQuestionsPassword");
38+
string content = "{\"username\":\"" + username + "\",\"password\":\"" + password + "\"}";
39+
StringContent stringContent = new StringContent(content, Encoding.UTF8, "application/json");
40+
return await PostAsync("api/authenticate", stringContent);
41+
}
42+
43+
/// <summary>Retrieves a secret from Azure Key Vault</summary>
44+
/// <param name="secretId">The secret's identifier in Azure Key Vault</param>
45+
private async Task<string> GetKeyVaultSecret(string secretId)
46+
{
47+
AzureServiceTokenProvider azureServiceTokenProvider = new AzureServiceTokenProvider();
48+
KeyVaultClient keyVaultClient = new KeyVaultClient(new KeyVaultClient.AuthenticationCallback(azureServiceTokenProvider.KeyVaultTokenCallback));
49+
SecretBundle secret = await keyVaultClient.GetSecretAsync(secretId).ConfigureAwait(false);
50+
return secret.Value;
2351
}
2452
}
2553
}

QbPackParser/QbPackParser.csproj

+2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
</PropertyGroup>
77

88
<ItemGroup>
9+
<PackageReference Include="Microsoft.Azure.KeyVault" Version="3.0.4" />
10+
<PackageReference Include="Microsoft.Azure.Services.AppAuthentication" Version="1.3.1" />
911
<PackageReference Include="Newtonsoft.Json" Version="12.0.2" />
1012
</ItemGroup>
1113

0 commit comments

Comments
 (0)