/**
   * Is the user configured to use this credential type
   *
   * @return
   */
  public boolean configuredForCredentialType(String type, RealmModel realm, UserModel user) {
    UserFederationProvider link = getFederationLink(realm, user);
    if (link != null) {
      Set<String> supportedCredentialTypes = link.getSupportedCredentialTypes(user);
      if (supportedCredentialTypes.contains(type)) return true;
    }
    if (UserCredentialModel.isOtp(type)) {
      if (!user.isOtpEnabled()) return false;
    }

    List<UserCredentialValueModel> creds = user.getCredentialsDirectly();
    for (UserCredentialValueModel cred : creds) {
      if (cred.getType().equals(type)) {
        if (UserCredentialModel.isOtp(type)) {
          OTPPolicy otpPolicy = realm.getOTPPolicy();
          if (!cred.getAlgorithm().equals(otpPolicy.getAlgorithm())
              || cred.getDigits() != otpPolicy.getDigits()) {
            return false;
          }
          if (type.equals(UserCredentialModel.TOTP) && cred.getPeriod() != otpPolicy.getPeriod()) {
            return false;
          }
        }
        return true;
      }
    }
    return false;
  }