Java Code Examples for org.apache.catalina.realm.GenericPrincipal#getRoles()

The following examples show how to use org.apache.catalina.realm.GenericPrincipal#getRoles() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TomcatSecurityService.java    From tomee with Apache License 2.0 6 votes vote down vote up
@Override
public boolean isCallerInRole(final String role) {
    final Principal principal = getCallerPrincipal();
    if (TomcatUser.class.isInstance(principal)) {
        if ("**".equals(role)) {
            return true; // ie logged in through tomcat
        }

        final TomcatUser tomcatUser = (TomcatUser) principal;
        final GenericPrincipal genericPrincipal = (GenericPrincipal) tomcatUser.getTomcatPrincipal();
        final String[] roles = genericPrincipal.getRoles();
        if (roles != null) {
            for (final String userRole : roles) {
                if (userRole.equals(role)) {
                    return true;
                }
            }
        }
        return false;
    }
    return super.isCallerInRole(role);
}
 
Example 2
Source File: SerializablePrincipal.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
public static SerializablePrincipal createPrincipal(GenericPrincipal principal)
{
    if ( principal==null) return null;
    return new SerializablePrincipal(principal.getName(),
                                     principal.getPassword(),
                                     principal.getRoles()!=null?Arrays.asList(principal.getRoles()):null,
                                     principal.getUserPrincipal()!=principal?principal.getUserPrincipal():null);
}
 
Example 3
Source File: SerializablePrincipal.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
public static void writePrincipal(GenericPrincipal p, ObjectOutput out)
        throws IOException {
    out.writeUTF(p.getName());
    out.writeBoolean(p.getPassword()!=null);
    if ( p.getPassword()!= null ) out.writeUTF(p.getPassword());
    String[] roles = p.getRoles();
    if ( roles == null ) roles = new String[0];
    out.writeInt(roles.length);
    for ( int i=0; i<roles.length; i++ ) out.writeUTF(roles[i]);
    boolean hasUserPrincipal = (p != p.getUserPrincipal() &&
            p.getUserPrincipal() instanceof Serializable);
    out.writeBoolean(hasUserPrincipal);
    if (hasUserPrincipal) out.writeObject(p.getUserPrincipal());
}
 
Example 4
Source File: SerializablePrincipal.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
public static SerializablePrincipal createPrincipal(GenericPrincipal principal)
{
    if ( principal==null) return null;
    return new SerializablePrincipal(principal.getName(),
                                     principal.getPassword(),
                                     principal.getRoles()!=null?Arrays.asList(principal.getRoles()):null,
                                     principal.getUserPrincipal()!=principal?principal.getUserPrincipal():null);
}
 
Example 5
Source File: SerializablePrincipal.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
public static void writePrincipal(GenericPrincipal p, ObjectOutput out)
        throws IOException {
    out.writeUTF(p.getName());
    out.writeBoolean(p.getPassword()!=null);
    if ( p.getPassword()!= null ) out.writeUTF(p.getPassword());
    String[] roles = p.getRoles();
    if ( roles == null ) roles = new String[0];
    out.writeInt(roles.length);
    for ( int i=0; i<roles.length; i++ ) out.writeUTF(roles[i]);
    boolean hasUserPrincipal = (p != p.getUserPrincipal() &&
            p.getUserPrincipal() instanceof Serializable);
    out.writeBoolean(hasUserPrincipal);
    if (hasUserPrincipal) out.writeObject(p.getUserPrincipal());
}
 
Example 6
Source File: CdiEventRealmTest.java    From tomee with Apache License 2.0 5 votes vote down vote up
@Test
public void digest() {
    final GenericPrincipal gp = getGenericPrincipal(new CdiEventRealm().authenticate("ryan", "md5", "nonce", "nc", "cnonce", "qop", "realm", "md5a2"));
    final String[] actual = gp.getRoles();
    final String[] expected = new String[] {"ryan", "md5", "nonce", "nc", "cnonce", "qop", "realm", "md5a2"};

    Arrays.sort(actual);
    Arrays.sort(expected);

    assertArrayEquals(actual, expected);
}
 
Example 7
Source File: CatalinaSamlSessionStore.java    From keycloak with Apache License 2.0 5 votes vote down vote up
@Override
public boolean isLoggedIn() {
    Session session = request.getSessionInternal(false);
    if (session == null) {
        log.debug("session was null, returning null");
        return false;
    }
    final SamlSession samlSession = SamlUtil.validateSamlSession(session.getSession().getAttribute(SamlSession.class.getName()), deployment);
    if (samlSession == null) {
        return false;
    }

    GenericPrincipal principal = (GenericPrincipal) session.getPrincipal();
    // in clustered environment in JBossWeb, principal is not serialized or saved
    if (principal == null) {
        principal = principalFactory.createPrincipal(request.getContext().getRealm(), samlSession.getPrincipal(), samlSession.getRoles());
        session.setPrincipal(principal);
        session.setAuthType("KEYCLOAK-SAML");

    }
    else if (samlSession.getPrincipal().getName().equals(principal.getName())){
        if (!principal.getUserPrincipal().getName().equals(samlSession.getPrincipal().getName())) {
            throw new RuntimeException("Unknown State");
        }
        log.debug("************principal already in");
        if (log.isDebugEnabled()) {
            for (String role : principal.getRoles()) {
                log.debug("principal role: " + role);
            }
        }

    }
    request.setUserPrincipal(principal);
    request.setAuthType("KEYCLOAK-SAML");
    restoreRequest();
    return true;
}