Example #1
0
    public void VerifyContactChallengeResponse(Guid correlationId, string code, out CommandRejectedException exceptionToThrowAfterSave) {
      RejectIfNullContactChallengeCorrelation(correlationId);
      ContactChallenge challenge = ContactChallenges[correlationId];
      exceptionToThrowAfterSave = null;

      if (challenge.IsCodeVerified)
        throw new CommandRejectedException(nameof(code), code, CommandRejectionReason.AlreadyApplied);

      if (challenge.IsMaxCodeAttemptsExhausted)
        throw new CommandRejectedException(nameof(code), code, CommandRejectionReason.MaxAttempts);

      bool isValid = ContactChallengers.TotpCodeProvider.Validate(code, Id, challenge.ContactValue, challenge.Purpose, challenge.Stamp);
      DateTime happenedOn = DateTime.UtcNow;

      if (isValid) {
        RaiseEvent(new ContactChallengeResponseVerified(Id, happenedOn, correlationId, challenge.NextCodeAttemptNumber));
      } else {
        RaiseEvent(new ContactChallengeResponseInvalidCodeAttempted(Id, happenedOn, correlationId, code, challenge.NextCodeAttemptNumber));
        if (challenge.CodeAttemptsRemainingCount <= 0) {
          RaiseEvent(new ContactChallengeResponseMaxInvalidCodesAttempted(Id, happenedOn, correlationId));
          exceptionToThrowAfterSave = new CommandRejectedException(nameof(code), code, CommandRejectionReason.MaxAttempts);
          return;
        }
        exceptionToThrowAfterSave = new CommandRejectedException(nameof(code), code, CommandRejectionReason.Unverified,
          new { challenge.CodeAttemptsRemainingCount, });
      }
    }
Example #2
0
    public void VerifyLogin(string login, string password, out CommandRejectedException exceptionToThrowAfterSave) {
      var unverifiedException = new CommandRejectedException(nameof(login), null, CommandRejectionReason.Unverified);
      if (!ConfirmedLogins.Any(x => string.Equals(x, ContactIdParser.Normalize(login))))
        throw unverifiedException;

      exceptionToThrowAfterSave = null;
      bool isRehashRequired; // = false;
      bool isVerified = ContactChallengers.PasswordHasher.Instance.VerifyHashedPassword(CurrentPasswordHash, password, out isRehashRequired);

      DateTime happenedOn = DateTime.UtcNow;
      if (isVerified) {
        string passwordRehash = null;
        if (isRehashRequired) {
          passwordRehash = ContactChallengers.PasswordHasher.Instance.HashPassword(password);
        }
        RaiseEvent(new LoginVerified(Id, happenedOn, login, passwordRehash));
      } else {
        exceptionToThrowAfterSave = unverifiedException;
        RaiseEvent(new LoginInvalidPasswordAttempted(Id, happenedOn, login));
      }
    }