Ejemplo n.º 1
0
        public ValidateResult Validate(string credentialTypeCode, string identifier, string secret)
        {
            var credentialTypes = _services.CredentialTypesService.GetAll();
            var credentialType  = credentialTypes.FirstOrDefault(ct => ct.Code.ToLower() == credentialTypeCode.ToLower());

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

            var credentials = _services.CredentialService.GetAll();
            var credential  = credentials.FirstOrDefault(c => c.CredentialTypeId == credentialType.Id && c.Identifier == identifier);

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

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

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

            var usr = _services.UserService.GetById(credential.UserId);

            return(new ValidateResult(user: usr, success: true));
        }
Ejemplo n.º 2
0
        public async Task <SignUpResult> SignUp(RegisterInput newUser, string credentialTypeCode, string identifier, string secret)
        {
            // Api a request göndererek yeni kullanıcı oluştur.
            var user = await _services.UserService.Register(newUser);

            var credentialTypes = _services.CredentialTypesService.GetAll();
            var credentialType  = credentialTypes.FirstOrDefault(ct => ct.Code.ToLower() == credentialTypeCode.ToLower());

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

            var credential = new Credential
            {
                UserId           = user.Id,
                CredentialTypeId = credentialType.Id,
                Identifier       = identifier
            };

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

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

            _services.CredentialService.Create(credential);

            return(new SignUpResult(user: user, success: true));
        }
Ejemplo n.º 3
0
        public ChangeSecretResult ChangeSecret(string credentialTypeCode, string identifier, string secret)
        {
            var credentialTypes = _services.CredentialTypesService.GetAll();
            var credentialType  = credentialTypes.FirstOrDefault(ct => ct.Code.ToLower() == credentialTypeCode.ToLower());

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

            var credentials = _services.CredentialService.GetAll();
            var credential  = credentials.FirstOrDefault(c => c.CredentialTypeId == credentialType.Id && c.Identifier == identifier);

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

            var salt = Pbkdf2Hasher.GenerateRandomSalt();
            var hash = Pbkdf2Hasher.ComputeHash(secret, salt);

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

            _services.CredentialService.Update(credential);

            return(new ChangeSecretResult(success: true));
        }