Skip to content

Commit 0d48c89

Browse files
authored
Merge pull request #6628 from kjac/v15/feature/api-users-and-external-access
API Users and external access to the Management API
2 parents a1b762d + 7badd7b commit 0d48c89

File tree

6 files changed

+121
-2
lines changed

6 files changed

+121
-2
lines changed

15/umbraco-cms/SUMMARY.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@
4242
* [Image Cropper](fundamentals/backoffice/property-editors/built-in-umbraco-property-editors/image-cropper.md)
4343
* [Block Editors](fundamentals/backoffice/property-editors/built-in-umbraco-property-editors/block-editor/README.md)
4444
* [Block Grid](fundamentals/backoffice/property-editors/built-in-umbraco-property-editors/block-editor/block-grid-editor.md)
45+
* [Data](fundamentals/data/README.md)
46+
* [Users](fundamentals/data/users/README.md)
47+
* [API Users](fundamentals/data/users/api-users.md)
4548

4649
## Implementation
4750

@@ -105,6 +108,8 @@
105108
* [Management](reference/management/README.md)
106109
* [Using Umbraco services](reference/management/using-services/README.md)
107110
* [Content Type Service](reference/management/using-services/contenttypeservice.md)
111+
* [Management API](reference/management-api/README.md)
112+
* [External Access](reference/management-api/external-access.md)
108113
* [Cache & Distributed Cache](reference/cache/README.md)
109114
* [Cache Seeding](reference/cache/cache-seeding.md)
110115
* [Examples](reference/cache/examples/README.md)
134 KB
Loading

15/umbraco-cms/fundamentals/data/users.md renamed to 15/umbraco-cms/fundamentals/data/users/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ description: >-
66

77
# Users
88

9-
Users (not to be confused with [Members](members.md)) are people who have access to the Umbraco backoffice. These could include Content Editors, Translators, Web Designers, and Developers.
9+
Users (not to be confused with [Members](../members.md)) are people who have access to the Umbraco backoffice. These could include Content Editors, Translators, Web Designers, and Developers.
1010

1111
This guide will explain how to define, create, and manage users in the backoffice.
1212

@@ -38,7 +38,7 @@ By default, the User Groups available to new users are **Administrators**, **Wri
3838
* **Editor**: Allowed to create and publish content items or nodes on the website without approval from others or restrictions (has permissions to **Public Access**, **Rollback**, **Browse Node**, **Create Content Template**, **Delete**, **Create**, **Publish**, **Unpublish**, **Update**, **Copy**, **Move** and **Sort**).
3939
* **Writer**: Allowed to browse nodes, create nodes, and request for publication of items. Not allowed to publish directly without someone elses approval like an Editor (has permissions to **Browse Node**, **Create**, **Send to Publish** and **Update**).
4040
* **Translator**: Are used for translating your website. Translators are allowed to browse and update nodes as well as grant dashboard access. Translations of site pages must be reviewed by others before publication (has permissions to **Browse Node** and **Update**).
41-
* **Sensitive data**: Any users added to this User group will have access to view any data marked as sensitive. Learn more about this feature in the [Sensitive Data](../../reference/security/sensitive-data-on-members.md) article.
41+
* **Sensitive data**: Any users added to this User group will have access to view any data marked as sensitive. Learn more about this feature in the [Sensitive Data](../../../reference/security/sensitive-data-on-members.md) article.
4242

4343
## Creating a User Group
4444

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
---
2+
description: This guide will explain the API Users concept and how they differ from regular Users how to define
3+
---
4+
5+
# API Users
6+
7+
API Users allow for authorizing [external access](../../../reference/management-api/external-access.md) to the Management API.
8+
9+
An API User is identical to a [regular User](README.md) except for one thing: It has no password. In fact, API Users are not allowed to log into the backoffice like regular Users.
10+
11+
Instead, API Users hold the Client Credentials used to authorize against the Management API. When an external source authorizes using Client Credentials, it effectively assumes the identity of the API User.
12+
13+
Since API Users are identical to regular Users their backoffice access can be controlled in the same way. This allows for imposing detailed access control on the external sources connected to the Management API.
14+
15+
![An API User in the backoffice](../images/api-user.png)
16+
17+
{% hint style="info" %}
18+
Client IDs for API Users are explicitly prefixed with `umbraco-back-office-`. This guards against API Users accidentally taking over one of the Client IDs used by the Umbraco core.
19+
{% endhint %}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
---
2+
description: How external applications can consume the Management API.
3+
---
4+
5+
# External access to the Management API
6+
7+
The Management API can be used directly for integrations between Umbraco and external systems.
8+
9+
When consuming the Management API from an external source, you must use the OpenId Connect Client Credentials flow for authorization. Refer to the [API Users](../../fundamentals/data/users/api-users.md) article for details on setting up Client Credentials.
10+
11+
With a set of Client Credentials in place, you can obtain an access token from the Management API token endpoint: `/umbraco/management/api/v1/security/back-office/token`.
12+
13+
The token endpoint response looks like this:
14+
15+
```json
16+
{
17+
"access_token": "ZnEAKg5YwDc7621y6xZlEdT9kwp_ULGQPc5mnY9cDw0",
18+
"token_type": "Bearer",
19+
"expires_in": 299
20+
}
21+
```
22+
23+
As shown, the access token should be used as a Bearer token when consuming the Management API.
24+
25+
Also, notice that access tokens have a fixed expiry. While you can keep issuing new tokens for the Client Credentials, please reuse tokens within their lifespan. This will be more performant and avoid flooding the Umbraco database with tokens.
26+
27+
{% hint style="info" %}
28+
The Management API does not support OpenID Connect Discovery. This is reserved for Members accessing protected content via the [Delivery API](../content-delivery-api/protected-content-in-the-delivery-api.md).
29+
{% endhint %}
30+
31+
The following code sample demonstrates how to consume the Management API by
32+
33+
1. Obtaining an access token from the token endpoint, and
34+
2. Fetching data from the "current user" endpoint.
35+
36+
![The "current user" endpoint in Swagger UI](images/current-user-endpoint.png)
37+
38+
{% hint style="info" %}
39+
This sample requires the [`IdentityModel`](https://www.nuget.org/packages/IdentityModel) NuGet package to run.
40+
{% endhint %}
41+
42+
{% code title="Program.cs" lineNumbers="true" %}
43+
```csharp
44+
using System.Net.Http.Json;
45+
using IdentityModel.Client;
46+
47+
// the base URL of the Umbraco site - change this to fit your setup
48+
const string host = "https://localhost:44391";
49+
50+
var client = new HttpClient();
51+
52+
// request a client credentials token from the Management API token endpoint
53+
var tokenResponse = await client.RequestClientCredentialsTokenAsync(
54+
new ClientCredentialsTokenRequest
55+
{
56+
Address = $"{host}/umbraco/management/api/v1/security/back-office/token",
57+
ClientId = "umbraco-back-office-my-client",
58+
ClientSecret = "my-client-secret"
59+
}
60+
);
61+
62+
if (tokenResponse.IsError || tokenResponse.AccessToken is null)
63+
{
64+
Console.WriteLine($"Error obtaining a token: {tokenResponse.ErrorDescription}");
65+
return;
66+
}
67+
68+
// use the access token as Bearer token
69+
client.SetBearerToken(tokenResponse.AccessToken);
70+
71+
// fetch user data from the "current user" Management API endpoint
72+
var apiResponse = await client.GetAsync($"{host}/umbraco/management/api/v1/user/current");
73+
var apiUserResponse = await apiResponse
74+
.EnsureSuccessStatusCode()
75+
.Content
76+
.ReadFromJsonAsync<ApiUserResponse>();
77+
78+
if (apiUserResponse is null)
79+
{
80+
Console.WriteLine("Could not parse a user from the API response.");
81+
return;
82+
}
83+
84+
Console.WriteLine($"Hello, {apiUserResponse.Name} ({apiUserResponse.Email})");
85+
86+
public class ApiUserResponse
87+
{
88+
public required Guid Id { get; set; }
89+
90+
public required string Name { get; set; }
91+
92+
public required string Email { get; set; }
93+
}
94+
```
95+
{% endcode %}
49.9 KB
Loading

0 commit comments

Comments
 (0)