Пример #1
0
        public static List<UserAccount> Get(ICrypto crypto, ApplicationOptions options)
        {
            var now = DateTime.UtcNow;

            var users = new List<UserAccount>
            {
                new UserAccount
                {
                    Id = Guid.Parse("0c2954d2-4c73-44e3-b0f2-c00403e4adef"),
                    Email = "alice@localhost",
                    PasswordHash  = crypto.HashPassword("alice@localhost", options.PasswordHashingIterationCount),
                    CreatedAt = now,
                    UpdatedAt = now,
                    IsEmailVerified = true,
                    IsLoginAllowed = true,
                    Claims = new List<UserAccountClaim>
                    {
                        new UserAccountClaim(JwtClaimTypes.Name, "Alice Smith") { Id = Guid.Parse("1338ad75-8453-4024-b5ff-657b1bc79a4c"), UserAccountId = Guid.Parse("0c2954d2-4c73-44e3-b0f2-c00403e4adef") },
                        new UserAccountClaim(JwtClaimTypes.GivenName, "Alice") { Id = Guid.Parse("a4342819-61c2-4ae0-8ce2-649a8ad89bd5"), UserAccountId = Guid.Parse("0c2954d2-4c73-44e3-b0f2-c00403e4adef") },
                        new UserAccountClaim(JwtClaimTypes.FamilyName, "Smith") { Id = Guid.Parse("a31f501c-6b77-466d-b1a0-3f92daef5e60"), UserAccountId = Guid.Parse("0c2954d2-4c73-44e3-b0f2-c00403e4adef") },
                        new UserAccountClaim(JwtClaimTypes.Email, "alice@localhost") { Id = Guid.Parse("a31f501c-6b77-466d-b1a0-3f92daef5e60"), UserAccountId = Guid.Parse("0c2954d2-4c73-44e3-b0f2-c00403e4adef") },
                        new UserAccountClaim(JwtClaimTypes.EmailVerified, "true", ClaimValueTypes.Boolean) { Id = Guid.Parse("ea7c1cd4-0346-44ce-8675-39b39d5c663d"), UserAccountId = Guid.Parse("0c2954d2-4c73-44e3-b0f2-c00403e4adef") },
                        new UserAccountClaim(JwtClaimTypes.Role, "Admin") { Id = Guid.Parse("a916297c-d8fb-455f-8da9-1ec00af90b71"), UserAccountId = Guid.Parse("0c2954d2-4c73-44e3-b0f2-c00403e4adef") },
                        new UserAccountClaim(JwtClaimTypes.Role, "Geek") { Id = Guid.Parse("0b6daa16-c51b-4510-b69b-3d402716a82a"), UserAccountId = Guid.Parse("0c2954d2-4c73-44e3-b0f2-c00403e4adef") },
                        new UserAccountClaim(JwtClaimTypes.WebSite, "http://alice.com") { Id = Guid.Parse("bb3d1029-973f-463c-92a7-4dab1a063453"), UserAccountId = Guid.Parse("0c2954d2-4c73-44e3-b0f2-c00403e4adef") },
                        new UserAccountClaim(JwtClaimTypes.Address, @"{ 'street_address': 'One Hacker Way', 'locality': 'Heidelberg', 'postal_code': 69118, 'country': 'Germany' }", IdentityServerConstants.ClaimValueTypes.Json) { Id = Guid.Parse("3b28ab5d-2976-4802-a2e5-b468d323e5c4"), UserAccountId = Guid.Parse("0c2954d2-4c73-44e3-b0f2-c00403e4adef") }
                    }
                },
                new UserAccount
                {
                    Id = Guid.Parse("28575826-68a0-4a1d-9428-674a2eb5db95"),
                    Email = "bob@localhost",
                    PasswordHash  = crypto.HashPassword("bob@localhost", options.PasswordHashingIterationCount),
                    CreatedAt = now,
                    UpdatedAt = now,
                    IsEmailVerified = true,
                    IsLoginAllowed = true,
                    Claims = new List<UserAccountClaim>
                    {
                        new UserAccountClaim(JwtClaimTypes.Name, "Bob Smith") { Id = Guid.Parse("58389f05-ed6c-431b-8318-a530e55d5450"), UserAccountId = Guid.Parse("28575826-68a0-4a1d-9428-674a2eb5db95") },
                        new UserAccountClaim(JwtClaimTypes.GivenName, "Bob") { Id = Guid.Parse("e7b6a0f6-5437-48ef-b868-a949991fadfe"), UserAccountId = Guid.Parse("28575826-68a0-4a1d-9428-674a2eb5db95") },
                        new UserAccountClaim(JwtClaimTypes.FamilyName, "Smith") { Id = Guid.Parse("6398ac60-67ce-48e4-9803-de9c244dcd7b"), UserAccountId = Guid.Parse("28575826-68a0-4a1d-9428-674a2eb5db95") },
                        new UserAccountClaim(JwtClaimTypes.Email, "bob@localhost") { Id = Guid.Parse("2ce9ee83-8fb6-4b41-bef1-0551e010fde6"), UserAccountId = Guid.Parse("28575826-68a0-4a1d-9428-674a2eb5db95") },
                        new UserAccountClaim(JwtClaimTypes.EmailVerified, "true", ClaimValueTypes.Boolean) { Id = Guid.Parse("26c8d08b-ff04-43d0-b80e-4252f04241e3"), UserAccountId = Guid.Parse("28575826-68a0-4a1d-9428-674a2eb5db95") },
                        new UserAccountClaim(JwtClaimTypes.Role, "Developer") { Id = Guid.Parse("cd6f4039-8fee-43fe-8058-48ffa680d5de"), UserAccountId = Guid.Parse("28575826-68a0-4a1d-9428-674a2eb5db95") },
                        new UserAccountClaim(JwtClaimTypes.Role, "Geek") { Id = Guid.Parse("298d7a32-cc8f-4ec0-837f-aeed1ae43e74"), UserAccountId = Guid.Parse("28575826-68a0-4a1d-9428-674a2eb5db95") },
                        new UserAccountClaim(JwtClaimTypes.WebSite, "http://bob.com") { Id = Guid.Parse("41e399c7-dec0-4696-be2b-0067f6b1c6a1"), UserAccountId = Guid.Parse("28575826-68a0-4a1d-9428-674a2eb5db95") },
                        new UserAccountClaim(JwtClaimTypes.Address, @"{ 'street_address': 'One Hacker Way', 'locality': 'Heidelberg', 'postal_code': 69118, 'country': 'Germany' }", IdentityServerConstants.ClaimValueTypes.Json) { Id = Guid.Parse("79d46959-3a46-4b26-8bf9-808c50651c90"), UserAccountId = Guid.Parse("28575826-68a0-4a1d-9428-674a2eb5db95") }
                    }
                }
            };

            return users;
        }
Пример #2
0
        public SignUpInResult SignUp(SignUpRequest request)
        {
            return(BaseInvokeCheckModel(request, () =>
            {
                var responseBuilder =
                    ResponseBuilder <SignUpInResult>
                    .Fail();

                if (_dbContext.Users.Any(x => x.Email == request.Email))
                {
                    return responseBuilder.SetInfoAndBuild("User with this email already registered");
                }

                var salt = _crypto.GenerateSalt();
                var passwordHah = _crypto.HashPassword(salt, request.Password);

                var dataUser = UserMapper.ToData.FromRequest(request, user =>
                {
                    user.Id = Guid.NewGuid();
                    user.IsUploadingAudio = false;
                    user.PasswordHash = passwordHah;
                    user.PasswordSalt = salt;
                    user.RegisterDate = DateTime.Now;
                });

                var dataAuthToken = new AuthToken
                {
                    Id = Guid.NewGuid(),
                    DateExpired = DateTime.Now.AddDays(AuthTokenDaysExpired),
                    UserId = dataUser.Id,
                    IsActive = true
                };

                var playlist = new Playlist
                {
                    Id = Guid.NewGuid(),
                    IsGeneral = true,
                    Title = GeneralPlaylistName,
                    OwnerId = dataUser.Id,
                    OrderValue = 0
                };

                _dbContext.AuthTokens.Add(dataAuthToken);
                _dbContext.Users.Add(dataUser);
                _dbContext.Playlists.Add(playlist);
                _dbContext.SaveChanges();

                return ResponseBuilder <SignUpInResult> .SuccessBuild(new SignUpInResult
                {
                    User = UserMapper.ToApi.FromData(dataUser),
                    AuthToken = AuthTokenMapper.ToApi.FromData(dataAuthToken)
                });
            }));
        }
Пример #3
0
        public UserAccount CreateNewLocalUserAccount(
            string email,
            string password  = null,
            string returnUrl = null)
        {
            DateTime now = DateTime.UtcNow;

            UserAccount userAccount = new UserAccount
            {
                Id               = Guid.NewGuid(),
                Email            = email,
                FailedLoginCount = 0,
                IsEmailVerified  = false,
                IsLoginAllowed   = applicationOptions
                                   .RequireLocalAccountVerification,
                PasswordChangedAt = now,
                CreatedAt         = now,
                UpdatedAt         = now
            };

            if (!String.IsNullOrWhiteSpace(password))
            {
                userAccount.PasswordHash = crypto.HashPassword(
                    password,
                    applicationOptions.PasswordHashingIterationCount
                    );
            }

            if (applicationOptions.RequireLocalAccountVerification &&
                !String.IsNullOrWhiteSpace(returnUrl))
            {
                this.SetVerification(
                    userAccount,
                    VerificationKeyPurpose.ConfirmAccount,
                    returnUrl,
                    now
                    );
            }

            return(userAccount);
        }
Пример #4
0
        public async Task <UserAccount> CreateNewLocalUserAccountAsync(string email, string password, string returnUrl = null)
        {
            var now = DateTime.UtcNow;

            var userAccount = new UserAccount
            {
                Id           = Guid.NewGuid(),
                Email        = email,
                PasswordHash = _crypto.HashPassword(password,
                                                    _applicationOptions.PasswordHashingIterationCount),
                FailedLoginCount  = 0,
                IsEmailVerified   = false,
                IsLoginAllowed    = _applicationOptions.RequireLocalAccountVerification,
                PasswordChangedAt = now,
                CreatedAt         = now,
                UpdatedAt         = now
            };

            if (_applicationOptions.RequireLocalAccountVerification &&
                !String.IsNullOrWhiteSpace(returnUrl))
            {
                // Set verification key
                userAccount.SetVerification(
                    _crypto.Hash(_crypto.GenerateSalt()).StripUglyBase64(),
                    VerificationKeyPurpose.ConfirmAccount,
                    returnUrl,
                    now);
            }

            await _userAccountStore.WriteAsync(userAccount);

            // Emit event
            await _eventService.RaiseSuccessfulUserAccountCreatedEventAsync(
                userAccount.Id,
                IdentityServerConstants.LocalIdentityProvider);

            return(userAccount);
        }
Пример #5
0
        private static void SeedAccounts()
        {
            var username       = "******";
            var hashedPassword = _crypto.HashPassword(username, 10);
            var user           = _context.UsersProfiles.SingleOrDefault(x => x.UserAccount.Username == username);

            if (user == null)
            {
                _context.Set <UserAccount>().Add(
                    new UserAccount
                {
                    Username          = username,
                    Guid              = Guid.NewGuid(),
                    HashedPassword    = hashedPassword,
                    Email             = "*****@*****.**",
                    LastUpdated       = DateTimeOffset.Now,
                    IsLoginAllowed    = true,
                    IsAccountVerified = true,
                    Created           = DateTimeOffset.UtcNow,
                    ClaimCollection   = new UserClaimCollection
                    {
                        new UserClaim(ClaimTypes.Role, UserAccessLevel.Admin.ToString())
                        {
                            ObjectState = ObjectState.Added
                        }
                    },
                    Profile = new UserProfile
                    {
                        FirstName     = "System",
                        LastName      = "Admin",
                        Iso2          = "CA",
                        ProvinceState = "BC",
                        Created       = DateTimeOffset.UtcNow,
                        ObjectState   = ObjectState.Added
                    },
                    ObjectState = ObjectState.Added
                }
                    );
                _hasUpdates = true;
            }

            username       = "******";
            hashedPassword = _crypto.HashPassword(username, 10);
            user           = _context.UsersProfiles.SingleOrDefault(x => x.UserAccount.Username == username);
            if (user != null)
            {
                return;
            }

            _context.Set <UserAccount>().Add(
                new UserAccount
            {
                Username          = username,
                Guid              = Guid.NewGuid(),
                HashedPassword    = hashedPassword,
                Email             = "*****@*****.**",
                LastUpdated       = DateTimeOffset.Now,
                IsLoginAllowed    = true,
                IsAccountVerified = true,
                Created           = DateTimeOffset.UtcNow,
                ClaimCollection   = new UserClaimCollection
                {
                    new UserClaim(ClaimTypes.Role, UserAccessLevel.User.ToString())
                    {
                        ObjectState = ObjectState.Added
                    }
                },
                Profile = new UserProfile
                {
                    FirstName     = "Demo",
                    LastName      = "user",
                    Iso2          = "CA",
                    ProvinceState = "BC",
                    Created       = DateTimeOffset.UtcNow,
                    ObjectState   = ObjectState.Added
                },
                ObjectState = ObjectState.Added
            }
                );
            _hasUpdates = true;
        }
Пример #6
0
        public IEnumerable <UserAccount> GetUserAccounts(
            ICrypto crypto,
            ApplicationOptions options)
        {
            var now = DateTime.UtcNow;

            var users = new List <UserAccount>
            {
                // Active user account with local account but no external accounts
                new UserAccount
                {
                    Id    = Guid.Parse("0c2954d2-4c73-44e3-b0f2-c00403e4adef"),
                    Email = "alice@localhost",

                    PasswordHash = crypto.HashPassword(
                        "alice@localhost",
                        options.PasswordHashingIterationCount),

                    CreatedAt       = now,
                    UpdatedAt       = now,
                    IsEmailVerified = true,
                    IsLoginAllowed  = true,

                    Claims = new List <UserAccountClaim>
                    {
                        new UserAccountClaim(JwtClaimTypes.Name,
                                             "Alice Smith"),

                        new UserAccountClaim(JwtClaimTypes.GivenName,
                                             "Alice"),

                        new UserAccountClaim(JwtClaimTypes.FamilyName,
                                             "Smith"),

                        new UserAccountClaim(JwtClaimTypes.Email,
                                             "alice@localhost"),

                        new UserAccountClaim(JwtClaimTypes.EmailVerified,
                                             "true",
                                             ClaimValueTypes.Boolean),

                        new UserAccountClaim(JwtClaimTypes.Role, "Admin"),

                        new UserAccountClaim(JwtClaimTypes.Role, "Geek"),

                        new UserAccountClaim(JwtClaimTypes.WebSite,
                                             "http://alice.com"),

                        new UserAccountClaim(JwtClaimTypes.Address,
                                             @"{ 'street_address': 'One Hacker Way', 'locality': 'Heidelberg', 'postal_code': 69118, 'country': 'Germany' }",
                                             IdentityServerConstants.ClaimValueTypes.Json)
                    }
                },
                // Active user account
                new UserAccount
                {
                    Id    = Guid.Parse("28575826-68a0-4a1d-9428-674a2eb5db95"),
                    Email = "bob@localhost",

                    PasswordHash = crypto.HashPassword(
                        "bob@localhost",
                        options.PasswordHashingIterationCount),

                    CreatedAt       = now,
                    UpdatedAt       = now,
                    IsEmailVerified = true,
                    IsLoginAllowed  = true,

                    Claims = new List <UserAccountClaim>
                    {
                        new UserAccountClaim(JwtClaimTypes.Name,
                                             "Bob Smith"),

                        new UserAccountClaim(JwtClaimTypes.GivenName,
                                             "Bob"),

                        new UserAccountClaim(JwtClaimTypes.FamilyName,
                                             "Smith"),

                        new UserAccountClaim(JwtClaimTypes.Email,
                                             "bob@localhost"),

                        new UserAccountClaim(JwtClaimTypes.EmailVerified,
                                             "true",
                                             ClaimValueTypes.Boolean),

                        new UserAccountClaim(JwtClaimTypes.Role,
                                             "Developer"),

                        new UserAccountClaim(JwtClaimTypes.Role,
                                             "Geek"),

                        new UserAccountClaim(JwtClaimTypes.WebSite,
                                             "http://bob.com"),

                        new UserAccountClaim(JwtClaimTypes.Address,
                                             @"{ 'street_address': 'One Hacker Way', 'locality': 'Heidelberg', 'postal_code': 69118, 'country': 'Germany' }",
                                             IdentityServerConstants.ClaimValueTypes.Json)
                    }
                },

                // Inactive user account with local account but no external accounts
                new UserAccount
                {
                    Id    = Guid.Parse("6b13d17c-55a6-482e-96b9-dc784015f927"),
                    Email = "jim@localhost",

                    PasswordHash = crypto.HashPassword(
                        "jim@localhost",
                        options.PasswordHashingIterationCount),

                    CreatedAt       = now,
                    UpdatedAt       = now,
                    IsEmailVerified = true,
                    EmailVerifiedAt = now,
                    IsLoginAllowed  = false,
                    Claims          = CreateClaims("Jim Panse", "Jim", "Panse"),
                },

                // Not verified user account with local account but no external accounts
                new UserAccount
                {
                    Id           = Guid.Parse("13808d08-b1c0-4f28-8d3e-8c9a4051efcb"),
                    Email        = "paul@localhost",
                    PasswordHash = crypto.HashPassword(
                        "paul@localhost",
                        options.PasswordHashingIterationCount),

                    CreatedAt       = now,
                    UpdatedAt       = now,
                    IsEmailVerified = false,
                    IsLoginAllowed  = false,
                    Claims          = CreateClaims("Paul Panzer", "Paul", "Panzer")
                                      // TODO: set VerificationKey, VerificationPurpose, VerificationKeySentAt
                },

                // External user account
                new UserAccount
                {
                    Id        = Guid.Parse("58631b04-9be5-454a-aa1d-f679cd454fa6"),
                    Email     = "bill@localhost",
                    CreatedAt = now,
                    UpdatedAt = now,
                    // had never confirmed the email, since he got via facebook
                    IsEmailVerified = false,
                    // is allowed to login since he registed via facebook
                    IsLoginAllowed = true,
                    Claims         = CreateClaims("Bill Smith", "Bill", "Smith"),
                    Accounts       = new List <ExternalAccount>()
                    {
                        new ExternalAccount
                        {
                            CreatedAt = now,
                            Email     = "bill@localhost",
                            Subject   = "123456789",
                            Provider  = "facebook"
                        }
                    }
                }
            };

            return(users);
        }
Пример #7
0
        public void InitializeStores()
        {
            // Cleanup
            // HACK: the memory db survives server dispose so i have to cleanup it here
            //       cannot run this tests in parallel
            _configurationDbContext.Clients.Clear();
            _configurationDbContext.IdentityResources.Clear();
            _configurationDbContext.ApiResources.Clear();
            _configurationDbContext.SaveChanges();
            _persistedGrantDbContext.PersistedGrants.Clear();
            _persistedGrantDbContext.SaveChanges();
            _defaultDbContext.UserAccounts.Clear();
            _defaultDbContext.ExternalAccounts.Clear();
            _defaultDbContext.UserAccountClaims.Clear();
            _defaultDbContext.SaveChanges();

            // Add default sample data clients
            foreach (var client in Clients.Get().ToList())
            {
                _configurationDbContext.Clients.Add(client.ToEntity());
            }
            _configurationDbContext.SaveChanges();

            foreach (var resource in Resources.GetIdentityResources().ToList())
            {
                _configurationDbContext.IdentityResources.Add(resource.ToEntity());
            }
            _configurationDbContext.SaveChanges();

            foreach (var resource in Resources.GetApiResources().ToList())
            {
                _configurationDbContext.ApiResources.Add(resource.ToEntity());
            }
            _configurationDbContext.SaveChanges();

            // Add test user accounts
            var now          = DateTime.UtcNow;
            var userAccounts = new List <UserAccount>
            {
                // Active user account with local account but no external accounts
                new UserAccount
                {
                    Id              = Guid.Parse("0c2954d2-4c73-44e3-b0f2-c00403e4adef"),
                    Email           = "alice@localhost",
                    PasswordHash    = _crypto.HashPassword("alice@localhost", _applicationOptions.PasswordHashingIterationCount),
                    CreatedAt       = now,
                    UpdatedAt       = now,
                    IsEmailVerified = true,
                    EmailVerifiedAt = now,
                    IsLoginAllowed  = true,
                    Claims          = this.CreateClaims("Alice Smith", "Alice", "Smith")
                },

                // Inactive user account with local account but no external accounts
                new UserAccount
                {
                    Id              = Guid.Parse("6b13d17c-55a6-482e-96b9-dc784015f927"),
                    Email           = "jim@localhost",
                    PasswordHash    = _crypto.HashPassword("jim@localhost", _applicationOptions.PasswordHashingIterationCount),
                    CreatedAt       = now,
                    UpdatedAt       = now,
                    IsEmailVerified = true,
                    EmailVerifiedAt = now,
                    IsLoginAllowed  = false,
                    Claims          = this.CreateClaims("Jim Panse", "Jim", "Panse"),
                },

                // Not verified user account with local account but no external accounts
                new UserAccount
                {
                    Id              = Guid.Parse("13808d08-b1c0-4f28-8d3e-8c9a4051efcb"),
                    Email           = "paul@localhost",
                    PasswordHash    = _crypto.HashPassword("paul@localhost", _applicationOptions.PasswordHashingIterationCount),
                    CreatedAt       = now,
                    UpdatedAt       = now,
                    IsEmailVerified = false,
                    IsLoginAllowed  = false,
                    Claims          = this.CreateClaims("Paul Panzer", "Paul", "Panzer")
                                      // TODO: set VerificationKey, VerificationPurpose, VerificationKeySentAt
                },

                // External user account
                new UserAccount
                {
                    Id              = Guid.Parse("58631b04-9be5-454a-aa1d-f679cd454fa6"),
                    Email           = "bob@localhost",
                    CreatedAt       = now,
                    UpdatedAt       = now,
                    IsEmailVerified = false, // had never confirmed the email, since he got via facebook
                    IsLoginAllowed  = true,  // is allowed to login since he registed via facebook
                    Claims          = this.CreateClaims("Bob Smith", "Bob", "Smith"),
                    Accounts        = new List <ExternalAccount>()
                    {
                        new ExternalAccount
                        {
                            CreatedAt = now,
                            Email     = "bob@localhost",
                            Subject   = "123456789",
                            Provider  = "facebook"
                        }
                    }
                }
            };

            // Map all references
            foreach (var userAccount in userAccounts)
            {
                foreach (var claim in userAccount.Claims)
                {
                    //claim.UserAccountId = userAccount.Id;
                    claim.UserAccount = userAccount;
                }

                if (userAccount.Accounts != null)
                {
                    foreach (var account in userAccount.Accounts)
                    {
                        account.UserAccountId = userAccount.Id;
                        account.UserAccount   = userAccount;
                    }
                }

                _defaultDbContext.UserAccounts.Add(userAccount.ToEntity());
            }

            _defaultDbContext.SaveChanges();
        }