import React from "react" import PropTypes from "prop-types" import oauth2Authorize from "core/oauth2-authorize" export default class Oauth2 extends React.Component { static propTypes = { name: PropTypes.string, authorized: PropTypes.object, getComponent: PropTypes.func.isRequired, schema: PropTypes.object.isRequired, authSelectors: PropTypes.object.isRequired, authActions: PropTypes.object.isRequired, errSelectors: PropTypes.object.isRequired, oas3Selectors: PropTypes.object.isRequired, specSelectors: PropTypes.object.isRequired, errActions: PropTypes.object.isRequired, getConfigs: PropTypes.any } constructor(props, context) { super(props, context) let { name, schema, authorized, authSelectors } = this.props let auth = authorized && authorized.get(name) let authConfigs = authSelectors.getConfigs() || {} let username = auth && auth.get("username") || "" let clientId = auth && auth.get("clientId") || authConfigs.clientId || "" let clientSecret = auth && auth.get("clientSecret") || authConfigs.clientSecret || "" let passwordType = auth && auth.get("passwordType") || "basic" let scopes = auth && auth.get("scopes") || authConfigs.scopes || [] if (typeof scopes === "string") { scopes = scopes.split(authConfigs.scopeSeparator || " ") } this.state = { appName: authConfigs.appName, name: name, schema: schema, scopes: scopes, clientId: clientId, clientSecret: clientSecret, username: username, password: "", passwordType: passwordType } } close = (e) => { e.preventDefault() let { authActions } = this.props authActions.showDefinitions(false) } authorize =() => { let { authActions, errActions, getConfigs, authSelectors, oas3Selectors } = this.props let configs = getConfigs() let authConfigs = authSelectors.getConfigs() errActions.clear({authId: name,type: "auth", source: "auth"}) oauth2Authorize({ auth: this.state, currentServer: oas3Selectors.serverEffectiveValue(oas3Selectors.selectedServer()), authActions, errActions, configs, authConfigs }) } onScopeChange =(e) => { let { target } = e let { checked } = target let scope = target.dataset.value if ( checked && this.state.scopes.indexOf(scope) === -1 ) { let newScopes = this.state.scopes.concat([scope]) this.setState({ scopes: newScopes }) } else if ( !checked && this.state.scopes.indexOf(scope) > -1) { this.setState({ scopes: this.state.scopes.filter((val) => val !== scope) }) } } onInputChange =(e) => { let { target : { dataset : { name }, value } } = e let state = { [name]: value } this.setState(state) } selectScopes =(e) => { if (e.target.dataset.all) { this.setState({ scopes: Array.from((this.props.schema.get("allowedScopes") || this.props.schema.get("scopes")).keys()) }) } else { this.setState({ scopes: [] }) } } logout =(e) => { e.preventDefault() let { authActions, errActions, name } = this.props errActions.clear({authId: name, type: "auth", source: "auth"}) authActions.logoutWithPersistOption([ name ]) } render() { let { schema, getComponent, authSelectors, errSelectors, name, specSelectors } = this.props const Input = getComponent("Input") const Row = getComponent("Row") const Col = getComponent("Col") const Button = getComponent("Button") const AuthError = getComponent("authError") const JumpToPath = getComponent("JumpToPath", true) const Markdown = getComponent("Markdown", true) const InitializedInput = getComponent("InitializedInput") const { isOAS3 } = specSelectors let oidcUrl = isOAS3() ? schema.get("openIdConnectUrl") : null // Auth type consts const AUTH_FLOW_IMPLICIT = "implicit" const AUTH_FLOW_PASSWORD = "password" const AUTH_FLOW_ACCESS_CODE = isOAS3() ? (oidcUrl ? "authorization_code" : "authorizationCode") : "accessCode" const AUTH_FLOW_APPLICATION = isOAS3() ? (oidcUrl ? "client_credentials" : "clientCredentials") : "application" let authConfigs = authSelectors.getConfigs() || {} let isPkceCodeGrant = !!authConfigs.usePkceWithAuthorizationCodeGrant let flow = schema.get("flow") let flowToDisplay = flow === AUTH_FLOW_ACCESS_CODE && isPkceCodeGrant ? flow + " with PKCE" : flow let scopes = schema.get("allowedScopes") || schema.get("scopes") let authorizedAuth = authSelectors.authorized().get(name) let isAuthorized = !!authorizedAuth let errors = errSelectors.allErrors().filter( err => err.get("authId") === name) let isValid = !errors.filter( err => err.get("source") === "validation").size let description = schema.get("description") return (

{name} (OAuth2, { flowToDisplay })

{ !this.state.appName ? null :
Application: { this.state.appName }
} { description && } { isAuthorized &&
Authorized
} { oidcUrl &&

OpenID Connect URL: { oidcUrl }

} { ( flow === AUTH_FLOW_IMPLICIT || flow === AUTH_FLOW_ACCESS_CODE ) &&

Authorization URL: { schema.get("authorizationUrl") }

} { ( flow === AUTH_FLOW_PASSWORD || flow === AUTH_FLOW_ACCESS_CODE || flow === AUTH_FLOW_APPLICATION ) &&

Token URL: { schema.get("tokenUrl") }

}

Flow: { flowToDisplay }

{ flow !== AUTH_FLOW_PASSWORD ? null : { isAuthorized ? { this.state.username } : } { } { isAuthorized ? ****** : } { isAuthorized ? { this.state.passwordType } : } } { ( flow === AUTH_FLOW_APPLICATION || flow === AUTH_FLOW_IMPLICIT || flow === AUTH_FLOW_ACCESS_CODE || flow === AUTH_FLOW_PASSWORD ) && ( !isAuthorized || isAuthorized && this.state.clientId) && { isAuthorized ? ****** : } } { ( (flow === AUTH_FLOW_APPLICATION || flow === AUTH_FLOW_ACCESS_CODE || flow === AUTH_FLOW_PASSWORD) && { isAuthorized ? ****** : } )} { !isAuthorized && scopes && scopes.size ?

Scopes: select all select none

{ scopes.map((description, name) => { return (
) }).toArray() }
: null } { errors.valueSeq().map( (error, key) => { return } ) }
{ isValid && ( isAuthorized ? : ) }
) } }