Skip to content
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

adding management api docs with some examples #5925

Merged
merged 78 commits into from
Mar 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
78 commits
Select commit Hold shift + click to select a range
69f1fcc
adding management api docs with some examples
alina-tincas Mar 7, 2024
b192c9f
Update 14/umbraco-cms/extending-cms/custom-swagger-api.md
alina-tincas Mar 7, 2024
2fad9a8
Update 14/umbraco-cms/reference/management-api/README.md
alina-tincas Mar 7, 2024
1fd25d7
added articles to summary
alina-tincas Mar 7, 2024
65e0d25
Delete 14/umbraco-cms/extending-cms/custom-swagger-api.md
alina-tincas Mar 7, 2024
945b94f
update to correct path
alina-tincas Mar 7, 2024
c93bfdc
Merge branch 'create-management-api-article' of https://github.com/um…
alina-tincas Mar 7, 2024
1b74f9e
Update 14/umbraco-cms/reference/custom-swagger-api.md
alina-tincas Mar 8, 2024
e0cbbc4
Update 14/umbraco-cms/reference/custom-swagger-api.md
alina-tincas Mar 8, 2024
6d17b4c
Update 14/umbraco-cms/reference/custom-swagger-api.md
alina-tincas Mar 8, 2024
9deb125
Update 14/umbraco-cms/reference/management-api/postman-setup-swagger.md
alina-tincas Mar 8, 2024
f337aa3
Update 14/umbraco-cms/reference/custom-swagger-api.md
alina-tincas Mar 8, 2024
0714c95
Update 14/umbraco-cms/reference/custom-swagger-api.md
alina-tincas Mar 8, 2024
c54b7e8
Update 14/umbraco-cms/reference/custom-swagger-api.md
alina-tincas Mar 8, 2024
319eb0f
Update 14/umbraco-cms/reference/management-api/README.md
alina-tincas Mar 8, 2024
4ea4b3b
Update 14/umbraco-cms/reference/management-api/README.md
alina-tincas Mar 8, 2024
32c5e39
Update 14/umbraco-cms/reference/management-api/README.md
alina-tincas Mar 8, 2024
21fb47e
Update 14/umbraco-cms/reference/management-api/postman-setup-swagger.md
alina-tincas Mar 8, 2024
2935e9f
Update 14/umbraco-cms/reference/management-api/postman-setup-swagger.md
alina-tincas Mar 8, 2024
a3ac136
Update 14/umbraco-cms/reference/management-api/postman-setup-swagger.md
alina-tincas Mar 8, 2024
6d73d7f
Update 14/umbraco-cms/reference/management-api/postman-setup-swagger.md
alina-tincas Mar 8, 2024
c0ec278
Update 14/umbraco-cms/reference/management-api/postman-setup-swagger.md
alina-tincas Mar 8, 2024
ca5cecb
Update 14/umbraco-cms/reference/management-api/postman-setup-swagger.md
alina-tincas Mar 8, 2024
3e763f0
Update 14/umbraco-cms/reference/management-api/postman-setup-swagger.md
alina-tincas Mar 8, 2024
02af0b9
Update 14/umbraco-cms/reference/management-api/postman-setup-swagger.md
alina-tincas Mar 8, 2024
b4725b5
Update 14/umbraco-cms/reference/management-api/postman-setup-swagger.md
alina-tincas Mar 8, 2024
a4e7950
Update 14/umbraco-cms/reference/management-api/postman-setup-swagger.md
alina-tincas Mar 8, 2024
cef709c
Update 14/umbraco-cms/reference/management-api/postman-setup-swagger.md
alina-tincas Mar 8, 2024
3ce751f
Update 14/umbraco-cms/reference/management-api/postman-setup-swagger.md
alina-tincas Mar 8, 2024
253a444
Update 14/umbraco-cms/reference/management-api/postman-setup-swagger.md
alina-tincas Mar 8, 2024
efa87ae
Update 14/umbraco-cms/reference/management-api/postman-setup-swagger.md
alina-tincas Mar 8, 2024
5cb2246
Update 14/umbraco-cms/reference/management-api/postman-setup-swagger.md
alina-tincas Mar 8, 2024
0ed863d
Update 14/umbraco-cms/reference/management-api/postman-setup-swagger.md
alina-tincas Mar 8, 2024
4db631a
Update 14/umbraco-cms/reference/management-api/postman-setup-swagger.md
alina-tincas Mar 8, 2024
70137b7
Update 14/umbraco-cms/reference/management-api/postman-setup-swagger.md
alina-tincas Mar 8, 2024
1b0e889
Update 14/umbraco-cms/reference/management-api/postman-setup-swagger.md
alina-tincas Mar 8, 2024
0f5210d
Update 14/umbraco-cms/reference/management-api/postman-setup-swagger.md
alina-tincas Mar 8, 2024
0212c89
Update 14/umbraco-cms/reference/management-api/postman-setup-swagger.md
alina-tincas Mar 8, 2024
d4868c4
Update 14/umbraco-cms/reference/management-api/postman-setup-swagger.md
alina-tincas Mar 8, 2024
b53231f
Update 14/umbraco-cms/reference/management-api/postman-setup-swagger.md
alina-tincas Mar 8, 2024
1448877
Update 14/umbraco-cms/reference/management-api/postman-setup-swagger.md
alina-tincas Mar 8, 2024
d768675
Update 14/umbraco-cms/reference/management-api/README.md
alina-tincas Mar 8, 2024
dcced66
Update 14/umbraco-cms/reference/custom-swagger-api.md
alina-tincas Mar 11, 2024
45e7275
Update 14/umbraco-cms/reference/custom-swagger-api.md
alina-tincas Mar 11, 2024
d557321
Update 14/umbraco-cms/reference/custom-swagger-api.md
alina-tincas Mar 11, 2024
d8eee5d
Update 14/umbraco-cms/reference/custom-swagger-api.md
alina-tincas Mar 11, 2024
36dc3df
Update 14/umbraco-cms/reference/custom-swagger-api.md
alina-tincas Mar 11, 2024
439dc68
Update 14/umbraco-cms/reference/custom-swagger-api.md
alina-tincas Mar 11, 2024
e4be3b4
Update 14/umbraco-cms/reference/custom-swagger-api.md
alina-tincas Mar 11, 2024
0a38955
Update 14/umbraco-cms/reference/management-api/README.md
alina-tincas Mar 11, 2024
7bf45ff
Update 14/umbraco-cms/reference/management-api/README.md
alina-tincas Mar 11, 2024
bf11133
Update 14/umbraco-cms/reference/management-api/README.md
alina-tincas Mar 11, 2024
0fc90bc
Update 14/umbraco-cms/reference/management-api/README.md
alina-tincas Mar 11, 2024
27c6452
Update 14/umbraco-cms/reference/management-api/README.md
alina-tincas Mar 11, 2024
242b2b8
Update 14/umbraco-cms/reference/management-api/README.md
alina-tincas Mar 11, 2024
c4f0e78
Update 14/umbraco-cms/reference/management-api/README.md
alina-tincas Mar 11, 2024
9eccd0a
Update 14/umbraco-cms/reference/management-api/README.md
alina-tincas Mar 11, 2024
01d1003
Update 14/umbraco-cms/reference/management-api/postman-setup-swagger.md
alina-tincas Mar 11, 2024
b84ca45
Update 14/umbraco-cms/reference/management-api/postman-setup-swagger.md
alina-tincas Mar 11, 2024
2e51de1
Update 14/umbraco-cms/reference/management-api/postman-setup-swagger.md
alina-tincas Mar 11, 2024
544a62d
Update 14/umbraco-cms/reference/management-api/postman-setup-swagger.md
alina-tincas Mar 11, 2024
cf38e15
Update 14/umbraco-cms/reference/management-api/postman-setup-swagger.md
alina-tincas Mar 11, 2024
7574d3e
Update 14/umbraco-cms/reference/management-api/postman-setup-swagger.md
alina-tincas Mar 11, 2024
5cb13cf
update images
alina-tincas Mar 11, 2024
1599ea7
Applying feedback from Kenn
alina-tincas Mar 13, 2024
ade10f2
Update 14/umbraco-cms/reference/custom-swagger-api.md
alina-tincas Mar 13, 2024
f63cae0
Update 14/umbraco-cms/reference/custom-swagger-api.md
alina-tincas Mar 13, 2024
01de158
Update 14/umbraco-cms/reference/custom-swagger-api.md
alina-tincas Mar 27, 2024
848a395
Update 14/umbraco-cms/reference/custom-swagger-api.md
alina-tincas Mar 27, 2024
a4d80cb
Update 14/umbraco-cms/reference/custom-swagger-api.md
alina-tincas Mar 27, 2024
7f51fcc
Update 14/umbraco-cms/reference/custom-swagger-api.md
alina-tincas Mar 27, 2024
70f869a
Update 14/umbraco-cms/reference/management-api/README.md
alina-tincas Mar 27, 2024
162d6fc
Update 14/umbraco-cms/reference/management-api/README.md
alina-tincas Mar 27, 2024
cf0b962
Update 14/umbraco-cms/reference/management-api/README.md
alina-tincas Mar 27, 2024
23cee13
Update 14/umbraco-cms/reference/management-api/README.md
alina-tincas Mar 27, 2024
df220e6
Update 14/umbraco-cms/reference/management-api/README.md
alina-tincas Mar 27, 2024
aa2c576
Update 14/umbraco-cms/reference/management-api/postman-setup-swagger.md
alina-tincas Mar 27, 2024
3237906
highlighted images
alina-tincas Mar 27, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions 14/umbraco-cms/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@
* [Route Registration](extending-backoffice/modals/route-registration.md)
* [Sorting](extending-backoffice/sorting.md)

## Reference

* [Management API](./reference/management-api/README.md)
* [Setup Authorization in Postman](./reference/management-api/postman-setup-swagger.md)
* [Custom Swagger API](./reference/custom-swagger-api.md)

## Tutorials

* [Creating a Basic Website](tutorials/creating-a-basic-website/README.md)
Expand Down
202 changes: 202 additions & 0 deletions 14/umbraco-cms/reference/custom-swagger-api.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,202 @@
---
description: Example of a Custom API with Authorization and Swagger
---

# Custom API with Authorization and Swagger

This article covers how to create a Custom API controller protected by the backoffice authorization policies. It also shows how to enable the authorization UI in Swagger docs.


{% hint style="info" %}

Before proceeding, make sure to read the [Management API](./management-api/README.md) article. It provides information about the Swagger documentation and Authorization used in this article.

{% endhint %}

This example can be a starting point for creating a secure custom API with automatic Swagger documentation. You can find other examples in the [API versioning and OpenAPI](./api-versioning-and-openapi.md) article.

1. Create a new `.cs` file called `MyBackOfficeSecurityRequirementsOperationFilter` in your Umbraco project.

2. Add the following code so that the new API shows in the Swagger documentation and Swagger UI:

{% code title="MyBackOfficeSecurityRequirementsOperationFilter.cs" lineNumbers="true" %}

```csharp

using Microsoft.Extensions.Options;
using Microsoft.OpenApi.Models;
using Swashbuckle.AspNetCore.SwaggerGen;
using Umbraco.Cms.Api.Management.OpenApi;
using Umbraco.Cms.Core.Composing;

namespace Umbraco.Cms.Web.UI.New.Custom;

//Necessary code for the new API to show in the Swagger documentation and Swagger UI
public class MyBackOfficeSecurityRequirementsOperationFilter : BackOfficeSecurityRequirementsOperationFilterBase
{
protected override string ApiName => "my-api-v1";
}

public class MyConfigureSwaggerGenOptions : IConfigureOptions<SwaggerGenOptions>
{
public void Configure(SwaggerGenOptions options)
{
options.SwaggerDoc("my-api-v1", new OpenApiInfo { Title = "My API v1", Version = "1.0" });
options.OperationFilter<MyBackOfficeSecurityRequirementsOperationFilter>();
}
}

public class MyComposer : IComposer
{
public void Compose(IUmbracoBuilder builder)
=> builder.Services.ConfigureOptions<MyConfigureSwaggerGenOptions>();
}

```

{% endcode %}

- Our filter inherits from `BackOfficeSecurityRequirementsOperationFilterBase`. This marks our API as supporting authorization via Swagger.
- `MyConfigureSwaggerGenOptions` configures our API swagger docs with our filter applied.
- `MyComposer ` makes sure the swagger generator knows about our API docs configuration at runtime.

3. Add the ApiController to setup the logic behind the endpoint:

{% code title="MyBackOfficeSecurityRequirementsOperationFilter.cs" lineNumbers="true" %}

```csharp

using Umbraco.Cms.Api.Common.Attributes;
using Umbraco.Cms.Api.Common.Filters;
using Umbraco.Cms.Core;
using Umbraco.Cms.Core.Models.Membership;
using Umbraco.Cms.Core.Security;
using Umbraco.Cms.Web.Common.Authorization;
using Asp.Versioning;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
...

//Creating the Controller
[ApiController]
[ApiVersion("1.0")]
[MapToApi("my-api-v1")]
[Authorize(Policy = AuthorizationPolicies.BackOfficeAccess)]
[JsonOptionsName(Constants.JsonOptionsNames.BackOffice)]
[Route("api/v{version:apiVersion}/my")]
public class MyApiController : Controller
{
private readonly IBackOfficeSecurityAccessor _backOfficeSecurityAccessor;

public MyApiController(IBackOfficeSecurityAccessor backOfficeSecurityAccessor)
=> _backOfficeSecurityAccessor = backOfficeSecurityAccessor;

[HttpGet("say-hello")]
[MapToApiVersion("1.0")]
[ProducesResponseType(typeof(string), StatusCodes.Status200OK)]
public IActionResult SayHello()
{
IUser currentUser = _backOfficeSecurityAccessor.BackOfficeSecurity?.CurrentUser
?? throw new InvalidOperationException("No backoffice user found");
return Ok($"Hello, {currentUser.Name}");
}
}

```

{% endcode %}

<details>

<summary>See the entire file: MyBackOfficeSecurityRequirementsOperationFilter.cs</summary>

{% code title="MyBackOfficeSecurityRequirementsOperationFilter.cs" lineNumbers="true" %}

```csharp
using Asp.Versioning;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using Microsoft.OpenApi.Models;
using Swashbuckle.AspNetCore.SwaggerGen;
using Umbraco.Cms.Api.Common.Attributes;
using Umbraco.Cms.Api.Common.Filters;
using Umbraco.Cms.Api.Management.OpenApi;
using Umbraco.Cms.Core;
using Umbraco.Cms.Core.Composing;
using Umbraco.Cms.Core.Models.Membership;
using Umbraco.Cms.Core.Security;
using Umbraco.Cms.Web.Common.Authorization;

namespace Umbraco.Cms.Web.UI.New.Custom;

//Necessary code for the new API to show in the Swagger documentation and Swagger UI
public class MyBackOfficeSecurityRequirementsOperationFilter : BackOfficeSecurityRequirementsOperationFilterBase
{
protected override string ApiName => "my-api-v1";
}

public class MyConfigureSwaggerGenOptions : IConfigureOptions<SwaggerGenOptions>
{
public void Configure(SwaggerGenOptions options)
{
options.SwaggerDoc("my-api-v1", new OpenApiInfo { Title = "My API v1", Version = "1.0" });
options.OperationFilter<MyBackOfficeSecurityRequirementsOperationFilter>();
}
}

public class MyComposer : IComposer
{
public void Compose(IUmbracoBuilder builder)
=> builder.Services.ConfigureOptions<MyConfigureSwaggerGenOptions>();
}

//Creating the Controller
[ApiController]
[ApiVersion("1.0")]
[MapToApi("my-api-v1")]
[Authorize(Policy = AuthorizationPolicies.BackOfficeAccess)]
[JsonOptionsName(Constants.JsonOptionsNames.BackOffice)]
[Route("api/v{version:apiVersion}/my")]
public class MyApiController : Controller
{
private readonly IBackOfficeSecurityAccessor _backOfficeSecurityAccessor;

public MyApiController(IBackOfficeSecurityAccessor backOfficeSecurityAccessor)
=> _backOfficeSecurityAccessor = backOfficeSecurityAccessor;

[HttpGet("say-hello")]
[MapToApiVersion("1.0")]
[ProducesResponseType(typeof(string), StatusCodes.Status200OK)]
public IActionResult SayHello()
{
IUser currentUser = _backOfficeSecurityAccessor.BackOfficeSecurity?.CurrentUser
?? throw new InvalidOperationException("No backoffice user found");
return Ok($"Hello, {currentUser.Name}");
}
}
```

{% endcode %}

</details>

4. Run the project and navigate to `{yourdomain}/umbraco/swagger`.
5. Choose the swagger documentation we created with the code above named **My API v1** from **Select a definition**.

![Created Custom API in Swagger Documentation](./images/custom-api-swagger-example.png)

Here, we can find the endpoint that we created:

```http
GET /api/v1/my/say-hello
```

6. Click on the **Authorize** button to authenticate.

7. Try out the endpoint using the **Try it out** button.
8. Click on **Execute**.

![Trying out the endpoint](./images/custom-api-swagger-example-response.png)

We now get the response we have setup using the code: `"Hello, {{userName}}"`.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
71 changes: 71 additions & 0 deletions 14/umbraco-cms/reference/management-api/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
---
description: Get started with the Management API.
---

# Management API

The Management API delivers headless management capabilities built directly into Umbraco. The Management API is used by the backoffice to communicate with the backend.

The Management API can also be used for Custom apps or Workflows with OpenID Connect.

{% hint style="info" %}
The Management API is a replacement for the backoffice controllers that lacked RESTful capabilities.
{% endhint %}

## Swagger Documentation

Umbraco ships with Swagger to document the Management API. Swagger and the Swagger UI are based on [Swashbuckle.AspNetCore](https://github.com/domaindrivendev/Swashbuckle.AspNetCore/) and is available at `{yourdomain}/umbraco/swagger`. For security reasons, both are disabled in non-production environments.

The Swagger documentation allows you to select a definition and go to either Umbraco Management API or Content Delivery API. If you are extending the Management API with your own controllers, you can also create custom documentation for these in Swagger. See [Custom Swagger API](../custom-swagger-api.md) article for details.

![Umbraco Management API documentation in Swagger](../images/management-api-swagger.png)

In the Swagger Umbraco Management API, you can find a collection of available endpoints in this version of Umbraco.

## Authorization

The Management API endpoints are protected by the backoffice authorization policies and need an authentication token to interact with them.

To set the authorization, click on the **Authorize** button:

![Umbraco Management API Authorize Button](../images/management-api-swagger-authorize-button.png)

Then a popup will appear with some setup information and a login form for authorization:

alina-tincas marked this conversation as resolved.
Show resolved Hide resolved
![Umbraco Management API Authorize Login](../images/management-api-swagger-authorize-instructions.png)

The available integration for the authorization is done via a backoffice user with the integration of `OAuth2, authorizationCode with PKCE`. Swagger is only enabled in non-production environments, so if you need to access the Management API in production, you need a different client.

{% hint style="info" %}

In production environment, only `umbraco-back-office` **client** is allowed to connect to the Management API. In non-production environments, the `umbraco-swagger` and `umbraco-postman` **clients** can be used.

You can see an example of how to connect a backoffice user via OAuth2 in Postman in the [Swagger Setup in Postman](./postman-setup-swagger) article.

{% endhint %}


# Test an Endpoint

To test a Management API endpoint, follow these steps:

1. Authenticate via the **Authorize** button. Make sure you use `umbraco-swagger` as the `client_id`:

![Umbraco Management API when Authenticated](../images/management-api-swagger-authenticated.png)

{% hint style="info" %}
In non-production environments, you will always need only the `client_id` to authenticate. The `client_secret` should always be left blank, as the authentication flow does not use a client secret.
{% endhint %}

2. Expand the first endpoint of **Audit Log** and click **Try it out**

![Umbraco Management API Endpoint - Try it Out Button](../images/management-api-try-it-out.png)

3. You now have the option to change the values of the **parameters**. In this case, let´s leave the default values as they are.
4. Click on **Execute** so that we can get some data for our **Audit Log** endpoint.

![Umbraco Management API Endpoint - Execute - Response](../images/management-api-execute-response.png)

In the **Response Body** we get the details of the **Audit Log** that we have requested.

5. You can continue changing the default **parameters**, **clear** the query, or **cancel** the trial of the endpoint.
Loading
Loading