From 930643b7bcb6e75d41c3d60943d1975042bca169 Mon Sep 17 00:00:00 2001 From: devhl-labs Date: Sat, 7 Aug 2021 09:54:23 -0400 Subject: [PATCH] Add switches (authentication collections) to support files (#9884) * added authentication switches to support files * build samples * build samples * added documentation --- .../codegen/DefaultGenerator.java | 70 ++++++++++++------ .../codegen/utils/ProcessUtils.java | 72 +++++++++++++++++++ .../typescript-node/default/api/petApi.ts | 2 +- .../typescript-node/default/api/storeApi.ts | 5 ++ .../typescript-node/default/api/userApi.ts | 8 +++ .../typescript-node/npm/api/petApi.ts | 2 +- .../typescript-node/npm/api/storeApi.ts | 5 ++ .../typescript-node/npm/api/userApi.ts | 8 +++ .../erlang-server/src/openapi_pet_handler.erl | 4 +- .../src/openapi_store_handler.erl | 4 +- .../src/openapi_user_handler.erl | 4 +- .../php-slim4/.openapi-generator/FILES | 3 + 12 files changed, 159 insertions(+), 28 deletions(-) diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultGenerator.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultGenerator.java index d2e1bb4500d..404c49fd0d2 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultGenerator.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultGenerator.java @@ -630,6 +630,8 @@ public class DefaultGenerator implements Generator { allOperations.add(new HashMap<>(operation)); + addAuthenticationSwitches(operation); + for (String templateName : config.apiTemplateFiles().keySet()) { String filename = config.apiFilename(templateName, tag); File written = processTemplateToFile(operation, templateName, filename, generateApis, CodegenConstants.APIS); @@ -779,30 +781,10 @@ public class DefaultGenerator implements Generator { bundle.put("models", allModels); bundle.put("apiFolder", config.apiPackage().replace('.', File.separatorChar)); bundle.put("modelPackage", config.modelPackage()); + bundle.put("library", config.getLibrary()); + // todo verify support and operation bundles have access to the common variables - Map securitySchemeMap = openAPI.getComponents() != null ? openAPI.getComponents().getSecuritySchemes() : null; - List authMethods = config.fromSecurity(securitySchemeMap); - if (authMethods != null && !authMethods.isEmpty()) { - bundle.put("authMethods", authMethods); - bundle.put("hasAuthMethods", true); - - if (ProcessUtils.hasOAuthMethods(authMethods)) { - bundle.put("hasOAuthMethods", true); - bundle.put("oauthMethods", ProcessUtils.getOAuthMethods(authMethods)); - } - if (ProcessUtils.hasHttpBearerMethods(authMethods)) { - bundle.put("hasHttpBearerMethods", true); - } - if (ProcessUtils.hasHttpSignatureMethods(authMethods)) { - bundle.put("hasHttpSignatureMethods", true); - } - if (ProcessUtils.hasHttpBasicMethods(authMethods)) { - bundle.put("hasHttpBasicMethods", true); - } - if (ProcessUtils.hasApiKeyMethods(authMethods)) { - bundle.put("hasApiKeyMethods", true); - } - } + addAuthenticationSwitches(bundle); List servers = config.fromServers(openAPI.getServers()); if (servers != null && !servers.isEmpty()) { @@ -830,6 +812,48 @@ public class DefaultGenerator implements Generator { return bundle; } + /** + * Add authentication methods to the given map + * This adds a boolean and a collection for each authentication type to the map. + *

+ * Examples: + *

+ * boolean hasOAuthMethods + *

+ * List<CodegenSecurity> oauthMethods + * + * @param bundle the map which the booleans and collections will be added + */ + void addAuthenticationSwitches(Map bundle){ + Map securitySchemeMap = openAPI.getComponents() != null ? openAPI.getComponents().getSecuritySchemes() : null; + List authMethods = config.fromSecurity(securitySchemeMap); + if (authMethods != null && !authMethods.isEmpty()) { + bundle.put("authMethods", authMethods); + bundle.put("hasAuthMethods", true); + + if (ProcessUtils.hasOAuthMethods(authMethods)) { + bundle.put("hasOAuthMethods", true); + bundle.put("oauthMethods", ProcessUtils.getOAuthMethods(authMethods)); + } + if (ProcessUtils.hasHttpBearerMethods(authMethods)) { + bundle.put("hasHttpBearerMethods", true); + bundle.put("httpBearerMethods", ProcessUtils.getHttpBearerMethods(authMethods)); + } + if (ProcessUtils.hasHttpSignatureMethods(authMethods)) { + bundle.put("hasHttpSignatureMethods", true); + bundle.put("httpSignatureMethods", ProcessUtils.getHttpSignatureMethods(authMethods)); + } + if (ProcessUtils.hasHttpBasicMethods(authMethods)) { + bundle.put("hasHttpBasicMethods", true); + bundle.put("httpBasicMethods", ProcessUtils.getHttpBasicMethods(authMethods)); + } + if (ProcessUtils.hasApiKeyMethods(authMethods)) { + bundle.put("hasApiKeyMethods", true); + bundle.put("apiKeyMethods", ProcessUtils.getApiKeyMethods(authMethods)); + } + } + } + @Override public List generate() { if (openAPI == null) { diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/utils/ProcessUtils.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/utils/ProcessUtils.java index 167c7d21124..9042eb92b39 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/utils/ProcessUtils.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/utils/ProcessUtils.java @@ -63,6 +63,24 @@ public class ProcessUtils { return false; } + /** + * Returns a list of OAS Codegen security objects + * + * @param authMethods List of auth methods. + * @return A list of OAS Codegen security objects + */ + public static List getHttpBasicMethods(List authMethods) { + List httpBasicMethods = new ArrayList<>(); + + for (CodegenSecurity cs : authMethods) { + if (Boolean.TRUE.equals(cs.isBasicBasic)) { + httpBasicMethods.add(cs); + } + } + + return httpBasicMethods; + } + /** * Returns true if the specified OAS model has at least one operation with API keys. * @@ -80,6 +98,24 @@ public class ProcessUtils { return false; } + /** + * Returns a list of OAS Codegen security objects + * + * @param authMethods List of auth methods. + * @return A list of OAS Codegen security objects + */ + public static List getApiKeyMethods(List authMethods) { + List apiKeyMethods = new ArrayList<>(); + + for (CodegenSecurity cs : authMethods) { + if (Boolean.TRUE.equals(cs.isApiKey)) { + apiKeyMethods.add(cs); + } + } + + return apiKeyMethods; + } + /** * Returns true if the specified OAS model has at least one operation with the HTTP basic * security scheme. @@ -99,6 +135,24 @@ public class ProcessUtils { return false; } + /** + * Returns a list of OAS Codegen security objects + * + * @param authMethods List of auth methods. + * @return A list of OAS Codegen security objects + */ + public static List getHttpSignatureMethods(List authMethods) { + List httpSignatureMethods = new ArrayList<>(); + + for (CodegenSecurity cs : authMethods) { + if (Boolean.TRUE.equals(cs.isHttpSignature)) { + httpSignatureMethods.add(cs); + } + } + + return httpSignatureMethods; + } + /** * Returns true if the specified OAS model has at least one operation with HTTP bearer. * @@ -116,6 +170,24 @@ public class ProcessUtils { return false; } + /** + * Returns a list of Bearer Codegen security objects + * + * @param authMethods List of auth methods. + * @return A list of Bearer Codegen security objects + */ + public static List getHttpBearerMethods(List authMethods) { + List httpBearerMethods = new ArrayList<>(); + + for (CodegenSecurity cs : authMethods) { + if (Boolean.TRUE.equals(cs.isBasicBearer)) { + httpBearerMethods.add(cs); + } + } + + return httpBearerMethods; + } + /** * Returns true if the specified OAS model has at least one operation with OAuth. * diff --git a/samples/client/petstore/typescript-node/default/api/petApi.ts b/samples/client/petstore/typescript-node/default/api/petApi.ts index 87004460f28..1dfd5447c6f 100644 --- a/samples/client/petstore/typescript-node/default/api/petApi.ts +++ b/samples/client/petstore/typescript-node/default/api/petApi.ts @@ -40,8 +40,8 @@ export class PetApi { protected authentications = { 'default': new VoidAuth(), - 'petstore_auth': new OAuth(), 'api_key': new ApiKeyAuth('header', 'api_key'), + 'petstore_auth': new OAuth(), } protected interceptors: Interceptor[] = []; diff --git a/samples/client/petstore/typescript-node/default/api/storeApi.ts b/samples/client/petstore/typescript-node/default/api/storeApi.ts index 5935259f0b8..500c6325664 100644 --- a/samples/client/petstore/typescript-node/default/api/storeApi.ts +++ b/samples/client/petstore/typescript-node/default/api/storeApi.ts @@ -40,6 +40,7 @@ export class StoreApi { protected authentications = { 'default': new VoidAuth(), 'api_key': new ApiKeyAuth('header', 'api_key'), + 'petstore_auth': new OAuth(), } protected interceptors: Interceptor[] = []; @@ -85,6 +86,10 @@ export class StoreApi { (this.authentications as any)[StoreApiApiKeys[key]].apiKey = value; } + set accessToken(token: string) { + this.authentications.petstore_auth.accessToken = token; + } + public addInterceptor(interceptor: Interceptor) { this.interceptors.push(interceptor); } diff --git a/samples/client/petstore/typescript-node/default/api/userApi.ts b/samples/client/petstore/typescript-node/default/api/userApi.ts index ab4675de90a..3a29f079eaf 100644 --- a/samples/client/petstore/typescript-node/default/api/userApi.ts +++ b/samples/client/petstore/typescript-node/default/api/userApi.ts @@ -18,6 +18,7 @@ import http from 'http'; import { User } from '../model/user'; import { ObjectSerializer, Authentication, VoidAuth, Interceptor } from '../model/models'; +import { HttpBasicAuth, HttpBearerAuth, ApiKeyAuth, OAuth } from '../model/models'; import { HttpError, RequestFile } from './apis'; @@ -28,6 +29,7 @@ let defaultBasePath = 'http://petstore.swagger.io/v2'; // =============================================== export enum UserApiApiKeys { + api_key, } export class UserApi { @@ -37,6 +39,8 @@ export class UserApi { protected authentications = { 'default': new VoidAuth(), + 'api_key': new ApiKeyAuth('header', 'api_key'), + 'petstore_auth': new OAuth(), } protected interceptors: Interceptor[] = []; @@ -82,6 +86,10 @@ export class UserApi { (this.authentications as any)[UserApiApiKeys[key]].apiKey = value; } + set accessToken(token: string) { + this.authentications.petstore_auth.accessToken = token; + } + public addInterceptor(interceptor: Interceptor) { this.interceptors.push(interceptor); } diff --git a/samples/client/petstore/typescript-node/npm/api/petApi.ts b/samples/client/petstore/typescript-node/npm/api/petApi.ts index 87004460f28..1dfd5447c6f 100644 --- a/samples/client/petstore/typescript-node/npm/api/petApi.ts +++ b/samples/client/petstore/typescript-node/npm/api/petApi.ts @@ -40,8 +40,8 @@ export class PetApi { protected authentications = { 'default': new VoidAuth(), - 'petstore_auth': new OAuth(), 'api_key': new ApiKeyAuth('header', 'api_key'), + 'petstore_auth': new OAuth(), } protected interceptors: Interceptor[] = []; diff --git a/samples/client/petstore/typescript-node/npm/api/storeApi.ts b/samples/client/petstore/typescript-node/npm/api/storeApi.ts index 5935259f0b8..500c6325664 100644 --- a/samples/client/petstore/typescript-node/npm/api/storeApi.ts +++ b/samples/client/petstore/typescript-node/npm/api/storeApi.ts @@ -40,6 +40,7 @@ export class StoreApi { protected authentications = { 'default': new VoidAuth(), 'api_key': new ApiKeyAuth('header', 'api_key'), + 'petstore_auth': new OAuth(), } protected interceptors: Interceptor[] = []; @@ -85,6 +86,10 @@ export class StoreApi { (this.authentications as any)[StoreApiApiKeys[key]].apiKey = value; } + set accessToken(token: string) { + this.authentications.petstore_auth.accessToken = token; + } + public addInterceptor(interceptor: Interceptor) { this.interceptors.push(interceptor); } diff --git a/samples/client/petstore/typescript-node/npm/api/userApi.ts b/samples/client/petstore/typescript-node/npm/api/userApi.ts index ab4675de90a..3a29f079eaf 100644 --- a/samples/client/petstore/typescript-node/npm/api/userApi.ts +++ b/samples/client/petstore/typescript-node/npm/api/userApi.ts @@ -18,6 +18,7 @@ import http from 'http'; import { User } from '../model/user'; import { ObjectSerializer, Authentication, VoidAuth, Interceptor } from '../model/models'; +import { HttpBasicAuth, HttpBearerAuth, ApiKeyAuth, OAuth } from '../model/models'; import { HttpError, RequestFile } from './apis'; @@ -28,6 +29,7 @@ let defaultBasePath = 'http://petstore.swagger.io/v2'; // =============================================== export enum UserApiApiKeys { + api_key, } export class UserApi { @@ -37,6 +39,8 @@ export class UserApi { protected authentications = { 'default': new VoidAuth(), + 'api_key': new ApiKeyAuth('header', 'api_key'), + 'petstore_auth': new OAuth(), } protected interceptors: Interceptor[] = []; @@ -82,6 +86,10 @@ export class UserApi { (this.authentications as any)[UserApiApiKeys[key]].apiKey = value; } + set accessToken(token: string) { + this.authentications.petstore_auth.accessToken = token; + } + public addInterceptor(interceptor: Interceptor) { this.interceptors.push(interceptor); } diff --git a/samples/server/petstore/erlang-server/src/openapi_pet_handler.erl b/samples/server/petstore/erlang-server/src/openapi_pet_handler.erl index a204918cc77..dc72a63c5d2 100644 --- a/samples/server/petstore/erlang-server/src/openapi_pet_handler.erl +++ b/samples/server/petstore/erlang-server/src/openapi_pet_handler.erl @@ -274,7 +274,9 @@ is_authorized( {false, AuthHeader, Req} -> {{false, AuthHeader}, Req, State} end; is_authorized(Req, State) -> - {true, Req, State}. + {{false, <<"">>}, Req, State}. +is_authorized(Req, State) -> + {{false, <<"">>}, Req, State}. -spec content_types_accepted(Req :: cowboy_req:req(), State :: state()) -> { diff --git a/samples/server/petstore/erlang-server/src/openapi_store_handler.erl b/samples/server/petstore/erlang-server/src/openapi_store_handler.erl index 1bc9cd38002..09245deb509 100644 --- a/samples/server/petstore/erlang-server/src/openapi_store_handler.erl +++ b/samples/server/petstore/erlang-server/src/openapi_store_handler.erl @@ -109,7 +109,9 @@ is_authorized( {false, AuthHeader, Req} -> {{false, AuthHeader}, Req, State} end; is_authorized(Req, State) -> - {true, Req, State}. + {{false, <<"">>}, Req, State}. +is_authorized(Req, State) -> + {{false, <<"">>}, Req, State}. -spec content_types_accepted(Req :: cowboy_req:req(), State :: state()) -> { diff --git a/samples/server/petstore/erlang-server/src/openapi_user_handler.erl b/samples/server/petstore/erlang-server/src/openapi_user_handler.erl index d466e7f5709..aad43103ac6 100644 --- a/samples/server/petstore/erlang-server/src/openapi_user_handler.erl +++ b/samples/server/petstore/erlang-server/src/openapi_user_handler.erl @@ -236,7 +236,9 @@ is_authorized( {false, AuthHeader, Req} -> {{false, AuthHeader}, Req, State} end; is_authorized(Req, State) -> - {true, Req, State}. + {{false, <<"">>}, Req, State}. +is_authorized(Req, State) -> + {{false, <<"">>}, Req, State}. -spec content_types_accepted(Req :: cowboy_req:req(), State :: state()) -> { diff --git a/samples/server/petstore/php-slim4/.openapi-generator/FILES b/samples/server/petstore/php-slim4/.openapi-generator/FILES index 463e80a1257..6d7926cf510 100644 --- a/samples/server/petstore/php-slim4/.openapi-generator/FILES +++ b/samples/server/petstore/php-slim4/.openapi-generator/FILES @@ -25,6 +25,9 @@ lib/Auth/AbstractAuthenticator.php lib/Auth/AbstractAuthenticator.php lib/Auth/AbstractAuthenticator.php lib/Auth/AbstractAuthenticator.php +lib/Auth/AbstractAuthenticator.php +lib/Auth/AbstractAuthenticator.php +lib/Auth/AbstractAuthenticator.php lib/BaseModel.php lib/Middleware/JsonBodyParserMiddleware.php lib/Model/ApiResponse.php