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

refactor(core): improve swagger auth description #6367

Merged
merged 1 commit into from
Jul 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
7 changes: 7 additions & 0 deletions .changeset/cyan-bottles-call.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@logto/core": patch
---

use native OpenAPI OAuth 2 security schema

The built-in OpenAPI OAuth 2 security schema is now used instead of the custom HTTP header-based security schema. This change improves compatibility with OpenAPI tools and libraries that support OAuth 2.
20 changes: 10 additions & 10 deletions packages/core/src/routes/swagger/consts.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
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/).
export const managementApiAuthDescription = `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/).

### Get started

Expand All @@ -21,13 +21,13 @@ For Logto Cloud users, the base URL is your Logto endpoint, i.e. \`https://[tena
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:

\`\`\`bash
curl --location \
--request POST 'https://[tenant-id].logto.app/oidc/token' \
--header 'Content-Type: application/x-www-form-urlencoded' \
--data-urlencode 'grant_type=client_credentials' \
--data-urlencode 'client_id=[app-id]' \
--data-urlencode 'client_secret=[app-secret]' \
--data-urlencode 'resource=https://[tenant-id].logto.app/api' \
curl --location \\
--request POST 'https://[tenant-id].logto.app/oidc/token' \\
--header 'Content-Type: application/x-www-form-urlencoded' \\
--data-urlencode 'grant_type=client_credentials' \\
--data-urlencode 'client_id=[app-id]' \\
--data-urlencode 'client_secret=[app-secret]' \\
--data-urlencode 'resource=https://[tenant-id].logto.app/api' \\
--data-urlencode 'scope=all'
\`\`\`

Expand All @@ -51,8 +51,8 @@ Once you have the access token, you can use it to authenticate your requests to
Here is an example of how to list the first page of users in your Logto tenant:

\`\`\`bash
curl --location \
--request GET 'https://[tenant-id].logto.app/api/users' \
curl --location \\
--request GET 'https://[tenant-id].logto.app/api/users' \\
--header 'Authorization: Bearer eyJhbG...2g'
\`\`\`

Expand Down
20 changes: 13 additions & 7 deletions packages/core/src/routes/swagger/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import { translationSchemas, zodTypeToSwagger } from '#src/utils/zod.js';

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

import { managementApiDescription } from './consts.js';
import { managementApiAuthDescription } from './consts.js';
import {
buildTag,
devFeatureTag,
Expand Down Expand Up @@ -270,14 +270,20 @@ export default function swaggerRoutes<T extends AnonymousRouter, R extends Route
version: 'Cloud',
},
paths: Object.fromEntries(pathMap),
security: [{ ManagementApi: [] }],
security: [{ OAuth2: ['all'] }],
components: {
securitySchemes: {
ManagementApi: {
type: 'http',
scheme: 'bearer',
bearerFormat: 'JWT',
description: managementApiDescription,
OAuth2: {
type: 'oauth2',
description: managementApiAuthDescription,
flows: {
clientCredentials: {
tokenUrl: '/oidc/token',
scopes: {
all: 'All scopes',
},
},
},
},
},
schemas: translationSchemas,
Expand Down
Loading