mirror of
https://github.com/Kingsrook/qqq.git
synced 2025-07-19 21:50:45 +00:00
cehckpoint - adding security to openapi spec
This commit is contained in:
@ -37,14 +37,19 @@ import com.kingsrook.qqq.api.model.openapi.ExampleWithListValue;
|
||||
import com.kingsrook.qqq.api.model.openapi.ExampleWithSingleValue;
|
||||
import com.kingsrook.qqq.api.model.openapi.Info;
|
||||
import com.kingsrook.qqq.api.model.openapi.Method;
|
||||
import com.kingsrook.qqq.api.model.openapi.OAuth2;
|
||||
import com.kingsrook.qqq.api.model.openapi.OAuth2Flow;
|
||||
import com.kingsrook.qqq.api.model.openapi.OpenAPI;
|
||||
import com.kingsrook.qqq.api.model.openapi.Parameter;
|
||||
import com.kingsrook.qqq.api.model.openapi.Path;
|
||||
import com.kingsrook.qqq.api.model.openapi.Response;
|
||||
import com.kingsrook.qqq.api.model.openapi.Schema;
|
||||
import com.kingsrook.qqq.api.model.openapi.SecurityScheme;
|
||||
import com.kingsrook.qqq.api.model.openapi.Server;
|
||||
import com.kingsrook.qqq.api.model.openapi.Tag;
|
||||
import com.kingsrook.qqq.backend.core.actions.AbstractQActionFunction;
|
||||
import com.kingsrook.qqq.backend.core.actions.permissions.PermissionsHelper;
|
||||
import com.kingsrook.qqq.backend.core.actions.permissions.TablePermissionSubType;
|
||||
import com.kingsrook.qqq.backend.core.context.QContext;
|
||||
import com.kingsrook.qqq.backend.core.exceptions.QException;
|
||||
import com.kingsrook.qqq.backend.core.model.metadata.QInstance;
|
||||
@ -91,11 +96,22 @@ public class GenerateOpenApiSpecAction extends AbstractQActionFunction<GenerateO
|
||||
openAPI.setTags(new ArrayList<>());
|
||||
openAPI.setPaths(new LinkedHashMap<>());
|
||||
|
||||
LinkedHashMap<Integer, Response> componentResponses = new LinkedHashMap<>();
|
||||
LinkedHashMap<String, Schema> componentSchemas = new LinkedHashMap<>();
|
||||
LinkedHashMap<Integer, Response> componentResponses = new LinkedHashMap<>();
|
||||
LinkedHashMap<String, Schema> componentSchemas = new LinkedHashMap<>();
|
||||
LinkedHashMap<String, SecurityScheme> securitySchemes = new LinkedHashMap<>();
|
||||
openAPI.setComponents(new Components()
|
||||
.withSchemas(componentSchemas)
|
||||
.withResponses(componentResponses)
|
||||
.withSecuritySchemes(securitySchemes)
|
||||
);
|
||||
|
||||
LinkedHashMap<String, String> scopes = new LinkedHashMap<>();
|
||||
securitySchemes.put("OAuth2", new OAuth2()
|
||||
.withFlows(MapBuilder.of("authorizationCode", new OAuth2Flow()
|
||||
.withAuthorizationUrl("https://nutrifresh-one-development.us.auth0.com/authorize")
|
||||
.withTokenUrl("https://nutrifresh-one-development.us.auth0.com/oauth/token")
|
||||
.withScopes(scopes)
|
||||
))
|
||||
);
|
||||
|
||||
componentSchemas.put("baseSearchResultFields", new Schema()
|
||||
@ -131,6 +147,12 @@ public class GenerateOpenApiSpecAction extends AbstractQActionFunction<GenerateO
|
||||
|
||||
List<? extends QFieldMetaData> tableApiFields = new GetTableApiFieldsAction().execute(new GetTableApiFieldsInput().withTableName(tableName).withVersion(version)).getFields();
|
||||
|
||||
String tableReadPermissionName = PermissionsHelper.getTablePermissionName(tableName, TablePermissionSubType.READ);
|
||||
if(StringUtils.hasContent(tableReadPermissionName))
|
||||
{
|
||||
scopes.put(tableReadPermissionName, "Permission to read the " + tableLabel + " table");
|
||||
}
|
||||
|
||||
////////////////////////
|
||||
// tag for this table //
|
||||
////////////////////////
|
||||
@ -224,7 +246,9 @@ public class GenerateOpenApiSpecAction extends AbstractQActionFunction<GenerateO
|
||||
.withContent(MapBuilder.of("application/json", new Content()
|
||||
.withSchema(new Schema().withRef("#/components/schemas/" + tableName + "SearchResult"))
|
||||
))
|
||||
);
|
||||
).withSecurity(ListBuilder.of(MapBuilder.of(
|
||||
"OAuth2", List.of(tableReadPermissionName)
|
||||
)));
|
||||
|
||||
for(QFieldMetaData tableApiField : tableApiFields)
|
||||
{
|
||||
|
@ -23,6 +23,7 @@ package com.kingsrook.qqq.api.model.metadata;
|
||||
|
||||
|
||||
import java.util.List;
|
||||
import com.kingsrook.qqq.api.ApiMiddlewareType;
|
||||
import com.kingsrook.qqq.api.model.APIVersion;
|
||||
import com.kingsrook.qqq.backend.core.model.metadata.QInstance;
|
||||
import com.kingsrook.qqq.backend.core.model.metadata.QMiddlewareInstanceMetaData;
|
||||
@ -40,6 +41,17 @@ public class ApiInstanceMetaData extends QMiddlewareInstanceMetaData
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Constructor
|
||||
**
|
||||
*******************************************************************************/
|
||||
public ApiInstanceMetaData()
|
||||
{
|
||||
setType(ApiMiddlewareType.NAME);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
**
|
||||
*******************************************************************************/
|
||||
|
@ -30,9 +30,10 @@ import java.util.Map;
|
||||
*******************************************************************************/
|
||||
public class Components
|
||||
{
|
||||
private Map<String, Example> examples;
|
||||
private Map<String, Schema> schemas;
|
||||
private Map<Integer, Response> responses;
|
||||
private Map<String, Example> examples;
|
||||
private Map<String, Schema> schemas;
|
||||
private Map<Integer, Response> responses;
|
||||
private Map<String, SecurityScheme> securitySchemes;
|
||||
|
||||
|
||||
|
||||
@ -127,4 +128,35 @@ public class Components
|
||||
return (this);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Getter for securitySchemes
|
||||
*******************************************************************************/
|
||||
public Map<String, SecurityScheme> getSecuritySchemes()
|
||||
{
|
||||
return (this.securitySchemes);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Setter for securitySchemes
|
||||
*******************************************************************************/
|
||||
public void setSecuritySchemes(Map<String, SecurityScheme> securitySchemes)
|
||||
{
|
||||
this.securitySchemes = securitySchemes;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Fluent setter for securitySchemes
|
||||
*******************************************************************************/
|
||||
public Components withSecuritySchemes(Map<String, SecurityScheme> securitySchemes)
|
||||
{
|
||||
this.securitySchemes = securitySchemes;
|
||||
return (this);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -40,6 +40,8 @@ public class Method
|
||||
private RequestBody requestBody;
|
||||
private List<Parameter> parameters;
|
||||
private Map<Integer, Response> responses;
|
||||
|
||||
private List<Map<String, List<String>>> security;
|
||||
|
||||
|
||||
|
||||
@ -303,4 +305,36 @@ public class Method
|
||||
this.responses.put(code, response);
|
||||
return (this);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Getter for security
|
||||
*******************************************************************************/
|
||||
public List<Map<String, List<String>>> getSecurity()
|
||||
{
|
||||
return (this.security);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Setter for security
|
||||
*******************************************************************************/
|
||||
public void setSecurity(List<Map<String, List<String>>> security)
|
||||
{
|
||||
this.security = security;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Fluent setter for security
|
||||
*******************************************************************************/
|
||||
public Method withSecurity(List<Map<String, List<String>>> security)
|
||||
{
|
||||
this.security = security;
|
||||
return (this);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -0,0 +1,77 @@
|
||||
/*
|
||||
* QQQ - Low-code Application Framework for Engineers.
|
||||
* Copyright (C) 2021-2023. Kingsrook, LLC
|
||||
* 651 N Broad St Ste 205 # 6917 | Middletown DE 19709 | United States
|
||||
* contact@kingsrook.com
|
||||
* https://github.com/Kingsrook/
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.kingsrook.qqq.api.model.openapi;
|
||||
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
**
|
||||
*******************************************************************************/
|
||||
public class OAuth2 extends SecurityScheme
|
||||
{
|
||||
private Map<String, OAuth2Flow> flows;
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Constructor
|
||||
**
|
||||
*******************************************************************************/
|
||||
public OAuth2()
|
||||
{
|
||||
setType("oauth2");
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Getter for flows
|
||||
*******************************************************************************/
|
||||
public Map<String, OAuth2Flow> getFlows()
|
||||
{
|
||||
return (this.flows);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Setter for flows
|
||||
*******************************************************************************/
|
||||
public void setFlows(Map<String, OAuth2Flow> flows)
|
||||
{
|
||||
this.flows = flows;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Fluent setter for flows
|
||||
*******************************************************************************/
|
||||
public OAuth2 withFlows(Map<String, OAuth2Flow> flows)
|
||||
{
|
||||
this.flows = flows;
|
||||
return (this);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,130 @@
|
||||
/*
|
||||
* QQQ - Low-code Application Framework for Engineers.
|
||||
* Copyright (C) 2021-2023. Kingsrook, LLC
|
||||
* 651 N Broad St Ste 205 # 6917 | Middletown DE 19709 | United States
|
||||
* contact@kingsrook.com
|
||||
* https://github.com/Kingsrook/
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.kingsrook.qqq.api.model.openapi;
|
||||
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
**
|
||||
*******************************************************************************/
|
||||
public class OAuth2Flow
|
||||
{
|
||||
private String authorizationUrl;
|
||||
private String tokenUrl;
|
||||
private Map<String, String> scopes;
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Getter for authorizationUrl
|
||||
*******************************************************************************/
|
||||
public String getAuthorizationUrl()
|
||||
{
|
||||
return (this.authorizationUrl);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Setter for authorizationUrl
|
||||
*******************************************************************************/
|
||||
public void setAuthorizationUrl(String authorizationUrl)
|
||||
{
|
||||
this.authorizationUrl = authorizationUrl;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Fluent setter for authorizationUrl
|
||||
*******************************************************************************/
|
||||
public OAuth2Flow withAuthorizationUrl(String authorizationUrl)
|
||||
{
|
||||
this.authorizationUrl = authorizationUrl;
|
||||
return (this);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Getter for tokenUrl
|
||||
*******************************************************************************/
|
||||
public String getTokenUrl()
|
||||
{
|
||||
return (this.tokenUrl);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Setter for tokenUrl
|
||||
*******************************************************************************/
|
||||
public void setTokenUrl(String tokenUrl)
|
||||
{
|
||||
this.tokenUrl = tokenUrl;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Fluent setter for tokenUrl
|
||||
*******************************************************************************/
|
||||
public OAuth2Flow withTokenUrl(String tokenUrl)
|
||||
{
|
||||
this.tokenUrl = tokenUrl;
|
||||
return (this);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Getter for scopes
|
||||
*******************************************************************************/
|
||||
public Map<String, String> getScopes()
|
||||
{
|
||||
return (this.scopes);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Setter for scopes
|
||||
*******************************************************************************/
|
||||
public void setScopes(Map<String, String> scopes)
|
||||
{
|
||||
this.scopes = scopes;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Fluent setter for scopes
|
||||
*******************************************************************************/
|
||||
public OAuth2Flow withScopes(Map<String, String> scopes)
|
||||
{
|
||||
this.scopes = scopes;
|
||||
return (this);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,63 @@
|
||||
/*
|
||||
* QQQ - Low-code Application Framework for Engineers.
|
||||
* Copyright (C) 2021-2023. Kingsrook, LLC
|
||||
* 651 N Broad St Ste 205 # 6917 | Middletown DE 19709 | United States
|
||||
* contact@kingsrook.com
|
||||
* https://github.com/Kingsrook/
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.kingsrook.qqq.api.model.openapi;
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
**
|
||||
*******************************************************************************/
|
||||
public class SecurityScheme
|
||||
{
|
||||
private String type;
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Getter for type
|
||||
*******************************************************************************/
|
||||
public String getType()
|
||||
{
|
||||
return (this.type);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Setter for type
|
||||
*******************************************************************************/
|
||||
public void setType(String type)
|
||||
{
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Fluent setter for type
|
||||
*******************************************************************************/
|
||||
public SecurityScheme withType(String type)
|
||||
{
|
||||
this.type = type;
|
||||
return (this);
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user