Checking the IP address of a user in a Microflow

0
Hi,    For our app with certain actions within the app we need to know if the person is from the USA. Therefore I was wondering if maybe through a Java action it would be possible to get the IP of the logged in user that triggered the microflow so that I can check if this person is connected to our app from USA.   Does anyone have any experience with this.  Any help or tips would be much appreciated!   Regards, Thomas
asked
1 answers
2

The HTTP Commons module in the Marketplace used to provide a GetIP action, but this is currently out of date.

I use this Java action based on the GetIP action to get IP addresses.

 

		// BEGIN USER CODE
		IContext ctx = this.getContext();
		if (ctx.getRuntimeRequest().isPresent()) {
			IMxRuntimeRequest req = ctx.getRuntimeRequest().get();

			// https://stackoverflow.com/questions/16558869/getting-ip-address-of-client
			String xForwardedForHeader = req.getHeader("X-Forwarded-For");
			if (xForwardedForHeader == null) {
				return req.getRemoteAddr();
			} else {
				// As of https://en.wikipedia.org/wiki/X-Forwarded-For
				// The general format of the field is: X-Forwarded-For: client,
				// proxy1, proxy2 ...
				// we only want the client
				String forwardIp = new StringTokenizer(xForwardedForHeader, ",").nextToken().trim();
				boolean validIP = checkIP(forwardIp);
				if (validIP) {
					return forwardIp;			
				} else {
					Core.getLogger("GetIP").error("Could not parse X-Forwarded-for header to an IP address. Header: "+forwardIp);
					return "";
				}
			}
		} else {
			return "";
		}
		// END USER CODE

I hope this helps.

answered