Skip to content

Commit

Permalink
add proxy support
Browse files Browse the repository at this point in the history
  • Loading branch information
erlendoksvoll committed Jan 15, 2025
1 parent f280d53 commit 70877f4
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
9 changes: 8 additions & 1 deletion src/Altinn.Dan.Plugin.Pensjon/Config/ApplicationSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ public class ApplicationSettings
public string KeyVaultName { get; set; }
public string CertificateName { get; set; }

//for production we need to use proxy
public bool UseProxy { get; set; }

public string ProxyUrl { get; set; }

public string CustomCertificateHeaderName { get; set; }

public X509Certificate2 Certificate
{
get
Expand All @@ -23,7 +30,7 @@ public X509Certificate2 Certificate
var secretClient = new SecretClient(new Uri($"https://{KeyVaultName}.vault.azure.net/"),
new DefaultAzureCredential());
var certWithPrivateKey = secretClient.GetSecret(CertificateName).Value;
_cert = new X509Certificate2(Convert.FromBase64String(certWithPrivateKey.Value), string.Empty, X509KeyStorageFlags.MachineKeySet);
_cert = new X509Certificate2(Convert.FromBase64String(certWithPrivateKey.Value), string.Empty, X509KeyStorageFlags.Exportable);
}
return _cert;
}
Expand Down
15 changes: 14 additions & 1 deletion src/Altinn.Dan.Plugin.Pensjon/Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
using Dan.Common.Models;
using Dan.Common.Util;
using System;
using System.Security.Policy;
using System.Runtime.ConstrainedExecution;
using System.Security.Cryptography.X509Certificates;
using System.Linq;

namespace Altinn.Dan.Plugin.Pensjon
{
Expand All @@ -26,6 +30,8 @@ public class Main
private readonly HttpClient _client;
private readonly ApplicationSettings _settings;

private const string CertificateHeaderName = "x-nadobe-cert";

public Main(IHttpClientFactory httpClientFactory, IOptions<ApplicationSettings> settings)
{
_client = httpClientFactory.CreateClient("ECHttpClient");
Expand Down Expand Up @@ -63,10 +69,17 @@ private async Task<PensionModel> MakeRequest(string target, Party subject)
{
fodselsnummer = subject.NorwegianSocialSecurityNumber
};

try
{
var request = new HttpRequestMessage(HttpMethod.Post, target);

if (_settings.UseProxy)
{
request.RequestUri = new Uri(string.Format(_settings.ProxyUrl, Uri.EscapeDataString(target.Replace("https://", "").Replace("http://", ""))));
request.Headers.TryAddWithoutValidation(CertificateHeaderName, Convert.ToBase64String(_settings.Certificate.Export(X509ContentType.Pkcs12)));
}

request.Content = new StringContent(JsonConvert.SerializeObject(requestBody), Encoding.UTF8, "application/json");
result = await _client.SendAsync(request);
switch (result.StatusCode)
Expand Down
3 changes: 2 additions & 1 deletion src/Altinn.Dan.Plugin.Pensjon/local.settings.json.template
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"BreakerRetryWaitTime": "",
"Certificate": "",
"KeyVaultName": "",
"CertificateName": ""
"CertificateName": "",
"CustomCertificateHeaderName": ""
}
}

0 comments on commit 70877f4

Please sign in to comment.