-
Notifications
You must be signed in to change notification settings - Fork 630
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fixes #1248 update the TokenHandler to support multiple OAuth 2.0 pro…
…viders (#1249)
- Loading branch information
Showing
5 changed files
with
246 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,6 @@ | ||
enabled: false | ||
scopeToken: true | ||
# This is the configuration file for the TokenHandler that is responsible for getting | ||
# a client credentials token in http-sidecar and light-gateway when calling others. | ||
# The configuration for one or multiple OAuth 2.0 providers is in the client.yml file. | ||
|
||
# indicate if the handler is enabled. | ||
enabled: ${token.enabled:false} |
140 changes: 140 additions & 0 deletions
140
egress-router/src/test/java/com/networknt/router/middleware/TokenHandlerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
package com.networknt.router.middleware; | ||
|
||
import com.networknt.client.Http2Client; | ||
import com.networknt.exception.ClientException; | ||
import com.networknt.httpstring.HttpStringConstants; | ||
import io.undertow.Handlers; | ||
import io.undertow.Undertow; | ||
import io.undertow.UndertowOptions; | ||
import io.undertow.client.ClientConnection; | ||
import io.undertow.client.ClientRequest; | ||
import io.undertow.client.ClientResponse; | ||
import io.undertow.server.HttpHandler; | ||
import io.undertow.server.RoutingHandler; | ||
import io.undertow.util.Headers; | ||
import io.undertow.util.Methods; | ||
import org.junit.*; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.xnio.IoUtils; | ||
import org.xnio.OptionMap; | ||
|
||
import java.net.URI; | ||
import java.util.concurrent.CountDownLatch; | ||
import java.util.concurrent.atomic.AtomicReference; | ||
|
||
/** | ||
* This is the test class to ensure that the TokenHandler is working with multiple | ||
* OAuth 2.0 providers with proper cache based on the serviceId and scopes. | ||
* | ||
* @author Steve Hu | ||
*/ | ||
public class TokenHandlerTest { | ||
static final Logger logger = LoggerFactory.getLogger(TokenHandlerTest.class); | ||
static Undertow server = null; | ||
|
||
@BeforeClass | ||
public static void setUp() throws Exception{ | ||
if(server == null) { | ||
logger.info("starting server"); | ||
HttpHandler handler = getTestHandler(); | ||
TokenHandler tokenHandler = new TokenHandler(); | ||
tokenHandler.setNext(handler); | ||
handler = tokenHandler; | ||
server = Undertow.builder() | ||
.setServerOption(UndertowOptions.ENABLE_HTTP2, true) | ||
.addHttpListener(7080, "localhost") | ||
.setHandler(handler) | ||
.build(); | ||
server.start(); | ||
} | ||
} | ||
|
||
@AfterClass | ||
public static void tearDown() throws Exception { | ||
if(server != null) { | ||
try { | ||
Thread.sleep(100); | ||
} catch (InterruptedException ignored) { | ||
|
||
} | ||
server.stop(); | ||
logger.info("The server is stopped."); | ||
} | ||
} | ||
|
||
static RoutingHandler getTestHandler() { | ||
return Handlers.routing() | ||
.add(Methods.POST, "/", exchange -> { | ||
exchange.getResponseSender().send("POST OK"); | ||
}) | ||
.add(Methods.GET, "/", exchange -> { | ||
exchange.getResponseSender().send("GET OK"); | ||
}); | ||
} | ||
|
||
@Test | ||
@Ignore | ||
public void testOneGetService1Request() throws Exception { | ||
final Http2Client client = Http2Client.getInstance(); | ||
final CountDownLatch latch = new CountDownLatch(1); | ||
final ClientConnection connection; | ||
try { | ||
connection = client.connect(new URI("http://localhost:7080"), Http2Client.WORKER, Http2Client.SSL, Http2Client.BUFFER_POOL, OptionMap.create(UndertowOptions.ENABLE_HTTP2, true)).get(); | ||
} catch (Exception e) { | ||
throw new ClientException(e); | ||
} | ||
final AtomicReference<ClientResponse> reference = new AtomicReference<>(); | ||
try { | ||
ClientRequest request = new ClientRequest().setPath("/service1").setMethod(Methods.GET); | ||
request.getRequestHeaders().put(HttpStringConstants.SERVICE_ID, "service1"); | ||
request.getRequestHeaders().put(Headers.HOST, "localhost"); | ||
connection.sendRequest(request, client.createClientCallback(reference, latch)); | ||
latch.await(); | ||
} catch (Exception e) { | ||
logger.error("Exception: ", e); | ||
throw new ClientException(e); | ||
} finally { | ||
IoUtils.safeClose(connection); | ||
} | ||
int statusCode = reference.get().getResponseCode(); | ||
String body = reference.get().getAttachment(Http2Client.RESPONSE_BODY); | ||
Assert.assertEquals(200, statusCode); | ||
if(statusCode == 200) { | ||
Assert.assertEquals("GET OK", body); | ||
} | ||
} | ||
|
||
@Test | ||
@Ignore | ||
public void testOnePostRequest() throws Exception { | ||
final Http2Client client = Http2Client.getInstance(); | ||
final CountDownLatch latch = new CountDownLatch(1); | ||
final ClientConnection connection; | ||
try { | ||
connection = client.connect(new URI("http://localhost:7080"), Http2Client.WORKER, Http2Client.SSL, Http2Client.BUFFER_POOL, OptionMap.create(UndertowOptions.ENABLE_HTTP2, true)).get(); | ||
} catch (Exception e) { | ||
throw new ClientException(e); | ||
} | ||
final AtomicReference<ClientResponse> reference = new AtomicReference<>(); | ||
String requestBody = "{\"clientId\":\"FSC_0030303343303x32AA2\",\"loggedInUserEmail\":\"steve.hu@networknt.com\"}"; | ||
try { | ||
ClientRequest request = new ClientRequest().setPath("/services/apexrest/NNT_ConquestApplicationServices/getAccountDetailsForConquestToUpdatePlan").setMethod(Methods.POST); | ||
request.getRequestHeaders().put(Headers.CONTENT_TYPE, "application/json"); | ||
request.getRequestHeaders().put(Headers.TRANSFER_ENCODING, "chunked"); | ||
request.getRequestHeaders().put(Headers.HOST, "localhost"); | ||
connection.sendRequest(request, client.createClientCallback(reference, latch, requestBody)); | ||
latch.await(); | ||
} catch (Exception e) { | ||
logger.error("Exception: ", e); | ||
throw new ClientException(e); | ||
} finally { | ||
IoUtils.safeClose(connection); | ||
} | ||
int statusCode = reference.get().getResponseCode(); | ||
String body = reference.get().getAttachment(Http2Client.RESPONSE_BODY); | ||
System.out.println("statusCode = " + statusCode + " body = " + body); | ||
Assert.assertEquals(200, statusCode); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters