Skip to content
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -630,6 +630,8 @@ void generateApis(List<File> files, List<Object> allOperations, List<Object> all

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);
Expand Down Expand Up @@ -779,30 +781,10 @@ Map<String, Object> buildSupportFileBundle(List<Object> allOperations, List<Obje
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()) {
Expand Down Expand Up @@ -830,6 +812,36 @@ Map<String, Object> buildSupportFileBundle(List<Object> allOperations, List<Obje
return bundle;
}

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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,24 @@ public static boolean hasHttpBasicMethods(List<CodegenSecurity> authMethods) {
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.
*
Expand All @@ -80,6 +98,24 @@ public static boolean hasApiKeyMethods(List<CodegenSecurity> authMethods) {
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.
Expand All @@ -99,6 +135,24 @@ public static boolean hasHttpSignatureMethods(List<CodegenSecurity> authMethods)
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.
*
Expand All @@ -116,6 +170,24 @@ public static boolean hasHttpBearerMethods(List<CodegenSecurity> authMethods) {
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.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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[] = [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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[] = [];
Expand Down Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -28,6 +29,7 @@ let defaultBasePath = 'http://petstore.swagger.io/v2';
// ===============================================

export enum UserApiApiKeys {
api_key,
}

export class UserApi {
Expand All @@ -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[] = [];
Expand Down Expand Up @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion samples/client/petstore/typescript-node/npm/api/petApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[] = [];
Expand Down
5 changes: 5 additions & 0 deletions samples/client/petstore/typescript-node/npm/api/storeApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[] = [];
Expand Down Expand Up @@ -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);
}
Expand Down
8 changes: 8 additions & 0 deletions samples/client/petstore/typescript-node/npm/api/userApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -28,6 +29,7 @@ let defaultBasePath = 'http://petstore.swagger.io/v2';
// ===============================================

export enum UserApiApiKeys {
api_key,
}

export class UserApi {
Expand All @@ -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[] = [];
Expand Down Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,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()) ->
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,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()) ->
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,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()) ->
{
Expand Down
3 changes: 3 additions & 0 deletions samples/server/petstore/php-slim4/.openapi-generator/FILES
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down