-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
configure zuul proxy and filters , store tokens in cookies
- Loading branch information
Showing
15 changed files
with
329 additions
and
21 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
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
28 changes: 28 additions & 0 deletions
28
src/main/java/org/radarcns/management/filters/CustomHttpServletRequest.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,28 @@ | ||
package org.radarcns.management.filters; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletRequestWrapper; | ||
|
||
public class CustomHttpServletRequest extends HttpServletRequestWrapper { | ||
private final Map<String, String[]> additionalParams; | ||
private final HttpServletRequest request; | ||
|
||
public CustomHttpServletRequest(final HttpServletRequest request, final Map<String, String[]> additionalParams) { | ||
super(request); | ||
this.request = request; | ||
this.additionalParams = additionalParams; | ||
} | ||
|
||
@Override | ||
public Map<String, String[]> getParameterMap() { | ||
final Map<String, String[]> map = request.getParameterMap(); | ||
final Map<String, String[]> param = new HashMap<String, String[]>(); | ||
param.putAll(map); | ||
param.putAll(additionalParams); | ||
return param; | ||
} | ||
|
||
} |
91 changes: 91 additions & 0 deletions
91
src/main/java/org/radarcns/management/filters/OAuth2TokenRequestPostZuulFilter.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,91 @@ | ||
package org.radarcns.management.filters; | ||
|
||
import static org.radarcns.management.filters.OAuth2TokenRequestPreZuulFilter.REFRESH_TOKEN_COOKIE; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.util.Map; | ||
|
||
import javax.servlet.http.Cookie; | ||
|
||
import org.apache.commons.io.IOUtils; | ||
import org.radarcns.management.config.ManagementPortalProperties; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Component; | ||
|
||
import com.fasterxml.jackson.core.type.TypeReference; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.netflix.zuul.ZuulFilter; | ||
import com.netflix.zuul.context.RequestContext; | ||
|
||
/** | ||
* A post-filter for all the request sent to Zuul proxy. | ||
* This investigates the response body and stores the Refresh token in a Cookie and remove it from response body. | ||
*/ | ||
@Component | ||
public class OAuth2TokenRequestPostZuulFilter extends ZuulFilter { | ||
|
||
@Autowired | ||
private ManagementPortalProperties managementPortalProperties; | ||
private final Logger logger = LoggerFactory.getLogger(this.getClass()); | ||
private final ObjectMapper mapper = new ObjectMapper(); | ||
|
||
@Override | ||
public Object run() { | ||
final RequestContext ctx = RequestContext.getCurrentContext(); | ||
logger.debug("in zuul filter " + ctx.getRequest().getRequestURI()); | ||
|
||
final String requestURI = ctx.getRequest().getRequestURI(); | ||
final String requestMethod = ctx.getRequest().getMethod(); | ||
|
||
try { | ||
final InputStream is = ctx.getResponseDataStream(); | ||
String responseBody = IOUtils.toString(is, "UTF-8"); | ||
if (responseBody.contains("refresh_token")) { | ||
final Map<String, Object> responseMap = mapper | ||
.readValue(responseBody, new TypeReference<Map<String, Object>>() { | ||
}); | ||
final String refreshToken = responseMap.get("refresh_token").toString(); | ||
responseMap.remove("refresh_token"); | ||
responseBody = mapper.writeValueAsString(responseMap); | ||
|
||
final Cookie cookie = new Cookie(REFRESH_TOKEN_COOKIE, refreshToken); | ||
cookie.setHttpOnly(true); | ||
cookie.setSecure(true); | ||
cookie.setPath(ctx.getRequest().getContextPath() + "oauthserver/oauth/token"); | ||
cookie.setMaxAge(this.managementPortalProperties.getFrontend().getSessionTimeout()); // 30 minites | ||
ctx.getResponse().addCookie(cookie); | ||
logger.info("refresh token = " + refreshToken); | ||
|
||
} | ||
if (requestURI.contains("oauth/token") && requestMethod.equals("DELETE")) { | ||
final Cookie cookie = new Cookie(REFRESH_TOKEN_COOKIE, ""); | ||
cookie.setMaxAge(0); | ||
cookie.setPath(ctx.getRequest().getContextPath() + "oauthserver/oauth/token"); | ||
ctx.getResponse().addCookie(cookie); | ||
} | ||
ctx.setResponseBody(responseBody); | ||
|
||
} catch (final IOException e) { | ||
logger.error("Error occured in zuul post filter", e); | ||
} | ||
return null; | ||
} | ||
|
||
@Override | ||
public boolean shouldFilter() { | ||
return true; | ||
} | ||
|
||
@Override | ||
public int filterOrder() { | ||
return 10; | ||
} | ||
|
||
@Override | ||
public String filterType() { | ||
return "post"; | ||
} | ||
} |
82 changes: 82 additions & 0 deletions
82
src/main/java/org/radarcns/management/filters/OAuth2TokenRequestPreZuulFilter.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,82 @@ | ||
package org.radarcns.management.filters; | ||
|
||
import com.netflix.zuul.ZuulFilter; | ||
import com.netflix.zuul.context.RequestContext; | ||
import java.io.UnsupportedEncodingException; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import javax.servlet.http.Cookie; | ||
import javax.servlet.http.HttpServletRequest; | ||
import org.radarcns.management.config.ManagementPortalProperties; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.security.crypto.codec.Base64; | ||
import org.springframework.stereotype.Component; | ||
|
||
/** | ||
* A pre-filter for all request sent to Zuul proxy. | ||
* This adds the client credentials as Basic authentication header and | ||
* scope of the client configured in application.*.yml. | ||
* This prevents exposing client credentials to front-end. | ||
*/ | ||
@Component | ||
public class OAuth2TokenRequestPreZuulFilter extends ZuulFilter { | ||
|
||
static final String REFRESH_TOKEN_COOKIE = "rft"; | ||
@Autowired | ||
private ManagementPortalProperties managementPortalProperties; | ||
|
||
Logger logger = LoggerFactory.getLogger(OAuth2TokenRequestPreZuulFilter.class); | ||
@Override | ||
public Object run() { | ||
RequestContext ctx = RequestContext.getCurrentContext(); | ||
if (ctx.getRequest().getRequestURI().contains("/oauth/token")) { | ||
byte[] encoded; | ||
try { | ||
// get this from properties, this will allow us to use ENV variables for docker | ||
encoded = Base64.encode((managementPortalProperties.getFrontend().getClientId()+":"+managementPortalProperties.getFrontend().getClientSecret()).getBytes("UTF-8")); | ||
ctx.addZuulRequestHeader("Authorization", "Basic " + new String(encoded)); | ||
final HttpServletRequest req = ctx.getRequest(); | ||
final Map<String, String[]> param = new HashMap<String, String[]>(); | ||
param.put("scope" , new String[] {managementPortalProperties.getFrontend().getClientScopes()}); | ||
final String refreshToken = extractRefreshToken(req); | ||
if (refreshToken != null) { | ||
param.put("refresh_token", new String[] { refreshToken }); | ||
param.put("grant_type", new String[] { "refresh_token" }); | ||
} | ||
ctx.setRequest(new CustomHttpServletRequest(req, param)); | ||
} catch (UnsupportedEncodingException e) { | ||
logger.error("Error occured in pre filter", e); | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
private String extractRefreshToken(HttpServletRequest req) { | ||
final Cookie[] cookies = req.getCookies(); | ||
if (cookies != null) { | ||
for (int i = 0; i < cookies.length; i++) { | ||
if (cookies[i].getName().equalsIgnoreCase(REFRESH_TOKEN_COOKIE)) { | ||
return cookies[i].getValue(); | ||
} | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
@Override | ||
public boolean shouldFilter() { | ||
return true; | ||
} | ||
|
||
@Override | ||
public int filterOrder() { | ||
return -2; | ||
} | ||
|
||
@Override | ||
public String filterType() { | ||
return "pre"; | ||
} | ||
} |
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
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
Oops, something went wrong.