From a4825d814233e13e9bba9d1cea9d30b04f70212b Mon Sep 17 00:00:00 2001 From: wing328 Date: Tue, 16 Feb 2016 17:17:42 +0800 Subject: [PATCH] add multi auth setting (based on #1961) --- .../io/swagger/codegen/DefaultGenerator.java | 48 ++++++++++--------- .../swagger/codegen/DefaultGeneratorTest.java | 30 ++++++++++-- .../src/test/resources/2_0/petstore.json | 38 ++++++++++++++- .../php/SwaggerClient-php/lib/Api/PetApi.php | 10 ++++ .../SwaggerClient-php/lib/Api/StoreApi.php | 28 +++++++++++ 5 files changed, 124 insertions(+), 30 deletions(-) diff --git a/modules/swagger-codegen/src/main/java/io/swagger/codegen/DefaultGenerator.java b/modules/swagger-codegen/src/main/java/io/swagger/codegen/DefaultGenerator.java index 94716051020..f315c0c3540 100644 --- a/modules/swagger-codegen/src/main/java/io/swagger/codegen/DefaultGenerator.java +++ b/modules/swagger-codegen/src/main/java/io/swagger/codegen/DefaultGenerator.java @@ -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 // "there is a logical OR between the security requirements" 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> security = securities.get(0); - for (String securityName : security.keySet()) { - SecuritySchemeDefinition securityDefinition = fromSecurity(securityName); - if (securityDefinition != null) { - if(securityDefinition instanceof OAuth2Definition) { - OAuth2Definition oauth2Definition = (OAuth2Definition) securityDefinition; - OAuth2Definition oauth2Operation = new OAuth2Definition(); - oauth2Operation.setType(oauth2Definition.getType()); - oauth2Operation.setAuthorizationUrl(oauth2Definition.getAuthorizationUrl()); - oauth2Operation.setFlow(oauth2Definition.getFlow()); - oauth2Operation.setTokenUrl(oauth2Definition.getTokenUrl()); - oauth2Operation.setScopes(new HashMap()); - for (String scope : security.get(securityName)) { - if (oauth2Definition.getScopes().containsKey(scope)) { - oauth2Operation.addScope(scope, oauth2Definition.getScopes().get(scope)); - } - } - authMethods.put(securityName, oauth2Operation); - } else { - authMethods.put(securityName, securityDefinition); - } - } + //Map> security = securities.get(0); + for (Map> security: securities) { + for (String securityName : security.keySet()) { + SecuritySchemeDefinition securityDefinition = fromSecurity(securityName); + if (securityDefinition != null) { + if(securityDefinition instanceof OAuth2Definition) { + OAuth2Definition oauth2Definition = (OAuth2Definition) securityDefinition; + OAuth2Definition oauth2Operation = new OAuth2Definition(); + oauth2Operation.setType(oauth2Definition.getType()); + oauth2Operation.setAuthorizationUrl(oauth2Definition.getAuthorizationUrl()); + oauth2Operation.setFlow(oauth2Definition.getFlow()); + oauth2Operation.setTokenUrl(oauth2Definition.getTokenUrl()); + oauth2Operation.setScopes(new HashMap()); + for (String scope : security.get(securityName)) { + if (oauth2Definition.getScopes().containsKey(scope)) { + oauth2Operation.addScope(scope, oauth2Definition.getScopes().get(scope)); + } + } + authMethods.put(securityName, oauth2Operation); + } else { + authMethods.put(securityName, securityDefinition); + } + } + } } if (!authMethods.isEmpty()) { co.authMethods = config.fromSecurity(authMethods); diff --git a/modules/swagger-codegen/src/test/java/io/swagger/codegen/DefaultGeneratorTest.java b/modules/swagger-codegen/src/test/java/io/swagger/codegen/DefaultGeneratorTest.java index 67f40c0cd93..bfc25b8f5cc 100644 --- a/modules/swagger-codegen/src/test/java/io/swagger/codegen/DefaultGeneratorTest.java +++ b/modules/swagger-codegen/src/test/java/io/swagger/codegen/DefaultGeneratorTest.java @@ -52,12 +52,23 @@ public class DefaultGeneratorTest { gen.opts(clientOptInput); Map> paths = gen.processPaths(swagger.getPaths()); - CodegenSecurity apiKey, petstoreAuth; + CodegenSecurity cs, apiKey, petstoreAuth; // security of "getPetById": api_key CodegenOperation getPetById = findCodegenOperationByOperationId(paths, "getPetById"); - assertEquals(getPetById.authMethods.size(), 1); - apiKey = getPetById.authMethods.iterator().next(); + assertEquals(getPetById.authMethods.size(), 2); + 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.type, "apiKey"); @@ -88,8 +99,17 @@ public class DefaultGeneratorTest { // security of "getPetById": api_key CodegenOperation getPetById = findCodegenOperationByOperationId(paths, "getPetById"); - assertEquals(getPetById.authMethods.size(), 1); - apiKey = getPetById.authMethods.iterator().next(); + assertEquals(getPetById.authMethods.size(), 2); + 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.type, "apiKey"); diff --git a/modules/swagger-codegen/src/test/resources/2_0/petstore.json b/modules/swagger-codegen/src/test/resources/2_0/petstore.json index 703b920db21..b49bd81631e 100644 --- a/modules/swagger-codegen/src/test/resources/2_0/petstore.json +++ b/modules/swagger-codegen/src/test/resources/2_0/petstore.json @@ -559,7 +559,13 @@ "400": { "description": "Invalid Order" } - } + }, + "security": [ + { + "test_api_client_id": [], + "test_api_client_secret": [] + } + ] } }, "/store/order/{orderId}": { @@ -596,7 +602,15 @@ "400": { "description": "Invalid ID supplied" } - } + }, + "security": [ + { + "test_api_key_header": [] + }, + { + "test_api_key_query": [] + } + ] }, "delete": { "tags": [ @@ -915,6 +929,26 @@ "write:pets": "modify pets in your account", "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": { diff --git a/samples/client/petstore/php/SwaggerClient-php/lib/Api/PetApi.php b/samples/client/petstore/php/SwaggerClient-php/lib/Api/PetApi.php index ed06acb525e..eb3d198892c 100644 --- a/samples/client/petstore/php/SwaggerClient-php/lib/Api/PetApi.php +++ b/samples/client/petstore/php/SwaggerClient-php/lib/Api/PetApi.php @@ -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 try { 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 try { list($response, $statusCode, $httpHeader) = $this->apiClient->callApi( diff --git a/samples/client/petstore/php/SwaggerClient-php/lib/Api/StoreApi.php b/samples/client/petstore/php/SwaggerClient-php/lib/Api/StoreApi.php index 0f67fa63d15..8163ef43b39 100644 --- a/samples/client/petstore/php/SwaggerClient-php/lib/Api/StoreApi.php +++ b/samples/client/petstore/php/SwaggerClient-php/lib/Api/StoreApi.php @@ -240,6 +240,20 @@ class StoreApi $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 try { list($response, $statusCode, $httpHeader) = $this->apiClient->callApi( @@ -335,6 +349,20 @@ class StoreApi $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 try { list($response, $statusCode, $httpHeader) = $this->apiClient->callApi(