Skip to content

Commit a76e21b

Browse files
committed
Update Simple Auth docs with ApiKeyCredentialsProvider
1 parent a332a8a commit a76e21b

File tree

1 file changed

+94
-25
lines changed

1 file changed

+94
-25
lines changed

MyApp/_pages/auth/admin-apikeys.md

Lines changed: 94 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@ that's easy for **Admin** Users to manage and control which trusted clients and
1919
Simple Auth Story with API Keys ideal for .NET 8 Microservices
2020
:::
2121

22-
The easiest way to get started is by creating a new Empty project with API Keys enabled with your preferred database
23-
to store the API Keys in. SQLite is a good choice for stand-alone Apps as it doesn't require any infrastructure dependencies.
22+
The easiest way to get started is by creating a new Empty project with API Keys enabled with your preferred database to store the API Keys in. SQLite is a good choice for stand-alone Apps as it doesn't require any infrastructure dependencies.
2423

2524
<div class="not-prose mx-auto">
2625
<h3 id="template" class="mb-4 text-4xl tracking-tight font-extrabold text-gray-900">
@@ -37,36 +36,39 @@ Existing projects not configured with Authentication can enable this simple Auth
3736
x mix apikeys-auth
3837
:::
3938

40-
Which will add the [ServiceStack.Server](https://nuget.org/packages/ServiceStack.Server) dependency and the
41-
[Modular Startup](/modular-startup) configuration below:
39+
Which will add the [ServiceStack.Server](https://nuget.org/packages/ServiceStack.Server) dependency and the [Modular Startup](/modular-startup) configuration below:
4240

4341
```csharp
4442
public class ConfigureApiKeys : IHostingStartup
4543
{
4644
public void Configure(IWebHostBuilder builder) => builder
47-
.ConfigureServices(services =>
48-
{
49-
services.AddPlugin(new AuthFeature(new AuthSecretAuthProvider("p@55wOrd")));
50-
services.AddPlugin(new ApiKeysFeature
45+
.ConfigureServices(services =>
46+
{
47+
services.AddPlugin(new AuthFeature([
48+
new ApiKeyCredentialsProvider(),
49+
new AuthSecretAuthProvider("p@55wOrd"),
50+
]));
51+
services.AddPlugin(new SessionFeature());
52+
services.AddPlugin(new ApiKeysFeature
53+
{
54+
// Optional: Available Scopes Admin Users can assign to any API Key
55+
// Features = [
56+
// "Paid",
57+
// "Tracking",
58+
// ],
59+
// Optional: Available Features Admin Users can assign to any API Key
60+
// Scopes = [
61+
// "todo:read",
62+
// "todo:write",
63+
// ],
64+
});
65+
})
66+
.ConfigureAppHost(appHost =>
5167
{
52-
// Optional: Available Scopes Admin Users can assign to any API Key
53-
// Features = [
54-
// "Paid",
55-
// "Tracking",
56-
// ],
57-
// Optional: Available Features Admin Users can assign to any API Key
58-
// Scopes = [
59-
// "todo:read",
60-
// "todo:write",
61-
// ],
68+
using var db = appHost.Resolve<IDbConnectionFactory>().Open();
69+
var feature = appHost.GetPlugin<ApiKeysFeature>();
70+
feature.InitSchema(db);
6271
});
63-
})
64-
.ConfigureAppHost(appHost =>
65-
{
66-
using var db = appHost.Resolve<IDbConnectionFactory>().Open();
67-
var feature = appHost.GetPlugin<ApiKeysFeature>();
68-
feature.InitSchema(db);
69-
});
7072
}
7173
```
7274

@@ -222,6 +224,73 @@ when trying to access an API Key protected Service:
222224

223225
![](/img/pages/auth/simple/apiexplorer-apikey-dialog.png)
224226

227+
## API Keys and Admin Secret Credentials Auth Provider
228+
229+
The usability of Simple Admin API Keys is greatly improved with the `ApiKeyCredentialsProvider` which enables .NET Microservices to provide persistent UserSession-like behavior for API Keys and Admin Auth Secrets to enable a Credentials Auth implementation which users can use with their API Keys or Admin AuthSecret.
230+
231+
When registered a **Credentials** SignIn dialog will appear for [ServiceStack Built-in UIs](https://servicestack.net/auto-ui) allowing users to Sign In with their **API Keys** or Admin **Auth Secret**.
232+
233+
![](/img/pages/auth/simple/ai-server-auth-apiexplorer.png)
234+
235+
### Session Auth with API Keys
236+
237+
Behind the scenes this creates a Server [Auth Session](/auth/sessions)
238+
but instead of maintaining an Authenticated User Session it saves the API Key in the session then attaches the API Key to each request. This makes it possible to make API Key validated requests with just a session cookie instead of requiring resubmission of API Keys for each request.
239+
240+
### Secure .NET Microservices and Docker Appliances
241+
242+
This is an ideal Auth Configuration for .NET Docker Appliances and Microservices like [AI Server](/posts/ai-server) that don't need the complexity of ASP .NET Core's Identity Auth machinery and just want to restrict access to their APIs with API Keys and restrict Admin functionality to Administrator's with an Auth Secret.
243+
244+
The benefit of `ApiKeyCredentialsProvider` is that it maintains a persistent Session so that end users
245+
only need to enter their API Key a single time and they'll be able to navigate to all of AI Server's protected pages using their API Key maintained in their Server User Session without needing to re-enter it for each UI and every request.
246+
247+
### User Access with API Keys
248+
249+
AI Server uses **API Keys** to restrict Access to their AI Features to **authorized Users** with Valid API Keys who
250+
are able to use its Built-in UIs for its AI Features with the Users preferred Name and issued API Key:
251+
252+
![](/img/pages/auth/simple/ai-server-auth-user.png)
253+
254+
After signing in a single time they'll be able to navigate to any protected page and start using AI Server's AI features:
255+
256+
![](/img/pages/auth/simple/ai-server-auth-user-chat.png)
257+
258+
### User Access to API Explorer
259+
260+
This also lets users use their existing Auth Session across completely different UIs
261+
like [API Explorer](/api-explorer)
262+
where they'll have the same access to APIs as they would when calling APIs programatically with their API Keys, e.g:
263+
264+
![](/img/pages/auth/simple/ai-server-auth-apiexplorer-api.png)
265+
266+
## Admin Access
267+
268+
AI Server also maintains an Admin UI and Admin APIs that are only accessible to **Admin** users who
269+
Authenticate with the App's configured Admin Auth Secret who are able to access AI Server's Admin
270+
UIs to monitor Live AI Requests, create new User API Keys, Manage registered AI Providers, etc.
271+
272+
![](/img/pages/auth/simple/ai-server-auth-admin-jobs.png)
273+
274+
### Admin Restricted APIs
275+
276+
You can restrict APIs to Admin Users by using `[ValidateAuthSecret]`:
277+
278+
```csharp
279+
[Tag(Tags.Admin)]
280+
[ValidateAuthSecret]
281+
[Api("Add an AI Provider to process AI Requests")]
282+
public class CreateAiProvider : ICreateDb<AiProvider>, IReturn<IdResponse>
283+
{
284+
//...
285+
}
286+
```
287+
288+
Which are identified in API Explorer with a **padlock** icon whilst APIs restricted by API Key are
289+
identified with a **key** icon:
290+
291+
![](/img/pages/auth/simple/ai-server-auth-apiexplorer-admin.png)
292+
293+
225294
### Client Usage
226295

227296
All HTTP and existing [Service Clients](https://docs.servicestack.net/clients-overview) can be configured to use API Keys

0 commit comments

Comments
 (0)