public void Register(IAppHost appHost) { hasRegistered = true; AuthenticateService.Init(sessionFactory, AuthProviders); var unitTest = appHost == null; if (unitTest) { return; } if (HostContext.StrictMode) { var sessionInstance = sessionFactory(); if (TypeSerializer.HasCircularReferences(sessionInstance)) { throw new StrictModeException($"User Session {sessionInstance.GetType().Name} cannot have circular dependencies", "sessionFactory", StrictModeCodes.CyclicalUserSession); } } foreach (var registerService in ServiceRoutes) { appHost.RegisterService(registerService.Key, registerService.Value); } var sessionFeature = RegisterPlugins.OfType <SessionFeature>().First(); sessionFeature.SessionExpiry = SessionExpiry; sessionFeature.PermanentSessionExpiry = PermanentSessionExpiry; appHost.LoadPlugin(RegisterPlugins.ToArray()); if (IncludeAuthMetadataProvider && appHost.TryResolve <IAuthMetadataProvider>() == null) { appHost.Register <IAuthMetadataProvider>(new AuthMetadataProvider()); } AuthProviders.OfType <IAuthPlugin>().Each(x => x.Register(appHost, this)); AuthenticateService.HtmlRedirect = HtmlRedirect; AuthenticateService.HtmlRedirectAccessDenied = HtmlRedirectAccessDenied; AuthenticateService.HtmlRedirectReturnParam = HtmlRedirectReturnParam; AuthenticateService.HtmlRedirectReturnPathOnly = HtmlRedirectReturnPathOnly; AuthenticateService.AuthResponseDecorator = AuthResponseDecorator; if (ValidateFn != null) { AuthenticateService.ValidateFn = ValidateFn; } var authNavItems = AuthProviders.Select(x => (x as AuthProvider)?.NavItem).Where(x => x != null); if (!ViewUtils.NavItemsMap.TryGetValue("auth", out var navItems)) { ViewUtils.NavItemsMap["auth"] = navItems = new List <NavItem>(); } navItems.AddRange(authNavItems); }
public static Dictionary <string, object> ToSafePartialObjectDictionary <T>(this T instance) { var to = new Dictionary <string, object>(); var propValues = instance.ToObjectDictionary(); if (propValues != null) { foreach (var entry in propValues) { var valueType = entry.Value?.GetType(); if (valueType == null || !valueType.IsClass || valueType == typeof(string)) { to[entry.Key] = entry.Value; } else if (!TypeSerializer.HasCircularReferences(entry.Value)) { if (entry.Value is IEnumerable enumerable) { to[entry.Key] = entry.Value; } else { to[entry.Key] = entry.Value.ToSafePartialObjectDictionary(); } } else { to[entry.Key] = entry.Value.ToString(); } } } return(to); }
public static object AssertNoCircularDeps(this object value) { if (value != null && TypeSerializer.HasCircularReferences(value)) { throw new NotSupportedException($"Cannot serialize type '{value.GetType().Name}' with cyclical dependencies"); } return(value); }
public void Register(IAppHost appHost) { SessionFeature.DefaultSessionFactory = sessionFactory; AuthenticateService.Init(authProviders); var unitTest = appHost == null; if (unitTest) { return; } if (appHost.Config.StrictMode.GetValueOrDefault()) { var sessionInstance = sessionFactory(); if (TypeSerializer.HasCircularReferences(sessionInstance)) { throw new StrictModeException($"User Session {sessionInstance.GetType().Name} cannot have circular dependencies", "sessionFactory", StrictModeCodes.CyclicalUserSession); } } foreach (var registerService in ServiceRoutes) { appHost.RegisterService(registerService.Key, registerService.Value); } var sessionFeature = RegisterPlugins.OfType <SessionFeature>().First(); sessionFeature.SessionExpiry = SessionExpiry; sessionFeature.PermanentSessionExpiry = PermanentSessionExpiry; appHost.LoadPlugin(RegisterPlugins.ToArray()); if (IncludeAuthMetadataProvider && appHost.TryResolve <IAuthMetadataProvider>() == null) { appHost.Register <IAuthMetadataProvider>(new AuthMetadataProvider()); } authProviders.OfType <IAuthPlugin>().Each(x => x.Register(appHost, this)); AuthenticateService.HtmlRedirect = HtmlRedirect; AuthenticateService.AuthResponseDecorator = AuthResponseDecorator; appHost.GetPlugin <MetadataFeature>() ?.AddLink(MetadataFeature.AvailableFeatures, "http://docs.servicestack.net/authentication-and-authorization", nameof(AuthFeature)); }
public void Register(IAppHost appHost) { hasRegistered = true; AuthenticateService.Init(sessionFactory, authProviders); var unitTest = appHost == null; if (unitTest) { return; } if (HostContext.StrictMode) { var sessionInstance = sessionFactory(); if (TypeSerializer.HasCircularReferences(sessionInstance)) { throw new StrictModeException($"User Session {sessionInstance.GetType().Name} cannot have circular dependencies", "sessionFactory", StrictModeCodes.CyclicalUserSession); } } foreach (var registerService in ServiceRoutes) { appHost.RegisterService(registerService.Key, registerService.Value); } var sessionFeature = RegisterPlugins.OfType <SessionFeature>().First(); sessionFeature.SessionExpiry = SessionExpiry; sessionFeature.PermanentSessionExpiry = PermanentSessionExpiry; appHost.LoadPlugin(RegisterPlugins.ToArray()); if (IncludeAuthMetadataProvider && appHost.TryResolve <IAuthMetadataProvider>() == null) { appHost.Register <IAuthMetadataProvider>(new AuthMetadataProvider()); } authProviders.OfType <IAuthPlugin>().Each(x => x.Register(appHost, this)); AuthenticateService.HtmlRedirect = HtmlRedirect; AuthenticateService.HtmlRedirectReturnParam = HtmlRedirectReturnParam; AuthenticateService.HtmlRedirectReturnPathOnly = HtmlRedirectReturnPathOnly; AuthenticateService.AuthResponseDecorator = AuthResponseDecorator; }
public void Can_detect_Circular_References_in_models() { var node = new Node(1, new Node(11, new Node(111)), new Node(12, new Node(121))); Assert.That(!TypeSerializer.HasCircularReferences(node)); var root = new Node(1, new Node(11)); var cyclicalNode = new Node(1, root); root.Children[0].Children = new[] { cyclicalNode }; Assert.That(TypeSerializer.HasCircularReferences(root)); }
public static Dictionary <string, object> ToSafePartialObjectDictionary <T>(this T instance) { var to = new Dictionary <string, object>(); var propValues = instance.ToObjectDictionary(); if (propValues != null) { foreach (var entry in propValues) { var valueType = entry.Value?.GetType(); try { if (valueType == null || !valueType.IsClass || valueType == typeof(string)) { to[entry.Key] = entry.Value; } else if (!TypeSerializer.HasCircularReferences(entry.Value)) { if (entry.Value is IEnumerable enumerable) { to[entry.Key] = entry.Value; } else { to[entry.Key] = entry.Value.ToSafePartialObjectDictionary(); } } else { to[entry.Key] = entry.Value.ToString(); } } catch (Exception ignore) { Tracer.Instance.WriteDebug($"Could not retrieve value from '{valueType?.GetType().Name}': ${ignore.Message}"); } } } return(to); }
public void Can_detect_Circular_References_in_OrmLite_scripts() { var dbFactory = new OrmLiteConnectionFactory(":memory:", SqliteDialect.Provider); using (var db = dbFactory.OpenDbConnection()) { db.DropAndCreateTable <User>(); db.Insert(new User { Id = 1, Name = "A", CreatedDate = DateTime.Now }); db.Insert(new User { Id = 2, Name = "B", CreatedDate = DateTime.Now }); db.Insert(new User { Id = 3, Name = "B", CreatedDate = DateTime.Now }); var rowsB = db.Select <User>("Name = @name", new { name = "B" }); var rowIds = rowsB.ConvertAll(x => x.Id); Assert.That(TypeSerializer.HasCircularReferences(SqliteDialect.Provider)); Assert.That(TypeSerializer.HasCircularReferences(dbFactory)); Assert.That(TypeSerializer.HasCircularReferences(db)); Assert.That(!TypeSerializer.HasCircularReferences(rowsB)); Assert.That(!TypeSerializer.HasCircularReferences(rowsB[0])); Assert.That(!TypeSerializer.HasCircularReferences(rowIds)); SqliteDialect.Provider.ToSafeJson().Print(); dbFactory.ToSafeJson().Print(); db.ToSafeJson().Print(); rowsB.ToSafeJson().Print(); rowsB[0].ToSafeJson().Print(); rowIds.ToSafeJson().Print(); } }
public static string ToSafeJson <T>(this T obj) { return(TypeSerializer.HasCircularReferences(obj) ? obj.ToSafePartialObjectDictionary().ToJson() : obj.ToJson()); }
public void Register(IAppHost appHost) { hasRegistered = true; AuthenticateService.Init(sessionFactory, AuthProviders); var unitTest = appHost == null; if (unitTest) { return; } if (HostContext.StrictMode) { var sessionInstance = sessionFactory(); if (TypeSerializer.HasCircularReferences(sessionInstance)) { throw new StrictModeException($"User Session {sessionInstance.GetType().Name} cannot have circular dependencies", "sessionFactory", StrictModeCodes.CyclicalUserSession); } } appHost.RegisterServices(ServiceRoutes); var sessionFeature = RegisterPlugins.OfType <SessionFeature>().First(); sessionFeature.SessionExpiry = SessionExpiry; sessionFeature.PermanentSessionExpiry = PermanentSessionExpiry; appHost.LoadPlugin(RegisterPlugins.ToArray()); if (IncludeAuthMetadataProvider && appHost.TryResolve <IAuthMetadataProvider>() == null) { appHost.Register <IAuthMetadataProvider>(new AuthMetadataProvider()); } AuthProviders.OfType <IAuthPlugin>().Each(x => x.Register(appHost, this)); AuthenticateService.HtmlRedirect = HtmlRedirect; AuthenticateService.HtmlRedirectAccessDenied = HtmlRedirectAccessDenied; AuthenticateService.HtmlRedirectReturnParam = HtmlRedirectReturnParam; AuthenticateService.HtmlRedirectReturnPathOnly = HtmlRedirectReturnPathOnly; AuthenticateService.AuthResponseDecorator = AuthResponseDecorator; if (ValidateFn != null) { AuthenticateService.ValidateFn = ValidateFn; } var authNavItems = AuthProviders.Select(x => (x as AuthProvider)?.NavItem).Where(x => x != null); if (!ViewUtils.NavItemsMap.TryGetValue("auth", out var navItems)) { ViewUtils.NavItemsMap["auth"] = navItems = new List <NavItem>(); } var isDefaultHtmlRedirect = HtmlRedirect == "~/" + LocalizedStrings.Login.Localize(); if (IncludeDefaultLogin && isDefaultHtmlRedirect && !appHost.VirtualFileSources.FileExists("/login.html")) { appHost.VirtualFileSources.GetMemoryVirtualFiles().WriteFile("/login.html", Templates.HtmlTemplates.GetLoginTemplate()); } navItems.AddRange(authNavItems); appHost.AddToAppMetadata(meta => { meta.Plugins.Auth = new AuthInfo { HasAuthSecret = (appHost.Config.AdminAuthSecret != null).NullIfFalse(), HasAuthRepository = appHost.GetContainer().Exists <IAuthRepository>().NullIfFalse(), IncludesRoles = IncludeRolesInAuthenticateResponse.NullIfFalse(), IncludesOAuthTokens = IncludeOAuthTokensInAuthenticateResponse.NullIfFalse(), HtmlRedirect = HtmlRedirect?.TrimStart('~'), AuthProviders = AuthenticateService.GetAuthProviders().Map(x => new MetaAuthProvider { Type = x.Type, Name = x.Provider, NavItem = (x as AuthProvider)?.NavItem, Meta = x.Meta, }) }; }); }
public void Does_recognize_Cyclical_Deps() { Assert.That(TypeSerializer.HasCircularReferences(new BadUserSession())); }
public void Does_detect_circular_references_of_CircularDictionary() { Assert.That(TypeSerializer.HasCircularReferences(CreateCircularDictionary())); }
public void Does_not_report_CircularReferences_of_Built_In_Types() { Assert.That(TypeSerializer.HasCircularReferences(new DateTime()), Is.False); Assert.That(TypeSerializer.HasCircularReferences(new TimeSpan()), Is.False); Assert.That(TypeSerializer.HasCircularReferences(Guid.NewGuid()), Is.False); }