示例#1
0
        public async Task changeUserPasswordAsync(UserIdentity user, string newPassword)
        {
            var lockEntry = serverContext.serviceTable.getOrCreate(user.username).userLock;
            await lockEntry.withExclusiveWriteAsync(Task.Run(async() => {
                // Recompute password crypto
                var cryptoConf        = PasswordCryptoConfiguration.createDefault();
                var cryptoHelper      = new AuthCryptoHelper(cryptoConf);
                var pwSalt            = cryptoHelper.generateSalt();
                var encryptedPassword =
                    cryptoHelper.calculateUserPasswordHash(newPassword, pwSalt);
                user.crypto = new ItemCrypto {
                    salt = pwSalt,
                    conf = cryptoConf,
                    key  = encryptedPassword
                };

                // Save changes
                await updateUserInDatabaseAsync(user);
            }));
        }
示例#2
0
        public async Task <UserIdentity> registerUserAsync(UserRegistrationRequest regRequest)
        {
            if (await findUserByUsernameAsync(regRequest.username) != null)
            {
                throw new SecurityException("a user with the same username already exists");
            }

            // Calculate cryptographic info
            var cryptoConf        = PasswordCryptoConfiguration.createDefault();
            var cryptoHelper      = new AuthCryptoHelper(cryptoConf);
            var pwSalt            = cryptoHelper.generateSalt();
            var encryptedPassword =
                cryptoHelper.calculateUserPasswordHash(regRequest.password, pwSalt);
            // Create user
            var user = new UserIdentity {
                identifier = Guid.NewGuid().ToString(),
                username   = regRequest.username,
                email      = regRequest.email,
                crypto     = new ItemCrypto {
                    salt = pwSalt,
                    conf = cryptoConf,
                    key  = encryptedPassword
                },
                enabled = true
            };

            // Add the user to the database
            _userCollection.Insert(user);

            // Index database
            _userCollection.EnsureIndex(x => x.identifier);
            _userCollection.EnsureIndex(x => x.username);

            serverContext.appState.userMetrics[user.identifier] = new UserMetrics();

            return(user);
        }