forked from swagger-api/swagger-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add mutualTLS auth option (swagger-api#9193)
Refs swagger-api#8020 Co-authored-by: Vladimír Gorej <[email protected]>
- Loading branch information
Showing
13 changed files
with
340 additions
and
5 deletions.
There are no files selected for viewing
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/** | ||
* @prettier | ||
*/ | ||
import { Map } from "immutable" | ||
import { createOnlyOAS31SelectorWrapper } from "../fn" | ||
|
||
export const definitionsToAuthorize = createOnlyOAS31SelectorWrapper( | ||
() => (oriSelector, system) => { | ||
const definitions = system.specSelectors.securityDefinitions() | ||
let list = oriSelector() | ||
|
||
if (!definitions) return list | ||
|
||
definitions.entrySeq().forEach(([defName, definition]) => { | ||
const type = definition.get("type") | ||
|
||
if (type === "mutualTLS") { | ||
list = list.push( | ||
new Map({ | ||
[defName]: definition, | ||
}) | ||
) | ||
} | ||
}) | ||
|
||
return list | ||
} | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,184 @@ | ||
/** | ||
* @prettier | ||
*/ | ||
import React from "react" | ||
import PropTypes from "prop-types" | ||
import ImPropTypes from "react-immutable-proptypes" | ||
|
||
class Auths extends React.Component { | ||
static propTypes = { | ||
definitions: ImPropTypes.iterable.isRequired, | ||
getComponent: PropTypes.func.isRequired, | ||
authSelectors: PropTypes.object.isRequired, | ||
authActions: PropTypes.object.isRequired, | ||
errSelectors: PropTypes.object.isRequired, | ||
specSelectors: PropTypes.object.isRequired, | ||
} | ||
|
||
constructor(props, context) { | ||
super(props, context) | ||
|
||
this.state = {} | ||
} | ||
|
||
onAuthChange = (auth) => { | ||
let { name } = auth | ||
|
||
this.setState({ [name]: auth }) | ||
} | ||
|
||
submitAuth = (e) => { | ||
e.preventDefault() | ||
|
||
let { authActions } = this.props | ||
authActions.authorizeWithPersistOption(this.state) | ||
} | ||
|
||
logoutClick = (e) => { | ||
e.preventDefault() | ||
|
||
let { authActions, definitions } = this.props | ||
let auths = definitions | ||
.map((val, key) => { | ||
return key | ||
}) | ||
.toArray() | ||
|
||
this.setState( | ||
auths.reduce((prev, auth) => { | ||
prev[auth] = "" | ||
return prev | ||
}, {}) | ||
) | ||
|
||
authActions.logoutWithPersistOption(auths) | ||
} | ||
|
||
close = (e) => { | ||
e.preventDefault() | ||
let { authActions } = this.props | ||
|
||
authActions.showDefinitions(false) | ||
} | ||
|
||
render() { | ||
let { definitions, getComponent, authSelectors, errSelectors } = this.props | ||
const AuthItem = getComponent("AuthItem") | ||
const Oauth2 = getComponent("oauth2", true) | ||
const Button = getComponent("Button") | ||
|
||
const authorized = authSelectors.authorized() | ||
const authorizedAuth = definitions.filter((definition, key) => { | ||
return !!authorized.get(key) | ||
}) | ||
const nonOauthDefinitions = definitions.filter( | ||
(schema) => | ||
schema.get("type") !== "oauth2" && schema.get("type") !== "mutualTLS" | ||
) | ||
const oauthDefinitions = definitions.filter( | ||
(schema) => schema.get("type") === "oauth2" | ||
) | ||
const mutualTLSDefinitions = definitions.filter( | ||
(schema) => schema.get("type") === "mutualTLS" | ||
) | ||
return ( | ||
<div className="auth-container"> | ||
{nonOauthDefinitions.size > 0 && ( | ||
<form onSubmit={this.submitAuth}> | ||
{nonOauthDefinitions | ||
.map((schema, name) => { | ||
return ( | ||
<AuthItem | ||
key={name} | ||
schema={schema} | ||
name={name} | ||
getComponent={getComponent} | ||
onAuthChange={this.onAuthChange} | ||
authorized={authorized} | ||
errSelectors={errSelectors} | ||
/> | ||
) | ||
}) | ||
.toArray()} | ||
<div className="auth-btn-wrapper"> | ||
{nonOauthDefinitions.size === authorizedAuth.size ? ( | ||
<Button | ||
className="btn modal-btn auth" | ||
onClick={this.logoutClick} | ||
aria-label="Remove authorization" | ||
> | ||
Logout | ||
</Button> | ||
) : ( | ||
<Button | ||
type="submit" | ||
className="btn modal-btn auth authorize" | ||
aria-label="Apply credentials" | ||
> | ||
Authorize | ||
</Button> | ||
)} | ||
<Button | ||
className="btn modal-btn auth btn-done" | ||
onClick={this.close} | ||
> | ||
Close | ||
</Button> | ||
</div> | ||
</form> | ||
)} | ||
|
||
{oauthDefinitions.size > 0 ? ( | ||
<div> | ||
<div className="scope-def"> | ||
<p> | ||
Scopes are used to grant an application different levels of | ||
access to data on behalf of the end user. Each API may declare | ||
one or more scopes. | ||
</p> | ||
<p> | ||
API requires the following scopes. Select which ones you want to | ||
grant to Swagger UI. | ||
</p> | ||
</div> | ||
{definitions | ||
.filter((schema) => schema.get("type") === "oauth2") | ||
.map((schema, name) => { | ||
return ( | ||
<div key={name}> | ||
<Oauth2 | ||
authorized={authorized} | ||
schema={schema} | ||
name={name} | ||
/> | ||
</div> | ||
) | ||
}) | ||
.toArray()} | ||
</div> | ||
) : null} | ||
{mutualTLSDefinitions.size > 0 && ( | ||
<div> | ||
{mutualTLSDefinitions | ||
.map((schema, name) => { | ||
return ( | ||
<AuthItem | ||
key={name} | ||
schema={schema} | ||
name={name} | ||
getComponent={getComponent} | ||
onAuthChange={this.onAuthChange} | ||
authorized={authorized} | ||
errSelectors={errSelectors} | ||
/> | ||
) | ||
}) | ||
.toArray()} | ||
</div> | ||
)} | ||
</div> | ||
) | ||
} | ||
} | ||
|
||
export default Auths |
29 changes: 29 additions & 0 deletions
29
src/core/plugins/oas31/components/auth/mutual-tls-auth.jsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/** | ||
* @prettier | ||
*/ | ||
import React from "react" | ||
import PropTypes from "prop-types" | ||
|
||
const MutualTLSAuth = ({ schema, getComponent }) => { | ||
const JumpToPath = getComponent("JumpToPath", true) | ||
return ( | ||
<div> | ||
<h4> | ||
{schema.get("name")} (mutualTLS){" "} | ||
<JumpToPath path={["securityDefinitions", schema.get("name")]} /> | ||
</h4> | ||
<p> | ||
Mutual TLS is required by this API/Operation. Certificates are managed | ||
via your Operating System and/or your browser. | ||
</p> | ||
<p>{schema.get("description")}</p> | ||
</div> | ||
) | ||
} | ||
|
||
MutualTLSAuth.propTypes = { | ||
schema: PropTypes.object.isRequired, | ||
getComponent: PropTypes.func.isRequired, | ||
} | ||
|
||
export default MutualTLSAuth |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/** | ||
* @prettier | ||
*/ | ||
import React from "react" | ||
|
||
import { createOnlyOAS31ComponentWrapper } from "../../fn" | ||
|
||
const AuthItem = createOnlyOAS31ComponentWrapper( | ||
({ originalComponent: Ori, ...props }) => { | ||
const { getComponent, schema } = props | ||
const MutualTLSAuth = getComponent("MutualTLSAuth", true) | ||
const type = schema.get("type") | ||
|
||
if (type === "mutualTLS") { | ||
return <MutualTLSAuth schema={schema} /> | ||
} | ||
|
||
return <Ori {...props} /> | ||
} | ||
) | ||
|
||
export default AuthItem |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
/** | ||
* @prettier | ||
*/ | ||
import React from "react" | ||
|
||
import { createOnlyOAS31ComponentWrapper } from "../fn" | ||
|
||
const AuthsWrapper = createOnlyOAS31ComponentWrapper( | ||
({ getSystem, ...props }) => { | ||
const system = getSystem() | ||
const OAS31Auths = system.getComponent("OAS31Auths", true) | ||
|
||
return <OAS31Auths {...props} /> | ||
} | ||
) | ||
|
||
export default AuthsWrapper |
Oops, something went wrong.