<commit_before>/*******************************************************************************
 * Copyright (c) 2014 Whizzo Software, LLC.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *******************************************************************************/
package com.whizzosoftware.hobson.bootstrap.api.hub;

import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.Logger;
import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.filter.LevelFilter;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.Appender;
import ch.qos.logback.core.FileAppender;
import ch.qos.logback.core.spi.FilterReply;
import com.whizzosoftware.hobson.api.HobsonNotFoundException;
import com.whizzosoftware.hobson.api.HobsonRuntimeException;
import com.whizzosoftware.hobson.api.event.EventManager;
import com.whizzosoftware.hobson.api.event.HubConfigurationUpdateEvent;
import com.whizzosoftware.hobson.api.hub.*;
import com.whizzosoftware.hobson.api.image.ImageInputStream;
import com.whizzosoftware.hobson.api.image.ImageMediaTypes;
import com.whizzosoftware.hobson.api.util.UserUtil;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.io.FileUtils;
import org.osgi.framework.BundleContext;
import org.osgi.framework.FrameworkUtil;
import org.osgi.service.cm.Configuration;
import org.osgi.service.cm.ConfigurationAdmin;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.util.Dictionary;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Properties;

/**
 * An OSGi implementation of HubManager.
 *
 * @author <NAME>
 */
public class OSGIHubManager implements HubManager {
    public static final String HUB_NAME = "hub.name";
    public static final String ADMIN_PASSWORD = "<PASSWORD>";
    public static final String SETUP_COMPLETE = "setup.complete";
    public static final String HOBSON_LOGGER = "com.whizzosoftware.hobson";

    volatile private ConfigurationAdmin configAdmin;
    volatile private EventManager eventManager;

    @Override
    public String getHubName(String userId, String hubId) {
        Configuration config = getConfiguration();
        Dictionary props = getConfigurationProperties(config);
        return (String)props.get(HUB_NAME);
    }

    @Override
    public void setHubName(String userId, String hubId, String name) {
        try {
            Configuration config = getConfiguration();
            Dictionary props = getConfigurationProperties(config);
            props.put(HUB_NAME, name);

            updateConfiguration(config, props);
        } catch (IOException e) {
            throw new HobsonRuntimeException("Error setting hub name", e);
        }
    }

    @Override
    public void setHubPassword(String userId, String hubId, PasswordChange change) {
        try {
            Configuration config = getConfiguration();
            Dictionary props = getConfigurationProperties(config);

            // verify the old password
            String shaOld = DigestUtils.sha256Hex(change.getCurrentPassword());
            String hubPassword = (String)props.get(ADMIN_PASSWORD);
            if (hubPassword != null && !hubPassword.equals(shaOld)) {
                throw new HobsonRuntimeException("The current hub password is invalid");
            }

            // set the new password
            props.put(ADMIN_PASSWORD, DigestUtils.sha256Hex(change.getNewPassword()));

            updateConfiguration(config, props);
        } catch (IOException e) {<commit_msg>Added hub password complexity enforcement.<commit_after>/*******************************************************************************
 * Copyright (c) 2014 Whizzo Software, LLC.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *******************************************************************************/
package com.whizzosoftware.hobson.bootstrap.api.hub;

import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.Logger;
import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.filter.LevelFilter;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.Appender;
import ch.qos.logback.core.FileAppender;
import ch.qos.logback.core.spi.FilterReply;
import com.whizzosoftware.hobson.api.HobsonInvalidRequestException;
import com.whizzosoftware.hobson.api.HobsonNotFoundException;
import com.whizzosoftware.hobson.api.HobsonRuntimeException;
import com.whizzosoftware.hobson.api.event.EventManager;
import com.whizzosoftware.hobson.api.event.HubConfigurationUpdateEvent;
import com.whizzosoftware.hobson.api.hub.*;
import com.whizzosoftware.hobson.api.image.ImageInputStream;
import com.whizzosoftware.hobson.api.image.ImageMediaTypes;
import com.whizzosoftware.hobson.api.util.UserUtil;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.io.FileUtils;
import org.osgi.framework.BundleContext;
import org.osgi.framework.FrameworkUtil;
import org.osgi.service.cm.Configuration;
import org.osgi.service.cm.ConfigurationAdmin;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.util.Dictionary;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Properties;

/**
 * An OSGi implementation of HubManager.
 *
 * @author <NAME>
 */
public class OSGIHubManager implements HubManager {
    public static final String HUB_NAME = "hub.name";
    public static final String ADMIN_PASSWORD = "<PASSWORD>";
    public static final String SETUP_COMPLETE = "setup.complete";
    public static final String HOBSON_LOGGER = "com.whizzosoftware.hobson";

    volatile private ConfigurationAdmin configAdmin;
    volatile private EventManager eventManager;

    @Override
    public String getHubName(String userId, String hubId) {
        Configuration config = getConfiguration();
        Dictionary props = getConfigurationProperties(config);
        return (String)props.get(HUB_NAME);
    }

    @Override
    public void setHubName(String userId, String hubId, String name) {
        try {
            Configuration config = getConfiguration();
            Dictionary props = getConfigurationProperties(config);
            props.put(HUB_NAME, name);

            updateConfiguration(config, props);
        } catch (IOException e) {
            throw new HobsonRuntimeException("Error setting hub name", e);
        }
    }

    @Override
    public void setHubPassword(String userId, String hubId, PasswordChange change) {
        try {
            Configuration config = getConfiguration();
            Dictionary props = getConfigurationProperties(config);

            // verify the old password
            String shaOld = DigestUtils.sha256Hex(change.getCurrentPassword());
            String hubPassword = (String)props.get(ADMIN_PASSWORD);
            if (hubPassword != null && !hubPassword.equals(shaOld)) {
                throw new HobsonRuntimeException("The current hub password is invalid");
            }

            // verify the password meets complexity requirements
            if (!change.isValid()) {
                throw new HobsonInvalidRequestException("New password does not meet complexity requirements");
            }

            // set the new password
            props.put(ADMIN_PASSWORD, DigestUtils.sha256Hex(change.getNewPassword()));

            updateConfiguration(config, props);
        } catch (IOException e) {
{'fraction_non_alphanumeric': 0.12880441761910877, 'fraction_numerical': 0.0038525748041607806, 'mean_word_length': 3.8614232209737827, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3405781', 'n_tokens_mistral': 2149, 'n_tokens_neox': 2078, 'n_words': 489}