/* ============================================================================ * Copyright (c) Palo Alto Networks * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. * ========================================================================== */ import React from "react"; import Link from "@docusaurus/Link"; import { translate } from "@docusaurus/Translate"; import { useTypedSelector } from "@theme/ApiItem/hooks"; import { OPENAPI_SECURITY_SCHEMES } from "@theme/translationIds"; function SecuritySchemes(props: any) { const options = useTypedSelector((state: any) => state.auth.options); const selected = useTypedSelector((state: any) => state.auth.selected); const infoAuthPath = `/${props.infoPath}#authentication`; if (selected === undefined) return null; if (options[selected]?.[0]?.type === undefined) { return null; } const selectedAuth = options[selected]; const keyTranslations: Record = { description: { id: OPENAPI_SECURITY_SCHEMES.DESCRIPTION, message: "description:", }, scheme: { id: OPENAPI_SECURITY_SCHEMES.SCHEME, message: "scheme:", }, bearerFormat: { id: OPENAPI_SECURITY_SCHEMES.BEARER_FORMAT, message: "bearerFormat:", }, openIdConnectUrl: { id: OPENAPI_SECURITY_SCHEMES.OPEN_ID_CONNECT_URL, message: "openIdConnectUrl:", }, }; const renderRest = (rest: Record) => Object.keys(rest).map((k) => { const translation = keyTranslations[k]; const label = translation ? translate({ id: translation.id, message: translation.message }) : `${k}:`; return ( {label} {typeof rest[k] === "object" ? JSON.stringify(rest[k], null, 2) : String(rest[k])} ); }); return (

Authorization: {selectedAuth[0].name ?? selectedAuth[0].type}

{selectedAuth.map((auth: any) => { const isHttp = auth.type === "http"; const isApiKey = auth.type === "apiKey"; const isOauth2 = auth.type === "oauth2"; const isOpenId = auth.type === "openIdConnect"; if (isHttp) { if (auth.scheme === "bearer") { const { name, key, type, scopes, ...rest } = auth; return (
                  
                    
                      {translate({
                        id: OPENAPI_SECURITY_SCHEMES.NAME,
                        message: "name:",
                      })}
                    {" "}
                    {name ?? key}
                  
                  
                    
                      {translate({
                        id: OPENAPI_SECURITY_SCHEMES.TYPE,
                        message: "type:",
                      })}
                    {" "}
                    {type}
                  
                  {scopes && scopes.length > 0 && (
                    
                      
                        {translate({
                          id: OPENAPI_SECURITY_SCHEMES.SCOPES,
                          message: "scopes:",
                        })}
                      {" "}
                      
                        {auth.scopes.length > 0 ? auth.scopes.toString() : "[]"}
                      
                    
                  )}
                  {renderRest(rest)}
                
); } if (auth.scheme === "basic") { const { name, key, type, scopes, ...rest } = auth; return (
                  
                    
                      {translate({
                        id: OPENAPI_SECURITY_SCHEMES.NAME,
                        message: "name:",
                      })}
                    {" "}
                    {name ?? key}
                  
                  
                    
                      {translate({
                        id: OPENAPI_SECURITY_SCHEMES.TYPE,
                        message: "type:",
                      })}
                    {" "}
                    {type}
                  
                  {scopes && scopes.length > 0 && (
                    
                      
                        {translate({
                          id: OPENAPI_SECURITY_SCHEMES.SCOPES,
                          message: "scopes:",
                        })}
                      {" "}
                      
                        {auth.scopes.length > 0 ? auth.scopes.toString() : "[]"}
                      
                    
                  )}
                  {renderRest(rest)}
                
); } return (
                
                  
                    {translate({
                      id: OPENAPI_SECURITY_SCHEMES.NAME,
                      message: "name:",
                    })}
                  {" "}
                  {auth.name ?? auth.key}
                
                
                  
                    {translate({
                      id: OPENAPI_SECURITY_SCHEMES.TYPE,
                      message: "type:",
                    })}
                  {" "}
                  {auth.type}
                
                
                  
                    {translate({
                      id: OPENAPI_SECURITY_SCHEMES.IN,
                      message: "in:",
                    })}
                  {" "}
                  {auth.in}
                
              
); } if (isApiKey) { const { name, key, type, scopes, ...rest } = auth; return (
                
                  
                    {translate({
                      id: OPENAPI_SECURITY_SCHEMES.NAME,
                      message: "name:",
                    })}
                  {" "}
                  {name ?? key}
                
                
                  
                    {translate({
                      id: OPENAPI_SECURITY_SCHEMES.TYPE,
                      message: "type:",
                    })}
                  {" "}
                  {type}
                
                {scopes && scopes.length > 0 && (
                  
                    
                      {translate({
                        id: OPENAPI_SECURITY_SCHEMES.SCOPES,
                        message: "scopes:",
                      })}
                    {" "}
                    
                      {auth.scopes.length > 0 ? auth.scopes.toString() : "[]"}
                    
                  
                )}
                {renderRest(rest)}
              
); } if (isOauth2) { const { name, key, type, scopes, flows, ...rest } = auth; return (
                
                  
                    {translate({
                      id: OPENAPI_SECURITY_SCHEMES.NAME,
                      message: "name:",
                    })}
                  {" "}
                  {name ?? key}
                
                
                  
                    {translate({
                      id: OPENAPI_SECURITY_SCHEMES.TYPE,
                      message: "type:",
                    })}
                  {" "}
                  {type}
                
                {scopes && scopes.length > 0 && (
                  
                    
                      {translate({
                        id: OPENAPI_SECURITY_SCHEMES.SCOPES,
                        message: "scopes:",
                      })}
                    {" "}
                    
                      {auth.scopes.length > 0 ? auth.scopes.toString() : "[]"}
                    
                  
                )}
                {renderRest(rest)}
                {flows && (
                  
                    
                      
                        {translate({
                          id: OPENAPI_SECURITY_SCHEMES.FLOWS,
                          message: "flows:",
                        })}
                      {" "}
                      {JSON.stringify(flows, null, 2)}
                    
                  
                )}
              
); } if (isOpenId) { const { name, key, scopes, type, ...rest } = auth; return (
                
                  
                    {translate({
                      id: OPENAPI_SECURITY_SCHEMES.NAME,
                      message: "name:",
                    })}
                  {" "}
                  {name ?? key}
                
                
                  
                    {translate({
                      id: OPENAPI_SECURITY_SCHEMES.TYPE,
                      message: "type:",
                    })}
                  {" "}
                  {type}
                
                {scopes && scopes.length > 0 && (
                  
                    
                      {translate({
                        id: OPENAPI_SECURITY_SCHEMES.SCOPES,
                        message: "scopes:",
                      })}
                    {" "}
                    
                      {auth.scopes.length > 0 ? auth.scopes.toString() : "[]"}
                    
                  
                )}
                {renderRest(rest)}
              
); } return undefined; })}
); } export default SecuritySchemes;