Example #1
0
  private void coordinator() throws InterruptedException {
    List<ActiveEvent> activeEvents = new ArrayList<>();
    long curTime = System.currentTimeMillis();
    for (User user : users) {
      if (user.isActive()) {
        for (Event event : user.getEvents()) {
          if (event.getDate().getTime() >= curTime) {
            activeEvents.add(new ActiveEvent(user.getName(), event.getDate(), event.getText()));
          }
        }
      }
    }
    Collections.sort(activeEvents);

    for (int i = 0; i < activeEvents.size(); i++) {
      if (i == 0) {
        Thread.sleep(activeEvents.get(i).getTime() - System.currentTimeMillis());
      } else {
        Thread.sleep(activeEvents.get(i).getTime() - activeEvents.get(i - 1).getTime());
      }
      activeEvents.get(i).print();
    }
  }
  protected final HandlerResult authenticateInternal(
      final OpenScienceFrameworkCredential credential)
      throws GeneralSecurityException, PreventedException {
    final String username = credential.getUsername();
    final String plainTextPassword = credential.getPassword();
    final String verificationKey = credential.getVerificationKey();
    final String oneTimePassword = credential.getOneTimePassword();

    final User user =
        this.mongoTemplate.findOne(
            new Query(
                new Criteria()
                    .orOperator(
                        Criteria.where("emails").is(username),
                        Criteria.where("username").is(username))),
            User.class);

    if (user == null) {
      throw new AccountNotFoundException(username + " not found with query");
    }

    Boolean validPassphrase = Boolean.FALSE;
    // verification key can substitute as a temporary password.
    if (verificationKey != null && verificationKey.equals(user.verificationKey)) {
      validPassphrase = Boolean.TRUE;
    } else if (BCrypt.checkpw(plainTextPassword, user.password)) {
      validPassphrase = Boolean.TRUE;
    }
    if (!validPassphrase) {
      throw new FailedLoginException(username + " invalid verification key or password");
    }

    TimeBasedOneTimePassword timeBasedOneTimePassword =
        this.mongoTemplate.findOne(
            new Query(
                Criteria.where("owner")
                    .is(user.id)
                    .and("is_confirmed")
                    .is(true)
                    .and("deleted")
                    .is(false)),
            TimeBasedOneTimePassword.class);

    if (timeBasedOneTimePassword != null && timeBasedOneTimePassword.totpSecret != null) {
      if (oneTimePassword == null) {
        throw new OneTimePasswordRequiredException("Time-based One Time Password required");
      }
      try {
        if (!TotpUtils.checkCode(
            timeBasedOneTimePassword.getTotpSecretBase32(), Long.valueOf(oneTimePassword), 30, 1)) {
          throw new OneTimePasswordFailedLoginException(
              username + " invalid time-based one time password");
        }
      } catch (Exception ex) {
        throw new OneTimePasswordFailedLoginException(
            username + " invalid time-based one time password");
      }
    }

    // Validate basic information such as username/password and a potential One-Time Password before
    // providing any indication of account status.
    if (!user.isRegistered) {
      throw new LoginNotAllowedException(username + " is not registered");
    }
    if (!user.isClaimed) {
      throw new LoginNotAllowedException(username + " is not claimed");
    }
    if (user.isMerged()) {
      throw new LoginNotAllowedException("Cannot log in to a merged user " + username);
    }
    if (user.isDisabled()) {
      throw new AccountDisabledException(username + " is disabled");
    }
    if (!user.isActive()) {
      throw new LoginNotAllowedException(username + " is not active");
    }

    final Map<String, Object> attributes = new HashMap<>();
    attributes.put("username", user.username);
    attributes.put("givenName", user.givenName);
    attributes.put("familyName", user.familyName);
    return createHandlerResult(
        credential, this.principalFactory.createPrincipal(user.id, attributes), null);
  }