Exemple #1
0
        public async Task <ChangeSecretResult> ChangeSecretAsync(string credentialTypeCode, string identifier, string secret)
        {
            Credential credential = (await this.credentialRepository.GetAllAsync(new CredentialFilter(credentialType: new CredentialTypeFilter(code: credentialTypeCode), identifier: new StringFilter(equals: identifier)))).FirstOrDefault();

            if (credential == null)
            {
                return(new ChangeSecretResult(success: false, error: ChangeSecretResultError.CredentialNotFound));
            }

            byte[] salt = Pbkdf2Hasher.GenerateRandomSalt();
            string hash = Pbkdf2Hasher.ComputeHash(secret, salt);

            credential.Secret = hash;
            credential.Extra  = Convert.ToBase64String(salt);
            this.credentialRepository.Edit(credential);
            await this.storage.SaveAsync();

            return(new ChangeSecretResult(success: true));
        }
Exemple #2
0
        public async Task <ValidateResult> ValidateAsync(string credentialTypeCode, string identifier, string secret)
        {
            Credential credential = (await this.credentialRepository.GetAllAsync(new CredentialFilter(credentialType: new CredentialTypeFilter(code: credentialTypeCode), identifier: new StringFilter(equals: identifier)), inclusions: new Inclusion <Credential>(c => c.User))).FirstOrDefault();

            if (credential == null)
            {
                return(new ValidateResult(success: false, error: ValidateResultError.CredentialNotFound));
            }

            if (!string.IsNullOrEmpty(secret))
            {
                byte[] salt = Convert.FromBase64String(credential.Extra);
                string hash = Pbkdf2Hasher.ComputeHash(secret, salt);

                if (credential.Secret != hash)
                {
                    return(new ValidateResult(success: false, error: ValidateResultError.SecretNotValid));
                }
            }

            return(new ValidateResult(user: credential.User, success: true));
        }
Exemple #3
0
        public async Task <SignUpResult> SignUpAsync(string name, string credentialTypeCode, string identifier, string secret)
        {
            User user = new User();

            user.Name    = name;
            user.Created = DateTime.Now;
            this.userRepository.Create(user);
            await this.storage.SaveAsync();

            CredentialType credentialType = (await this.credentialTypeRepository.GetAllAsync(new CredentialTypeFilter(code: credentialTypeCode))).FirstOrDefault();

            if (credentialType == null)
            {
                return(new SignUpResult(success: false, error: SignUpResultError.CredentialTypeNotFound));
            }

            Credential credential = new Credential();

            credential.UserId           = user.Id;
            credential.CredentialTypeId = credentialType.Id;
            credential.Identifier       = identifier;

            if (!string.IsNullOrEmpty(secret))
            {
                byte[] salt = Pbkdf2Hasher.GenerateRandomSalt();
                string hash = Pbkdf2Hasher.ComputeHash(secret, salt);

                credential.Secret = hash;
                credential.Extra  = Convert.ToBase64String(salt);
            }

            this.credentialRepository.Create(credential);
            await this.storage.SaveAsync();

            return(new SignUpResult(user: user, success: true));
        }