Skip to content

Commit

Permalink
Add support for API Keys
Browse files Browse the repository at this point in the history
  • Loading branch information
mythz committed Jun 4, 2024
1 parent 1081091 commit e6519cc
Show file tree
Hide file tree
Showing 5 changed files with 685 additions and 0 deletions.
14 changes: 14 additions & 0 deletions MyApp/Components/Account/Pages/Manage/ApiKeys.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
@page "/Account/Manage/ApiKeys"

<PageTitle>Manage API Keys</PageTitle>

<div>
<Heading3>Manage API Keys</Heading3>
<div data-component="pages/Account/Manage/ManageUserApiKeys.mjs" data-props="@ToProps()"></div>
</div>

@code {
public MarkupString ToProps() => BlazorHtml.RawJson(new {
info = HostContext.AppHost.GetPlugin<ApiKeysFeature>()?.GetApiKeyInfo(),
});
}
7 changes: 7 additions & 0 deletions MyApp/Components/Account/Shared/ManageNavMenu.razor
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@
<NavLink class="@ManageNavPages.PersonalDataNavClass(NavigationManager)"
id="personal-data" href="@ManageNavPages.PersonalData">Personal data</NavLink>
</li>
@if (HostContext.AppHost.HasPlugin<ApiKeysFeature>())
{
<li>
<NavLink class="@ManageNavPages.PageNavClass(NavigationManager, "Account/Manage/ApiKeys")"
id="api-keys" href="Account/Manage/ApiKeys">API Keys</NavLink>
</li>
}
</ul>

@code {
Expand Down
60 changes: 60 additions & 0 deletions MyApp/Configure.ApiKeys.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
using MyApp.Data;
using ServiceStack;
using ServiceStack.Data;
using ServiceStack.OrmLite;
using ServiceStack.Configuration;

[assembly: HostingStartup(typeof(MyApp.ConfigureApiKeys))]

namespace MyApp;

public class ConfigureApiKeys : IHostingStartup
{
public void Configure(IWebHostBuilder builder) => builder
.ConfigureServices(services =>
{
services.AddPlugin(new ApiKeysFeature
{
// Optional: Available Scopes Admin Users can assign to any API Key
// Features = [
// "Paid",
// "Tracking",
// ],
// Optional: Available Features Admin Users can assign to any API Key
// Scopes = [
// "todo:read",
// "todo:write",
// ],
// Optional: Limit available Scopes Users can assign to their own API Keys
// UserScopes = [
// "todo:read",
// ],
// Optional: Limit available Features Users can assign to their own API Keys
// UserFeatures = [
// "Tracking",
// ],
});
})
.ConfigureAppHost(appHost =>
{
using var db = appHost.Resolve<IDbConnectionFactory>().Open();
var apiKeysFeature = appHost.GetPlugin<ApiKeysFeature>();
apiKeysFeature.InitSchema(db);
// Optional: Create API Key for specified Users on Startup
if (apiKeysFeature.ApiKeyCount(db) == 0)
{
var createApiKeysFor = new [] { "admin@email.com", "manager@email.com" };
var users = db.Select<ApplicationUser>(x => createApiKeysFor.Contains(x.UserName));
foreach (var user in users)
{
List<string> scopes = user.UserName == "admin@email.com"
? [RoleNames.Admin]
: [];
apiKeysFeature.Insert(db,
new() { Name = "Seed API Key", UserId = user.Id, UserName = user.UserName, Scopes = scopes });
}
}
});
}
Loading

0 comments on commit e6519cc

Please sign in to comment.