package lc.esp.security.sdk;

import java.util.HashMap;

/**
 * The SecurityContext class provides management of a principal-based security context. It provides the necessary
 * logic to evaluate security claims and requests.
 * <p>
 * This class implements a label based mandatory access security model. A given object to be secured is assigned
 * a classification level, represented internally by an integer where higher is more secure, and an optional set of
 * labels.
 * <p>
 * A principal is assigned a series of labels with associated classification levels. To be granted access to an
 * object the principal must possess all labels assigned to the object granted with a high enough security level
 * to meet the classification level of the object.
 *
 * @author Alex Leigh
 * @since mk20 (TOKYO PROTOCOL)
 */
public class SecurityContext {
    private final Principal principal;
    private final HashMap<Integer, ClassificationLevel> levels = new HashMap<>();

    public SecurityContext(Principal principal) {
        this.principal = principal;

        addLevel(new ClassificationLevel(0, "UNCLASSIFIED", "UC"));
        addLevel(new ClassificationLevel(10, "CONFIDENTIAL", "C"));
        addLevel(new ClassificationLevel(20, "SECRET", "S"));
        addLevel(new ClassificationLevel(30, "TOP SECRET", "TS"));
    }

    public void addLevel(ClassificationLevel level) {
        levels.put(level.getIndex(), level);
    }

    public ClassificationLevel getLevel(int index) {
        return levels.get(index);
    }
}
