-
Notifications
You must be signed in to change notification settings - Fork 812
API Users and external access to the Management API #6628
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
sofietoft
merged 9 commits into
umbraco:main
from
kjac:v15/feature/api-users-and-external-access
Nov 12, 2024
Merged
Changes from 6 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
bc11a6a
API Users and external access to the Management API
kjac 678b14c
Merge branch 'refs/heads/master' into v15/feature/api-users-and-exter…
kjac 5618a4c
Make the linter happy
kjac 5fcac1a
Make the linter happier
kjac 58a6fc3
SMall fixes
sofietoft f475f32
Grammar and typo fixes
sofietoft bd1d0be
Update 15/umbraco-cms/reference/management-api/external-access.md
kjac 0797a88
Update 15/umbraco-cms/reference/management-api/external-access.md
kjac 7badd7b
GitBook checks
sofietoft File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| --- | ||
| description: >- | ||
| This guide will explain the API Users concept and how they differ from regular Users how to define | ||
| --- | ||
|
|
||
| # API Users | ||
|
|
||
| API Users allow for authorizing [external access](../../../reference/management-api/external-access.md) to the Management API. | ||
|
|
||
| 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. | ||
|
|
||
| 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. | ||
|
|
||
| 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. | ||
|
|
||
|  | ||
|
|
||
| {% hint style="info" %} | ||
| 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. | ||
| {% endhint %} |
95 changes: 95 additions & 0 deletions
95
15/umbraco-cms/reference/management-api/external-access.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| --- | ||
| description: How external applications can consume the Management API. | ||
| --- | ||
|
|
||
| # External access to the Management API | ||
|
|
||
| The Management API can be used directly for integrations between Umbraco and external systems. | ||
|
|
||
| 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. | ||
|
|
||
| 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`. | ||
|
|
||
| The token endpoint response looks like this: | ||
|
|
||
| ```json | ||
| { | ||
| "access_token": "ZnEAKg5YwDc7621y6xZlEdT9kwp_ULGQPc5mnY9cDw0", | ||
| "token_type": "Bearer", | ||
| "expires_in": 299 | ||
| } | ||
| ``` | ||
|
|
||
| As shown, the access token should be used as a Bearer token when consuming the Management API. | ||
|
|
||
| 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. | ||
|
|
||
| {% hint style="info" %} | ||
| The Management API does not support OIDC Discovery. This is reserved for accessing protected for Members via the [Delivery API](../content-delivery-api/protected-content-in-the-delivery-api.md). | ||
|
Check failure on line 28 in 15/umbraco-cms/reference/management-api/external-access.md
|
||
| {% endhint %} | ||
|
|
||
| The following code sample demonstrates how to consume the Management API by: | ||
|
|
||
| 1. Obtaining an access token from the token endpoint. | ||
| 2. Fetching data from the "current user" endpoint. | ||
kjac marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
|  | ||
|
|
||
| {% hint style="info" %} | ||
| This sample requires the [`IdentityModel`](https://www.nuget.org/packages/IdentityModel) NuGet package to run. | ||
| {% endhint %} | ||
|
|
||
| {% code title="Program.cs" lineNumbers="true" %} | ||
| ```csharp | ||
| using System.Net.Http.Json; | ||
| using IdentityModel.Client; | ||
|
|
||
| // the base URL of the Umbraco site - change this to fit your setup | ||
| const string host = "https://localhost:44391"; | ||
|
|
||
| var client = new HttpClient(); | ||
|
|
||
| // request a client credentials token from the Management API token endpoint | ||
| var tokenResponse = await client.RequestClientCredentialsTokenAsync( | ||
| new ClientCredentialsTokenRequest | ||
| { | ||
| Address = $"{host}/umbraco/management/api/v1/security/back-office/token", | ||
| ClientId = "umbraco-back-office-my-client", | ||
| ClientSecret = "my-client-secret" | ||
| } | ||
| ); | ||
|
|
||
| if (tokenResponse.IsError || tokenResponse.AccessToken is null) | ||
| { | ||
| Console.WriteLine($"Error obtaining a token: {tokenResponse.ErrorDescription}"); | ||
| return; | ||
| } | ||
|
|
||
| // use the access token as Bearer token | ||
| client.SetBearerToken(tokenResponse.AccessToken); | ||
|
|
||
| // fetch user data from the "current user" Management API endpoint | ||
| var apiResponse = await client.GetAsync($"{host}/umbraco/management/api/v1/user/current"); | ||
| var apiUserResponse = await apiResponse | ||
| .EnsureSuccessStatusCode() | ||
| .Content | ||
| .ReadFromJsonAsync<ApiUserResponse>(); | ||
|
|
||
| if (apiUserResponse is null) | ||
| { | ||
| Console.WriteLine("Could not parse a user from the API response."); | ||
| return; | ||
| } | ||
|
|
||
| Console.WriteLine($"Hello, {apiUserResponse.Name} ({apiUserResponse.Email})"); | ||
|
|
||
| public class ApiUserResponse | ||
| { | ||
| public required Guid Id { get; set; } | ||
|
|
||
| public required string Name { get; set; } | ||
|
|
||
| public required string Email { get; set; } | ||
| } | ||
| ``` | ||
| {% endcode %} | ||
Binary file added
BIN
+49.9 KB
15/umbraco-cms/reference/management-api/images/current-user-endpoint.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.