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 3, 2024
1 parent e5af02d commit cf02421
Show file tree
Hide file tree
Showing 7 changed files with 688 additions and 0 deletions.
2 changes: 2 additions & 0 deletions MyApp.ServiceInterface/Data/ApplicationUser.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using Microsoft.AspNetCore.Identity;
using ServiceStack.DataAnnotations;

namespace MyApp.Data;

// Add profile data for application users by adding properties to the ApplicationUser class
[Alias("AspNetUsers")]
public class ApplicationUser : IdentityUser
{
public string? FirstName { get; set; }
Expand Down
17 changes: 17 additions & 0 deletions MyApp/Components/Account/Pages/Manage/ApiKeys.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
@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(),
});

[CascadingParameter]
private HttpContext HttpContext { get; set; } = default!;
}
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
53 changes: 53 additions & 0 deletions MyApp/Configure.ApiKeys.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using MyApp.Data;
using ServiceStack.Configuration;
using ServiceStack.Data;
using ServiceStack.OrmLite;

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

namespace MyApp;

public class ConfigureApiKeys : IHostingStartup
{
public void Configure(IWebHostBuilder builder) => builder
.ConfigureServices(services =>
{
services.AddPlugin(new ApiKeysFeature
{
Features = [
"Tracking"
],

// Optional: Limit scope of API Key access
UserScopes = [
"todo:read",
"todo:write",
],
// Optional: Tag API Keys with additional features
UserFeatures = [
"Tracking",
],
});
})
.ConfigureAppHost(appHost =>
{
var apiKeysFeature = appHost.GetPlugin<ApiKeysFeature>();
using var db = appHost.Resolve<IDbConnectionFactory>().Open();
apiKeysFeature.InitSchema(db);

// Optional, create API Key for specified Users
if (apiKeysFeature.ApiKeyCount(db) == 0)
{
var createApiKeysFor = new [] { "[email protected]", "[email protected]" };
var users = db.Select<ApplicationUser>(x => createApiKeysFor.Contains(x.UserName));
foreach (var user in users)
{
List<string> scopes = user.UserName == "[email protected]"
? [RoleNames.Admin]
: [];
apiKeysFeature.Insert(db,
new() { Name = "Seed API Key", UserId = user.Id, UserName = user.UserName, Scopes = scopes });
}
}
});
}
Loading

0 comments on commit cf02421

Please sign in to comment.