@page "/api-keys" @attribute [Authorize] @inherits AuthComponentBase @inject IDataService DataService @inject AuthenticationStateProvider AuthProvider @inject IJsInterop JsInterop @inject IModalService ModalService

API Keys

@if (!string.IsNullOrWhiteSpace(_alertMessage)) { } @if (!string.IsNullOrWhiteSpace(_newKeySecret)) {
Warning: The key's secret will only be shown once. Save it now.
} @if (User.IsAdministrator) {
@foreach (var apiToken in _apiTokens) { }
Name ID Last Used
@apiToken.Name @apiToken.ID @apiToken.LastUsed
} else {
Only organization administrators can view this page.
} @code { private readonly List _apiTokens = new(); private string _alertMessage; private string _createKeyName; private string _newKeySecret; protected override async Task OnInitializedAsync() { RefreshData(); await base.OnInitializedAsync(); } private async Task CreateNewKey() { var secret = PasswordGenerator.GeneratePassword(36); var secretHash = new PasswordHasher().HashPassword(null, secret); await DataService.CreateApiToken(Username, _createKeyName, secret); RefreshData(); _alertMessage = "Key created."; _newKeySecret = secret; } private async Task DeleteKey(string keyId) { var result = await JsInterop.Confirm("Are you sure you want to delete this key?"); if (result) { await DataService.DeleteApiToken(Username, keyId); RefreshData(); _alertMessage = "Key deleted."; } } private void RefreshData() { _apiTokens.Clear(); _apiTokens.AddRange(DataService.GetAllApiTokens(User.Id)); _createKeyName = null; _alertMessage = null; _newKeySecret = null; } private async Task RenameKey(string keyId) { var newName = await JsInterop.Prompt("New key name"); if (!string.IsNullOrWhiteSpace(newName)) { await DataService.RenameApiToken(Username, keyId, newName); RefreshData(); _alertMessage = "Key renamed."; } } private void ShowApiKeyHelp() { ModalService.ShowModal("Using API Keys", new[] { "API keys should be added to the request header when making API calls. The key should be \"Authorization\", and value should be \"{key-id}:{key-secret}\". Note the colon in between.", "Example: Authorization=e5da1c09-e851-4bd4-a8c1-532144b3f894:7uY6h5zBYm4+90pZVek4lD6ewbQ83nKcDpghBfG00hhZu6Ew" }); } }