Fix: SignalR Authorize & Firefox Compatibility
Changes in architecture to support authorization and temporary workaround for NTLM in Firefox (to be removed in SignalR 2.x). Thanks to @davidfowl
This commit is contained in:
@@ -0,0 +1,16 @@
|
||||
using Disco.Models.Repository;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Disco.BI.Interop.SignalRHandlers
|
||||
{
|
||||
public class AdminAuthorizedPersistentConnection : AuthorizedPersistentConnection
|
||||
{
|
||||
private string[] authorizedUserTypes = { User.Types.Admin };
|
||||
|
||||
protected override string[] AuthorizedUserTypes { get { return authorizedUserTypes; } }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
using Microsoft.AspNet.SignalR;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Disco.BI.Interop.SignalRHandlers
|
||||
{
|
||||
public class AuthorizedPersistentConnection : PersistentConnection
|
||||
{
|
||||
private string[] authorizedUserTypes = null;
|
||||
|
||||
protected virtual string[] AuthorizedUserTypes { get { return authorizedUserTypes; } }
|
||||
|
||||
protected override bool AuthorizeRequest(IRequest request)
|
||||
{
|
||||
if (!request.User.Identity.IsAuthenticated)
|
||||
return false;
|
||||
else
|
||||
{
|
||||
var user = UserBI.UserCache.CurrentUser;
|
||||
if (user == null)
|
||||
return false;
|
||||
|
||||
if (AuthorizedUserTypes == null || AuthorizedUserTypes.Length == 0)
|
||||
return true;
|
||||
|
||||
if (AuthorizedUserTypes.Contains(user.Type))
|
||||
return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
using Disco.Services.Logging;
|
||||
using Disco.Services.Logging.Models;
|
||||
using Microsoft.AspNet.SignalR;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Disco.BI.Interop.SignalRHandlers
|
||||
{
|
||||
public class LogNotifications : AdminAuthorizedPersistentConnection
|
||||
{
|
||||
public static bool initialized = false;
|
||||
|
||||
public LogNotifications()
|
||||
{
|
||||
if (!initialized)
|
||||
{
|
||||
initialized = true;
|
||||
Disco.Services.Logging.Targets.LogLiveContext.LogBroadcast += Broadcast;
|
||||
}
|
||||
}
|
||||
|
||||
protected override System.Threading.Tasks.Task OnReceived(IRequest request, string connectionId, string data)
|
||||
{
|
||||
// Add to Group
|
||||
if (!string.IsNullOrWhiteSpace(data) && data.StartsWith("/addToGroups:") && data.Length > 13)
|
||||
{
|
||||
var groups = data.Substring(13).Split(',');
|
||||
foreach (var g in groups)
|
||||
{
|
||||
this.Groups.Add(connectionId, g);
|
||||
}
|
||||
}
|
||||
|
||||
return base.OnReceived(request, connectionId, data);
|
||||
}
|
||||
|
||||
internal static void Broadcast(LogBase logModule, LogEventType eventType, DateTime Timestamp, params object[] Arguments)
|
||||
{
|
||||
var message = LogLiveEvent.Create(logModule, eventType, Timestamp, Arguments);
|
||||
|
||||
var connectionManager = GlobalHost.ConnectionManager;
|
||||
var connectionContext = connectionManager.GetConnectionContext<LogNotifications>();
|
||||
connectionContext.Groups.Send(_GroupNameAll, message);
|
||||
connectionContext.Groups.Send(logModule.ModuleName, message);
|
||||
}
|
||||
|
||||
private const string _GroupNameAll = "__All";
|
||||
|
||||
public static string AllNotifications
|
||||
{
|
||||
get
|
||||
{
|
||||
return _GroupNameAll;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -8,7 +8,7 @@ using System.Threading.Tasks;
|
||||
|
||||
namespace Disco.BI.Interop.SignalRHandlers
|
||||
{
|
||||
public class RepositoryMonitorNotifications : PersistentConnection
|
||||
public class RepositoryMonitorNotifications : AdminAuthorizedPersistentConnection
|
||||
{
|
||||
public static void Initialize()
|
||||
{
|
||||
|
||||
@@ -0,0 +1,56 @@
|
||||
using Disco.Services.Tasks;
|
||||
using Microsoft.AspNet.SignalR;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Disco.BI.Interop.SignalRHandlers
|
||||
{
|
||||
public class ScheduledTasksStatusNotifications : AdminAuthorizedPersistentConnection
|
||||
{
|
||||
public static bool initialized = false;
|
||||
|
||||
public ScheduledTasksStatusNotifications()
|
||||
{
|
||||
if (!initialized)
|
||||
{
|
||||
initialized = true;
|
||||
Disco.Services.Tasks.ScheduledTaskStatus.UpdatedBroadcast += Broadcast;
|
||||
}
|
||||
}
|
||||
|
||||
protected override System.Threading.Tasks.Task OnReceived(IRequest request, string connectionId, string data)
|
||||
{
|
||||
// Add to Group
|
||||
if (!string.IsNullOrWhiteSpace(data) && data.StartsWith("/addToGroups:") && data.Length > 13)
|
||||
{
|
||||
var groups = data.Substring(13).Split(',');
|
||||
foreach (var g in groups)
|
||||
{
|
||||
this.Groups.Add(connectionId, g);
|
||||
}
|
||||
}
|
||||
return base.OnReceived(request, connectionId, data);
|
||||
}
|
||||
|
||||
internal static void Broadcast(ScheduledTaskStatusLive SessionStatus)
|
||||
{
|
||||
var connectionManager = GlobalHost.ConnectionManager;
|
||||
var connectionContext = connectionManager.GetConnectionContext<ScheduledTasksStatusNotifications>();
|
||||
connectionContext.Groups.Send(_GroupNameAll, SessionStatus);
|
||||
connectionContext.Groups.Send(SessionStatus.SessionId, SessionStatus);
|
||||
}
|
||||
|
||||
private const string _GroupNameAll = "__All";
|
||||
|
||||
public static string AllNotifications
|
||||
{
|
||||
get
|
||||
{
|
||||
return _GroupNameAll;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using System.Web;
|
||||
using System.Web.Routing;
|
||||
using System.Web.Security;
|
||||
using System.Web.SessionState;
|
||||
using Microsoft.AspNet.SignalR;
|
||||
using Owin;
|
||||
using AppFunc = System.Func<System.Collections.Generic.IDictionary<string, object>, System.Threading.Tasks.Task>;
|
||||
|
||||
namespace Disco.BI.Interop.SignalRHandlers
|
||||
{
|
||||
/// <summary>
|
||||
/// Required for SignalR 1.1.0 NTLM support in Firefox & Safari
|
||||
/// Returns 401 (Unauthorized) instead of 403 (Forbidden) when an unauthenticated request is processed
|
||||
///
|
||||
/// TODO: Remove this workaround when implementing SignalR 2.x
|
||||
///
|
||||
/// Thanks to David Fowler (@davidfowl)
|
||||
/// </summary>
|
||||
public static class SignalRAuthenticationWorkaround
|
||||
{
|
||||
public static void AddMiddleware(IAppBuilder app)
|
||||
{
|
||||
Func<AppFunc, AppFunc> convert403To401 = Convert403To401;
|
||||
|
||||
app.Use(convert403To401);
|
||||
}
|
||||
|
||||
private static AppFunc Convert403To401(AppFunc next)
|
||||
{
|
||||
return env =>
|
||||
{
|
||||
// Execute the SignalR pipeline
|
||||
Task task = next(env);
|
||||
|
||||
// Get the status code
|
||||
int statusCode = 0;
|
||||
if (env.ContainsKey("owin.ResponseStatusCode"))
|
||||
{
|
||||
statusCode = (int)env["owin.ResponseStatusCode"];
|
||||
}
|
||||
|
||||
// If its 403 then convert it to 401 (we shouldn't do
|
||||
// this if it's a cross domain request since it doesn't make sense)
|
||||
if (statusCode == 403)
|
||||
{
|
||||
env["owin.ResponseStatusCode"] = 401;
|
||||
}
|
||||
|
||||
// Return the original task
|
||||
return task;
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
+3
-3
@@ -9,12 +9,12 @@ using Disco.Models.Repository;
|
||||
|
||||
namespace Disco.BI.Interop.SignalRHandlers
|
||||
{
|
||||
public class UserHeldDevices : PersistentConnection
|
||||
public class UserHeldDeviceNotifications : PersistentConnection
|
||||
{
|
||||
private static bool subscribed = false;
|
||||
private static object subscribeLock = new object();
|
||||
|
||||
static UserHeldDevices()
|
||||
static UserHeldDeviceNotifications()
|
||||
{
|
||||
if (!subscribed)
|
||||
lock (subscribeLock)
|
||||
@@ -32,7 +32,7 @@ namespace Disco.BI.Interop.SignalRHandlers
|
||||
if (j.UserId != null)
|
||||
{
|
||||
var connectionManager = GlobalHost.ConnectionManager;
|
||||
var connectionContext = connectionManager.GetConnectionContext<UserHeldDevices>();
|
||||
var connectionContext = connectionManager.GetConnectionContext<UserHeldDeviceNotifications>();
|
||||
if (connectionContext != null)
|
||||
connectionContext.Connection.Broadcast(j.UserId);
|
||||
}
|
||||
Reference in New Issue
Block a user