0% found this document useful (0 votes)
4 views2 pages

New 1

This Java code parses URL requests and validates authentication tokens and CSRF tokens. It takes in valid auth tokens and request URLs, extracts the parameters from the URLs, validates the auth and CSRF tokens, and returns valid or invalid responses.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views2 pages

New 1

This Java code parses URL requests and validates authentication tokens and CSRF tokens. It takes in valid auth tokens and request URLs, extracts the parameters from the URLs, validates the auth and CSRF tokens, and returns valid or invalid responses.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

import java.util.

ArrayList;
import java.util.Arrays;
import java.util.List;

public class RequestParser {

public static List<String> getResponses(String[] validAuthTokens, String[][]


requests) {
List<String> responses = new ArrayList<>();

for (String[] request : requests) {


String requestType = request[0];
String url = request[1];

// Extract parameters from the URL


String[] urlParts = url.split("\\?");
if (urlParts.length != 2) {
responses.add("INVALID");
continue;
}

// Extract authentication token and parameters


String[] queryParams = urlParts[1].split("&");
String authToken = null;
String csrfToken = null;
List<String> params = new ArrayList<>();

for (String param : queryParams) {


String[] keyValue = param.split("=");
if (keyValue.length == 2) {
String key = keyValue[0];
String value = keyValue[1];

if ("token".equals(key)) {
authToken = value;
} else if ("csrf".equals(key)) {
csrfToken = value;
} else {
params.add(key);
params.add(value);
}
}
}

// Validate authentication token


if (authToken == null || !
Arrays.asList(validAuthTokens).contains(authToken)) {
responses.add("INVALID");
continue;
}

// Validate CSRF token for POST requests


if ("POST".equals(requestType) && (csrfToken == null ||
csrfToken.length() < 8 || !csrfToken.matches("[a-zA-Z0-9]+"))) {
responses.add("INVALID");
continue;
}

// Construct response string


StringBuilder response = new StringBuilder("VALID");
for (int i = 0; i < params.size(); i += 2) {
response.append(",
").append(params.get(i)).append(",").append(params.get(i + 1));
}

responses.add(response.toString());
}

return responses;
}

public static void main(String[] args) {


String[] validAuthTokens = {"ah37j2ha483u", "safh34ywb0p5",
"ba34wyi8t902"};
String[][] requests = {
{"GET", "https://example.com/?token=347sd6yk8iu2&name=alex"},
{"GET", "https://example.com/?token=safh34ywb0p5&name=sam"},
{"POST", "https://example.com/?token=safh34ywb0p5&name=alex"},
{"POST", "https://example.com/?
token=safh34ywb0p5&csrf=ak2sh32dy&name=chris"}
};

List<String> responses = getResponses(validAuthTokens, requests);

for (String response : responses) {


System.out.println(response);
}
}
}

You might also like