Skip to content

Commit dda2aba

Browse files
authored
fixes #266 add skipVerifyScopeWithoutSpec flag to openapi-security.yml (#267)
1 parent b9b0b4b commit dda2aba

File tree

3 files changed

+47
-5
lines changed

3 files changed

+47
-5
lines changed

openapi-security/src/main/java/com/networknt/openapi/JwtVerifyHandler.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import com.networknt.exception.ExpiredTokenException;
2121
import com.networknt.handler.Handler;
2222
import com.networknt.handler.MiddlewareHandler;
23+
import com.networknt.handler.config.HandlerConfig;
2324
import com.networknt.httpstring.AttachmentConstants;
2425
import com.networknt.httpstring.HttpStringConstants;
2526
import com.networknt.oas.model.Operation;
@@ -81,6 +82,9 @@ public JwtVerifyHandler() {
8182
// And the basePath is the correct one from the OpenApiHandler helper or helperMap if multiple is used.
8283
config = SecurityConfig.load(OPENAPI_SECURITY_CONFIG);
8384
jwtVerifier = new JwtVerifier(config);
85+
// in case that the specification doesn't exist, get the basePath from the handler.yml for endpoint lookup.
86+
HandlerConfig handlerConfig = (HandlerConfig) Config.getInstance().getJsonObjectConfig(HANDLER_CONFIG, HandlerConfig.class);
87+
this.basePath = handlerConfig == null ? "/" : handlerConfig.getBasePath();
8488
}
8589

8690
@Override
@@ -168,6 +172,12 @@ public void handleRequest(final HttpServerExchange exchange) throws Exception {
168172
OpenApiOperation openApiOperation = (OpenApiOperation) auditInfo.get(Constants.OPENAPI_OPERATION_STRING);
169173
Operation operation = this.getOperation(exchange, openApiOperation, auditInfo);
170174
if(operation == null) {
175+
if(config.isSkipVerifyScopeWithoutSpec()) {
176+
if (logger.isDebugEnabled()) logger.debug("JwtVerifyHandler.handleRequest ends without verifying scope due to spec.");
177+
Handler.next(exchange, next);
178+
} else {
179+
// this will return an error message to the client.
180+
}
171181
return;
172182
}
173183

@@ -271,7 +281,9 @@ protected Operation getOperation(HttpServerExchange exchange, OpenApiOperation o
271281
final Optional<NormalisedPath> maybeApiPath = OpenApiHandler.getHelper(exchange.getRequestPath()).findMatchingApiPath(requestPath);
272282

273283
if (maybeApiPath.isEmpty()) {
274-
setExchangeStatus(exchange, STATUS_INVALID_REQUEST_PATH);
284+
if(!config.isSkipVerifyScopeWithoutSpec()) {
285+
setExchangeStatus(exchange, STATUS_INVALID_REQUEST_PATH);
286+
}
275287
return null;
276288
}
277289

openapi-security/src/main/resources/config/openapi-security.yml

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,30 @@
33
# same server instance. If this file cannot be found, the generic security.yml will be
44
# loaded for backward compatibility.
55
---
6-
# Enable JWT verification flag.
6+
# Enable the JWT verification flag. The JwtVerifierHandler will skip the JWT token verification
7+
# if this flag is false. It should only be set to false on the dev environment for testing
8+
# purposes. If you have some endpoints that want to skip the JWT verification, you can put the
9+
# request path prefix in skipPathPrefixes.
710
enableVerifyJwt: ${openapi-security.enableVerifyJwt:true}
811

912
# Extract JWT scope token from the X-Scope-Token header and validate the JWT token
1013
enableExtractScopeToken: ${openapi-security.enableExtractScopeToken:true}
1114

12-
# Enable JWT scope verification. Only valid when enableVerifyJwt is true.
15+
# Enable JWT scope verification. This flag is valid when enableVerifyJwt is true. When using the
16+
# light gateway as a centralized gateway without backend API specifications, you can still enable
17+
# this flag to allow the admin endpoints to have scopes verified. And all backend APIs without
18+
# specifications skip the scope verification if the spec does not exist with the skipVerifyScopeWithoutSpec
19+
# flag to true. Also, you need to have the openapi.yml specification file in the config folder to
20+
# enable it, as the scope verification compares the scope from the JWT token and the scope in the
21+
# endpoint specification.
1322
enableVerifyScope: ${openapi-security.enableVerifyScope:true}
1423

24+
# Users should only use this flag in a shared light gateway if the backend API specifications are
25+
# unavailable in the gateway config folder. If this flag is true and the enableVerifyScope is true,
26+
# the security handler will invoke the scope verification for all endpoints. However, if the endpoint
27+
# doesn't have a specification to retrieve the defined scopes, the handler will skip the scope verification.
28+
skipVerifyScopeWithoutSpec: ${openapi-security.skipVerifyScopeWithoutSpec:false}
29+
1530
# Enable JWT scope verification.
1631
# Only valid when (enableVerifyJwt is true) AND (enableVerifyScope is true)
1732
enableVerifyJwtScopeToken: ${openapi-security.enableVerifyJwtScopeToken:true}

openapi-security/src/test/resources/config/openapi-security.yml

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,30 @@
33
# same server instance. If this file cannot be found, the generic security.yml will be
44
# loaded for backward compatibility.
55
---
6-
# Enable JWT verification flag.
6+
# Enable the JWT verification flag. The JwtVerifierHandler will skip the JWT token verification
7+
# if this flag is false. It should only be set to false on the dev environment for testing
8+
# purposes. If you have some endpoints that want to skip the JWT verification, you can put the
9+
# request path prefix in skipPathPrefixes.
710
enableVerifyJwt: ${openapi-security.enableVerifyJwt:true}
811

912
# Extract JWT scope token from the X-Scope-Token header and validate the JWT token
1013
enableExtractScopeToken: ${openapi-security.enableExtractScopeToken:true}
1114

12-
# Enable JWT scope verification. Only valid when enableVerifyJwt is true.
15+
# Enable JWT scope verification. This flag is valid when enableVerifyJwt is true. When using the
16+
# light gateway as a centralized gateway without backend API specifications, you can still enable
17+
# this flag to allow the admin endpoints to have scopes verified. And all backend APIs without
18+
# specifications skip the scope verification if the spec does not exist with the skipVerifyScopeWithoutSpec
19+
# flag to true. Also, you need to have the openapi.yml specification file in the config folder to
20+
# enable it, as the scope verification compares the scope from the JWT token and the scope in the
21+
# endpoint specification.
1322
enableVerifyScope: ${openapi-security.enableVerifyScope:true}
1423

24+
# Users should only use this flag in a shared light gateway if the backend API specifications are
25+
# unavailable in the gateway config folder. If this flag is true and the enableVerifyScope is true,
26+
# the security handler will invoke the scope verification for all endpoints. However, if the endpoint
27+
# doesn't have a specification to retrieve the defined scopes, the handler will skip the scope verification.
28+
skipVerifyScopeWithoutSpec: ${openapi-security.skipVerifyScopeWithoutSpec:false}
29+
1530
# Enable JWT scope verification.
1631
# Only valid when (enableVerifyJwt is true) AND (enableVerifyScope is true)
1732
enableVerifyJwtScopeToken: ${openapi-security.enableVerifyJwtScopeToken:true}

0 commit comments

Comments
 (0)