refactoring and testing OpenVpnCertificatesServer with token

This commit is contained in:
Kim 2023-03-23 17:17:10 +01:00
parent f62375ae54
commit b8da0c379d
3 changed files with 30 additions and 17 deletions

View File

@ -0,0 +1,12 @@
namespace InnovEnergy.App.OpenVpnCertificatesServer;
internal class AccToken
{
public String token { get; init; }
public String bearer { get; init; }
public UInt64 idUser { get; init; }
public String verification_mode { get; init; }
public String idAccessToken { get; init; }
public Boolean verification_sent { get; init; }
public Boolean success { get; init; }
}

View File

@ -12,5 +12,11 @@
<ProjectReference Include="../../Lib/Victron/VictronVRM/VictronVRM.csproj" /> <ProjectReference Include="../../Lib/Victron/VictronVRM/VictronVRM.csproj" />
<ProjectReference Include="../../Lib/Utils/Utils.csproj" /> <ProjectReference Include="../../Lib/Utils/Utils.csproj" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<None Update="token.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project> </Project>

View File

@ -32,8 +32,14 @@ public static class Program
private const String CcdDir = VpnDir + "/ccd"; private const String CcdDir = VpnDir + "/ccd";
private const String CertsDir = VpnDir + "/certs"; private const String CertsDir = VpnDir + "/certs";
private const String Endpoint = "http://localhost:4000/"; private const String Endpoint = "http://localhost:4000/";
private static readonly AccToken AccToken = JsonSerializer.Deserialize<AccToken>(File.OpenRead("./token.json"))!; //"d4179e69413ad8c507e0965a55bb90fe712184af9c81c196b9d19db5bb083d5f"; private static AccToken ReadAccessToken()
{
var content = File.ReadAllText("./token.json");
return JsonSerializer.Deserialize<AccToken>(content)!;
}
private const String ManualPw = "MwBRbQb3QaX7l9XIaakq"; private const String ManualPw = "MwBRbQb3QaX7l9XIaakq";
private const String QueryPath = "/get_cert"; private const String QueryPath = "/get_cert";
@ -136,8 +142,8 @@ public static class Program
Console.WriteLine($"looking up unique id {uniqueId} on VRM"); Console.WriteLine($"looking up unique id {uniqueId} on VRM");
//var installationName = await LookupInstallationNameByUniqueId(ccdName); //var installationName = await LookupInstallationNameByUniqueId(ccdName);
var token = ReadAccessToken();
using var vrm = VrmAccount.Token(AccToken.idUser, AccToken.token); using var vrm = VrmAccount.Token(token.idUser, token.token);
var installations = await vrm.GetInstallations(); var installations = await vrm.GetInstallations();
var installationName = installations var installationName = installations
@ -153,8 +159,8 @@ public static class Program
private static async Task<(String ccdName, String humanReadableName)?> LookupInstallationNameByMachineSerial(String ccdName) private static async Task<(String ccdName, String humanReadableName)?> LookupInstallationNameByMachineSerial(String ccdName)
{ {
Console.WriteLine($"looking up {ccdName} on VRM"); Console.WriteLine($"looking up {ccdName} on VRM");
var token = ReadAccessToken();
using var vrm = VrmAccount.Token(AccToken.idUser, AccToken.token); using var vrm = VrmAccount.Token(token.idUser, token.token);
var installations = await vrm.GetInstallations(); var installations = await vrm.GetInstallations();
foreach (var installation in installations) foreach (var installation in installations)
@ -268,15 +274,4 @@ public static class Program
return $"{VpnSubnet}{hi}.{lo}"; return $"{VpnSubnet}{hi}.{lo}";
} }
}
internal interface AccToken
{
public String token { get; init; }
public String bearer { get; init; }
public UInt64 idUser { get; init; }
public String verification_mode { get; init; }
public String idAccessToken { get; init; }
public Boolean verification_sent { get; init; }
public Boolean success { get; init; }
} }