Esempio n. 1
0
  /**
   * Get user password. Returns the encrypted value.
   *
   * <p>
   *
   * <pre>
   * If the password value is not null
   *    password = new password
   * else
   *   if user does exist
   *     password = old password
   *   else
   *     password = &quot;&quot;
   * </pre>
   */
  private String getPassword(User usr) {
    String name = usr.getName();
    String password = usr.getPassword();

    if (password != null) {
      password = passwordEncryptor.encrypt(password);
    } else {
      String blankPassword = passwordEncryptor.encrypt("");

      if (doesExist(name)) {
        String key = PREFIX + name + '.' + ATTR_PASSWORD;
        password = userDataProp.getProperty(key, blankPassword);
      } else {
        password = blankPassword;
      }
    }
    return password;
  }
 public User login(String userName, String password) throws Exception {
   Session session = HibernateSessionFactory.getSession();
   try {
     UserDAO dao = new UserDAO();
     return dao.login(userName, PasswordEncryptor.md5(password));
   } finally {
     HibernateSessionFactory.closeSession();
   }
 }
  protected void updateSettings(List<MirrorSettings> mirrorSettings, Settings settings) {

    Map<String, Object> values = new HashMap<String, Object>();

    // Store each mirror setting
    for (MirrorSettings ms : mirrorSettings) {
      values.put(SETTING_MIRROR_REPO_URL + ms.suffix, ms.mirrorRepoUrl);
      values.put(SETTING_USERNAME + ms.suffix, ms.username);
      values.put(
          SETTING_PASSWORD + ms.suffix,
          (ms.password.isEmpty() ? ms.password : passwordEncryptor.encrypt(ms.password)));
    }

    // Unfortunately the settings are stored in an immutable map, so need to cheat with reflection
    settingsReflectionHelper.set(values, settings);
  }
Esempio n. 4
0
  /** User authenticate method */
  public synchronized User authenticate(Authentication authentication)
      throws AuthenticationFailedException {
    lazyInit();

    if (authentication instanceof UsernamePasswordAuthentication) {
      UsernamePasswordAuthentication upauth = (UsernamePasswordAuthentication) authentication;

      String user = upauth.getUsername();
      String password = upauth.getPassword();

      if (user == null) {
        throw new AuthenticationFailedException("Authentication failed");
      }

      if (password == null) {
        password = "";
      }

      String storedPassword = userDataProp.getProperty(PREFIX + user + '.' + ATTR_PASSWORD);

      if (storedPassword == null) {
        // user does not exist
        throw new AuthenticationFailedException("Authentication failed");
      }

      if (passwordEncryptor.matches(password, storedPassword)) {
        return getUserByName(user);
      } else {
        throw new AuthenticationFailedException("Authentication failed");
      }

    } else if (authentication instanceof AnonymousAuthentication) {
      if (doesExist("anonymous")) {
        return getUserByName("anonymous");
      } else {
        throw new AuthenticationFailedException("Authentication failed");
      }
    } else {
      throw new IllegalArgumentException("Authentication not supported by this user manager");
    }
  }
  public MirrorRepositoryHook(
      GitScm gitScm,
      I18nService i18nService,
      ScheduledExecutorService executor,
      PasswordEncryptor passwordEncryptor,
      SettingsReflectionHelper settingsReflectionHelper,
      PluginSettingsFactory pluginSettingsFactory,
      RepositoryMetadataService repositoryMetadataService) {
    logger.debug("MirrorRepositoryHook: init started");

    // Set fields
    this.gitScm = gitScm;
    this.i18nService = i18nService;
    this.executor = executor;
    this.passwordEncryptor = passwordEncryptor;
    this.settingsReflectionHelper = settingsReflectionHelper;
    this.repositoryMetadataService = repositoryMetadataService;

    // Init password encryptor
    PluginSettings pluginSettings = pluginSettingsFactory.createSettingsForKey(PLUGIN_SETTINGS_KEY);
    passwordEncryptor.init(pluginSettings);

    logger.debug("MirrorRepositoryHook: init completed");
  }
  void runMirrorCommand(MirrorSettings settings, final Repository repository) {
    if (repositoryMetadataService.isEmpty(repository)) {
      return;
    }

    try {
      final String password = passwordEncryptor.decrypt(settings.password);
      final String authenticatedUrl =
          getAuthenticatedUrl(settings.mirrorRepoUrl, settings.username, password);

      executor.submit(
          new Callable<Void>() {

            int attempts = 0;

            @Override
            public Void call() throws Exception {
              try {
                GitScmCommandBuilder builder =
                    gitScm.getCommandBuilderFactory().builder(repository);
                PasswordHandler passwordHandler = getPasswordHandler(builder, password);

                // Call push command with the prune flag and refspecs for heads and tags
                // Do not use the mirror flag as pull-request refs are included
                String result =
                    builder
                        .command("push")
                        .argument("--prune") // this deletes locally deleted branches
                        .argument(authenticatedUrl)
                        .argument(
                            "--force") // Canonical repository should always take precedence over
                                       // mirror
                        .argument("+refs/heads/*:refs/heads/*") // Only mirror heads
                        .argument("+refs/tags/*:refs/tags/*") // and tags
                        .errorHandler(passwordHandler)
                        .exitHandler(passwordHandler)
                        .build(passwordHandler)
                        .call();

                logger.debug(
                    "MirrorRepositoryHook: postReceive completed with result '{}'.", result);

              } catch (Exception e) {
                if (++attempts >= MAX_ATTEMPTS) {
                  logger.error(
                      "Failed to mirror repository "
                          + repository.getName()
                          + " after "
                          + attempts
                          + " attempts.",
                      e);
                } else {
                  logger.warn(
                      "Failed to mirror repository "
                          + repository.getName()
                          + ", "
                          + "retrying in 1 minute (attempt {} of {}).",
                      attempts,
                      MAX_ATTEMPTS);
                  executor.schedule(this, 1, TimeUnit.MINUTES);
                }
              }

              return null;
            }
          });

    } catch (Exception e) {
      logger.error("MirrorRepositoryHook: Error running mirror hook", e);
    }
  }
  public Integer createUser(
      Date birthdate,
      TEthnicity ethnicity,
      TSex sex,
      TSex sexLookingFor,
      Integer height,
      TColor eyeColor,
      TColor hairColor,
      TChoice smoke,
      TChoice drink,
      THasChildren haveChildren,
      TWantsChildren wantsMoreChildren,
      TMaritalStatus maritalStatus,
      TBodytype userBodyType,
      String catchphrase,
      String aboutme,
      Integer idealHeightStart,
      Integer idealHeightEnd,
      TChoice idealSmokes,
      TChoice idealDrinks,
      THasChildren idealHasChildren,
      TWantsChildren idealWantsChildren,
      TMaritalStatus idealMaritalStatus,
      Integer termserviceagreement,
      Integer profileStatus,
      Zipcode zipcode,
      String password,
      String username,
      String email,
      List idealEthnicities,
      List idealBodyTypes,
      List idealLookingfor,
      Zipcode idealZipcode,
      Integer idealAgeStart,
      Integer idealAgeEnd,
      Integer idealDistance)
      throws Exception {
    Session session = HibernateSessionFactory.getSession();
    Transaction transaction = session.beginTransaction();
    User user = new User();
    try {
      user.setEthnicity(ethnicity);
      user.setBirthdate(birthdate);
      user.setSex(sex);
      user.setSexLookingFor(sexLookingFor);
      user.setEyeColor(eyeColor);
      user.setHairColor(hairColor);
      user.setHeight(height);
      user.setSmoke(smoke);
      user.setDrink(drink);
      user.setHaveChildern(haveChildren);
      user.setWantChildern(wantsMoreChildren);
      user.setMaritalStatus(maritalStatus);
      user.setBodyType(userBodyType);
      user.setCatchphrase(catchphrase);
      user.setAboutme(aboutme);
      user.setIdealHeightStart(idealHeightStart);
      user.setIdealHeightEnd(idealHeightEnd);
      user.setIdealSmokes(idealSmokes);
      user.setIdealDrinks(idealDrinks);
      user.setIdealHasChildern(idealHasChildren);
      user.setIdealWantsChildern(idealWantsChildren);
      user.setIdealMaritalStatus(idealMaritalStatus);
      user.setTermserviceagreement(termserviceagreement);
      user.setProfileStatus(profileStatus);
      user.setZipcode(zipcode);
      user.setPassword(PasswordEncryptor.md5(password));
      user.setUserName(username);
      user.setEmail(email);
      user.setIdealAgeStart(idealAgeStart);
      user.setIdealAgeEnd(idealAgeEnd);
      user.setIdealDistance(idealDistance);
      user.setIdealZipcode(idealZipcode);
      user.setIdealEthnicities(idealEthnicities);
      user.setIdealBodyTypes(idealBodyTypes);
      user.setIdealLookingfor(idealLookingfor);

      UserDAO dao = new UserDAO();
      dao.addUserOrUpdate(user);

      transaction.commit();
      return user.getUserId();
    } catch (Exception e) {
      transaction.rollback();
      throw e;
    } finally {
      HibernateSessionFactory.closeSession();
    }
  }