Skip to content

Commit 7a00880

Browse files
authored
Improve support for OAuth2 security type (#3304)
1 parent b6b5975 commit 7a00880

File tree

13 files changed

+154
-13
lines changed

13 files changed

+154
-13
lines changed

.changeset/fifty-ducks-press.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@gitbook/react-openapi': patch
3+
'gitbook': patch
4+
---
5+
6+
Improve support for OAuth2 security type

packages/gitbook/src/components/DocumentView/OpenAPI/style.css

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,26 @@
310310
@apply py-2 border-b border-tint-subtle max-w-full flex-1;
311311
}
312312

313+
.openapi-securities-oauth-flows {
314+
@apply flex flex-col gap-2 divide-y divide-tint-subtle;
315+
}
316+
317+
.openapi-securities-oauth-content {
318+
@apply prose *:!prose-sm *:text-tint;
319+
}
320+
321+
.openapi-securities-oauth-content.openapi-markdown code {
322+
@apply text-xs;
323+
}
324+
325+
.openapi-securities-oauth-content ul {
326+
@apply !my-0;
327+
}
328+
329+
.openapi-securities-url {
330+
@apply ml-0.5 px-0.5 rounded hover:bg-tint transition-colors;
331+
}
332+
313333
.openapi-securities-body {
314334
@apply flex flex-col gap-2;
315335
}

packages/react-openapi/src/OpenAPISchemaName.tsx

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,14 @@ export function OpenAPISchemaName(props: OpenAPISchemaNameProps) {
2727
{propertyName}
2828
</span>
2929
) : null}
30-
<span>
31-
{type ? <span className="openapi-schema-type">{type}</span> : null}
32-
{additionalItems ? (
33-
<span className="openapi-schema-type">{additionalItems}</span>
34-
) : null}
35-
</span>
30+
{type || additionalItems ? (
31+
<span>
32+
{type ? <span className="openapi-schema-type">{type}</span> : null}
33+
{additionalItems ? (
34+
<span className="openapi-schema-type">{additionalItems}</span>
35+
) : null}
36+
</span>
37+
) : null}
3638
{schema?.readOnly ? (
3739
<span className="openapi-schema-readonly">
3840
{t(context.translation, 'read_only')}

packages/react-openapi/src/OpenAPISecurities.tsx

Lines changed: 111 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
import type { OpenAPIV3 } from '@gitbook/openapi-parser';
12
import { InteractiveSection } from './InteractiveSection';
23
import { Markdown } from './Markdown';
4+
import { OpenAPICopyButton } from './OpenAPICopyButton';
35
import { OpenAPISchemaName } from './OpenAPISchemaName';
46
import type { OpenAPIClientContext } from './context';
57
import { t } from './translate';
@@ -105,13 +107,7 @@ function getLabelForType(security: OpenAPISecurityWithRequired, context: OpenAPI
105107
/>
106108
);
107109
case 'oauth2':
108-
return (
109-
<OpenAPISchemaName
110-
context={context}
111-
propertyName="OAuth2"
112-
required={security.required}
113-
/>
114-
);
110+
return <OpenAPISchemaOAuth2Flows context={context} security={security} />;
115111
case 'openIdConnect':
116112
return (
117113
<OpenAPISchemaName
@@ -125,3 +121,111 @@ function getLabelForType(security: OpenAPISecurityWithRequired, context: OpenAPI
125121
return security.type;
126122
}
127123
}
124+
125+
function OpenAPISchemaOAuth2Flows(props: {
126+
context: OpenAPIClientContext;
127+
security: OpenAPIV3.OAuth2SecurityScheme & { required?: boolean };
128+
}) {
129+
const { context, security } = props;
130+
131+
const flows = Object.entries(security.flows ?? {});
132+
133+
return (
134+
<div className="openapi-securities-oauth-flows">
135+
{flows.map(([name, flow], index) => (
136+
<OpenAPISchemaOAuth2Item
137+
key={index}
138+
flow={flow}
139+
name={name}
140+
context={context}
141+
security={security}
142+
/>
143+
))}
144+
</div>
145+
);
146+
}
147+
148+
function OpenAPISchemaOAuth2Item(props: {
149+
flow: NonNullable<OpenAPIV3.OAuth2SecurityScheme['flows']>[keyof NonNullable<
150+
OpenAPIV3.OAuth2SecurityScheme['flows']
151+
>];
152+
name: string;
153+
context: OpenAPIClientContext;
154+
security: OpenAPIV3.OAuth2SecurityScheme & { required?: boolean };
155+
}) {
156+
const { flow, context, security, name } = props;
157+
158+
if (!flow) {
159+
return null;
160+
}
161+
162+
const scopes = Object.entries(flow?.scopes ?? {});
163+
164+
return (
165+
<div>
166+
<OpenAPISchemaName
167+
context={context}
168+
propertyName="OAuth2"
169+
type={name}
170+
required={security.required}
171+
/>
172+
<div className="openapi-securities-oauth-content openapi-markdown">
173+
{security.description ? <Markdown source={security.description} /> : null}
174+
{'authorizationUrl' in flow && flow.authorizationUrl ? (
175+
<span>
176+
Authorization URL:{' '}
177+
<OpenAPICopyButton
178+
value={flow.authorizationUrl}
179+
context={context}
180+
className="openapi-securities-url"
181+
withTooltip
182+
>
183+
{flow.authorizationUrl}
184+
</OpenAPICopyButton>
185+
</span>
186+
) : null}
187+
{'tokenUrl' in flow && flow.tokenUrl ? (
188+
<span>
189+
Token URL:{' '}
190+
<OpenAPICopyButton
191+
value={flow.tokenUrl}
192+
context={context}
193+
className="openapi-securities-url"
194+
withTooltip
195+
>
196+
{flow.tokenUrl}
197+
</OpenAPICopyButton>
198+
</span>
199+
) : null}
200+
{'refreshUrl' in flow && flow.refreshUrl ? (
201+
<span>
202+
Refresh URL:{' '}
203+
<OpenAPICopyButton
204+
value={flow.refreshUrl}
205+
context={context}
206+
className="openapi-securities-url"
207+
withTooltip
208+
>
209+
{flow.refreshUrl}
210+
</OpenAPICopyButton>
211+
</span>
212+
) : null}
213+
{scopes.length ? (
214+
<div>
215+
{t(context.translation, 'available_scopes')}:{' '}
216+
<ul>
217+
{scopes.map(([key, value]) => (
218+
<li key={key}>
219+
<OpenAPICopyButton value={key} context={context} withTooltip>
220+
<code>{key}</code>
221+
</OpenAPICopyButton>
222+
: {value}
223+
</li>
224+
))}
225+
</ul>
226+
</div>
227+
) : null}
228+
</div>
229+
</div>
230+
);
231+
}

packages/react-openapi/src/translations/de.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ export const de = {
3535
show: 'Zeige ${1}',
3636
hide: 'Verstecke ${1}',
3737
available_items: 'Verfügbare Elemente',
38+
available_scopes: 'Verfügbare scopes',
3839
properties: 'Eigenschaften',
3940
or: 'oder',
4041
and: 'und',

packages/react-openapi/src/translations/en.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ export const en = {
3535
show: 'Show ${1}',
3636
hide: 'Hide ${1}',
3737
available_items: 'Available items',
38+
available_scopes: 'Available scopes',
3839
possible_values: 'Possible values',
3940
properties: 'Properties',
4041
or: 'or',

packages/react-openapi/src/translations/es.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ export const es = {
3535
show: 'Mostrar ${1}',
3636
hide: 'Ocultar ${1}',
3737
available_items: 'Elementos disponibles',
38+
available_scopes: 'Scopes disponibles',
3839
properties: 'Propiedades',
3940
or: 'o',
4041
and: 'y',

packages/react-openapi/src/translations/fr.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ export const fr = {
3535
show: 'Afficher ${1}',
3636
hide: 'Masquer ${1}',
3737
available_items: 'Éléments disponibles',
38+
available_scopes: 'Scopes disponibles',
3839
properties: 'Propriétés',
3940
or: 'ou',
4041
and: 'et',

packages/react-openapi/src/translations/ja.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ export const ja = {
3535
show: '${1}を表示',
3636
hide: '${1}を非表示',
3737
available_items: '利用可能なアイテム',
38+
available_scopes: '利用可能なスコープ',
3839
properties: 'プロパティ',
3940
or: 'または',
4041
and: 'かつ',

packages/react-openapi/src/translations/nl.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ export const nl = {
3535
show: 'Toon ${1}',
3636
hide: 'Verberg ${1}',
3737
available_items: 'Beschikbare items',
38+
available_scopes: 'Beschikbare scopes',
3839
properties: 'Eigenschappen',
3940
or: 'of',
4041
and: 'en',

0 commit comments

Comments
 (0)