Skip to content

Instantly share code, notes, and snippets.

View PascalSenn's full-sized avatar
🌶️

PascalSenn

🌶️
View GitHub Profile
@PascalSenn
PascalSenn / objectStateHook
Created October 24, 2019 08:34
ObjectStatehook
function useObjectState<T, K extends keyof T>(
key: K,
objState: [T, Dispatch<SetStateAction<T>>]
): [T[K], (val: T[K]) => void] {
const [obj, setObj] = objState;
const setKey = (val: T[K]) => setObj({ ...obj, [key]: val });
return [obj[key], setKey];
}
docker run --name mongo -p 27017:27017 -d mongo mongod
public sealed class UseOffsetPagingAttribute : DescriptorAttribute
{
protected override void TryConfigure(
HotChocolate.Types.Descriptors.IDescriptorContext context,
IDescriptor descriptor,
ICustomAttributeProvider element)
{
if (element is MemberInfo m)
{
@PascalSenn
PascalSenn / .cs
Created March 12, 2020 20:33
HotChocolate SocketInterceptor for Apollo Client Subscriptions
namespace ApolloClient.Examples
{
public class AuthenticationSocketInterceptor : ISocketConnectionInterceptor<HttpContext>
{
// This is the key to the auth token in the HTTP Context
public static readonly string HTTP_CONTEXT_WEBSOCKET_AUTH_KEY = "websocket-auth-token";
// This is the key that apollo uses in the connection init request
public static readonly string WEBOCKET_PAYLOAD_AUTH_KEY = "authToken";
private readonly IAuthenticationSchemeProvider _schemes;
@PascalSenn
PascalSenn / .cs
Created March 12, 2020 20:57
Authentication Provider
services.AddAuthentication(options =>
{
options.DefaultScheme = "YOURSCHEMANAME";
options.DefaultAuthenticateScheme = "YOURSCHEMANAME";
})
.AddJwtBearer("Websockets", ctx => { })
.AddIdentityServerAuthentication("YOURSCHEMANAME", options =>
{
options.ForwardDefaultSelector = context =>
{
@PascalSenn
PascalSenn / auth-apollo-how-to.md
Last active March 31, 2023 12:20
Authentication Apollo Client - How To

The problem with authentication of web sockets is that you can only authenticate a http request once against a authentication scheme. After that the authentication is cached and will always yield the same result.

A web socket connection starts over HTTP. A HTTP request with the Upgrade header is send to the back end. This request is then "upgraded" into a web socket that runs over the web socket pipeline. The main issue with web sockets is that you cannot set additional headers with this initial HTTP header. Therefore the authentication will fail and the request will be unauthenticated. The HTTP context of the initial request will last as long as the web socket connection is running.

The trick we do is that we add a stub authentication scheme:

[AttributeUsage(AttributeTargets.Property | AttributeTargets.Method)]
public abstract class DataLoaderAttribute : ObjectFieldDescriptorAttribute
{
public static MethodInfo ConfigureMethod = typeof(DataLoaderAttribute)
.GetMethod(nameof(DataLoaderAttribute.Configure));
public DataLoaderAttribute(Type dataloaderType)
{
DataloaderType = dataloaderType;
}
public Type DataloaderType { get; }
// Place your key bindings in this file to overwrite the defaults
[
{
"key": "shift+alt+q",
"command": "tslint.fixAllProblems"
},
{
"key": "shift+alt+u",
"command": "-extension.updateSettings"
},
{
"before": ["<tab>"],
"commands": ["editor.action.indentLines"]
},
{
"before": ["<S-Tab>"],
"commands": ["editor.action.outdentLines"]
},
{
"before": [","],
@PascalSenn
PascalSenn / MultipartRequestMiddleware
Last active April 7, 2024 20:27
MultiPartRquestMiddlware made with ❤️ by https://github.com/acelot
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;
using System.Text.Json;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Newtonsoft.Json;