forked from loafle/openapi-generator-original
add multi auth setting (based on #1961)
This commit is contained in:
parent
812ff2f2c5
commit
a4825d8142
@ -618,30 +618,32 @@ public class DefaultGenerator extends AbstractGenerator implements Generator {
|
|||||||
// https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md#swagger-object
|
// https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md#swagger-object
|
||||||
// "there is a logical OR between the security requirements"
|
// "there is a logical OR between the security requirements"
|
||||||
if (securities.size() > 1) {
|
if (securities.size() > 1) {
|
||||||
LOGGER.warn("More than 1 security requirements are found, using only the first one");
|
// LOGGER.warn("More than 1 security requirements are found, using only the first one");
|
||||||
}
|
}
|
||||||
Map<String, List<String>> security = securities.get(0);
|
//Map<String, List<String>> security = securities.get(0);
|
||||||
for (String securityName : security.keySet()) {
|
for (Map<String, List<String>> security: securities) {
|
||||||
SecuritySchemeDefinition securityDefinition = fromSecurity(securityName);
|
for (String securityName : security.keySet()) {
|
||||||
if (securityDefinition != null) {
|
SecuritySchemeDefinition securityDefinition = fromSecurity(securityName);
|
||||||
if(securityDefinition instanceof OAuth2Definition) {
|
if (securityDefinition != null) {
|
||||||
OAuth2Definition oauth2Definition = (OAuth2Definition) securityDefinition;
|
if(securityDefinition instanceof OAuth2Definition) {
|
||||||
OAuth2Definition oauth2Operation = new OAuth2Definition();
|
OAuth2Definition oauth2Definition = (OAuth2Definition) securityDefinition;
|
||||||
oauth2Operation.setType(oauth2Definition.getType());
|
OAuth2Definition oauth2Operation = new OAuth2Definition();
|
||||||
oauth2Operation.setAuthorizationUrl(oauth2Definition.getAuthorizationUrl());
|
oauth2Operation.setType(oauth2Definition.getType());
|
||||||
oauth2Operation.setFlow(oauth2Definition.getFlow());
|
oauth2Operation.setAuthorizationUrl(oauth2Definition.getAuthorizationUrl());
|
||||||
oauth2Operation.setTokenUrl(oauth2Definition.getTokenUrl());
|
oauth2Operation.setFlow(oauth2Definition.getFlow());
|
||||||
oauth2Operation.setScopes(new HashMap<String, String>());
|
oauth2Operation.setTokenUrl(oauth2Definition.getTokenUrl());
|
||||||
for (String scope : security.get(securityName)) {
|
oauth2Operation.setScopes(new HashMap<String, String>());
|
||||||
if (oauth2Definition.getScopes().containsKey(scope)) {
|
for (String scope : security.get(securityName)) {
|
||||||
oauth2Operation.addScope(scope, oauth2Definition.getScopes().get(scope));
|
if (oauth2Definition.getScopes().containsKey(scope)) {
|
||||||
}
|
oauth2Operation.addScope(scope, oauth2Definition.getScopes().get(scope));
|
||||||
}
|
}
|
||||||
authMethods.put(securityName, oauth2Operation);
|
}
|
||||||
} else {
|
authMethods.put(securityName, oauth2Operation);
|
||||||
authMethods.put(securityName, securityDefinition);
|
} else {
|
||||||
}
|
authMethods.put(securityName, securityDefinition);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (!authMethods.isEmpty()) {
|
if (!authMethods.isEmpty()) {
|
||||||
co.authMethods = config.fromSecurity(authMethods);
|
co.authMethods = config.fromSecurity(authMethods);
|
||||||
|
@ -52,12 +52,23 @@ public class DefaultGeneratorTest {
|
|||||||
gen.opts(clientOptInput);
|
gen.opts(clientOptInput);
|
||||||
Map<String, List<CodegenOperation>> paths = gen.processPaths(swagger.getPaths());
|
Map<String, List<CodegenOperation>> paths = gen.processPaths(swagger.getPaths());
|
||||||
|
|
||||||
CodegenSecurity apiKey, petstoreAuth;
|
CodegenSecurity cs, apiKey, petstoreAuth;
|
||||||
|
|
||||||
// security of "getPetById": api_key
|
// security of "getPetById": api_key
|
||||||
CodegenOperation getPetById = findCodegenOperationByOperationId(paths, "getPetById");
|
CodegenOperation getPetById = findCodegenOperationByOperationId(paths, "getPetById");
|
||||||
assertEquals(getPetById.authMethods.size(), 1);
|
assertEquals(getPetById.authMethods.size(), 2);
|
||||||
apiKey = getPetById.authMethods.iterator().next();
|
cs = getPetById.authMethods.get(0);
|
||||||
|
if ("api_key".equals(cs.name)) {
|
||||||
|
apiKey = cs;
|
||||||
|
petstoreAuth = getPetById.authMethods.get(1);
|
||||||
|
} else {
|
||||||
|
petstoreAuth = cs;
|
||||||
|
apiKey = getPetById.authMethods.get(1);
|
||||||
|
}
|
||||||
|
assertEquals(petstoreAuth.name, "petstore_auth");
|
||||||
|
assertEquals(petstoreAuth.type, "oauth2");
|
||||||
|
|
||||||
|
|
||||||
assertEquals(apiKey.name, "api_key");
|
assertEquals(apiKey.name, "api_key");
|
||||||
assertEquals(apiKey.type, "apiKey");
|
assertEquals(apiKey.type, "apiKey");
|
||||||
|
|
||||||
@ -88,8 +99,17 @@ public class DefaultGeneratorTest {
|
|||||||
|
|
||||||
// security of "getPetById": api_key
|
// security of "getPetById": api_key
|
||||||
CodegenOperation getPetById = findCodegenOperationByOperationId(paths, "getPetById");
|
CodegenOperation getPetById = findCodegenOperationByOperationId(paths, "getPetById");
|
||||||
assertEquals(getPetById.authMethods.size(), 1);
|
assertEquals(getPetById.authMethods.size(), 2);
|
||||||
apiKey = getPetById.authMethods.iterator().next();
|
cs = getPetById.authMethods.get(0);
|
||||||
|
if ("api_key".equals(cs.name)) {
|
||||||
|
apiKey = cs;
|
||||||
|
petstoreAuth = getPetById.authMethods.get(1);
|
||||||
|
} else {
|
||||||
|
petstoreAuth = cs;
|
||||||
|
apiKey = getPetById.authMethods.get(1);
|
||||||
|
}
|
||||||
|
assertEquals(petstoreAuth.type, "oauth2");
|
||||||
|
assertEquals(petstoreAuth.name, "petstore_auth");
|
||||||
assertEquals(apiKey.name, "api_key");
|
assertEquals(apiKey.name, "api_key");
|
||||||
assertEquals(apiKey.type, "apiKey");
|
assertEquals(apiKey.type, "apiKey");
|
||||||
|
|
||||||
|
@ -559,7 +559,13 @@
|
|||||||
"400": {
|
"400": {
|
||||||
"description": "Invalid Order"
|
"description": "Invalid Order"
|
||||||
}
|
}
|
||||||
}
|
},
|
||||||
|
"security": [
|
||||||
|
{
|
||||||
|
"test_api_client_id": [],
|
||||||
|
"test_api_client_secret": []
|
||||||
|
}
|
||||||
|
]
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"/store/order/{orderId}": {
|
"/store/order/{orderId}": {
|
||||||
@ -596,7 +602,15 @@
|
|||||||
"400": {
|
"400": {
|
||||||
"description": "Invalid ID supplied"
|
"description": "Invalid ID supplied"
|
||||||
}
|
}
|
||||||
}
|
},
|
||||||
|
"security": [
|
||||||
|
{
|
||||||
|
"test_api_key_header": []
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"test_api_key_query": []
|
||||||
|
}
|
||||||
|
]
|
||||||
},
|
},
|
||||||
"delete": {
|
"delete": {
|
||||||
"tags": [
|
"tags": [
|
||||||
@ -915,6 +929,26 @@
|
|||||||
"write:pets": "modify pets in your account",
|
"write:pets": "modify pets in your account",
|
||||||
"read:pets": "read your pets"
|
"read:pets": "read your pets"
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
"test_api_client_id": {
|
||||||
|
"type": "apiKey",
|
||||||
|
"name": "x-test_api_client_id",
|
||||||
|
"in": "header"
|
||||||
|
},
|
||||||
|
"test_api_client_secret": {
|
||||||
|
"type": "apiKey",
|
||||||
|
"name": "x-test_api_client_secret",
|
||||||
|
"in": "header"
|
||||||
|
},
|
||||||
|
"test_api_key_header": {
|
||||||
|
"type": "apiKey",
|
||||||
|
"name": "test_api_key_header",
|
||||||
|
"in": "header"
|
||||||
|
},
|
||||||
|
"test_api_key_query": {
|
||||||
|
"type": "apiKey",
|
||||||
|
"name": "test_api_key_query",
|
||||||
|
"in": "query"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"definitions": {
|
"definitions": {
|
||||||
|
@ -527,6 +527,11 @@ class PetApi
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// this endpoint requires OAuth (access token)
|
||||||
|
if (strlen($this->apiClient->getConfig()->getAccessToken()) !== 0) {
|
||||||
|
$headerParams['Authorization'] = 'Bearer ' . $this->apiClient->getConfig()->getAccessToken();
|
||||||
|
}
|
||||||
|
|
||||||
// make the API Call
|
// make the API Call
|
||||||
try {
|
try {
|
||||||
list($response, $statusCode, $httpHeader) = $this->apiClient->callApi(
|
list($response, $statusCode, $httpHeader) = $this->apiClient->callApi(
|
||||||
@ -949,6 +954,11 @@ class PetApi
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// this endpoint requires OAuth (access token)
|
||||||
|
if (strlen($this->apiClient->getConfig()->getAccessToken()) !== 0) {
|
||||||
|
$headerParams['Authorization'] = 'Bearer ' . $this->apiClient->getConfig()->getAccessToken();
|
||||||
|
}
|
||||||
|
|
||||||
// make the API Call
|
// make the API Call
|
||||||
try {
|
try {
|
||||||
list($response, $statusCode, $httpHeader) = $this->apiClient->callApi(
|
list($response, $statusCode, $httpHeader) = $this->apiClient->callApi(
|
||||||
|
@ -240,6 +240,20 @@ class StoreApi
|
|||||||
$httpBody = $formParams; // for HTTP post (form)
|
$httpBody = $formParams; // for HTTP post (form)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// this endpoint requires API key authentication
|
||||||
|
$apiKey = $this->apiClient->getApiKeyWithPrefix('x-test_api_client_id');
|
||||||
|
if (strlen($apiKey) !== 0) {
|
||||||
|
$headerParams['x-test_api_client_id'] = $apiKey;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// this endpoint requires API key authentication
|
||||||
|
$apiKey = $this->apiClient->getApiKeyWithPrefix('x-test_api_client_secret');
|
||||||
|
if (strlen($apiKey) !== 0) {
|
||||||
|
$headerParams['x-test_api_client_secret'] = $apiKey;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// make the API Call
|
// make the API Call
|
||||||
try {
|
try {
|
||||||
list($response, $statusCode, $httpHeader) = $this->apiClient->callApi(
|
list($response, $statusCode, $httpHeader) = $this->apiClient->callApi(
|
||||||
@ -335,6 +349,20 @@ class StoreApi
|
|||||||
$httpBody = $formParams; // for HTTP post (form)
|
$httpBody = $formParams; // for HTTP post (form)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// this endpoint requires API key authentication
|
||||||
|
$apiKey = $this->apiClient->getApiKeyWithPrefix('test_api_key_header');
|
||||||
|
if (strlen($apiKey) !== 0) {
|
||||||
|
$headerParams['test_api_key_header'] = $apiKey;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// this endpoint requires API key authentication
|
||||||
|
$apiKey = $this->apiClient->getApiKeyWithPrefix('test_api_key_query');
|
||||||
|
if (strlen($apiKey) !== 0) {
|
||||||
|
$queryParams['test_api_key_query'] = $apiKey;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// make the API Call
|
// make the API Call
|
||||||
try {
|
try {
|
||||||
list($response, $statusCode, $httpHeader) = $this->apiClient->callApi(
|
list($response, $statusCode, $httpHeader) = $this->apiClient->callApi(
|
||||||
|
Loading…
x
Reference in New Issue
Block a user