Reading cookie from Wasm - go

I am attempting to read a cookie from Wasm but have not found any examples so I guessed, but I guessed wrong.
c := js.Global().Get("Cookie").Get("cookiename")
fmt.Println(c)
This gives me an error:
panic: syscall/js: call of Value.Get on undefined
Given that I have not found any documentation regarding reading cookies from Wasm.
Is this even possible?

There are three issues here:
the field is on document, not body
the field name is cookie, not Cookie
the cookie object is a string, you need to parse it to find the cookie by name.
To get the cookie string, use the following:
cookies := js.Global().Get("document").Get("cookie").String()
You will then need to process the string to iterate through the cookies and extract the one with the desired name. See Get cookie by name
As always with wasm, figure out the javascript code first then convert it to wasm.

Related

Not able to read JSON cookie

I have a situation where I want to read the contents of a cookie in Go. However the contents of the cookie is in a JSON format. (Changing the format of the cookie is not an option)
For example the contents of the cookie might be:
{"id":"abc","data":"information","on_off":false}
In JavaScript I'm easily able to read the contents of the cookie and parse it.
With Go, on the other hand, when I try to read the cookie with r.Cookie('my_cookie') I get the following error: http: named cookie not present.
If I modify the cookie to a simple string, then it works as expected.
Does anyone know what to do in this case? Is it just not possible to read a such a cookie in Go?
When I use r.Header.Get["Cookie"], the output it returns does contain the cookie and it's JSON value (listed among all the other cookies)
JSON uses many characters not permitted in HTTP cookie values per the RFC - for example, double quotes, commas, and whitespace characters are not permitted. The eastiest way to transfer JSON data via cookie would probably be to Base64 encode it when setting the cookie, and Base64 decode it when reading the cookie, using the encoding/base64 package.

GOLANG load JWK (JSON Web Keys), from memory not loading from a URL ? Fetch

I'm using "github.com/lestrrat/go-jwx/jwk" to Validate Jason Web Tokens (JWT). https://github.com/lestrrat/go-jwx
The JWK Library has a great function to load keys from a URL, but this would be running in a serverless environment, so I want to cache the keys rather than fetching them like this:
set, err := jwk.Fetch(keyUrl)
I'm trying to figure out how to load them directly, or create them from the keys I already have.
The keys are the JSON format.
{"keys":
[{"alg":"RS256","e":"AQAB","n":"jqm5oX5Vth4JW1gZQHywIki2beYCgBSL-
EYlefDUlI6SShtEKfi-vWYbFh2pNNUAE4NHuYpYP-
FG1uRSKs6WK2k6KMB2Hyx3hBkWyu7Aqo_pb1WItkPSZS-AWOMp4N-
I just can't find an example of how to do this. There must be a way
In case anyone else get's stuck on this, although I think it's unlikely, and thank you Stephen and Jonah for your patience.
jwk.Parse was exactly what I needed. There is even an option for a string.
set, err := jwk.ParseString(`{"keys":[{"alg":"RS256","e":"AQAB","n":"jqm5oX5Vth4JW1gZQHywIki2beYCgBSL-EYlefDUlI6SShtEKfi-vWYbFh2pNNUAE4NHuYpYP-FG1uRSKs6WK2k6KMB2Hyx3hBkWyu7Aqo_pb1WItkPSZS-AWOMp4N-ENPGyNQ4u2ill17ZwBKt1wgDvdaWpNaqPEcSaUNSXA7ZD5scFFnZt36r6crhgK7vHXChhXU3nlKEN2OwFdyEGdX01l-HxgPk0HPIfnUyYxuGI_CL-7aV_rf3KY4uw6yNfEO0jom0NZ-I8F1NzpbaDxj4hD4xlDZIQI9PxLq4nAtb4ZVQC1xhnRv-uufs9Wa4XFjs6jJSWGcWV1Z7K5HmMaw","kid":"tNtnDgeXepbf2NZlVjzKGdtPU9mnOidtw0puo3s-_8Y","kty":"RSA","use":"sig"},{"alg":"RS256","e":"AQAB","n":"o_CYYN5Lo9XuCRADNxqDTVolcWshv7LD7EmwFCvtbdHErvfOQPHyAFMLMDKracToe2Rg7cBihYC_wgHRItXAgxAM6P7mNT_DvbLpp2HtbMtmYLtWyGqHJsJQE9gNSOMsxEugxq9lpGjROiU2nZm7TZ-QwVV-F7dfBl0NBVy5oFbTCRUDxFcKOr_zh3xK-fRIsJbqbB4VPt8IhA_GjvjyoMlH0pfhoRqDMWk8vLXHhRxYlT5bZP1PDulFMGGO8KH7Cvh4D0cZE9mSiUFyVlDDzCbAH88a0TAGYkqERdVfHBYaTsCCseBqsvUoqbdzQwd2fOLf-N0jpSdNMNxHGNwl3w","kid":"ddPUDDyUpH2N5wGSXvnqQZy-OmTFSVu5PYansjpsoAs","kty":"RSA","use":"sig"}]}`)

setting cookie for use in warden. "invalid cookie value"

I am writing specs for my api and am manually writing a http request.
I am trying to set a cookie by adding the following key-val header
cookies
cookies: {
'warden.user.user.key' => ActiveSupport::JSON.encode(
[[session_data[0]], session_data[1]]
)
}
That's a String-String key-val pair, but the value is invalid. I get ArgumentError: Invalid cookie value for the JSON-serialized value (which ends up as
"\"[[Xy2LppWrQ53yHmpxVOkF7w], $2a$10$a/kS05VaSlNv2wwBXPfGU.]\""
I honestly have no idea how to fix this. I am trying to make a cookie with this format because in my Controller, I figured out that the session cookie is stored in warden.user.user.key and the value is a nested array of [[object_uid], session_token]
Appreciate whatever help! thanks
If it makes any difference, I am using airborne to make the requests (which uses rest-client under the hood, but doesn't throw errors on non-OK status codes.)
In the bigger picture I'm just trying to pass a session token to warden, which is used by devise.

how to keep key case sensitive in request header using golang?

I am recently using golang library "net/http",while add some header info to request, I found that the header keys are changing, e.g
request, _ := &http.NewRequest("GET", fakeurl, nil)
request.Header.Add("MyKey", "MyValue")
request.Header.Add("MYKEY2", "MyNewValue")
request.Header.Add("DONT-CHANGE-ME","No")
however, when I fetch the http message package, I found the header key changed like this:
Mykey: MyValue
Mykey2: MyNewValue
Dont-Change-Me: No
I using golang 1.3, then how to keep key case sensitive or keep its origin looking?
thx.
The http.Header Add and Set methods canonicalize the header name when adding values to the header map. You can sneak around the canonicalization by adding values using map operations:
request.Header["MyKey"] = []string{"MyValue"}
request.Header["MYKEY2"] = []string{"MyNewValue"}
request.Header["DONT-CHANGE-ME"] = []string{"No"}
As long as you use canonical names for headers known to the transport, this should work.

JMeter, authenticity token has been encoded, how to decode

My script get an authenticity token from first page, then pass it to the second page I got this error when running the test:
{"errors":{"error:":["ActionController::InvalidAuthenticityToken"]}}
I checked the token variable, apparently the string such as /, = has been encoded into %2F and %3D etc..
On my second page's HTTP request window, I have NOT selected "encoded" for token variable defined in the "Send parameters with the request" section.
Apparently the problem is I did not asked for encoded variable, JMeter just encode it. Is it a known bug or I should do something to decode it or is there a work around on this issue?
Did you pass token in POST data?
I'v faced the same problem and just pass body data as work around, to do it you need:
Select you HTTP sampler
Switch from "Parameter" to "Body Data" tab (you have to delete all parameters to do it)
Write body data (variable substitution works)
Body data example:
username=Some_name&token=AbRaCaDaBrA
NB, if something need to be encoded, you'll have to do it manually.

Resources