Added a check for characters which an API key cannot contain (#1227)

This commit is contained in:
Thorsten Sommer authored and GitHub committed 2026-11-24 17:20:13 +01:00
1 parent c72a1948b3
commit e5a7f5423f
40 files changed
+1179 -71

No files matched your search

@@ -56,7 +56,7 @@ public abstract partial class ConfigurationBase : MSGComponentBase
protected bool IsDisabled => this.Disabled() || this.IsLocked();
private string Classes => $"{this.GetClassForBase} {JUSTIFIED_HELP_CLASS} {this.MarginClass}";
private string Classes => $"{this.GetClassForBase} {this.MarginClass}";
/// <summary>
/// The bottom margin of the option. Options inside settings panels need the default
@@ -66,8 +66,6 @@ public abstract partial class ConfigurationBase : MSGComponentBase
private protected virtual RenderFragment? Body => null;
private const string JUSTIFIED_HELP_CLASS = "configuration-help-justified";
private const string MARGIN_CLASS = "mb-6";
protected static readonly Dictionary<string, object?> SPELLCHECK_ATTRIBUTES = new();
@@ -9,7 +9,7 @@
@foreach (var issue in this.IssuesData)
{
<MudListItem Icon="@AppIcons.WARNING" IconColor="Color.Error">
@issue
<MudJustifiedText Typo="Typo.body1">@issue</MudJustifiedText>
</MudListItem>
}
</MudList>
@@ -7,12 +7,12 @@
}
else
{
<MudText Typo="Typo.body1" Class="mb-3">
<MudJustifiedText Typo="Typo.body1" Class="mb-3">
@TA("Your organization has set up AI providers for you. To use them, AI Studio needs your personal API key. Please enter it below.")
</MudText>
</MudJustifiedText>
}
<SecretInputField Secret="@this.apiKey" SecretChanged="@(value => this.apiKey = value)" Label="@T("API key")"/>
<SecretInputField Secret="@this.apiKey" SecretChanged="@(value => this.apiKey = value)" Label="@T("API key")" Validation="@ValidateAPIKey"/>
@if (!string.IsNullOrWhiteSpace(this.issue))
{
@@ -22,7 +22,7 @@
}
<MudStack Row="@true" AlignItems="AlignItems.Center">
<MudButton Variant="Variant.Filled" Color="Color.Primary" StartIcon="@AppIcons.SAVE" Disabled="@(this.isBusy || string.IsNullOrWhiteSpace(this.apiKey))" OnClick="@this.StoreAPIKeyAsync">
<MudButton Variant="Variant.Filled" Color="Color.Primary" StartIcon="@AppIcons.SAVE" Disabled="@(!this.CanStoreAPIKey)" OnClick="@this.StoreAPIKeyAsync">
@T("Check and save API key")
</MudButton>
@if (this.isBusy)
@@ -1,5 +1,6 @@
using AIStudio.Tools.Services;
using AIStudio.Tools.UserAPIKeys;
using AIStudio.Tools.Validation;
using Microsoft.AspNetCore.Components;
@@ -42,12 +43,20 @@ public partial class UserAPIKeyPrompt : MSGComponentBase
private string Title => string.IsNullOrWhiteSpace(this.Group.Prompt?.Title) ? TA("Your API key is missing") : this.Group.Prompt.Title;
/// <summary>
/// Whether the entered key can be checked and stored: it is there, and it can be sent.
/// </summary>
private bool CanStoreAPIKey => !this.isBusy && !string.IsNullOrWhiteSpace(this.apiKey) && !APIKeyValidation.Check(this.apiKey).IsProblem;
private static string? ValidateAPIKey(string apiKey) => APIKeyValidation.Validate(apiKey);
private async Task StoreAPIKeyAsync()
{
var enteredAPIKey = this.apiKey.Trim();
if (this.isBusy || string.IsNullOrWhiteSpace(enteredAPIKey))
if (!this.CanStoreAPIKey)
return;
var enteredAPIKey = APIKeyValidation.Normalize(this.apiKey);
this.isBusy = true;
this.issue = string.Empty;
try