From f905717a99941d2b81259b5f3cb2feccfe5b1243 Mon Sep 17 00:00:00 2001 From: Tom Sakks Date: Fri, 3 Sep 2021 13:04:22 -0500 Subject: [PATCH] Add mechanism of adding/deleting API keys from the settings page --- JsonPayloads/AuthAPIKeyPayload.cs | 26 +++++++++ Pages/Settings.razor | 45 +++++++++++++++ Pages/Settings.razor.cs | 95 +++++++++++++++++++++++++++++++ 3 files changed, 166 insertions(+) create mode 100644 JsonPayloads/AuthAPIKeyPayload.cs diff --git a/JsonPayloads/AuthAPIKeyPayload.cs b/JsonPayloads/AuthAPIKeyPayload.cs new file mode 100644 index 0000000..d90626f --- /dev/null +++ b/JsonPayloads/AuthAPIKeyPayload.cs @@ -0,0 +1,26 @@ +using System; + +namespace FIOWeb.JsonPayloads +{ + public class JsonAuthCreateAPIKeyPayload + { + public string UserName { get; set; } + public string Password { get; set; } + public string Application { get; set; } + } + + public class JsonAuthDeleteAPIKeyPayload + { + public string UserName { get; set; } + public string Password { get; set; } + public string ApiKeyToRevoke { get; set; } + } + + public class JsonAuthAPIKeyPayload + { + public string AuthAPIKey { get; set; } + public string Application { get; set; } + public DateTime LastAccessTime { get; set; } + } + +} \ No newline at end of file diff --git a/Pages/Settings.razor b/Pages/Settings.razor index 851330f..164aa68 100644 --- a/Pages/Settings.razor +++ b/Pages/Settings.razor @@ -78,6 +78,51 @@ else Change Password


+

API Keys

+ Create API Key + @if (createAPIKeyDialogVisible) + { + +
+ + +
+
+ } + + @if (deleteAPIKeyDialogVisible) + { + +
+ +
+
+ } + + + + + + + + + + +
+

Permissions

Add users so they can view your game data. Some pages require multiple permissions to be provided before they function.
diff --git a/Pages/Settings.razor.cs b/Pages/Settings.razor.cs index cc844be..8ec78e4 100644 --- a/Pages/Settings.razor.cs +++ b/Pages/Settings.razor.cs @@ -1,3 +1,4 @@ +using System; using System.Collections.Generic; using System.Linq; using System.Net; @@ -10,6 +11,7 @@ using Newtonsoft.Json; using FIOWeb.JsonPayloads; + namespace FIOWeb.Pages { public partial class Settings @@ -17,6 +19,9 @@ namespace FIOWeb.Pages private ITable permissionTable; private List permissions = null; + private ITable apiKeyTable; + private List apiKeys = null; + private bool UserDrawerVisible = false; private IEnumerable SelectedUsers; private List AllUsers = null; @@ -59,6 +64,15 @@ namespace FIOWeb.Pages var permissionRequest = new Web.Request(HttpMethod.Get, "/auth/permissions", await GlobalAppState.GetAuthToken()); permissions = await permissionRequest.GetResponseAsync>(); + + var apiKeysRequest = new Web.Request(HttpMethod.Get, "/auth/listapikeys", await GlobalAppState.GetAuthToken()); + apiKeys = await apiKeysRequest.GetResponseAsync>(); + } + + private async Task RefreshAPIKeys() + { + var apiKeysRequest = new Web.Request(HttpMethod.Get, "/auth/listapikeys", await GlobalAppState.GetAuthToken()); + apiKeys = await apiKeysRequest.GetResponseAsync>(); } public void Dispose() @@ -80,6 +94,87 @@ namespace FIOWeb.Pages permissions.RemoveAll(p => p.UserName == UserName); } + private string apiKeyApplication = null; + private string apiKeyGuid = null; + private string apiKeyPassword = null; + + private bool createAPIKeyDialogVisible = false; + private async Task CreateAPIKeyOnOk() + { + JsonAuthCreateAPIKeyPayload createPayload = new JsonAuthCreateAPIKeyPayload + { + UserName = await GlobalAppState.GetUserName(), + Password = apiKeyPassword, + Application = apiKeyApplication + }; + var createAPIKeyRequest = new Web.Request(HttpMethod.Post, "/auth/createapikey", await GlobalAppState.GetAuthToken(), JsonConvert.SerializeObject(createPayload)); + await createAPIKeyRequest.GetResultNoResponse(); + if (createAPIKeyRequest.StatusCode == HttpStatusCode.OK) + { + Toaster.Add("API Key Created", MatToastType.Success, "Success"); + await RefreshAPIKeys(); + StateHasChanged(); + } + else if (createAPIKeyRequest.StatusCode == HttpStatusCode.Unauthorized) + { + Toaster.Add("Failed to authenticate", MatToastType.Danger, "Login failure"); + } + else if (createAPIKeyRequest.StatusCode == HttpStatusCode.NotAcceptable) + { + Toaster.Add("Too many API keys", MatToastType.Danger, "Limit 20"); + } + else + { + Toaster.Add("Unknown error occurred", MatToastType.Danger, "???"); + } + + createAPIKeyDialogVisible = false; + await Task.Delay(500); + } + + private async Task CreateAPIKeyOnCancel() + { + createAPIKeyDialogVisible = false; + await Task.Delay(500); + } + + private bool deleteAPIKeyDialogVisible = false; + private async Task DeleteAPIKeyOnOk() + { + var deletePayload = new JsonAuthDeleteAPIKeyPayload + { + UserName = await GlobalAppState.GetUserName(), + Password = apiKeyPassword, + ApiKeyToRevoke = apiKeyGuid + }; + + var deleteApiKey = new Web.Request(HttpMethod.Post, "/auth/revokeapikey", await GlobalAppState.GetAuthToken(), JsonConvert.SerializeObject(deletePayload)); + await deleteApiKey.GetResultNoResponse(); + if (deleteApiKey.StatusCode == HttpStatusCode.OK) + { + Toaster.Add("API Key Deleted", MatToastType.Success, "Success"); + apiKeys.RemoveAll(ak => ak.AuthAPIKey == apiKeyGuid); + StateHasChanged(); + } + else if (deleteApiKey.StatusCode == HttpStatusCode.Unauthorized) + { + Toaster.Add("Failed to authenticate", MatToastType.Danger, "Login failure"); + } + else + { + Toaster.Add("Unknown error occurred", MatToastType.Danger, "???"); + } + + deleteAPIKeyDialogVisible = false; + await Task.Delay(500); + } + + private async Task DeleteAPIKeyOnCancel() + { + deleteAPIKeyDialogVisible = false; + await Task.Delay(500); + } + private void OnUserSelectionClosed() { if (SelectedUsers != null) -- GitLab