Skip to content

Commit

Permalink
add source certificat renewal
Browse files Browse the repository at this point in the history
  • Loading branch information
gballigand committed Nov 9, 2023
1 parent 2ea5e2e commit 59ce6b5
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
1 change: 1 addition & 0 deletions Foundation.SourceClients/FoundationRoutes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ namespace Foundation.SourceClients.Services
public static class FoundationRoutes
{
public const string ACCOUNT_PATH = "/api/v1/source-accounts";
public const string RENEW_PATH = "/api/v1/source-accounts/renew";
public const string LOGIN_PATH = "/api/v1/source-accounts/login";
public const string SOURCE_PATH = "/api/sources/v1/sources";
}
Expand Down
22 changes: 20 additions & 2 deletions Foundation.SourceClients/Services/FoundationAccountClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ public async Task<X509Certificate2> Create(string token, string machineId, Cance
token, _client.BaseAddress + ACCOUNT_PATH
);

var response = await _client.PostAsJsonAsync(ACCOUNT_PATH, new { Token = token, MachineId = machineId });
var response = await _client.PostAsJsonAsync(ACCOUNT_PATH, new { Token = token, MachineId = machineId }, cancellationToken: ct);
response.EnsureSuccessStatusCode();

var payload = await response.Content.ReadFromJsonAsync<CredentialPayload>();
var payload = await response.Content.ReadFromJsonAsync<CredentialPayload>(cancellationToken: ct);
var secret = Convert.FromBase64String(payload.Secret);

var certificate = new X509Certificate2(secret);
Expand Down Expand Up @@ -135,6 +135,24 @@ private async Task<string> ReceiveBearerToken(ClientWebSocket client, Memory<byt

return token;
}

public async Task<X509Certificate2> RenewCertificate(CancellationToken ct)
{
_logger.LogInformation(
"Renewing credential on {uri}",
_client.BaseAddress + RENEW_PATH
);

var response = await _client.PostAsJsonAsync(RENEW_PATH, ct);
response.EnsureSuccessStatusCode();

var payload = await response.Content.ReadFromJsonAsync<CredentialPayload>(cancellationToken: ct);
var secret = Convert.FromBase64String(payload.Secret);

var certificate = new X509Certificate2(secret);

return certificate;
}
}

public class CredentialPayload
Expand Down

0 comments on commit 59ce6b5

Please sign in to comment.