Skip to content
This repository was archived by the owner on Jun 30, 2023. It is now read-only.

Commit 59bb979

Browse files
committed
fix JWT + API key auth config generation
Right now, if wishing to use JWT auth plus API keys, the OpenAPI config generator places the API key in a separate security requirement, which allows JWT auth OR API key. This change adds the API key to every existing security requirement to change OR to AND. If no current security requirements exist, a new one is created to solely allow API key validation.
1 parent 33c2e0f commit 59bb979

3 files changed

Lines changed: 46 additions & 1 deletion

File tree

endpoints-framework/src/main/java/com/google/api/server/spi/swagger/SwaggerGenerator.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,17 @@ private void writeApiMethod(
340340
operation.response(200, response);
341341
writeAuthConfig(swagger, methodConfig, operation);
342342
if (methodConfig.isApiKeyRequired()) {
343-
operation.addSecurity(API_KEY, ImmutableList.<String>of());
343+
List<Map<String, List<String>>> security = operation.getSecurity();
344+
// Loop through each existing security requirement for this method, which is currently just a
345+
// JWT config id, and add an API key requirement to it. If there are currently no new
346+
// security requirements, add a new one for just the API key.
347+
if (security != null) {
348+
for (Map<String, List<String>> securityEntry : security) {
349+
securityEntry.put(API_KEY, ImmutableList.<String>of());
350+
}
351+
} else {
352+
operation.addSecurity(API_KEY, ImmutableList.<String>of());
353+
}
344354
Map<String, SecuritySchemeDefinition> definitions = swagger.getSecurityDefinitions();
345355
if (definitions == null || !definitions.containsKey(API_KEY)) {
346356
swagger.securityDefinition(API_KEY, new ApiKeyAuthDefinition(API_KEY_PARAM, In.QUERY));

endpoints-framework/src/test/java/com/google/api/server/spi/swagger/SwaggerGeneratorTest.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,12 +211,22 @@ public void googleAuth() { }
211211
}
212212

213213
@Api(name = "apikeys", version = "v1",
214+
issuers = {
215+
@ApiIssuer(name = "auth0", issuer = "https://test.auth0.com/authorize",
216+
jwksUri = "https://test.auth0.com/.wellknown/jwks.json")
217+
},
214218
apiKeyRequired = AnnotationBoolean.TRUE)
215219
private static class ApiKeysEndpoint {
216220
@ApiMethod(apiKeyRequired = AnnotationBoolean.FALSE)
217221
public void overrideApiKeySetting() { }
218222

219223
@ApiMethod
220224
public void inheritApiKeySetting() { }
225+
226+
@ApiMethod(
227+
issuerAudiences = {
228+
@ApiIssuerAudience(name = "auth0", audiences = "auth0audmethod")
229+
})
230+
public void apiKeyWithAuth() { }
221231
}
222232
}

endpoints-framework/src/test/resources/com/google/api/server/spi/swagger/api_keys.swagger

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,23 @@
1616
"application/json"
1717
],
1818
"paths": {
19+
"/apikeys/v1/apiKeyWithAuth": {
20+
"post": {
21+
"operationId": "ApikeysApiKeyWithAuth",
22+
"parameters": [],
23+
"responses": {
24+
"200": {
25+
"description": "A successful response"
26+
}
27+
},
28+
"security": [
29+
{
30+
"auth0-6fa4a909": [],
31+
"api_key": []
32+
}
33+
]
34+
}
35+
},
1936
"/apikeys/v1/inheritApiKeySetting": {
2037
"post": {
2138
"operationId": "ApikeysInheritApiKeySetting",
@@ -45,6 +62,14 @@
4562
}
4663
},
4764
"securityDefinitions": {
65+
"auth0-6fa4a909": {
66+
"type": "oauth2",
67+
"authorizationUrl": "",
68+
"flow": "implicit",
69+
"x-google-issuer": "https://test.auth0.com/authorize",
70+
"x-google-jwks_uri": "https://test.auth0.com/.wellknown/jwks.json",
71+
"x-google-audiences": "auth0audmethod"
72+
},
4873
"api_key": {
4974
"type": "apiKey",
5075
"name": "key",

0 commit comments

Comments
 (0)