internal static async Task CallServer(this SignalX signalX, SignalXRequest request, string correlationId) { signalX.Advanced.Trace(correlationId, $"Running call to server {request?.Handler}..."); ServerHandlerDetails executionDetails = signalX.SignalXServerExecutionDetails[request.Handler]; if (executionDetails.IsSingleWriter) { using (executionDetails.SingleWriter.Write()) { await signalX.SignalXServers[request.Handler].Invoke(request, signalX.SignalXServerExecutionDetails[request.Handler].State).ConfigureAwait(false); } } else { await signalX.SignalXServers[request.Handler].Invoke(request, signalX.SignalXServerExecutionDetails[request.Handler].State).ConfigureAwait(false); } }
internal static async Task <bool> IsAuthenticated(this SignalX signalX, string correlationId, IRequest request, SignalXRequest sRequest) { bool result; try { //var cookie = request?.Cookies[AuthenticationCookieName]; //var ip = request?.Environment["server.RemoteIpAddress"]?.ToString(); result = await signalX.Settings.AuthenticatedWhen(sRequest).ConfigureAwait(false); if (!result) { signalX.Settings.ConnectionEventsHandler.ForEach(h => h?.Invoke(ConnectionEvents.SignalXRequestAuthorizationFailed.ToString(), "Authorization failed after checking with Custom Authorization provided")); } } catch (Exception e) { result = false; signalX.Settings.ConnectionEventsHandler.ForEach(h => h?.Invoke(ConnectionEvents.SignalXRequestAuthorizationFailed.ToString(), "Custom Authorization threw an exception " + e.Message)); signalX.Settings.ExceptionHandler.ForEach(h => h?.Invoke("Authentication failed", e)); } return(result); }
internal static async Task <bool> CanProcess(this SignalX signalX, string correlationId, HubCallerContext context, string serverHandlerName, SignalXRequest request, bool isScriptRequest) { signalX.Advanced.Trace(correlationId, $"Checking if request can be processed for {serverHandlerName}..."); bool result = false; if (isScriptRequest) { signalX.Advanced.Trace(correlationId, $"Its a script request so no further checks are necessary for {serverHandlerName}..."); return(true); } if (signalX.SignalXServerExecutionDetails.ContainsKey(serverHandlerName)) { List <string> allowedGroups = signalX.SignalXServerExecutionDetails[serverHandlerName].AllowedGroups; signalX.Advanced.Trace(correlationId, $"Checking if request is coming from a client with group allowed to access server {serverHandlerName}..."); foreach (string allowedGroup in allowedGroups) { if (!request.Groups.Contains(allowedGroup)) { signalX.Settings.ConnectionEventsHandler.ForEach(h => h?.Invoke(ConnectionEvents.SignalXRequestAuthorizationFailed.ToString(), $"Authorization failed : The request does not contain group {allowedGroup} and is therefore denied access to {serverHandlerName}")); return(false); } } } if (signalX.Settings.RequireAuthorizationForAllHandlers || signalX.SignalXServerExecutionDetails.ContainsKey(serverHandlerName) && signalX.SignalXServerExecutionDetails[serverHandlerName].RequiresAuthorization) { signalX.Advanced.Trace(correlationId, $"Checking if request is authorized to access the server {serverHandlerName} because authorization function has been set ..."); if (signalX.Settings.AuthenticatedWhen != null) { result = await signalX.IsAuthenticated(correlationId, context.Request, request).ConfigureAwait(false); } else { result = context.User.Identity.IsAuthenticated; if (!result) { signalX.Settings.ConnectionEventsHandler.ForEach(h => h?.Invoke(ConnectionEvents.SignalXRequestAuthorizationFailed.ToString(), "Authorization failed after checking with context.User.Identity.IsAuthenticated. Custom Authorization check is not yet setup ")); } } } else { signalX.Settings.WarningHandler.ForEach(h => h?.Invoke("AuthorizationNotSet", "Try setting an authorization handler to prevent anonymous access into your server")); result = true; } signalX.Advanced.Trace(correlationId, $"request allowed to access the server {serverHandlerName} : {request}"); return(result); }