Added optional api keys

This commit is contained in:
frikky
2020-06-11 17:11:15 +02:00
parent 31df620d71
commit 8b606620d0
2 changed files with 32 additions and 10 deletions
+9 -6
View File
@@ -242,6 +242,7 @@ func buildStructure(swagger *openapi3.Swagger, curHash string) (string, error) {
return appPath, nil return appPath, nil
} }
// This function generates the python code that's being used.
func makePythoncode(swagger *openapi3.Swagger, name, url, method string, parameters, optionalQueries []string) (string, string) { func makePythoncode(swagger *openapi3.Swagger, name, url, method string, parameters, optionalQueries []string) (string, string) {
method = strings.ToLower(method) method = strings.ToLower(method)
queryString := "" queryString := ""
@@ -259,9 +260,6 @@ func makePythoncode(swagger *openapi3.Swagger, name, url, method string, paramet
} }
} }
// How to add authentication?
// I think it should be like:
// async def(self, auth, baseurl, data):
// api.Authentication.Parameters[0].Value = "BearerAuth" // api.Authentication.Parameters[0].Value = "BearerAuth"
authenticationParameter := "" authenticationParameter := ""
authenticationSetup := "" authenticationSetup := ""
@@ -270,14 +268,15 @@ func makePythoncode(swagger *openapi3.Swagger, name, url, method string, paramet
if swagger.Components.SecuritySchemes != nil { if swagger.Components.SecuritySchemes != nil {
if swagger.Components.SecuritySchemes["BearerAuth"] != nil { if swagger.Components.SecuritySchemes["BearerAuth"] != nil {
authenticationParameter = ", apikey" authenticationParameter = ", apikey"
authenticationSetup = "headers[\"Authorization\"] = f\"Bearer {apikey}\"" authenticationSetup = "if apikey != \" \": headers[\"Authorization\"] = f\"Bearer {apikey}\""
} else if swagger.Components.SecuritySchemes["BasicAuth"] != nil { } else if swagger.Components.SecuritySchemes["BasicAuth"] != nil {
authenticationParameter = ", username, password" authenticationParameter = ", username, password"
authenticationAddin = ", auth=(username, password)" authenticationAddin = ", auth=(username, password)"
} else if swagger.Components.SecuritySchemes["ApiKeyAuth"] != nil { } else if swagger.Components.SecuritySchemes["ApiKeyAuth"] != nil {
authenticationParameter = ", apikey" authenticationParameter = ", apikey"
if swagger.Components.SecuritySchemes["ApiKeyAuth"].Value.In == "header" { if swagger.Components.SecuritySchemes["ApiKeyAuth"].Value.In == "header" {
authenticationSetup = fmt.Sprintf("headers[\"%s\"] = apikey", swagger.Components.SecuritySchemes["ApiKeyAuth"].Value.Name) // This is a way to bypass apikeys by passing " "
authenticationSetup = fmt.Sprintf(`if apikey != " ": headers["%s"] = apikey`, swagger.Components.SecuritySchemes["ApiKeyAuth"].Value.Name)
} else if swagger.Components.SecuritySchemes["ApiKeyAuth"].Value.In == "query" { } else if swagger.Components.SecuritySchemes["ApiKeyAuth"].Value.In == "query" {
// This might suck lol // This might suck lol
key := "?" key := "?"
@@ -285,7 +284,7 @@ func makePythoncode(swagger *openapi3.Swagger, name, url, method string, paramet
key = "&" key = "&"
} }
authenticationSetup = fmt.Sprintf("url+=f\"%s%s={apikey}\"", key, swagger.Components.SecuritySchemes["ApiKeyAuth"].Value.Name) authenticationSetup = fmt.Sprintf("if apikey != \" \": url+=f\"%s%s={apikey}\"", key, swagger.Components.SecuritySchemes["ApiKeyAuth"].Value.Name)
} }
} }
} }
@@ -444,6 +443,7 @@ func generateYaml(swagger *openapi3.Swagger, newmd5 string) (*openapi3.Swagger,
Description: "The apikey to use", Description: "The apikey to use",
Multiline: false, Multiline: false,
Required: true, Required: true,
Example: "The API key to use. Space = skip",
Schema: SchemaDefinition{ Schema: SchemaDefinition{
Type: "string", Type: "string",
}, },
@@ -460,6 +460,7 @@ func generateYaml(swagger *openapi3.Swagger, newmd5 string) (*openapi3.Swagger,
Description: "The apikey to use", Description: "The apikey to use",
Multiline: false, Multiline: false,
Required: true, Required: true,
Example: "The API key to use. Space = skip",
Schema: SchemaDefinition{ Schema: SchemaDefinition{
Type: "string", Type: "string",
}, },
@@ -475,6 +476,7 @@ func generateYaml(swagger *openapi3.Swagger, newmd5 string) (*openapi3.Swagger,
Description: "The username to use", Description: "The username to use",
Multiline: false, Multiline: false,
Required: true, Required: true,
Example: "The username to use",
Schema: SchemaDefinition{ Schema: SchemaDefinition{
Type: "string", Type: "string",
}, },
@@ -484,6 +486,7 @@ func generateYaml(swagger *openapi3.Swagger, newmd5 string) (*openapi3.Swagger,
Description: "The password to use", Description: "The password to use",
Multiline: false, Multiline: false,
Required: true, Required: true,
Example: "The password to use",
Schema: SchemaDefinition{ Schema: SchemaDefinition{
Type: "string", Type: "string",
}, },
+23 -4
View File
@@ -4,11 +4,13 @@ import {BrowserView, MobileView} from "react-device-detect";
import {Link} from 'react-router-dom'; import {Link} from 'react-router-dom';
import Paper from '@material-ui/core/Paper'; import Paper from '@material-ui/core/Paper';
import FormControlLabel from '@material-ui/core/FormControlLabel';
import Button from '@material-ui/core/Button'; import Button from '@material-ui/core/Button';
import Divider from '@material-ui/core/Divider'; import Divider from '@material-ui/core/Divider';
import Select from '@material-ui/core/Select'; import Select from '@material-ui/core/Select';
import MenuItem from '@material-ui/core/MenuItem'; import MenuItem from '@material-ui/core/MenuItem';
import FormControl from '@material-ui/core/FormControl'; import FormControl from '@material-ui/core/FormControl';
import Switch from '@material-ui/core/Switch';
import Dialog from '@material-ui/core/Dialog'; import Dialog from '@material-ui/core/Dialog';
import DialogTitle from '@material-ui/core/DialogTitle'; import DialogTitle from '@material-ui/core/DialogTitle';
import DialogContent from '@material-ui/core/DialogContent'; import DialogContent from '@material-ui/core/DialogContent';
@@ -196,6 +198,7 @@ const AppCreator = (props) => {
const [updater, setUpdater] = useState("tmp") const [updater, setUpdater] = useState("tmp")
const [baseUrl, setBaseUrl] = useState(""); const [baseUrl, setBaseUrl] = useState("");
const [actionsModalOpen, setActionsModalOpen] = useState(false); const [actionsModalOpen, setActionsModalOpen] = useState(false);
const [authenticationRequired, setAuthenticationRequired] = useState(false);
const [authenticationOption, setAuthenticationOption] = useState(authenticationOptions[0]); const [authenticationOption, setAuthenticationOption] = useState(authenticationOptions[0]);
const [parameterName, setParameterName] = useState(""); const [parameterName, setParameterName] = useState("");
const [parameterLocation, setParameterLocation] = useState(apikeySelection.length > 0 ? apikeySelection[0] : ""); const [parameterLocation, setParameterLocation] = useState(apikeySelection.length > 0 ? apikeySelection[0] : "");
@@ -433,6 +436,7 @@ const AppCreator = (props) => {
for (const [key, value] of Object.entries(securitySchemes)) { for (const [key, value] of Object.entries(securitySchemes)) {
if (value.scheme === "bearer") { if (value.scheme === "bearer") {
setAuthenticationOption("Bearer auth") setAuthenticationOption("Bearer auth")
setAuthenticationRequired(true)
break break
} else if (value.type === "apiKey") { } else if (value.type === "apiKey") {
setAuthenticationOption("API key") setAuthenticationOption("API key")
@@ -446,9 +450,11 @@ const AppCreator = (props) => {
console.log("PARAM NAME: ", value.name) console.log("PARAM NAME: ", value.name)
setParameterName(value.name) setParameterName(value.name)
setAuthenticationRequired(true)
break break
} else if (value.scheme === "basic") { } else if (value.scheme === "basic") {
setAuthenticationOption("Basic auth") setAuthenticationOption("Basic auth")
setAuthenticationRequired(true)
break break
} }
} }
@@ -704,7 +710,6 @@ const AppCreator = (props) => {
</a> </a>
</h4> </h4>
Users will be required to submit their API as the header "Authorization: Bearer APIKEY" Users will be required to submit their API as the header "Authorization: Bearer APIKEY"
<Divider style={{marginBottom: "10px", marginTop: "30px", height: "1px", width: "100%", backgroundColor: "grey"}}/>
</div> </div>
: null : null
@@ -717,7 +722,6 @@ const AppCreator = (props) => {
</a> </a>
</h4> </h4>
Users will be required to submit a valid username and password before using the API Users will be required to submit a valid username and password before using the API
<Divider style={{marginBottom: "10px", marginTop: "30px", height: "1px", width: "100%", backgroundColor: "grey"}}/>
</div> </div>
: null : null
@@ -850,7 +854,6 @@ const AppCreator = (props) => {
)} )}
)} )}
</Select> </Select>
<Divider style={{marginBottom: "10px", marginTop: "30px", height: "1px", width: "100%", backgroundColor: "grey"}}/>
</div> </div>
: null : null
@@ -1559,11 +1562,17 @@ const AppCreator = (props) => {
}} }}
/> />
<FormControl style={{marginTop: "15px",}} variant="outlined"> <FormControl style={{marginTop: "15px",}} variant="outlined">
<h5 style={{marginBottom: "10px", color: "white",}}>Authentication</h5> <h5 style={{marginBottom: "10px", color: "white",}}>Authentication
</h5>
<Select <Select
fullWidth fullWidth
onChange={(e) => { onChange={(e) => {
setAuthenticationOption(e.target.value) setAuthenticationOption(e.target.value)
if (e.target.value === "No authentication") {
setAuthenticationRequired(false)
} else {
setAuthenticationRequired(true)
}
}} }}
value={authenticationOption} value={authenticationOption}
style={{backgroundColor: inputColor, color: "white", height: "50px"}} style={{backgroundColor: inputColor, color: "white", height: "50px"}}
@@ -1578,6 +1587,16 @@ const AppCreator = (props) => {
{basicAuth} {basicAuth}
{bearerAuth} {bearerAuth}
{apiKey} {apiKey}
{authenticationOption === "No authentication" ? null :
<FormControlLabel
style={{color: "white", marginBottom: 0, marginTop: 20}}
label=<div style={{color: "white"}}>Authentication required (default true)</div>
control={<Switch checked={authenticationRequired} onChange={() => {
setAuthenticationRequired(!authenticationRequired)
}} />}
/>}
<Divider style={{marginBottom: "10px", marginTop: "30px", height: "1px", width: "100%", backgroundColor: "grey"}}/>
<div style={{marginTop: "25px"}}> <div style={{marginTop: "25px"}}>
{actionView} {actionView}
</div> </div>