public ChangeSecretResult ChangeSecret(string credentialTypeCode, string identifier, string secret) { CredentialType credentialType = this.credentialTypeRepository.WithCode(credentialTypeCode); if (credentialType == null) { return(new ChangeSecretResult(success: false, error: ChangeSecretResultError.CredentialTypeNotFound)); } Credential credential = this.credentialRepository.WithCredentialTypeIdAndIdentifier(credentialType.Id, identifier); 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); this.requestHandler.Storage.Save(); return(new ChangeSecretResult(success: true)); }
public ValidateResult Validate(string credentialTypeCode, string identifier, string secret) { CredentialType credentialType = this.credentialTypeRepository.WithCode(credentialTypeCode); if (credentialType == null) { return(new ValidateResult(success: false, error: ValidateResultError.CredentialTypeNotFound)); } Credential credential = this.credentialRepository.WithCredentialTypeIdAndIdentifier(credentialType.Id, identifier); 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: this.userRepository.WithKey(credential.UserId), success: true)); }
public SignUpResult SignUp(string name, string credentialTypeCode, string identifier, string secret) { User user = new User(); user.Name = name; user.Created = DateTime.Now; this.userRepository.Create(user); this.requestHandler.Storage.Save(); CredentialType credentialType = this.credentialTypeRepository.WithCode(credentialTypeCode); 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); this.requestHandler.Storage.Save(); return(new SignUpResult(user: user, success: true)); }