1
+ using Microsoft . Azure . KeyVault ;
2
+ using Microsoft . Azure . KeyVault . Models ;
3
+ using Microsoft . Azure . Services . AppAuthentication ;
1
4
using System . Net . Http ;
5
+ using System . Net . Http . Headers ;
2
6
using System . Text ;
3
7
using System . Threading . Tasks ;
4
8
@@ -18,8 +22,32 @@ public QbQuestionsClient() : base()
18
22
/// <param name="content">The questions to add in the form of a JSON array</param>
19
23
public async Task < HttpResponseMessage > AddQuestionsToDbAsync ( string content )
20
24
{
25
+ HttpResponseMessage authorizationResponse = await GetAuthorization ( ) ;
26
+ string token = await authorizationResponse . Content . ReadAsStringAsync ( ) ;
27
+ DefaultRequestHeaders . Authorization = new AuthenticationHeaderValue ( "Bearer" , token ) ;
21
28
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 ;
23
51
}
24
52
}
25
53
}
0 commit comments