Add switches (authentication collections) to support files (#9884)

* added authentication switches to support files

* build samples

* build samples

* added documentation
This commit is contained in:
devhl-labs 2021-08-07 09:54:23 -04:00 committed by GitHub
parent dbd80fbf8a
commit 930643b7bc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 159 additions and 28 deletions

View File

@ -630,6 +630,8 @@ public class DefaultGenerator implements Generator {
allOperations.add(new HashMap<>(operation)); allOperations.add(new HashMap<>(operation));
addAuthenticationSwitches(operation);
for (String templateName : config.apiTemplateFiles().keySet()) { for (String templateName : config.apiTemplateFiles().keySet()) {
String filename = config.apiFilename(templateName, tag); String filename = config.apiFilename(templateName, tag);
File written = processTemplateToFile(operation, templateName, filename, generateApis, CodegenConstants.APIS); File written = processTemplateToFile(operation, templateName, filename, generateApis, CodegenConstants.APIS);
@ -779,30 +781,10 @@ public class DefaultGenerator implements Generator {
bundle.put("models", allModels); bundle.put("models", allModels);
bundle.put("apiFolder", config.apiPackage().replace('.', File.separatorChar)); bundle.put("apiFolder", config.apiPackage().replace('.', File.separatorChar));
bundle.put("modelPackage", config.modelPackage()); bundle.put("modelPackage", config.modelPackage());
bundle.put("library", config.getLibrary());
// todo verify support and operation bundles have access to the common variables
Map<String, SecurityScheme> securitySchemeMap = openAPI.getComponents() != null ? openAPI.getComponents().getSecuritySchemes() : null; addAuthenticationSwitches(bundle);
List<CodegenSecurity> 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);
}
}
List<CodegenServer> servers = config.fromServers(openAPI.getServers()); List<CodegenServer> servers = config.fromServers(openAPI.getServers());
if (servers != null && !servers.isEmpty()) { if (servers != null && !servers.isEmpty()) {
@ -830,6 +812,48 @@ public class DefaultGenerator implements Generator {
return bundle; return bundle;
} }
/**
* Add authentication methods to the given map
* This adds a boolean and a collection for each authentication type to the map.
* <p>
* Examples:
* <p>
* boolean hasOAuthMethods
* <p>
* List&lt;CodegenSecurity&gt; oauthMethods
*
* @param bundle the map which the booleans and collections will be added
*/
void addAuthenticationSwitches(Map<String, Object> bundle){
Map<String, SecurityScheme> securitySchemeMap = openAPI.getComponents() != null ? openAPI.getComponents().getSecuritySchemes() : null;
List<CodegenSecurity> 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 @Override
public List<File> generate() { public List<File> generate() {
if (openAPI == null) { if (openAPI == null) {

View File

@ -63,6 +63,24 @@ public class ProcessUtils {
return false; 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<CodegenSecurity> getHttpBasicMethods(List<CodegenSecurity> authMethods) {
List<CodegenSecurity> 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. * Returns true if the specified OAS model has at least one operation with API keys.
* *
@ -80,6 +98,24 @@ public class ProcessUtils {
return false; 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<CodegenSecurity> getApiKeyMethods(List<CodegenSecurity> authMethods) {
List<CodegenSecurity> 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 * Returns true if the specified OAS model has at least one operation with the HTTP basic
* security scheme. * security scheme.
@ -99,6 +135,24 @@ public class ProcessUtils {
return false; 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<CodegenSecurity> getHttpSignatureMethods(List<CodegenSecurity> authMethods) {
List<CodegenSecurity> 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. * Returns true if the specified OAS model has at least one operation with HTTP bearer.
* *
@ -116,6 +170,24 @@ public class ProcessUtils {
return false; 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<CodegenSecurity> getHttpBearerMethods(List<CodegenSecurity> authMethods) {
List<CodegenSecurity> 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. * Returns true if the specified OAS model has at least one operation with OAuth.
* *

View File

@ -40,8 +40,8 @@ export class PetApi {
protected authentications = { protected authentications = {
'default': <Authentication>new VoidAuth(), 'default': <Authentication>new VoidAuth(),
'petstore_auth': new OAuth(),
'api_key': new ApiKeyAuth('header', 'api_key'), 'api_key': new ApiKeyAuth('header', 'api_key'),
'petstore_auth': new OAuth(),
} }
protected interceptors: Interceptor[] = []; protected interceptors: Interceptor[] = [];

View File

@ -40,6 +40,7 @@ export class StoreApi {
protected authentications = { protected authentications = {
'default': <Authentication>new VoidAuth(), 'default': <Authentication>new VoidAuth(),
'api_key': new ApiKeyAuth('header', 'api_key'), 'api_key': new ApiKeyAuth('header', 'api_key'),
'petstore_auth': new OAuth(),
} }
protected interceptors: Interceptor[] = []; protected interceptors: Interceptor[] = [];
@ -85,6 +86,10 @@ export class StoreApi {
(this.authentications as any)[StoreApiApiKeys[key]].apiKey = value; (this.authentications as any)[StoreApiApiKeys[key]].apiKey = value;
} }
set accessToken(token: string) {
this.authentications.petstore_auth.accessToken = token;
}
public addInterceptor(interceptor: Interceptor) { public addInterceptor(interceptor: Interceptor) {
this.interceptors.push(interceptor); this.interceptors.push(interceptor);
} }

View File

@ -18,6 +18,7 @@ import http from 'http';
import { User } from '../model/user'; import { User } from '../model/user';
import { ObjectSerializer, Authentication, VoidAuth, Interceptor } from '../model/models'; import { ObjectSerializer, Authentication, VoidAuth, Interceptor } from '../model/models';
import { HttpBasicAuth, HttpBearerAuth, ApiKeyAuth, OAuth } from '../model/models';
import { HttpError, RequestFile } from './apis'; import { HttpError, RequestFile } from './apis';
@ -28,6 +29,7 @@ let defaultBasePath = 'http://petstore.swagger.io/v2';
// =============================================== // ===============================================
export enum UserApiApiKeys { export enum UserApiApiKeys {
api_key,
} }
export class UserApi { export class UserApi {
@ -37,6 +39,8 @@ export class UserApi {
protected authentications = { protected authentications = {
'default': <Authentication>new VoidAuth(), 'default': <Authentication>new VoidAuth(),
'api_key': new ApiKeyAuth('header', 'api_key'),
'petstore_auth': new OAuth(),
} }
protected interceptors: Interceptor[] = []; protected interceptors: Interceptor[] = [];
@ -82,6 +86,10 @@ export class UserApi {
(this.authentications as any)[UserApiApiKeys[key]].apiKey = value; (this.authentications as any)[UserApiApiKeys[key]].apiKey = value;
} }
set accessToken(token: string) {
this.authentications.petstore_auth.accessToken = token;
}
public addInterceptor(interceptor: Interceptor) { public addInterceptor(interceptor: Interceptor) {
this.interceptors.push(interceptor); this.interceptors.push(interceptor);
} }

View File

@ -40,8 +40,8 @@ export class PetApi {
protected authentications = { protected authentications = {
'default': <Authentication>new VoidAuth(), 'default': <Authentication>new VoidAuth(),
'petstore_auth': new OAuth(),
'api_key': new ApiKeyAuth('header', 'api_key'), 'api_key': new ApiKeyAuth('header', 'api_key'),
'petstore_auth': new OAuth(),
} }
protected interceptors: Interceptor[] = []; protected interceptors: Interceptor[] = [];

View File

@ -40,6 +40,7 @@ export class StoreApi {
protected authentications = { protected authentications = {
'default': <Authentication>new VoidAuth(), 'default': <Authentication>new VoidAuth(),
'api_key': new ApiKeyAuth('header', 'api_key'), 'api_key': new ApiKeyAuth('header', 'api_key'),
'petstore_auth': new OAuth(),
} }
protected interceptors: Interceptor[] = []; protected interceptors: Interceptor[] = [];
@ -85,6 +86,10 @@ export class StoreApi {
(this.authentications as any)[StoreApiApiKeys[key]].apiKey = value; (this.authentications as any)[StoreApiApiKeys[key]].apiKey = value;
} }
set accessToken(token: string) {
this.authentications.petstore_auth.accessToken = token;
}
public addInterceptor(interceptor: Interceptor) { public addInterceptor(interceptor: Interceptor) {
this.interceptors.push(interceptor); this.interceptors.push(interceptor);
} }

View File

@ -18,6 +18,7 @@ import http from 'http';
import { User } from '../model/user'; import { User } from '../model/user';
import { ObjectSerializer, Authentication, VoidAuth, Interceptor } from '../model/models'; import { ObjectSerializer, Authentication, VoidAuth, Interceptor } from '../model/models';
import { HttpBasicAuth, HttpBearerAuth, ApiKeyAuth, OAuth } from '../model/models';
import { HttpError, RequestFile } from './apis'; import { HttpError, RequestFile } from './apis';
@ -28,6 +29,7 @@ let defaultBasePath = 'http://petstore.swagger.io/v2';
// =============================================== // ===============================================
export enum UserApiApiKeys { export enum UserApiApiKeys {
api_key,
} }
export class UserApi { export class UserApi {
@ -37,6 +39,8 @@ export class UserApi {
protected authentications = { protected authentications = {
'default': <Authentication>new VoidAuth(), 'default': <Authentication>new VoidAuth(),
'api_key': new ApiKeyAuth('header', 'api_key'),
'petstore_auth': new OAuth(),
} }
protected interceptors: Interceptor[] = []; protected interceptors: Interceptor[] = [];
@ -82,6 +86,10 @@ export class UserApi {
(this.authentications as any)[UserApiApiKeys[key]].apiKey = value; (this.authentications as any)[UserApiApiKeys[key]].apiKey = value;
} }
set accessToken(token: string) {
this.authentications.petstore_auth.accessToken = token;
}
public addInterceptor(interceptor: Interceptor) { public addInterceptor(interceptor: Interceptor) {
this.interceptors.push(interceptor); this.interceptors.push(interceptor);
} }

View File

@ -274,7 +274,9 @@ is_authorized(
{false, AuthHeader, Req} -> {{false, AuthHeader}, Req, State} {false, AuthHeader, Req} -> {{false, AuthHeader}, Req, State}
end; end;
is_authorized(Req, State) -> 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()) -> -spec content_types_accepted(Req :: cowboy_req:req(), State :: state()) ->
{ {

View File

@ -109,7 +109,9 @@ is_authorized(
{false, AuthHeader, Req} -> {{false, AuthHeader}, Req, State} {false, AuthHeader, Req} -> {{false, AuthHeader}, Req, State}
end; end;
is_authorized(Req, State) -> 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()) -> -spec content_types_accepted(Req :: cowboy_req:req(), State :: state()) ->
{ {

View File

@ -236,7 +236,9 @@ is_authorized(
{false, AuthHeader, Req} -> {{false, AuthHeader}, Req, State} {false, AuthHeader, Req} -> {{false, AuthHeader}, Req, State}
end; end;
is_authorized(Req, State) -> 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()) -> -spec content_types_accepted(Req :: cowboy_req:req(), State :: state()) ->
{ {

View File

@ -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/Auth/AbstractAuthenticator.php
lib/Auth/AbstractAuthenticator.php
lib/Auth/AbstractAuthenticator.php
lib/BaseModel.php lib/BaseModel.php
lib/Middleware/JsonBodyParserMiddleware.php lib/Middleware/JsonBodyParserMiddleware.php
lib/Model/ApiResponse.php lib/Model/ApiResponse.php