<commit_before>package com.jivesoftware.os.upena.deployable.okta;

import com.jivesoftware.os.mlogger.core.MetricLogger;
import com.jivesoftware.os.mlogger.core.MetricLoggerFactory;
import java.io.File;
import java.nio.file.Files;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAccount;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;

/**
 * Created by jonathan.colt on 11/7/16.
 */
public class OktaRealm extends AuthorizingRealm {

    public static final MetricLogger LOG = MetricLoggerFactory.getLogger();
    public static OktaLog oktaLog;

    public OktaRealm() {
        super();
    }

    public String getName() {
        return "OktaRealm";
    }

    private Map<String, SimpleAccount> userAccount = new ConcurrentHashMap<>(16);

    protected SimpleAccount getAccount(String username) {

        SimpleAccount account = userAccount.computeIfAbsent(username, s -> {

            SimpleAccount account1 = new SimpleAccount(username, "password", getName());

            String userRolesDirectory = System.getProperty("okta.user.roles.directory");
            if (userRolesDirectory == null) {
                return null;
            }

            File roles = new File(userRolesDirectory, username);
            if (roles.exists()) {
                try {
                    List<String> lines = Files.readAllLines(roles.toPath());
                    for (String line : lines) {
                        if (line.trim().startsWith("#")) {
                            continue;
                        }
                        if (line.trim().toLowerCase().startsWith("role") && line.contains(":")) {
                            String role = line.trim().split(":")[1].trim();
                            account1.addRole(role);
                        }

                        if (line.trim().toLowerCase().startsWith("perm") && line.contains(":")) {
                            String perm = line.trim().split(":")[1].trim();
                            account1.addStringPermission(perm);
                        }
                    }

                } catch (Exception e) {
                    LOG.error("Failed parsing {}",new Object[]{roles}, e);
                }
            } else {
                account1.addStringPermission("read");
            }

            //account.addStringPermission("blogEntry:edit"); //this user is allowed to 'edit' _any_ blogEntry
            //account.addStringPermission("printer:print:laserjet2000"); //allowed to 'print' to the 'printer' identified
            return account1;
        });
        return account;
    }


    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        //we can safely cast to a UsernamePasswordToken here, because this class 'supports' UsernamePasswordToken<commit_msg>tweaks to okta realm<commit_after>package com.jivesoftware.os.upena.deployable.okta;

import com.jivesoftware.os.mlogger.core.MetricLogger;
import com.jivesoftware.os.mlogger.core.MetricLoggerFactory;
import java.io.File;
import java.nio.file.Files;
import java.util.List;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAccount;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;

/**
 * Created by jonathan.colt on 11/7/16.
 */
public class OktaRealm extends AuthorizingRealm {

    public static final MetricLogger LOG = MetricLoggerFactory.getLogger();
    public static OktaLog oktaLog;

    public OktaRealm() {
        super();
    }

    public String getName() {
        return "OktaRealm";
    }

    //private Map<String, SimpleAccount> userAccount = new ConcurrentHashMap<>(16);

    protected SimpleAccount getAccount(String username) {

        //SimpleAccount account = userAccount.computeIfAbsent(username, s -> {

        SimpleAccount account1 = new SimpleAccount(username, "password", getName());

        String userRolesDirectory = System.getProperty("okta.user.roles.directory");
        if (userRolesDirectory == null) {
            return null;
        }

        File roles = new File(userRolesDirectory, username);
        if (roles.exists()) {
            try {
                List<String> lines = Files.readAllLines(roles.toPath());
                for (String line : lines) {
                    if (line.trim().startsWith("#")) {
                        continue;
                    }
                    if (line.trim().toLowerCase().startsWith("role") && line.contains(":")) {
                        String role = line.trim().split(":")[1].trim();
                        account1.addRole(role);
                    }

                    if (line.trim().toLowerCase().startsWith("perm") && line.contains(":")) {
                        String perm = line.trim().split(":")[1].trim();
                        account1.addStringPermission(perm);
                    }
                }

            } catch (Exception e) {
                LOG.error("Failed parsing {}", new Object[] { roles }, e);
            }
        } else {
            account1.addStringPermission("read");
        }

        //account.addStringPermission("blogEntry:edit"); //this user is allowed to 'edit' _any_ blogEntry
        //account.addStringPermission("printer:print:laserjet2000"); //allowed to 'print' to the 'printer' identified
        return account1;
        //});
        //return account;
    }


    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        //we can safely cast to a UsernamePasswordToken here, because this class 'supports' UsernamePasswordToken
{'fraction_non_alphanumeric': 0.1127951499680919, 'fraction_numerical': 0.00574345883854499, 'mean_word_length': 2.3186871360508206, 'pattern_counts': {'":': 8, '<': 9, '<?xml version=': 0, '>': 11, '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': '1120697', 'n_tokens_mistral': 1692, 'n_tokens_neox': 1642, 'n_words': 339}