Пример #1
0
        public async Task Test_add_user_modify_lockout_end_date_delete()
        {
            var dao = Global.TenantDao;
            await dao.EstablishConnectionAsync();

            var guidId    = Guid.NewGuid();
            var userStore = new CassandraUserStore();

            string userName = Guid.NewGuid().ToString();
            var    user     = new CassandraUser()
            {
                Email                = userName,
                UserName             = userName,
                EmailConfirmed       = false,
                PhoneNumberConfirmed = false,
                PhoneNumber          = "310.383.1111",
                LockoutEnabled       = false,
                PasswordHash         = "1234",
                SecurityStamp        = "1234",
                LockoutEndDate       = DateTimeOffset.UtcNow
            };
            await userStore.CreateAsync(user);

            var foundUser = await userStore.FindByEmailAsync(userName);

            Assert.IsNotNull(foundUser);
            var lockOutDate = await userStore.GetLockoutEndDateAsync(foundUser);

            Assert.AreEqual(lockOutDate.Day, user.LockoutEndDate.Day);
            Assert.AreEqual(lockOutDate.Hour, user.LockoutEndDate.Hour);
            Assert.AreEqual(lockOutDate.Minute, user.LockoutEndDate.Minute);
            Assert.AreEqual(lockOutDate.Year, user.LockoutEndDate.Year);

            var future = user.LockoutEndDate.AddDays(5);
            await userStore.SetLockoutEndDateAsync(foundUser, future);

            await userStore.UpdateAsync(foundUser);

            foundUser = await userStore.FindByEmailAsync(userName);

            Assert.IsNotNull(foundUser);
            lockOutDate = await userStore.GetLockoutEndDateAsync(foundUser);

            Assert.AreEqual(lockOutDate.Day, future.Day);
            Assert.AreEqual(lockOutDate.Hour, future.Hour);
            Assert.AreEqual(lockOutDate.Minute, future.Minute);
            Assert.AreEqual(lockOutDate.Year, future.Year);

            await userStore.DeleteAsync(foundUser);

            foundUser = await userStore.FindByEmailAsync(userName);

            Assert.IsNull(foundUser);
        }
Пример #2
0
        public async Task Test_add_user_modify_twofactor_enabled_delete()
        {
            var dao = Global.TenantDao;
            await dao.EstablishConnectionAsync();

            var guidId    = Guid.NewGuid();
            var userStore = new CassandraUserStore();

            string userName = Guid.NewGuid().ToString();
            var    user     = new CassandraUser()
            {
                Email                = userName,
                UserName             = userName,
                EmailConfirmed       = false,
                PhoneNumberConfirmed = false,
                PhoneNumber          = "310.383.1111",
                LockoutEnabled       = false,
                PasswordHash         = "1234",
                SecurityStamp        = "1234",
                TwoFactorEnabled     = false
            };
            await userStore.CreateAsync(user);

            var foundUser = await userStore.FindByEmailAsync(userName);

            Assert.IsNotNull(foundUser);
            Assert.IsFalse(await userStore.GetTwoFactorEnabledAsync(foundUser));

            await userStore.SetTwoFactorEnabledAsync(foundUser, true);

            await userStore.UpdateAsync(foundUser);

            foundUser = await userStore.FindByEmailAsync(userName);

            Assert.IsNotNull(foundUser);
            Assert.IsTrue(await userStore.GetTwoFactorEnabledAsync(foundUser));

            await userStore.DeleteAsync(foundUser);

            foundUser = await userStore.FindByEmailAsync(userName);

            Assert.IsNull(foundUser);
        }
        public virtual void TestSetup()
        {
            var cluster = Cluster.Builder()
                .AddContactPoint("127.0.0.1")
                .Build();
            _session = cluster.Connect();

            // Use a unique keyspace for each test fixture named after the test fixture's class name
            _keyspaceName = string.Format(TestKeyspaceFormat, GetType().Name.Replace(".", string.Empty));

            // Drop and re-create the keyspace
            _session.DeleteKeyspaceIfExists(_keyspaceName);
            _session.CreateKeyspaceIfNotExists(_keyspaceName);
            _session.ChangeKeyspace(_keyspaceName);

            _userStore = new CassandraUserStore(_session);

            // Exercise the UserManager class in tests since that's how consumers will use CassandarUserStore
            UserManager = new UserManager<CassandraUser, Guid>(_userStore);
        }
Пример #4
0
        public async Task Test_add_user_modify_password_hash_delete()
        {
            var dao = Global.TenantDao;
            await dao.EstablishConnectionAsync();

            var guidId    = Guid.NewGuid();
            var userStore = new CassandraUserStore();

            string userName = Guid.NewGuid().ToString();
            var    user     = new CassandraUser()
            {
                Email                = userName,
                UserName             = userName,
                EmailConfirmed       = false,
                PhoneNumberConfirmed = false,
                PhoneNumber          = "310.383.1111",
                LockoutEnabled       = false,
                PasswordHash         = "1234"
            };
            await userStore.CreateAsync(user);

            var foundUser = await userStore.FindByEmailAsync(userName);

            Assert.IsNotNull(foundUser);
            Assert.AreEqual(await userStore.GetPasswordHashAsync(foundUser), "1234");

            await userStore.SetPasswordHashAsync(foundUser, "abcd");

            await userStore.UpdateAsync(foundUser);

            foundUser = await userStore.FindByEmailAsync(userName);

            Assert.IsNotNull(foundUser);
            Assert.AreEqual(await userStore.GetPasswordHashAsync(foundUser), "abcd");

            await userStore.DeleteAsync(foundUser);

            foundUser = await userStore.FindByEmailAsync(userName);

            Assert.IsNull(foundUser);
        }
Пример #5
0
        public async Task Test_add_user_modify_phone_confirmed_delete()
        {
            var dao = Global.TenantDao;
            await dao.EstablishConnectionAsync();

            var guidId    = Guid.NewGuid();
            var userStore = new CassandraUserStore();

            string userName = Guid.NewGuid().ToString();
            var    user     = new CassandraUser()
            {
                Email                = userName,
                UserName             = userName,
                EmailConfirmed       = false,
                PhoneNumberConfirmed = false
            };
            await userStore.CreateAsync(user);

            var foundUser = await userStore.FindByEmailAsync(userName);

            Assert.IsNotNull(foundUser);
            Assert.IsFalse(await userStore.GetPhoneNumberConfirmedAsync(foundUser));

            await userStore.SetPhoneNumberConfirmedAsync(foundUser, true);

            await userStore.UpdateAsync(foundUser);

            foundUser = await userStore.FindByEmailAsync(userName);

            Assert.IsNotNull(foundUser);
            Assert.IsTrue(await userStore.GetPhoneNumberConfirmedAsync(foundUser));

            await userStore.DeleteAsync(foundUser);

            foundUser = await userStore.FindByEmailAsync(userName);

            Assert.IsNull(foundUser);
        }
 public AspNetIdentityServerService(CassandraUserStore userStore)
 {
     _userStore = userStore;
 }
Пример #7
0
        public void Configuration(IAppBuilder app)
        {
            string _domain_root = ConfigurationManager.AppSettings["IdentityServer:Domain"];

            IdentityServerSettings.DomainRoot = _domain_root;

            Log.Logger = new LoggerConfiguration()
                         .MinimumLevel.Debug()
                         .WriteTo.Trace()
                         .CreateLogger();

            var builder = new ContainerBuilder();

            ConfigureAuth(app);

            var path = HostingEnvironment.MapPath("~/App_Data");
            var cassandraUserStore = new CassandraUserStore(CassandraAspNetIdentityOptions.CassandraConfig,
                                                            CassandraAspNetApplicationConstants.TenantGuid);
            var userService = new Registration <IUserService>(new AspNetIdentityServerService(cassandraUserStore));

            userService = new Registration <IUserService>(new ArbitraryUserService());
            IdentityServerServiceFactory identityServerServiceFactory;



#if CASSANDRA_STORE
            app.UseAspNetCassandraStores(new CassandraOptions()
            {
                ContactPoints = new List <string> {
                    "cassandra"
                },
                Credentials = new CassandraCredentials()
                {
                    Password = "", UserName = ""
                },
                KeySpace = "aspnetidentity"
            });

            app.UseIdentityServerCassandraStores(userService, out identityServerServiceFactory,
                                                 new CassandraOptions()
            {
                ContactPoints = new List <string> {
                    "cassandra"
                },
                Credentials = new CassandraCredentials()
                {
                    Password = "", UserName = ""
                },
                KeySpace = "identityserver3"
            });

            /*
             * TODO: Cassandra may be down, need a robust app that can reconnect
             * foreach (var client in Clients.Get())
             * {
             *  IdentityServer3CassandraDao.UpsertClientAsync(
             *      new FlattenedClientRecord(new FlattenedClientHandle(client)));
             * }
             * foreach (var scope in Scopes.Get())
             * {
             *  IdentityServer3CassandraDao.UpsertScopeAsync(new FlattenedScopeRecord(new FlattenedScopeHandle(scope)));
             * }
             * */
#endif
#if BIGGY_STORE
            // Create and modify default settings
            StoreSettings settings = StoreSettings.DefaultSettings;
            settings.Folder = path;

            ClientStore myClientStore = new ClientStore(settings);
            foreach (var client in Clients.Get())
            {
                myClientStore.CreateAsync(new ClientHandle(client));
            }
            ScopeStore myScopeStore = new ScopeStore(settings);
            foreach (var scope in Scopes.Get())
            {
                myScopeStore.CreateAsync(new ScopeHandle(scope));
            }

            // Create the BiggyIdentityService factory
            identityServerServiceFactory = new ServiceFactory(userService, settings);
#endif
            identityServerServiceFactory.Register(new Registration <IDictionary <string, IClaimsProvider> >(resolver =>
            {
                var result = new Dictionary <string, IClaimsProvider>
                {
                    {
                        CustomClaimsProviderHub.WellKnown_DefaultProviderName,
                        new DefaultClaimsProvider(resolver.Resolve <IUserService>())
                    },
                    {
                        "openid-provider", new CustomOpenIdClaimsProvider(resolver.Resolve <IUserService>())
                    },
                    {
                        "arbitrary-provider", new ArbitraryClaimsProvider(resolver.Resolve <IUserService>())
                    }
                };
                return(result);
            }));

            identityServerServiceFactory.ClaimsProvider = new Registration <IClaimsProvider>(typeof(CustomClaimsProviderHub));

            var options = new IdentityServerOptions
            {
                Factory            = identityServerServiceFactory,
                RequireSsl         = false,
                SigningCertificate = Certificate.Get(),
                SiteName           = "P5 IdentityServer3"
            };


            app.Map("/idsrv3core", idsrvApp =>
            {
                idsrvApp.UseIdentityServer(options);
            });
            app.UseIdentityServerBearerTokenAuthentication(new IdentityServerBearerTokenAuthenticationOptions
            {
                Authority      = "http://localhost:55970/idsrv3core",
                ValidationMode = ValidationMode.ValidationEndpoint,

                RequiredScopes              = new[] { "api1" },
                PreserveAccessToken         = true,
                EnableValidationResultCache = true
            });

            HttpConfiguration config = new HttpConfiguration();

            /*          config.Routes.MapHttpRoute(
             *            name: "DefaultApi",
             *            routeTemplate: "api/{controller}/{id}",
             *            defaults: new {id = RouteParameter.Optional}
             *            );
             */
            // Register your Web API controllers.
            builder.RegisterApiControllers(Assembly.GetExecutingAssembly());
            builder.RegisterType <ApplicationUserManager>();

            // Run other optional steps, like registering filters,
            // per-controller-type services, etc., then set the dependency resolver
            // to be Autofac.
            var container = builder.Build();
            config.DependencyResolver = new AutofacWebApiDependencyResolver(container);

            config.Services.Replace(typeof(IAssembliesResolver), new WebApi2HubAssembliesResolver());
            WebApiConfig.Register(config); // NEW way

            // OWIN WEB API SETUP:

            // Register the Autofac middleware FIRST, then the Autofac Web API middleware,
            // and finally the standard Web API middleware.
            app.UseAutofacMiddleware(container);
            app.UseAutofacWebApi(config);
            app.UseWebApi(config);
        }