Skip to content

Commit

Permalink
Make ports configurable (#132)
Browse files Browse the repository at this point in the history
* Made port for JwtGenerator and XsuaaMockWebServer overridable

* renamed constant

* removed comma in javadoc

* moved port field up

* formatter

* more consistent naming
  • Loading branch information
hassler-d authored and nenaraab committed Sep 5, 2019
1 parent b858d08 commit ec73534
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,26 @@ public class XsuaaMockWebServer extends PropertySource<MockWebServer> implements
public static final String MOCK_XSUAA_PROPERTY_SOURCE_NAME = "mockxsuaaserver";
public static final String MOCK_XSUAA_URL = "mockxsuaaserver.url";
// must match the port defined in JwtGenerator
private static final int MOCK_XSUAA_PORT = 33195;
private static final int MOCK_XSUAA_DEFAULT_PORT = 33195;
private final int mockXsuaaPort;

private static final Log logger = LogFactory.getLog(XsuaaMockWebServer.class);

private static boolean started;

public XsuaaMockWebServer() {
this(MOCK_XSUAA_DEFAULT_PORT);
}

/**
* Overwrites the port the mock server listens to.
*
* @param mockXsuaaPort
* the port the mock server listens to.
*/
public XsuaaMockWebServer(int mockXsuaaPort) {
super(MOCK_XSUAA_PROPERTY_SOURCE_NAME, createMockWebServer(new XsuaaRequestDispatcher()));
this.mockXsuaaPort = mockXsuaaPort;
}

/**
Expand All @@ -37,6 +49,7 @@ public XsuaaMockWebServer() {
*/
public XsuaaMockWebServer(Dispatcher dispatcher) {
super(MOCK_XSUAA_PROPERTY_SOURCE_NAME, createMockWebServer(dispatcher));
mockXsuaaPort = MOCK_XSUAA_DEFAULT_PORT;
}

private static MockWebServer createMockWebServer(Dispatcher dispatcher) {
Expand Down Expand Up @@ -71,13 +84,14 @@ private String getUrl(MockWebServer mockWebServer) {

private void intializeMockXsuaa(MockWebServer mockWebServer) {
try {
mockWebServer.start(MOCK_XSUAA_PORT);
mockWebServer.start(mockXsuaaPort);
this.started = true;
logger.warn(
">>>>>>>>>>>Started Xsuaa mock Server that provides public keys for offline JWT Token validation. NEVER run in productive environment!<<<<<<");
} catch (IllegalStateException | IOException e) {
throw new RuntimeException(
"Could not start XSUAA mock webserver (localhost:33195). Make sure that it is not yet started in another process.",
String.format("Could not start XSUAA mock webserver (localhost:%d). " +
"Make sure that it is not yet started in another process.", mockXsuaaPort),
e);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ private TokenHeaders() {
}

// must match the port defined in XsuaaMockWebServer
private static final int MOCK_XSUAA_PORT = 33195;

private static final int MOCK_XSUAA_DEFAULT_PORT = 33195;
private final int mockXsuaaPort;
public static final Date NO_EXPIRE_DATE = new GregorianCalendar(2190, 11, 31).getTime();
public static final int NO_EXPIRE = Integer.MAX_VALUE;
public static final String CLIENT_ID = "sb-xsapplication!t895";
Expand All @@ -76,16 +76,32 @@ private TokenHeaders() {
/**
* Specifies clientId of the JWT token claim.
*
* @param clientId,
* @param clientId
* the XSUAA client id, e.g. sb-applicationName!t123, defines the
* value of the JWT token claims "client_id" and "cid". A token is
* considered to be valid when it matches the "xsuaa.clientid" xsuaa
* service configuration (VCAP_SERVICES).
* @param port
* the port that is used to connect to the XSUAA mock web server.
*/
public JwtGenerator(String clientId) {
public JwtGenerator(String clientId, int port) {
this.clientId = clientId;
this.identityZoneId = DEFAULT_IDENTITY_ZONE_ID;
this.jku = createJku(null);
this.mockXsuaaPort = port;
this.jku = createJku(null, port);
}

/**
* Specifies clientId of the JWT token claim.
*
* @param clientId
* the XSUAA client id, e.g. sb-applicationName!t123, defines the
* value of the JWT token claims "client_id" and "cid". A token is
* considered to be valid when it matches the "xsuaa.clientid" xsuaa
* service configuration (VCAP_SERVICES).
*/
public JwtGenerator(String clientId) {
this(clientId, MOCK_XSUAA_DEFAULT_PORT);
}

/**
Expand All @@ -109,7 +125,17 @@ public JwtGenerator(String clientId, String subdomain, String identityZoneId) {
this.clientId = clientId;
this.subdomain = subdomain;
this.identityZoneId = identityZoneId;
this.jku = createJku(subdomain);
this.mockXsuaaPort = MOCK_XSUAA_DEFAULT_PORT;
this.jku = createJku(subdomain, mockXsuaaPort);
}

/**
*
* @param port
* the port that is used to connect to the XSUAA mock web server.
*/
public JwtGenerator(int port) {
this(CLIENT_ID, port);
}

public JwtGenerator() {
Expand Down Expand Up @@ -243,9 +269,9 @@ public Jwt getToken() {
return createFromClaims(claimsSetBuilder.build().toString(), getHeaderMap(jwtHeaderKeyId, jku));
}

private static String createJku(String subdomain) {
private static String createJku(String subdomain, int port) {
String subdomainPart = subdomain != null && !subdomain.equals("") ? "/" + subdomain : "";
return "http://localhost:" + MOCK_XSUAA_PORT + subdomainPart + "/token_keys";
return "http://localhost:" + port + subdomainPart + "/token_keys";
}

private List<String> deriveAudiencesFromScopes(String[] scopes) {
Expand Down Expand Up @@ -354,7 +380,7 @@ public JWTClaimsSet.Builder getBasicClaimSet() {
* @return a basic set of claims
*/
public Map<String, String> getBasicHeaders() {
return getHeaderMap(jwtHeaderKeyId, createJku(subdomain));
return getHeaderMap(jwtHeaderKeyId, createJku(subdomain, mockXsuaaPort));
}

private static Jwt createFromClaims(String claims, Map<String, String> headers) {
Expand Down

0 comments on commit ec73534

Please sign in to comment.