mirror of
https://github.com/OpenAPITools/openapi-generator.git
synced 2026-03-18 06:49:07 +00:00
Add Bearer authentication support to Python client (#1999)
* add bearer auth support to python * add bearer auth support to python * update python oas2 petstore samples * update samples * add bearer format * update php symfony samle
This commit is contained in:
@@ -29,6 +29,7 @@ public class CodegenSecurity {
|
||||
public Boolean hasMore, isBasic, isOAuth, isApiKey;
|
||||
// is Basic is true for all http authentication type. Those are to differentiate basic and bearer authentication
|
||||
public Boolean isBasicBasic, isBasicBearer;
|
||||
public String bearerFormat;
|
||||
public Map<String, Object> vendorExtensions = new HashMap<String, Object>();
|
||||
// ApiKey specific
|
||||
public String keyParamName;
|
||||
@@ -58,6 +59,12 @@ public class CodegenSecurity {
|
||||
return false;
|
||||
if (isBasic != null ? !isBasic.equals(that.isBasic) : that.isBasic != null)
|
||||
return false;
|
||||
if (isBasicBasic != null ? !isBasicBasic.equals(that.isBasicBasic) : that.isBasicBasic != null)
|
||||
return false;
|
||||
if (isBasicBearer != null ? !isBasicBearer.equals(that.isBasicBearer) : that.isBasicBearer != null)
|
||||
return false;
|
||||
if (bearerFormat != null ? !bearerFormat.equals(that.bearerFormat) : that.bearerFormat != null)
|
||||
return false;
|
||||
if (isOAuth != null ? !isOAuth.equals(that.isOAuth) : that.isOAuth != null)
|
||||
return false;
|
||||
if (isApiKey != null ? !isApiKey.equals(that.isApiKey) : that.isApiKey != null)
|
||||
@@ -94,6 +101,9 @@ public class CodegenSecurity {
|
||||
result = 31 * result + (type != null ? type.hashCode() : 0);
|
||||
result = 31 * result + (hasMore != null ? hasMore.hashCode() : 0);
|
||||
result = 31 * result + (isBasic != null ? isBasic.hashCode() : 0);
|
||||
result = 31 * result + (isBasicBasic != null ? isBasicBasic.hashCode() : 0);
|
||||
result = 31 * result + (isBasicBearer != null ? isBasicBearer.hashCode() : 0);
|
||||
result = 31 * result + (bearerFormat != null ? bearerFormat.hashCode() : 0);
|
||||
result = 31 * result + (isOAuth != null ? isOAuth.hashCode() : 0);
|
||||
result = 31 * result + (isApiKey != null ? isApiKey.hashCode() : 0);
|
||||
result = 31 * result + (vendorExtensions != null ? vendorExtensions.hashCode() : 0);
|
||||
|
||||
@@ -3124,6 +3124,7 @@ public class DefaultCodegen implements CodegenConfig {
|
||||
}
|
||||
else if ("bearer".equals(securityScheme.getScheme())) {
|
||||
cs.isBasicBearer = true;
|
||||
cs.bearerFormat = securityScheme.getBearerFormat();
|
||||
}
|
||||
} else if (SecurityScheme.Type.OAUTH2.equals(securityScheme.getType())) {
|
||||
cs.isKeyInHeader = cs.isKeyInQuery = cs.isKeyInCookie = cs.isApiKey = cs.isBasic = false;
|
||||
|
||||
@@ -841,6 +841,10 @@ public class DefaultGenerator extends AbstractGenerator implements Generator {
|
||||
bundle.put("hasOAuthMethods", true);
|
||||
bundle.put("oauthMethods", getOAuthMethods(authMethods));
|
||||
}
|
||||
|
||||
if (hasBearerMethods(authMethods)) {
|
||||
bundle.put("hasBearerMethods", true);
|
||||
}
|
||||
}
|
||||
|
||||
List<CodegenServer> servers = config.fromServers(openAPI.getServers());
|
||||
@@ -1194,6 +1198,16 @@ public class DefaultGenerator extends AbstractGenerator implements Generator {
|
||||
return false;
|
||||
}
|
||||
|
||||
private boolean hasBearerMethods(List<CodegenSecurity> authMethods) {
|
||||
for (CodegenSecurity cs : authMethods) {
|
||||
if (cs.isBasicBearer) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private List<CodegenSecurity> getOAuthMethods(List<CodegenSecurity> authMethods) {
|
||||
List<CodegenSecurity> oauthMethods = new ArrayList<>();
|
||||
|
||||
|
||||
@@ -60,4 +60,28 @@ public class ProcessUtils {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if at least one operation has Bearer security schema defined
|
||||
*
|
||||
* @param objs Map of operations
|
||||
* @return True if at least one operation has Bearer security schema defined
|
||||
*/
|
||||
public static boolean hasBearerMethods(Map<String, Object> objs) {
|
||||
Map<String, Object> operations = (Map<String, Object>) objs.get("operations");
|
||||
if (operations != null) {
|
||||
List<CodegenOperation> ops = (List<CodegenOperation>) operations.get("operation");
|
||||
for (CodegenOperation operation : ops) {
|
||||
if (operation.authMethods != null && !operation.authMethods.isEmpty()) {
|
||||
for (CodegenSecurity cs : operation.authMethods) {
|
||||
if (cs.isBasicBearer) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user