我必須使用帶有 C# 和 Hot Chocolate 的 GraphQL 來實作 API。其中一項要求是請求的正文必須加密。
例如,如果我用這個主體呼叫我的“Hello World”服務:
query{
hello(name: "stackoverflow.com")
}
服務的回應是這樣的:
{
"data": {
"hello": "Hello, stackoverflow.com "
}
}
我必須能夠發送一個用某種演算法完全加密的正文,比如說 SHA256,如下所示:
//This is the query of graphql encrypted in SHA256
5777bb8378c6caf7f29e7b6aae9ddcb168cbba74ffd60dbe6ef21c2f70b16736
然后,解密請求并獲取要作為回應發送的資料。
我的第一個方法是添加一個攔截器 獲取 HttpRequest 正文,解密,然后繼續正常流程。
public class HttpRequestInterceptor : DefaultHttpRequestInterceptor
{
public override ValueTask OnCreateAsync(HttpContext context,
IRequestExecutor requestExecutor,
IQueryRequestBuilder requestBuilder,
CancellationToken cancellationToken)
{
//Get the raw body of the request
var rawBody = GetRequestBodyAsync(context.Request);
//Decrypt the raw body and set the decrypted body into the HttpContext request body
GetDecryptedBodyAsync(context, rawBody.Result);
return base.OnCreateAsync(context, requestExecutor, requestBuilder, cancellationToken);
}
private async void GetDecryptedBodyAsync(HttpContext context, string rawBody)
{
var requestContent = new StringContent(rawBody, Encoding.UTF8, "application/json");
context.Request.Body = await requestContent.ReadAsStreamAsync();
}
}
問題是當我測驗這種方法時,它不起作用,因為作為正文的加密哈希輸入無效:
{
"errors": [
{
"message": "Unexpected token: Name.",
"locations": [
{
"line": 1,
"column": 1
}
],
"extensions": {
"code": "HC0011"
}
}
]
}
有沒有辦法做到這一點?
uj5u.com熱心網友回復:
所以加密和散列是有區別的。SHA256 是散列,與加密無關。如果您有檔案,則無法檢索原始檔案。
如果您不想通過網路發送查詢,那么您可能正在尋找持久查詢:
https://chillicream.com/docs/hotchocolate/v12/performance/persisted-queries 或作為視頻:https ://www.youtube.com/watch?v=ZZ5PF3_P_r4
如果這對您沒有幫助,并且您想要進行加密,那么您必須首先定義要保護您的資料的內容。也許 TLS 已經足夠加密了,那么你只需要強制用戶使用 HTTPS。
如果您最終使用某種自定義端到端加密,那么您可能必須在 HotChocolate 執行之前執行此操作。所以作為一個 AspNet 核心中間件,而不是一個 GraphQL 組件。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/527956.html
上一篇:我在BlazorServerApp中的AzureADB2C的refresh_token是空的,而我的id_token是提供的