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

feat: remove auth section #2022

Merged
merged 18 commits into from
May 30, 2022
Merged
Show file tree
Hide file tree
Changes from 15 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
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,6 @@ You can use all of the following options with the standalone version of the <red
* `lazyRendering` - _Not implemented yet_ ~~if set, enables lazy rendering mode in ReDoc. This mode is useful for APIs with big number of operations (e.g. > 50). In this mode ReDoc shows initial screen ASAP and then renders the rest operations asynchronously while showing progress bar on the top. Check out the [demo](\\redocly.github.io/redoc) for the example.~~
* `menuToggle` - if true clicking second time on expanded menu item will collapse it, default `true`.
* `nativeScrollbars` - use native scrollbar for sidemenu instead of perfect-scroll (scrolling performance optimization for big specs).
* `noAutoAuth` - do not inject Authentication section automatically.
* `onlyRequiredInSamples` - shows only required fields in request samples.
* `pathInMiddlePanel` - show path link and HTTP verb in the middle panel instead of the right one.
* `requiredPropsFirst` - show required properties first ordered in the same order as in `required` array.
Expand Down
70 changes: 70 additions & 0 deletions src/components/SecurityRequirement/OAuthFlow.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import * as React from 'react';
import { OpenAPISecurityScheme } from '../../types';
import { SecurityRow } from './styled.elements';
import { SeeMore } from '../SeeMore/SeeMore';
import { Markdown } from '../Markdown/Markdown';

export interface OAuthFlowProps {
type: string;
flow: OpenAPISecurityScheme['flows'][keyof OpenAPISecurityScheme['flows']];
RequiredScopes?: JSX.Element;
}

export class OAuthFlow extends React.PureComponent<OAuthFlowProps> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be great to use functional component here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

Copy link
Contributor

@Oprysk Oprysk May 30, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@anastasiia-developer Previously it was a PureComponent after refactoring it to the function component we need to keep the performance in the rendering perspective. So we might use React memo HOC here, what do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, we might
I'm going to add that

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

render() {
const { type, flow, RequiredScopes } = this.props;
const scopesNames = Object.keys(flow?.scopes || {});
return (
<>
<SecurityRow>
<b>Flow type: </b>
<code>{type} </code>
</SecurityRow>
{(type === 'implicit' || type === 'authorizationCode') && (
<SecurityRow>
<strong> Authorization URL: </strong>
<code>
<a target="_blank" rel="noopener noreferrer" href={(flow as any).authorizationUrl}>
{(flow as any).authorizationUrl}
</a>
</code>
</SecurityRow>
)}
{(type === 'password' || type === 'clientCredentials' || type === 'authorizationCode') && (
<SecurityRow>
<b> Token URL: </b>
<code>{(flow as any).tokenUrl}</code>
</SecurityRow>
)}
{flow!.refreshUrl && (
<SecurityRow>
<strong> Refresh URL: </strong>
{flow!.refreshUrl}
</SecurityRow>
)}
{!!scopesNames.length && (
<>
{RequiredScopes || null}
<SecurityRow>
<b> Scopes: </b>
</SecurityRow>
<SeeMore height="4em">
<ul>
{scopesNames.map(scope => (
<li key={scope}>
<code>{scope}</code> -{' '}
<Markdown
className={'redoc-markdown'}
inline={true}
source={flow!.scopes[scope] || ''}
/>
</li>
))}
</ul>
</SeeMore>
</>
)}
</>
);
}
}
18 changes: 18 additions & 0 deletions src/components/SecurityRequirement/RequiredScopesRow.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import * as React from 'react';

export const RequiredScopesRow = ({ scopes }: { scopes: string[] }): JSX.Element | null => {
if (!scopes.length) return null;

return (
<div>
<b>Required scopes: </b>
{scopes.map((scope, idx) => {
return (
<React.Fragment key={idx}>
<code>{scope}</code>{' '}
</React.Fragment>
);
})}
</div>
);
};
65 changes: 65 additions & 0 deletions src/components/SecurityRequirement/SecurityDetails.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import * as React from 'react';
import { SecuritySchemeModel } from '../../services';
import { titleize } from '../../utils';
import { StyledMarkdownBlock } from '../Markdown/styled.elements';
import { SecurityRow } from './styled.elements';
import { OAuthFlow } from './OAuthFlow';

interface SecuritySchemaProps {
RequiredScopes?: JSX.Element;
scheme: SecuritySchemeModel;
}
export function SecurityDetails(props: SecuritySchemaProps) {
const { RequiredScopes, scheme } = props;

return (
<StyledMarkdownBlock>
{scheme.apiKey ? (
<>
<SecurityRow>
<b>{titleize(scheme.apiKey.in || '')} parameter name: </b>
<code>{scheme.apiKey.name}</code>
</SecurityRow>
{RequiredScopes}
</>
) : scheme.http ? (
<>
<SecurityRow>
<b>HTTP Authorization Scheme: </b>
<code>{scheme.http.scheme}</code>
</SecurityRow>
<SecurityRow>
{scheme.http.scheme === 'bearer' && scheme.http.bearerFormat && (
<>
<b>Bearer format: </b>
<code>{scheme.http.bearerFormat}</code>
</>
)}
</SecurityRow>
{RequiredScopes}
</>
) : scheme.openId ? (
<>
<SecurityRow>
<b>Connect URL: </b>
<code>
<a target="_blank" rel="noopener noreferrer" href={scheme.openId.connectUrl}>
{scheme.openId.connectUrl}
</a>
</code>
</SecurityRow>
{RequiredScopes}
</>
) : scheme.flows ? (
Object.keys(scheme.flows).map(type => (
<OAuthFlow
key={type}
type={type}
RequiredScopes={RequiredScopes}
flow={scheme.flows[type]}
/>
))
) : null}
</StyledMarkdownBlock>
);
}
43 changes: 43 additions & 0 deletions src/components/SecurityRequirement/SecurityHeader.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { SecurityRequirementModel } from '../../services/models/SecurityRequirement';
import {
ScopeName,
SecurityRequirementAndWrap,
SecurityRequirementOrWrap,
} from './styled.elements';
import * as React from 'react';
import { AUTH_TYPES } from '../SecuritySchemes/SecuritySchemes';

export interface SecurityRequirementProps {
security: SecurityRequirementModel;
showSecuritySchemeType?: boolean;
expanded: boolean;
}

export function SecurityHeader(props: SecurityRequirementProps) {
const { security, showSecuritySchemeType, expanded } = props;

const grouping = security.schemes.length > 1;
return (
<SecurityRequirementOrWrap expanded={expanded}>
{grouping && '('}
{security.schemes.map(scheme => {
return (
<SecurityRequirementAndWrap key={scheme.id}>
{showSecuritySchemeType && `${AUTH_TYPES[scheme.type] || scheme.type}: `}
<i>{scheme.displayName}</i>
{expanded && scheme.scopes.length
? [
' (',
scheme.scopes.map<React.ReactNode>(scope => (
<ScopeName key={scope}>{scope}</ScopeName>
)),
') ',
]
: null}
</SecurityRequirementAndWrap>
);
})}
{grouping && ') '}
</SecurityRequirementOrWrap>
);
}
Loading