예제 #1
0
        public async Task <bool> checkPasswordAsync(string password, UserIdentity user)
        {
            var ret       = false;
            var lockEntry = serverContext.serviceTable.getOrCreate(user.username).userLock;
            await lockEntry.withConcurrentReadAsync(Task.Run(() => {
                //Calculate hash and compare
                var cryptoHelper = new AuthCryptoHelper(user.crypto.conf);
                var pwKey        =
                    cryptoHelper.calculateUserPasswordHash(password, user.crypto.salt);
                ret = StructuralComparisons.StructuralEqualityComparer.Equals(pwKey, user.crypto.key);
            }));

            return(ret);
        }
예제 #2
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);
            }));
        }
예제 #3
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);
        }