<commit_before>package com.agilefreaks.calin.securityflaxexample;

import android.util.Log;

import java.security.Security;
import java.util.HashMap;
import java.util.Map;

/*
Things to consider:
- clarity with respect to security (we want to look at a abstraction and the security model should be very clear)
- catch flaws at compile time
- proper usage to be obvious
- separate and protect security state
 */

/*
Build abstractions that are hard to use insecurely
 */

/*
Add a enum
 */

/*
Expanding the bounds of values for our security level, increases complexity
 */

public class StorageStrategy {
  public static final int MAX_SECURITY = 1;
  public static final int NO_SECURITY = 0;
  public static final int REQUIRE_ENCRYPTION = 3;


  private interface StorageHandler {
    public void store(String key, String data);
  }

  private class PublicStoreAdapter implements StorageHandler {

    @Override
    public void store(String key, String data) {
      Log.d("PublicStoreAdapter", "Store on sdcard");
    }
  }

  private class PrivateStoreAdapter implements StorageHandler {

    @Override
    public void store(String key, String data) {
      Log.d("PublicStoreAdapter", "Store on private folder");
    }
  }

  private Map<String, StorageHandler> handlerMapping = new HashMap<String, StorageHandler>();

  public StorageStrategy() {
    handlerMapping.put("private", new PrivateStoreAdapter());
    handlerMapping.put("public", new PublicStoreAdapter());
  }

  public void storeData(String key, String data, int security) {
    StorageHandler storageHandler;

    if (security < REQUIRE_ENCRYPTION) {
      storageHandler = getPublicHandler();
    } else {
      storageHandler = getSecureHandler();
    }

    storageHandler.store(key, data);
  }

  private StorageHandler getPublicHandler() {
    return handlerMapping.get("public");
  }

  private StorageHandler getSecureHandler() {
    return handlerMapping.get("private");
  }

  public void saveSettings() {
    storeData("name", "private stuff", MAX_SECURITY);
    storeData("private.groups", "some more private stuff", MAX_SECURITY);
    storeData("address", "private address", MAX_SECURITY);

    storeData("profilephoto", "public info", NO_SECURITY);
    storeData("homepageurl", "public info", NO_SECURITY);
  }
}<commit_msg>revert to the good aproach and guard our security level with compile time<commit_after>package com.agilefreaks.calin.securityflaxexample;

import android.util.Log;

import java.util.HashMap;
import java.util.Map;

/*
Things to consider:
- clarity with respect to security (we want to look at a abstraction and the security model should be very clear)
- catch flaws at compile time
- proper usage to be obvious
- separate and protect security state
 */

/*
Build abstractions that are hard to use insecurely
 */

/*
Add a enum
 */

public class StorageStrategy {
  public enum Security {MAX, NONE}

  private interface StorageHandler {
    public void store(String key, String data);
  }

  private class PublicStoreAdapter implements StorageHandler {

    @Override
    public void store(String key, String data) {
      Log.d("PublicStoreAdapter", "Store on sdcard");
    }
  }

  private class PrivateStoreAdapter implements StorageHandler {

    @Override
    public void store(String key, String data) {
      Log.d("PublicStoreAdapter", "Store on private folder");
    }
  }

  private Map<String, StorageHandler> handlerMapping = new HashMap<String, StorageHandler>();

  public StorageStrategy() {
    handlerMapping.put("private", new PrivateStoreAdapter());
    handlerMapping.put("public", new PublicStoreAdapter());
  }

  public void storeData(String key, String data, Security security) {
    StorageHandler storageHandler;

    if (security == Security.NONE) {
      storageHandler = getPublicHandler();
    } else {
      storageHandler = getSecureHandler();
    }

    storageHandler.store(key, data);
  }

  private StorageHandler getPublicHandler() {
    return handlerMapping.get("public");
  }

  private StorageHandler getSecureHandler() {
    return handlerMapping.get("private");
  }

  public void saveSettings() {
    storeData("name", "private stuff", Security.MAX);
    storeData("private.groups", "some more private stuff", Security.MAX);
    storeData("address", "private address", Security.MAX);

    storeData("profilephoto", "public info", Security.NONE);
    storeData("homepageurl", "public info", Security.NONE);
  }
}
{'fraction_non_alphanumeric': 0.097779771249159, 'fraction_numerical': 0.0006727965911639381, 'mean_word_length': 4.062429057888763, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '2373850', 'n_tokens_mistral': 1236, 'n_tokens_neox': 1185, 'n_words': 392}