Skip to content

Commit dfa0135

Browse files
authored
refactor: add brief intro in swagger.json (#6102)
1 parent 92dc008 commit dfa0135

File tree

2 files changed

+82
-2
lines changed

2 files changed

+82
-2
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
export const managementApiDescription = `Logto Management API is a comprehensive set of REST APIs that gives you the full control over Logto to suit your product needs and tech stack. To see the full guide on Management API interactions, visit [Interact with Management API](https://docs.logto.io/docs/recipes/interact-with-management-api/).
2+
3+
### Get started
4+
5+
The API follows the same authentication principles as other API resources in Logto, with some slight differences. To use Logto Management API:
6+
7+
1. A machine-to-machine (M2M) application needs to be created.
8+
2. A machine-to-machine (M2M) role with Management API permission \`all\` needs to be assigned to the application.
9+
10+
Once you have them set up, you can use the \`client_credentials\` grant type to fetch an access token and use it to authenticate your requests to the Logto Management API.
11+
12+
### Fetch an access token
13+
14+
To fetch an access token, you need to make a \`POST\` request to the \`/oidc/token\` endpoint of your Logto tenant.
15+
16+
For Logto Cloud users, the base URL is your Logto endpoint, i.e. \`https://[tenant-id].logto.app\`. The tenant ID can be found in the following places:
17+
18+
- The first path segment of the URL when you are signed in to Logto Cloud. For example, if the URL is \`https://cloud.logto.io/foo/get-started\`, the tenant ID is \`foo\`.
19+
- In the "Settings" tab of Logto Cloud.
20+
21+
The request should follow the OAuth 2.0 [client credentials](https://datatracker.ietf.org/doc/html/rfc6749#section-4.4) grant type. Here is a non-normative example of how to fetch an access token:
22+
23+
\`\`\`bash
24+
curl --location \
25+
--request POST 'https://[tenant-id].logto.app/oidc/token' \
26+
--header 'Content-Type: application/x-www-form-urlencoded' \
27+
--data-urlencode 'grant_type=client_credentials' \
28+
--data-urlencode 'client_id=[app-id]' \
29+
--data-urlencode 'client_secret=[app-secret]' \
30+
--data-urlencode 'resource=https://[tenant-id].logto.app/api' \
31+
--data-urlencode 'scope=all'
32+
\`\`\`
33+
34+
Replace \`[tenant-id]\`, \`[app-id]\`, and \`[app-secret]\` with your Logto tenant ID, application ID, and application secret, respectively.
35+
36+
The response will be like:
37+
38+
\`\`\`json
39+
{
40+
"access_token": "eyJhbG...2g", // Use this value for accessing the Logto Management API
41+
"expires_in": 3600, // Token expiration in seconds
42+
"token_type": "Bearer", // Token type for your request when using the access token
43+
"scope": "all" // Scope \`all\` for Logto Management API
44+
}
45+
\`\`\`
46+
47+
### Use the access token
48+
49+
Once you have the access token, you can use it to authenticate your requests to the Logto Management API. The access token should be included in the \`Authorization\` header of your requests with the \`Bearer\` authentication scheme.
50+
51+
Here is an example of how to list the first page of users in your Logto tenant:
52+
53+
\`\`\`bash
54+
curl --location \
55+
--request GET 'https://[tenant-id].logto.app/api/users' \
56+
--header 'Authorization: Bearer eyJhbG...2g'
57+
\`\`\`
58+
59+
Replace \`[tenant-id]\` with your Logto tenant ID and \`eyJhbG...2g\` with the access token you fetched earlier.`;

packages/core/src/routes/swagger/index.ts

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import fs from 'node:fs/promises';
22
import { fileURLToPath } from 'node:url';
33

44
import { httpCodeToMessage } from '@logto/core-kit';
5-
import { condArray, condString, conditionalArray, deduplicate } from '@silverhand/essentials';
5+
import { cond, condArray, condString, conditionalArray, deduplicate } from '@silverhand/essentials';
66
import deepmerge from 'deepmerge';
77
import { findUp } from 'find-up';
88
import type { IMiddleware } from 'koa-router';
@@ -21,6 +21,7 @@ import { translationSchemas, zodTypeToSwagger } from '#src/utils/zod.js';
2121

2222
import type { AnonymousRouter } from '../types.js';
2323

24+
import { managementApiDescription } from './consts.js';
2425
import {
2526
buildTag,
2627
devFeatureTag,
@@ -38,6 +39,14 @@ import {
3839
customParameters,
3940
} from './utils/parameters.js';
4041

42+
const anonymousPaths = new Set<string>([
43+
'interaction',
44+
'.well-known',
45+
'authn',
46+
'swagger.json',
47+
'status',
48+
]);
49+
4150
type RouteObject = {
4251
path: string;
4352
method: OpenAPIV3.HttpMethods;
@@ -99,11 +108,14 @@ const buildOperation = (
99108
})
100109
);
101110

111+
const [firstSegment] = path.split('/').slice(1);
112+
102113
return {
103114
tags: [buildTag(path)],
104115
parameters: [...pathParameters, ...queryParameters],
105116
requestBody,
106117
responses,
118+
security: cond(firstSegment && anonymousPaths.has(firstSegment) && []),
107119
};
108120
};
109121

@@ -232,15 +244,24 @@ export default function swaggerRoutes<T extends AnonymousRouter, R extends Route
232244
info: {
233245
title: 'Logto API references',
234246
description:
235-
'API references for Logto services. To learn more about how to interact with Logto APIs, see [Interact with Management API](https://docs.logto.io/docs/recipes/interact-with-management-api/).' +
247+
'API references for Logto services.' +
236248
condString(
237249
EnvSet.values.isCloud &&
238250
'\n\nNote: The documentation is for Logto Cloud. If you are using Logto OSS, please refer to the response of `/api/swagger.json` endpoint on your Logto instance.'
239251
),
240252
version: 'Cloud',
241253
},
242254
paths: Object.fromEntries(pathMap),
255+
security: [{ ManagementApi: [] }],
243256
components: {
257+
securitySchemes: {
258+
ManagementApi: {
259+
type: 'http',
260+
scheme: 'bearer',
261+
bearerFormat: 'JWT',
262+
description: managementApiDescription,
263+
},
264+
},
244265
schemas: translationSchemas,
245266
parameters: identifiableEntityNames.reduce(
246267
(previous, entityName) => ({

0 commit comments

Comments
 (0)