forked from loafle/openapi-generator-original
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:
parent
dbd80fbf8a
commit
930643b7bc
@ -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<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);
|
||||
}
|
||||
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<CodegenServer> 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.
|
||||
* <p>
|
||||
* Examples:
|
||||
* <p>
|
||||
* boolean hasOAuthMethods
|
||||
* <p>
|
||||
* List<CodegenSecurity> 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
|
||||
public List<File> generate() {
|
||||
if (openAPI == null) {
|
||||
|
@ -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<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.
|
||||
*
|
||||
@ -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<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
|
||||
* 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<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.
|
||||
*
|
||||
@ -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<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.
|
||||
*
|
||||
|
@ -40,8 +40,8 @@ export class PetApi {
|
||||
|
||||
protected authentications = {
|
||||
'default': <Authentication>new VoidAuth(),
|
||||
'petstore_auth': new OAuth(),
|
||||
'api_key': new ApiKeyAuth('header', 'api_key'),
|
||||
'petstore_auth': new OAuth(),
|
||||
}
|
||||
|
||||
protected interceptors: Interceptor[] = [];
|
||||
|
@ -40,6 +40,7 @@ export class StoreApi {
|
||||
protected authentications = {
|
||||
'default': <Authentication>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);
|
||||
}
|
||||
|
@ -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': <Authentication>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);
|
||||
}
|
||||
|
@ -40,8 +40,8 @@ export class PetApi {
|
||||
|
||||
protected authentications = {
|
||||
'default': <Authentication>new VoidAuth(),
|
||||
'petstore_auth': new OAuth(),
|
||||
'api_key': new ApiKeyAuth('header', 'api_key'),
|
||||
'petstore_auth': new OAuth(),
|
||||
}
|
||||
|
||||
protected interceptors: Interceptor[] = [];
|
||||
|
@ -40,6 +40,7 @@ export class StoreApi {
|
||||
protected authentications = {
|
||||
'default': <Authentication>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);
|
||||
}
|
||||
|
@ -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': <Authentication>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);
|
||||
}
|
||||
|
@ -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()) ->
|
||||
{
|
||||
|
@ -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()) ->
|
||||
{
|
||||
|
@ -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()) ->
|
||||
{
|
||||
|
@ -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
|
||||
|
Loading…
x
Reference in New Issue
Block a user