Skip to content

Commit

Permalink
Merge pull request swagger-api#2155 from wing328/multi_auth_setting
Browse files Browse the repository at this point in the history
Add support for multi-authentication setting
  • Loading branch information
wing328 committed Feb 17, 2016
2 parents 5678939 + 4aa0dc5 commit c4d799a
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -613,35 +613,29 @@ public void processOperation(String resourcePath, String httpMethod, Operation o
continue;
}
Map<String, SecuritySchemeDefinition> authMethods = new HashMap<String, SecuritySchemeDefinition>();
// NOTE: Use only the first security requirement for now.
// See the "security" field of "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"
if (securities.size() > 1) {
LOGGER.warn("More than 1 security requirements are found, using only the first one");
}
Map<String, List<String>> 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<String, String>());
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);
}
}
for (Map<String, List<String>> 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<String, String>());
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,23 @@ public void testSecurityWithoutGlobal() throws Exception {
gen.opts(clientOptInput);
Map<String, List<CodegenOperation>> 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");

Expand Down Expand Up @@ -88,8 +99,17 @@ public void testSecurityWithGlobal() throws Exception {

// 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");

Expand Down
38 changes: 36 additions & 2 deletions modules/swagger-codegen/src/test/resources/2_0/petstore.json
Original file line number Diff line number Diff line change
Expand Up @@ -559,7 +559,13 @@
"400": {
"description": "Invalid Order"
}
}
},
"security": [
{
"test_api_client_id": [],
"test_api_client_secret": []
}
]
}
},
"/store/order/{orderId}": {
Expand Down Expand Up @@ -596,7 +602,15 @@
"400": {
"description": "Invalid ID supplied"
}
}
},
"security": [
{
"test_api_key_header": []
},
{
"test_api_key_query": []
}
]
},
"delete": {
"tags": [
Expand Down Expand Up @@ -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": {
Expand Down
10 changes: 10 additions & 0 deletions samples/client/petstore/php/SwaggerClient-php/lib/Api/PetApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,11 @@ public function getPetByIdWithHttpInfo($pet_id)
}


// 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(
Expand Down Expand Up @@ -949,6 +954,11 @@ public function getPetByIdWithByteArrayWithHttpInfo($pet_id)
}


// 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(
Expand Down
28 changes: 28 additions & 0 deletions samples/client/petstore/php/SwaggerClient-php/lib/Api/StoreApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,20 @@ public function placeOrderWithHttpInfo($body = null)
$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(
Expand Down Expand Up @@ -335,6 +349,20 @@ public function getOrderByIdWithHttpInfo($order_id)
$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(
Expand Down

0 comments on commit c4d799a

Please sign in to comment.