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

Authentication of Websocket connection #1732

Closed
Zaixu opened this issue Apr 17, 2020 · 11 comments
Closed

Authentication of Websocket connection #1732

Zaixu opened this issue Apr 17, 2020 · 11 comments
Labels
Area: Security Issue is related to security 🌶️ hot chocolate ❓ question This issue is a question about feature of Hot Chocolate.
Milestone

Comments

@Zaixu
Copy link

Zaixu commented Apr 17, 2020

As the title. How is this supposed to work with HotChocolate? I cant seem to wrap my head around it.
As websockets doesnt send headers. If the connection cant be stopped, data can be sniffed.

Am i missing something here? Seing as authorize attribute will only return unauthorized, as there is no bearer token. I could send a parameter in with websocket, but it looks to me hotchocolate is taking care of the whole connection in its middleware, where there is no auth handling.

`

[ExtendObjectType(Name = "Query")]
public class OrganizationQueries
{
    public IQueryable<OrganizationDTO> GetOrganizations([Service] IMapper mapper, [Service]DatabaseContext dbContext) {
        return mapper.ProjectTo<OrganizationDTO>(dbContext.Organizations);
    }
}

`

@PascalSenn
Copy link
Member

PascalSenn commented Apr 17, 2020

Hi @Zaixu
HotChocolate itself does not do the authentication only the authorization
If you would like to do authenticate with a bearer token you can either pass it over the querystring of the websocket connection url or you use a message interceptor of HotChocolate and do the authentication yourself
If you use Apollo you can follow the guide here:
https://gist.github.com/PascalSenn/43fedfbc1bc96692d99263a9da2d9ac4

@PascalSenn PascalSenn added ❓ question This issue is a question about feature of Hot Chocolate. 🌶 hot chocolate labels Apr 17, 2020
@Zaixu
Copy link
Author

Zaixu commented Apr 18, 2020

Hi @PascalSenn
Thanks that was exactly what i was looking for.
Any good ideas to handling expiring access token for WebSocket in HotChocolate? Not exactly sure how to deal with that.

@PascalSenn
Copy link
Member

@Zaixu
As far as i know, there are two different cases to consider.
First one would the token expiries, then we would have to renew it whenever possible
Second one would be that the token is revoked from the identity provider

I think we would need to intercept every single message to achieve this. (Especially in the second case 😄 )

I am not even sure if such an interceptor even exists atm.
@michaelstaib any ideas?

@PetterRein
Copy link

PetterRein commented Jul 10, 2020

I was working on adding subscription, and got user not authorized after implementing the code Pascal wrote.

To get things working with the Authorize attribute I had to add the changes I made in this commit: add ClaimsPrincipal to ContextData workaround websocket auth

I'am no expert at all but it looks like when using websocket the ClaimsPrincipal is not forwarded, is this on intention or a bug? @PascalSenn @michaelstaib

@promontis
Copy link
Contributor

@PetterRein this one is related #1319

Also discussed at: dotnet/aspnetcore#2881

@PetterRein
Copy link

Thank you!

So this is a "problem" with aspnetcore itself and not HotChocolate, looks like I have to look into SingalR.

@promontis
Copy link
Contributor

@PetterRein AFAIK SignalR has the same problem. But the code in your repo seems to works just fine. I will try and see if I can do a PR for your code to HC. Will make sure it is still configurable.

@Arsync
Copy link

Arsync commented Sep 5, 2020

@PetterRein

I had to add the changes I made in this commit

You save my weekend! But, we can do it easer with IHttpContextAccessor, no need for WebSocketContext

// The second parameter of the AddGraphQL method:
(IQueryExecutionBuilder builder) => builder
    .Use(next => async context =>
    {
        var hca = context.Services.GetRequiredService<IHttpContextAccessor>();

        context.ContextData["ClaimsPrincipal"] = hca.HttpContext.User;

        await next(context);
    })
    .UseDefaultPipeline()

@tobias-tengler tobias-tengler added the Area: Security Issue is related to security label Jun 26, 2021
@michaelstaib michaelstaib added this to the Backlog milestone Nov 22, 2021
@PascalSenn PascalSenn added the 🙋 good first issue Good for newcomers label Mar 16, 2022
@martinmesserli
Copy link

@michaelstaib @PascalSenn: What is the proper way of authentication/authorization for subscriptions in the GraphQL server? I could not find a complete example in the docs.
Hint: Our scenario includes Keycloak which issues JWT's and the sending GraphQL client is Apollo Angular.

@github-actions
Copy link

Stale issue message

@github-actions github-actions bot added the ⌛ stale Nothing happened with this issue in quite a while label Jun 12, 2022
@michaelstaib
Copy link
Member

have a look at the gits of pascal:
https://gist.github.com/PascalSenn/43fedfbc1bc96692d99263a9da2d9ac4

@stale stale bot removed the ⌛ stale Nothing happened with this issue in quite a while label Jun 13, 2022
@stale stale bot added the ⌛ stale Nothing happened with this issue in quite a while label Oct 11, 2022
@ChilliCream ChilliCream deleted a comment from stale bot Feb 7, 2023
@michaelstaib michaelstaib removed 🙋 good first issue Good for newcomers ⌛ stale Nothing happened with this issue in quite a while labels Feb 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: Security Issue is related to security 🌶️ hot chocolate ❓ question This issue is a question about feature of Hot Chocolate.
Projects
None yet
Development

No branches or pull requests

8 participants