Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose "fetchInit" in RpcOptions to allow sending credentials #95

Closed
frederikhors opened this issue Apr 7, 2021 · 11 comments · Fixed by #105
Closed

Expose "fetchInit" in RpcOptions to allow sending credentials #95

frederikhors opened this issue Apr 7, 2021 · 11 comments · Fixed by #105
Labels
enhancement New feature or request

Comments

@frederikhors
Copy link

I'm using /~https://github.com/timostamm/protobuf-ts/blob/master/MANUAL.md#grpc-web-transport but I don't know if there is a way to send credentials like in a classic web client:

initClient({
  baseUrl: "localhost:3000",
  fetchOptions: { credentials: "include" },
});

Is it possible or am I totally wrong?

@frederikhors
Copy link
Author

My grpc-web server doesn't get cookies.

@frederikhors
Copy link
Author

I think we have some useful hints about this problem: /~https://github.com/improbable-eng/grpc-web/blob/master/client/grpc-web/docs/transport.md.

@timostamm
Copy link
Owner

Why not send credentials / token in a header?

rpc-options take metadata, which translate to HTTP headers.

For example:

chatPanel.postCallback = async (text) => {
try {
await client.post({
message: text
}, {
meta: {'x-token': headers['x-token']},
})

Note that CORS applies across domains.

Adding something like "fetchOptions" should be pretty straight-forward to add to the grpc-web and twirp transports.

@frederikhors
Copy link
Author

frederikhors commented Apr 7, 2021

Because we are using cookies not readable in browser with Javascript: HTTP-ONLY.

@timostamm
Copy link
Owner

A valid point...

Yes, this should work like improbable-eng.

The following property should be added to twirp-options.ts and grpc-web-options.ts:

    /**
     * Pass options to fetch API calls. 
     */
    fetchInit?: Exclude<RequestInit, "body" | "headers" | "method" | "signal">;

And passed to all fetch calls.

I'm a bit swamped right now. Do you want to open a PR?

@frederikhors
Copy link
Author

Do you want to open a PR?

I am not able to. 🤣

@frederikhors
Copy link
Author

What a pity! This blocks all the plans I had with this library, at least for now.

Do you have a temporary workaround?

@timostamm
Copy link
Owner

I am not able to. 🤣

Just click fork in the top right, check out your fork and do the changes.

Do you have a temporary workaround?

I'd just copy grpc-web-transport.ts into my project and add the necessary properties to the fetch calls. Maybe you have to copy another file over and add a tsconfig for some settings, but it shouldn't be too difficult.

@frederikhors
Copy link
Author

do the changes

This is what I am not able to do. 🤣

@timostamm
Copy link
Owner

Then just copy grpc-web-transport.ts There are only 2 places where fetch() is called

        globalThis.fetch(url, {
            method: 'POST',
            headers: createGrpcWebRequestHeader(new globalThis.Headers(), format, opt.deadline, opt.meta),
            body: createGrpcWebRequestBody(inputBytes, format),
            signal: options.abort ?? null, // node-fetch@3.0.0-beta.9 rejects `undefined`
            credentials: 'include'
//          ^^^^^^^^^^^^^^^^^^^^^^
// add this. or maybe 'same-origin', depending on what you need.
        })

@frederikhors
Copy link
Author

@timostamm Yeah, I'm hacking around like this... Thanks!

@timostamm timostamm changed the title grpcWeb transport: how to send credentials? Expose "fetchInit" in RpcOptions to allow sending credentials Apr 15, 2021
@timostamm timostamm added the enhancement New feature or request label Apr 15, 2021
timostamm added a commit that referenced this issue May 7, 2021
add fetchInit to RpcOptions for grpc-web and twirp (#95)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants