The process of initiating an action on a server is done through HTTP
requests which are messages sent by the client.
The two most commonly used HTTP
requests are GET
and POST
. This task involves validating requests and parsing URL
parameters as a form of authentication. Authentication tokens for both GET
and POST
requests are sent as a URL
parameter named "token"
. For validation of authentication, the tokens must be in a set of valid authentication tokens.
In the case of a POST
request, a CSRF (cross-site request forgery)
token must also be provided. A POST
request is
considered valid if its authentication token is valid and its CSRF
token is an alphanumeric value consisting only of
lowercase letters and/or numbers with a minimum length of 8. Once a request is validated, the URL parameters must be
parsed as a comma-separated string.
URL parameters are identified by the portion of the URL that comes after a question mark (?)
. They consist of a key
and a value, separated by an equal sign (=)
. Multiple parameters are separated by an ampersand (&)
.
Implementation request parser prototype. Given an array of strings, valid_auth_tokens
, representing the
valid authentication tokens, and a 2D array of strings, requests
, representing the requests and URLs,
for each request, return the request status ("VALID" or "INVALID"). If VALID, include a comma-separated list of
parameters, i.e. "VALID,param1_key=param1_value,param2_key=param2_value".
Function Description
Complete the function getResponses
in the editor.
getResponses
has the following parameters(s):
- 1. string
valid_auth_tokens[m]
: The list of valid authentication tokens - 2. string
requests[n][2]
: The list of requests made, their types, and the associated URLs.
Returns
string[n]
: the responses for each request
π₯ spike is dat excellect π GG of Error-Free Excellence!! π₯
Example 1:
Input: valid_auth_tokens = ["ah37iz2ha483u", "sah34ywbd0op5", "ba34av8iy902j"], requests = [["GET", "https://example.com?token=34dy58kiu2"], ["GET", "https://example.com?token=sah34ywbd0op5&name=sam"], ["POST", "https://example.com?token=sah34ywbd0op5"], ["POST", "https://example.com?token=sah34ywbd0op5&name=chris&csrf_token=ak2sh32dy"]]
Output: ["INVALID", "VALID,name=sam", "INVALID", "VALID,name=chris"]
Explanation:In the first request, the auth_token "34dy58kiu2", which is not in the list of given tokens, so the request is INVALID. The string to be returned is "INVALID". In the second request, the auth_token = sah34ywbd0op5, which is in the list of valid tokens, the request is VALID. The request parameters are _name = sam. The string to be returned is "VALID,name=sam". In the third request, the auth_token = sah34ywbd0op5, which is in the list of valid tokens, but since the request is a POST request, it must have a valid CSRF token. Since the given request doesn't have a CSRF token, the request is INVALID. The string to be returned is "INVALID". In the fourth request, the auth_token = sah34ywbd0op5, which is in the list of valid tokens, but since the request is a POST request, it must have a valid CSRF token. CSRF_token = ak2sh32dy, it is an alphanumeric string with length 9(>=8), so the given request is VALID. The request parameters are _name = chris. The string to be returned is "VALID,name=chris".
- 1 <= m <= 2*103
- 1 <= n <= 5*103
- size(valid_auth_token[i]) = 12
- size(requests[i][0]) <= 20
input:
output: