mirror of
https://github.com/GitbookIO/gitbook.git
synced 2026-09-16 23:55:20 +00:00
Handle optional security headers (#3128)
This commit is contained in:
@@ -0,0 +1,5 @@
|
|||||||
|
---
|
||||||
|
'@gitbook/react-openapi': patch
|
||||||
|
---
|
||||||
|
|
||||||
|
Handle optional security headers
|
||||||
@@ -1,8 +1,11 @@
|
|||||||
import type { OpenAPIV3_1 } from '@gitbook/openapi-parser';
|
|
||||||
import { InteractiveSection } from './InteractiveSection';
|
import { InteractiveSection } from './InteractiveSection';
|
||||||
import { Markdown } from './Markdown';
|
import { Markdown } from './Markdown';
|
||||||
import { OpenAPISchemaName } from './OpenAPISchemaName';
|
import { OpenAPISchemaName } from './OpenAPISchemaName';
|
||||||
import type { OpenAPIClientContext, OpenAPIOperationData } from './types';
|
import type {
|
||||||
|
OpenAPIClientContext,
|
||||||
|
OpenAPIOperationData,
|
||||||
|
OpenAPISecurityWithRequired,
|
||||||
|
} from './types';
|
||||||
import { resolveDescription } from './utils';
|
import { resolveDescription } from './utils';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -50,26 +53,36 @@ export function OpenAPISecurities(props: {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
function getLabelForType(security: OpenAPIV3_1.SecuritySchemeObject) {
|
function getLabelForType(security: OpenAPISecurityWithRequired) {
|
||||||
switch (security.type) {
|
switch (security.type) {
|
||||||
case 'apiKey':
|
case 'apiKey':
|
||||||
return (
|
return (
|
||||||
<OpenAPISchemaName
|
<OpenAPISchemaName
|
||||||
propertyName={security.name ?? 'apiKey'}
|
propertyName={security.name ?? 'apiKey'}
|
||||||
type="string"
|
type="string"
|
||||||
required
|
required={security.required}
|
||||||
/>
|
/>
|
||||||
);
|
);
|
||||||
case 'http':
|
case 'http':
|
||||||
if (security.scheme === 'basic') {
|
if (security.scheme === 'basic') {
|
||||||
return <OpenAPISchemaName propertyName="Authorization" type="string" required />;
|
return (
|
||||||
|
<OpenAPISchemaName
|
||||||
|
propertyName="Authorization"
|
||||||
|
type="string"
|
||||||
|
required={security.required}
|
||||||
|
/>
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (security.scheme === 'bearer') {
|
if (security.scheme === 'bearer') {
|
||||||
const description = resolveDescription(security);
|
const description = resolveDescription(security);
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<OpenAPISchemaName propertyName="Authorization" type="string" required />
|
<OpenAPISchemaName
|
||||||
|
propertyName="Authorization"
|
||||||
|
type="string"
|
||||||
|
required={security.required}
|
||||||
|
/>
|
||||||
{/** Show a default description if none is provided */}
|
{/** Show a default description if none is provided */}
|
||||||
{!description ? (
|
{!description ? (
|
||||||
<Markdown
|
<Markdown
|
||||||
@@ -81,11 +94,11 @@ function getLabelForType(security: OpenAPIV3_1.SecuritySchemeObject) {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
return <OpenAPISchemaName propertyName="HTTP" required />;
|
return <OpenAPISchemaName propertyName="HTTP" required={security.required} />;
|
||||||
case 'oauth2':
|
case 'oauth2':
|
||||||
return <OpenAPISchemaName propertyName="OAuth2" required />;
|
return <OpenAPISchemaName propertyName="OAuth2" required={security.required} />;
|
||||||
case 'openIdConnect':
|
case 'openIdConnect':
|
||||||
return <OpenAPISchemaName propertyName="OpenID Connect" required />;
|
return <OpenAPISchemaName propertyName="OpenID Connect" required={security.required} />;
|
||||||
default:
|
default:
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
return security.type;
|
return security.type;
|
||||||
|
|||||||
@@ -40,16 +40,27 @@ export async function resolveOpenAPIOperation(
|
|||||||
}
|
}
|
||||||
|
|
||||||
const servers = 'servers' in schema ? (schema.servers ?? []) : [];
|
const servers = 'servers' in schema ? (schema.servers ?? []) : [];
|
||||||
const security = flattenSecurities(operation.security ?? schema.security ?? []);
|
const security: OpenAPIV3_1.SecurityRequirementObject[] =
|
||||||
|
operation.security ?? schema.security ?? [];
|
||||||
|
|
||||||
|
// If security includes an empty object, it means that the security is optional
|
||||||
|
const isOptionalSecurity = security.some((entry) => Object.keys(entry).length === 0);
|
||||||
|
const flatSecurities = flattenSecurities(security);
|
||||||
|
|
||||||
// Resolve securities
|
// Resolve securities
|
||||||
const securities: OpenAPIOperationData['securities'] = [];
|
const securities: OpenAPIOperationData['securities'] = [];
|
||||||
for (const entry of security) {
|
for (const entry of flatSecurities) {
|
||||||
const securityKey = Object.keys(entry)[0];
|
const securityKey = Object.keys(entry)[0];
|
||||||
if (securityKey) {
|
if (securityKey) {
|
||||||
const securityScheme = schema.components?.securitySchemes?.[securityKey];
|
const securityScheme = schema.components?.securitySchemes?.[securityKey];
|
||||||
if (securityScheme && !checkIsReference(securityScheme)) {
|
if (securityScheme && !checkIsReference(securityScheme)) {
|
||||||
securities.push([securityKey, securityScheme]);
|
securities.push([
|
||||||
|
securityKey,
|
||||||
|
{
|
||||||
|
...securityScheme,
|
||||||
|
required: !isOptionalSecurity,
|
||||||
|
},
|
||||||
|
]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -57,6 +57,8 @@ export interface OpenAPIContext extends OpenAPIClientContext {
|
|||||||
specUrl: string;
|
specUrl: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export type OpenAPISecurityWithRequired = OpenAPIV3.SecuritySchemeObject & { required?: boolean };
|
||||||
|
|
||||||
export interface OpenAPIOperationData extends OpenAPICustomSpecProperties {
|
export interface OpenAPIOperationData extends OpenAPICustomSpecProperties {
|
||||||
path: string;
|
path: string;
|
||||||
method: string;
|
method: string;
|
||||||
@@ -68,5 +70,5 @@ export interface OpenAPIOperationData extends OpenAPICustomSpecProperties {
|
|||||||
operation: OpenAPIV3.OperationObject<OpenAPICustomOperationProperties>;
|
operation: OpenAPIV3.OperationObject<OpenAPICustomOperationProperties>;
|
||||||
|
|
||||||
/** Securities that should be used for this operation */
|
/** Securities that should be used for this operation */
|
||||||
securities: [string, OpenAPIV3.SecuritySchemeObject][];
|
securities: [string, OpenAPISecurityWithRequired][];
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user