Example usage for java.security Principal getClass

List of usage examples for java.security Principal getClass

Introduction

In this page you can find the example usage for java.security Principal getClass.

Prototype

@HotSpotIntrinsicCandidate
public final native Class<?> getClass();

Source Link

Document

Returns the runtime class of this Object .

Usage

From source file:Main.java

public static String toString(Principal[] principals) {
    if (principals == null || principals.length == 0) {
        return "<empty principals>";
    }//from w  w w  .j av  a2  s. co m
    StringBuffer buf = new StringBuffer();

    buf.append("<");
    for (int i = 0; i < principals.length; i++) {
        Principal p = principals[i];
        buf.append("(class=");
        buf.append(p.getClass());
        buf.append(", name=");
        buf.append(p.getName());
        buf.append(")");
        if (i < principals.length) {
            buf.append(", ");
        }

    }
    buf.append(">");

    return buf.toString();
}

From source file:cn.vlabs.duckling.vwb.service.auth.policy.PolicyUtil.java

public static String acl2PolicyString(Acl acl) {
    Enumeration<AclEntry> entryEnum = acl.entries();
    StringBuffer sb = new StringBuffer();
    while (entryEnum.hasMoreElements()) {
        AclEntry entry = entryEnum.nextElement();
        sb.append("grant principal ");
        Principal principal = entry.getPrincipal();
        sb.append(principal.getClass().getName()).append(" \"");
        sb.append(principal.getName()).append("\" {\n\r");
        Enumeration<Permission> permissEnum = entry.permissions();
        while (permissEnum.hasMoreElements()) {
            Permission permission = permissEnum.nextElement();
            sb.append("\t").append(PolicyUtil.permiss2PolicyString(permission));
        }/*from w ww.j  ava 2  s  .  c  o  m*/
        sb.append("};\n\r");
    }
    return sb.toString();
}

From source file:cn.vlabs.duckling.vwb.service.auth.policy.PolicyUtil.java

public static Acl restoreReserved(Acl policy, Acl origin) {
    Enumeration<AclEntry> entryEnum = origin.entries();
    //// ww  w.  j  a v a2 s.c om
    while (entryEnum.hasMoreElements()) {
        AclEntry entry = entryEnum.nextElement();
        Principal principal = entry.getPrincipal();

        String principalClass = principal.getClass().getName();
        String principalName = principal.getName();

        if (principalClass == null || principalName == null)
            continue;
        if (principalClass.equals("cn.vlabs.duckling.vwb.service.auth.Role") && principalName.equals("All")) {
            continue;
        }
        if (principalClass.equals("cn.vlabs.duckling.vwb.service.auth.GroupPrincipal")) {
            continue;
        }
        policy.addEntry(entry);
    }
    return policy;
}

From source file:cn.vlabs.duckling.vwb.service.auth.policy.PolicyUtil.java

public static Acl removeReserved(Acl policy) {
    Enumeration<AclEntry> entryEnum = policy.entries();
    // ?//from  w  ww  . j  av  a  2 s  . co  m
    while (entryEnum.hasMoreElements()) {
        AclEntry entry = entryEnum.nextElement();
        Principal principal = entry.getPrincipal();

        String principalClass = principal.getClass().getName();
        String principalName = principal.getName();

        if (principalClass == null || principalName == null)
            continue;
        if (principalClass.equals("cn.vlabs.duckling.vwb.service.auth.Role") && principalName.equals("All")) {
            continue;
        }
        if (principalClass.equals("cn.vlabs.duckling.vwb.service.auth.GroupPrincipal")) {
            continue;
        }
        policy.removeEntry(entry);
    }

    return policy;
}

From source file:cn.vlabs.duckling.vwb.service.auth.policy.PolicyUtil.java

public static Acl replacePolicy(Acl policy, String vogroup) {
    Enumeration<AclEntry> entryEnum = policy.entries();

    while (entryEnum.hasMoreElements()) {
        AclEntry entry = entryEnum.nextElement();

        Principal principal = entry.getPrincipal();

        String principalClass = principal.getClass().getName();
        String principalName = principal.getName();

        if (principalClass == null || principalName == null)
            continue;
        if (principalClass.equals("cn.vlabs.duckling.vwb.service.auth.GroupPrincipal")) {
            Principal principalNew = new GroupPrincipal(vogroup);
            entry.setPrincipal(principalNew);
        }/*from w  w  w . j a v  a2 s  . c  o  m*/
        if (principalClass.equals("cn.vlabs.duckling.vwb.service.auth.Role")) {
            int pos = principalName.indexOf('.');
            if (pos != -1) {
                String principalNameNew = vogroup + principalName.substring(pos, principalName.length());
                Principal principalNew = new Role(principalNameNew);
                entry.setPrincipal(principalNew);
            }
        }
    }

    return policy;
}

From source file:cn.vlabs.duckling.vwb.service.auth.policy.PolicyUtil.java

public static void remove(Acl policy, PolicyData pd) {
    Enumeration<AclEntry> entryEnum = policy.entries();

    while (entryEnum.hasMoreElements()) {
        AclEntry entry = entryEnum.nextElement();
        Principal principal = entry.getPrincipal();

        String principalClass = principal.getClass().getName();
        String principalName = principal.getName();

        if (principalClass == null || principalName == null)
            continue;
        if (principalClass.equals(pd.getPrincipalClass()) && principalName.equals(pd.getPrincipalName())) {
            Enumeration<Permission> permissEnum = entry.permissions();
            while (permissEnum.hasMoreElements()) {
                Permission permission = permissEnum.nextElement();

                String permissionClass = permission.getClass().getName();
                String permissionName = permission.getName();
                String permissionActions = permission.getActions();

                if (permissionClass == null || permissionName == null || permissionActions == null)
                    continue;
                if (permissionClass.equals(pd.getPermissionClass())
                        && permissionName.equals(pd.getPermissionName())
                        && permissionActions.equals(pd.getPermissionActions())) {
                    // 
                    AclEntry policyEntry = policy.getEntry(principal);
                    policyEntry.removePermission(permission);
                }//  ww w  . j  a  v  a  2s  .  com
            }
        }
    }
}

From source file:cn.vlabs.duckling.vwb.service.auth.policy.PolicyUtil.java

public static List<PolicyData> acl2PolicyData(Acl acl) {
    List<PolicyData> pdl = new ArrayList<PolicyData>();

    Enumeration<AclEntry> entryEnum = acl.entries();
    while (entryEnum.hasMoreElements()) {
        AclEntry entry = entryEnum.nextElement();
        Principal principal = entry.getPrincipal();
        String principalClass = principal.getClass().getName();
        String principalName = principal.getName();
        String objectname = null;
        if (principalClass.equals("cn.vlabs.duckling.vwb.service.auth.Role")
                && principalName.equalsIgnoreCase("All")) {
            objectname = "";
        }//from w w  w. jav a  2 s .c  om
        if (principalClass.equals("cn.vlabs.duckling.vwb.service.auth.GroupPrincipal")) {
            objectname = "";
        }
        if (objectname == null) {
            continue;
        }
        Enumeration<Permission> permissEnum = entry.permissions();
        while (permissEnum.hasMoreElements()) {
            Permission permission = permissEnum.nextElement();

            String permissionClass = permission.getClass().getName();
            String permissionType = null;
            if (permissionClass.equals("cn.vlabs.duckling.vwb.service.auth.permissions.PagePermission")) {
                permissionType = "?";
            }
            if (permissionClass.equals("cn.vlabs.duckling.vwb.service.auth.permissions.VWBPermission")) {
                permissionType = "";
            }
            if (permissionType == null) {
                continue;
            }

            String permissionActions = permission.getActions();
            String operations = permissionActions;

            String permissionName = permission.getName();

            String resource = null;
            if (permission instanceof UserPermission) {
                resource = ((UserPermission) permission).getWiki();
            } else if (permission instanceof VWBPermission) {
                resource = "*";
            } else if (permission instanceof PagePermission) {
                resource = ((PagePermission) permission).getPage();
            } else if (permission instanceof AllPermission) {
                resource = "*";
            } else {
                resource = permission.getName();
            }

            PolicyData pd = new PolicyData();

            pd.setId(pdl.size());
            pd.setPrincipalClass(principalClass);
            pd.setPrincipalName(principalName);

            pd.setPermissionClass(permissionClass);
            pd.setPermissionActions(permissionActions);
            pd.setPermissionName(permissionName);

            pd.setPrincipal(objectname);
            pd.setPermission(permissionType);
            pd.setOperation(operations);
            pd.setResource(resource);

            pdl.add(pd);
        }
    }

    return pdl;
}

From source file:com.dtolabs.rundeck.core.authorization.RuleEvaluator.java

static Decision createAuthorize(final boolean authorized, final Explanation explanation,
        final Map<String, String> resource, final Subject subject, final String action,
        final Set<Attribute> environment, final long evaluationTime) {

    return new Decision() {
        private String representation;

        public boolean isAuthorized() {
            return authorized;
        }//from  w w w .  j a v  a  2s .c  om

        public Map<String, String> getResource() {
            return resource;
        }

        public String getAction() {
            return action;
        }

        public Set<Attribute> getEnvironment() {
            return environment;
        }

        public Subject getSubject() {
            return subject;
        }

        public String toString() {
            if (representation == null) {
                StringBuilder builder = new StringBuilder();
                builder.append("Decision for: ");
                builder.append("res<");
                Iterator<Map.Entry<String, String>> riter = resource.entrySet().iterator();
                while (riter.hasNext()) {
                    Map.Entry<String, String> s = riter.next();
                    builder.append(s.getKey()).append(':').append(s.getValue());
                    if (riter.hasNext()) {
                        builder.append(", ");
                    }
                }

                builder.append("> subject<");
                Iterator<Principal> iter = subject.getPrincipals().iterator();
                while (iter.hasNext()) {
                    Principal principal = iter.next();
                    builder.append(principal.getClass().getSimpleName());
                    builder.append(':');
                    builder.append(principal.getName());
                    if (iter.hasNext()) {
                        builder.append(' ');
                    }
                }

                builder.append("> action<");
                builder.append(action);

                builder.append("> env<");
                Iterator<Attribute> eiter = environment.iterator();
                while (eiter.hasNext()) {
                    Attribute a = eiter.next();
                    builder.append(a);
                    if (eiter.hasNext()) {
                        builder.append(", ");
                    }
                }
                builder.append(">");
                builder.append(": authorized: ");
                builder.append(isAuthorized());
                builder.append(": ");
                builder.append(explanation.toString());

                this.representation = builder.toString();
            }
            return this.representation;
        }

        public Explanation explain() {
            return explanation;
        }

        public long evaluationDuration() {
            return evaluationTime;
        }
    };
}

From source file:io.fabric8.maven.impl.MavenSecureHttpContext.java

public Subject doAuthenticate(final String username, final String password) {
    try {//from w w  w  . j av  a  2  s . com
        Subject subject = new Subject();
        LoginContext loginContext = new LoginContext(realm, subject, new CallbackHandler() {
            public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
                for (int i = 0; i < callbacks.length; i++) {
                    if (callbacks[i] instanceof NameCallback) {
                        ((NameCallback) callbacks[i]).setName(username);
                    } else if (callbacks[i] instanceof PasswordCallback) {
                        ((PasswordCallback) callbacks[i]).setPassword(password.toCharArray());
                    } else {
                        throw new UnsupportedCallbackException(callbacks[i]);
                    }
                }
            }
        });
        loginContext.login();
        if (role != null && role.length() > 0) {
            String clazz = "org.apache.karaf.jaas.boot.principal.RolePrincipal";
            String name = role;
            int idx = role.indexOf(':');
            if (idx > 0) {
                clazz = role.substring(0, idx);
                name = role.substring(idx + 1);
            }
            boolean found = false;
            for (Principal p : subject.getPrincipals()) {
                if (p.getClass().getName().equals(clazz) && p.getName().equals(name)) {
                    found = true;
                    break;
                }
            }
            if (!found) {
                throw new FailedLoginException("User does not have the required role " + role);
            }
        }
        return subject;
    } catch (AccountException e) {
        LOGGER.warn("Account failure", e);
        return null;
    } catch (LoginException e) {
        LOGGER.debug("Login failed", e);
        return null;
    } catch (GeneralSecurityException e) {
        LOGGER.error("General Security Exception", e);
        return null;
    }
}

From source file:cn.vlabs.duckling.vwb.service.auth.policy.PolicyUtil.java

public static PolicyData add(String voGroup, Acl policy, String principalAlias, String permissionAlias,
        String operation, String resource) {

    Principal principal = null;
    if (principalAlias.equals("All")) {
        principal = new Role("All");
        principalAlias = "";
    } else if (principalAlias.equals("VO")) {
        principal = new GroupPrincipal(voGroup);
        principalAlias = "";
    } else {// ww w .  j a  va  2s  .  c  o m
        return null;
    }

    AclEntry policyEntry = policy.getEntry(principal);
    Permission permission = null;
    if (permissionAlias.equals("Page")) {
        permission = new PagePermission(resource, operation);
        permissionAlias = "?";
    } else if (permissionAlias.equals("VWB")) {
        permission = new VWBPermission(resource, operation);
        permissionAlias = "";
    } else {
        return null;
    }

    policyEntry.addPermission(permission);

    PolicyData pd = new PolicyData();
    pd.setPrincipalClass(principal.getClass().getName());
    pd.setPrincipalName(principal.getName());
    pd.setPrincipal(principalAlias);

    pd.setPermissionClass(permission.getClass().getName());
    pd.setPermissionName(permission.getName());
    pd.setPermissionActions(permission.getActions());
    pd.setPermission(permissionAlias);

    pd.setOperation(operation);
    pd.setResource(resource);

    return pd;
}