@Test public void getClientIpFromXForwardedForWithSingleValue() throws Exception { doReturn(CLIENT_IP).when(servletRequest).getHeader("X-Forwarded-For"); doReturn(null).when(servletRequest).getHeader("Forwarded"); String clientIP = service.retrieveClientIp(servletRequest); assertEquals(clientIP, CLIENT_IP); }
@VisibleForTesting String retrieveClientIp(HttpServletRequest servletRequest) { String ip = null; String xForwardedFor = servletRequest.getHeader("X-Forwarded-For"); String forwarded = servletRequest.getHeader("Forwarded"); if (xForwardedFor != null) { LOG.info( "Looking for reCaptcha verification user IP into the request `X-Forwarded-For` header"); int index = xForwardedFor.indexOf(','); if (index > 0) { xForwardedFor = xForwardedFor.substring(0, index).trim(); } ip = xForwardedFor; } else if (forwarded != null) { LOG.info("Looking for reCaptcha verification user IP into the request `Forwarded` header"); int index = forwarded.indexOf(','); if (index > 0) { forwarded = forwarded.substring(0, index).trim(); } for (String part : forwarded.split(";")) { part = part.trim(); if (part.startsWith("for=")) { ip = part.substring(4); break; } } } if (ip == null) { LOG.info("Looking for reCaptcha verification user IP into the request remote address"); ip = servletRequest.getRemoteAddr(); } return ip; }
End2EndFlowService extends Service { @VisibleForTesting String retrieveClientIp(HttpServletRequest servletRequest) { String ip = null; String xForwardedFor = servletRequest.getHeader("X-Forwarded-For"); String forwarded = servletRequest.getHeader("Forwarded"); if (xForwardedFor != null) { LOG.info( "Looking for reCaptcha verification user IP into the request `X-Forwarded-For` header"); int index = xForwardedFor.indexOf(','); if (index > 0) { xForwardedFor = xForwardedFor.substring(0, index).trim(); } ip = xForwardedFor; } else if (forwarded != null) { LOG.info("Looking for reCaptcha verification user IP into the request `Forwarded` header"); int index = forwarded.indexOf(','); if (index > 0) { forwarded = forwarded.substring(0, index).trim(); } for (String part : forwarded.split(";")) { part = part.trim(); if (part.startsWith("for=")) { ip = part.substring(4); break; } } } if (ip == null) { LOG.info("Looking for reCaptcha verification user IP into the request remote address"); ip = servletRequest.getRemoteAddr(); } return ip; } }
End2EndFlowService extends Service { @VisibleForTesting String retrieveClientIp(HttpServletRequest servletRequest) { String ip = null; String xForwardedFor = servletRequest.getHeader("X-Forwarded-For"); String forwarded = servletRequest.getHeader("Forwarded"); if (xForwardedFor != null) { LOG.info( "Looking for reCaptcha verification user IP into the request `X-Forwarded-For` header"); int index = xForwardedFor.indexOf(','); if (index > 0) { xForwardedFor = xForwardedFor.substring(0, index).trim(); } ip = xForwardedFor; } else if (forwarded != null) { LOG.info("Looking for reCaptcha verification user IP into the request `Forwarded` header"); int index = forwarded.indexOf(','); if (index > 0) { forwarded = forwarded.substring(0, index).trim(); } for (String part : forwarded.split(";")) { part = part.trim(); if (part.startsWith("for=")) { ip = part.substring(4); break; } } } if (ip == null) { LOG.info("Looking for reCaptcha verification user IP into the request remote address"); ip = servletRequest.getRemoteAddr(); } return ip; } @Inject  End2EndFlowService(
      @Nullable @Named("che.fabric8.end2end.protect.site_key") String siteKey,
      @Nullable @Named("che.fabric8.end2end.protect.secret_key") String secretKey,
      @Named("che.fabric8.end2end.protect.verify_with_ip") boolean verifyWithIp); }
End2EndFlowService extends Service { @VisibleForTesting String retrieveClientIp(HttpServletRequest servletRequest) { String ip = null; String xForwardedFor = servletRequest.getHeader("X-Forwarded-For"); String forwarded = servletRequest.getHeader("Forwarded"); if (xForwardedFor != null) { LOG.info( "Looking for reCaptcha verification user IP into the request `X-Forwarded-For` header"); int index = xForwardedFor.indexOf(','); if (index > 0) { xForwardedFor = xForwardedFor.substring(0, index).trim(); } ip = xForwardedFor; } else if (forwarded != null) { LOG.info("Looking for reCaptcha verification user IP into the request `Forwarded` header"); int index = forwarded.indexOf(','); if (index > 0) { forwarded = forwarded.substring(0, index).trim(); } for (String part : forwarded.split(";")) { part = part.trim(); if (part.startsWith("for=")) { ip = part.substring(4); break; } } } if (ip == null) { LOG.info("Looking for reCaptcha verification user IP into the request remote address"); ip = servletRequest.getRemoteAddr(); } return ip; } @Inject  End2EndFlowService(
      @Nullable @Named("che.fabric8.end2end.protect.site_key") String siteKey,
      @Nullable @Named("che.fabric8.end2end.protect.secret_key") String secretKey,
      @Named("che.fabric8.end2end.protect.verify_with_ip") boolean verifyWithIp); @GET @Path("site-key") String siteKey(); @POST @Path("verify") @Consumes("text/plain") @Produces(APPLICATION_JSON) Response verifyReCaptchaToken(String token, @Context HttpServletRequest servletRequest); @POST @Path("warning") @Consumes("text/plain") void warning(String message, @Context HttpServletRequest servletRequest); @POST @Path("error") @Consumes("text/plain") void error(String message, @Context HttpServletRequest servletRequest); @GET @Path("{path: files\\/.*}") Response staticResources(@PathParam("path") final String path); }
End2EndFlowService extends Service { @VisibleForTesting String retrieveClientIp(HttpServletRequest servletRequest) { String ip = null; String xForwardedFor = servletRequest.getHeader("X-Forwarded-For"); String forwarded = servletRequest.getHeader("Forwarded"); if (xForwardedFor != null) { LOG.info( "Looking for reCaptcha verification user IP into the request `X-Forwarded-For` header"); int index = xForwardedFor.indexOf(','); if (index > 0) { xForwardedFor = xForwardedFor.substring(0, index).trim(); } ip = xForwardedFor; } else if (forwarded != null) { LOG.info("Looking for reCaptcha verification user IP into the request `Forwarded` header"); int index = forwarded.indexOf(','); if (index > 0) { forwarded = forwarded.substring(0, index).trim(); } for (String part : forwarded.split(";")) { part = part.trim(); if (part.startsWith("for=")) { ip = part.substring(4); break; } } } if (ip == null) { LOG.info("Looking for reCaptcha verification user IP into the request remote address"); ip = servletRequest.getRemoteAddr(); } return ip; } @Inject  End2EndFlowService(
      @Nullable @Named("che.fabric8.end2end.protect.site_key") String siteKey,
      @Nullable @Named("che.fabric8.end2end.protect.secret_key") String secretKey,
      @Named("che.fabric8.end2end.protect.verify_with_ip") boolean verifyWithIp); @GET @Path("site-key") String siteKey(); @POST @Path("verify") @Consumes("text/plain") @Produces(APPLICATION_JSON) Response verifyReCaptchaToken(String token, @Context HttpServletRequest servletRequest); @POST @Path("warning") @Consumes("text/plain") void warning(String message, @Context HttpServletRequest servletRequest); @POST @Path("error") @Consumes("text/plain") void error(String message, @Context HttpServletRequest servletRequest); @GET @Path("{path: files\\/.*}") Response staticResources(@PathParam("path") final String path);  }