diff --git a/JsonPayloads/AuthAPIKeyPayload.cs b/JsonPayloads/AuthAPIKeyPayload.cs new file mode 100644 index 0000000000000000000000000000000000000000..d90626fdd767caf957b56cfce01c6756a694cddb --- /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 851330f3477d2454f1e7d3661009c5952bc2f002..164aa68925105719f0cb7e60063213a15d167182 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 cc844be10d0df9513201cb593bba69e8a55eb803..8ec78e478fd0b873570ef5500b939b30ee84e297 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)