private async Task <string> CheckBitId(HttpContext httpContext, string sig, string id) { httpContext.Request.EnableBuffering(); string body = string.Empty; if (httpContext.Request.ContentLength != 0 && httpContext.Request.Body != null) { using (StreamReader reader = new StreamReader(httpContext.Request.Body, Encoding.UTF8, true, 1024, true)) { body = await reader.ReadToEndAsync(); } httpContext.Request.Body.Position = 0; } var url = httpContext.Request.GetEncodedUrl(); try { var key = new PubKey(id); if (BitIdExtensions.CheckBitIDSignature(key, sig, url, body)) { return(key.GetBitIDSIN()); } } catch { } return(null); }
private async Task <(string StoreId, bool SuccessAuth)> CheckBitId(HttpContext httpContext, string sig, string id, List <Claim> claims) { httpContext.Request.EnableRewind(); string storeId = null; string body = string.Empty; if (httpContext.Request.ContentLength != 0 && httpContext.Request.Body != null) { using (StreamReader reader = new StreamReader(httpContext.Request.Body, Encoding.UTF8, true, 1024, true)) { body = reader.ReadToEnd(); } httpContext.Request.Body.Position = 0; } var url = httpContext.Request.GetEncodedUrl(); try { var key = new PubKey(id); if (BitIdExtensions.CheckBitIDSignature(key, sig, url, body)) { var sin = key.GetBitIDSIN(); claims.Add(new Claim(Claims.SIN, sin)); string token = null; if (httpContext.Request.Query.TryGetValue("token", out var tokenValues)) { token = tokenValues[0]; } if (token == null && !String.IsNullOrEmpty(body) && httpContext.Request.Method == "POST") { try { token = JObject.Parse(body)?.Property("token")?.Value?.Value <string>(); } catch { } } if (token != null) { var bitToken = await GetTokenPermissionAsync(sin, token); if (bitToken == null) { return(null, false); } storeId = bitToken.StoreId; } } else { return(storeId, false); } } catch (FormatException) { } return(storeId, true); }
public async Task Invoke(HttpContext httpContext) { RewriteHostIfNeeded(httpContext); httpContext.Request.Headers.TryGetValue("x-signature", out StringValues values); var sig = values.FirstOrDefault(); httpContext.Request.Headers.TryGetValue("x-identity", out values); var id = values.FirstOrDefault(); if (!string.IsNullOrEmpty(sig) && !string.IsNullOrEmpty(id)) { httpContext.Request.EnableRewind(); string body = string.Empty; if (httpContext.Request.ContentLength != 0 && httpContext.Request.Body != null) { using (StreamReader reader = new StreamReader(httpContext.Request.Body, Encoding.UTF8, true, 1024, true)) { body = reader.ReadToEnd(); } httpContext.Request.Body.Position = 0; } var url = httpContext.Request.GetEncodedUrl(); try { var key = new PubKey(id); if (BitIdExtensions.CheckBitIDSignature(key, sig, url, body)) { var bitid = new BitIdentity(key); httpContext.User = new GenericPrincipal(bitid, new string[0]); Logs.PayServer.LogDebug($"BitId signature check success for SIN {bitid.SIN}"); } } catch (FormatException) { } if (!(httpContext.User.Identity is BitIdentity)) { Logs.PayServer.LogDebug("BitId signature check failed"); } } try { await _Next(httpContext); } catch (UnauthorizedAccessException ex) { await HandleBitpayHttpException(httpContext, new BitpayHttpException(401, ex.Message)); } catch (BitpayHttpException ex) { await HandleBitpayHttpException(httpContext, ex); } catch (Exception ex) { Logs.PayServer.LogCritical(new EventId(), ex, "Unhandled exception in BTCPayMiddleware"); throw; } }
public async Task Invoke(HttpContext httpContext) { if (!_Registered) { var callback = await _CallbackController.RegisterCallbackBlockUriAsync(httpContext.Request); Logs.PayServer.LogInformation($"Registering block callback to " + callback); _Registered = true; } // Make sure that code executing after this point think that the external url has been hit. if (_Options.ExternalUrl != null) { httpContext.Request.Scheme = _Options.ExternalUrl.Scheme; if (_Options.ExternalUrl.IsDefaultPort) { httpContext.Request.Host = new HostString(_Options.ExternalUrl.Host); } else { httpContext.Request.Host = new HostString(_Options.ExternalUrl.Host, _Options.ExternalUrl.Port); } } httpContext.Request.Headers.TryGetValue("x-signature", out StringValues values); var sig = values.FirstOrDefault(); httpContext.Request.Headers.TryGetValue("x-identity", out values); var id = values.FirstOrDefault(); if (!string.IsNullOrEmpty(sig) && !string.IsNullOrEmpty(id)) { httpContext.Request.EnableRewind(); string body = string.Empty; if (httpContext.Request.ContentLength != 0 && httpContext.Request.Body != null) { using (StreamReader reader = new StreamReader(httpContext.Request.Body, Encoding.UTF8, true, 1024, true)) { body = reader.ReadToEnd(); } httpContext.Request.Body.Position = 0; } var url = httpContext.Request.GetEncodedUrl(); try { var key = new PubKey(id); if (BitIdExtensions.CheckBitIDSignature(key, sig, url, body)) { var bitid = new BitIdentity(key); httpContext.User = new GenericPrincipal(bitid, new string[0]); Logs.PayServer.LogDebug($"BitId signature check success for SIN {bitid.SIN}"); } } catch (FormatException) { } if (!(httpContext.User.Identity is BitIdentity)) { Logs.PayServer.LogDebug("BitId signature check failed"); } } try { await _Next(httpContext); } catch (UnauthorizedAccessException ex) { await HandleBitpayHttpException(httpContext, new BitpayHttpException(401, ex.Message)); } catch (BitpayHttpException ex) { await HandleBitpayHttpException(httpContext, ex); } catch (Exception ex) { Logs.PayServer.LogCritical(new EventId(), ex, "Unhandled exception in BTCPayMiddleware"); throw; } }