aboutsummaryrefslogtreecommitdiff
path: root/ShareGuard.Web.Client/Pages/Redeem.razor
blob: d6abc959b28202bf410517731aee4a4bfaeae021 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
@page "/redeem/{Token}/"
@using System.Net
@rendermode @(new InteractiveWebAssemblyRenderMode(false))
@inject IJSRuntime Js
@inject NavigationManager navigation

@if (_responseCode == null)
{
    <p>Loading...</p>
}
else if (_responseCode == HttpStatusCode.Forbidden)
{
    <b>Invalid invitation!</b>
}
else if (_responseCode == HttpStatusCode.NoContent)
{
    <b>No template providen by admin.</b>
}
else if (_responseCode == HttpStatusCode.BadRequest)
{
    <b>An internal error occured.</b>
}
else
{
    @foreach (var configLine in _config!.Split("\n"))
    {
        <p>@configLine</p>
    }
}

@code {
    private readonly HttpClient _httpClient = new();
    [Parameter] public string Token { get; set; }
    private HttpStatusCode? _responseCode;
    private string? _config;

    protected override async Task OnInitializedAsync()
    {
        var keypair = await Js.InvokeAsync<Dictionary<string, string>>("wireguard.generateKeypair");
        var requestBody = new Dictionary<string, string>
        {
            { "PublicKey", keypair["publicKey"] }
        };
        var response = await _httpClient.PostAsync($"{navigation.BaseUri}api/redeem/{Token}/", new FormUrlEncodedContent(requestBody));
        _responseCode = response.StatusCode;
        var template = await response.Content.ReadAsStringAsync();

        if (_responseCode == HttpStatusCode.OK)
            _config = template.Replace("{publicKey}", keypair["publicKey"])
                .Replace("{privateKey}", keypair["privateKey"]);
    }

}