Search in sources :

Example 21 with GenericPrincipal

use of org.apache.catalina.realm.GenericPrincipal in project tomee by apache.

the class CdiEventRealmTest method userPassword.

@Test
public void userPassword() {
    final GenericPrincipal gp = getGenericPrincipal(new CdiEventRealm().authenticate("john", "secret"));
    assertEquals("john", gp.getName());
    assertEquals("", gp.getPassword());
    assertEquals(1, gp.getRoles().length);
    assertEquals("admin", gp.getRoles()[0]);
}
Also used : CdiEventRealm(org.apache.tomee.catalina.realm.CdiEventRealm) GenericPrincipal(org.apache.catalina.realm.GenericPrincipal) Test(org.junit.Test)

Example 22 with GenericPrincipal

use of org.apache.catalina.realm.GenericPrincipal in project tomee by apache.

the class CdiEventRealmTest method digest.

@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);
}
Also used : CdiEventRealm(org.apache.tomee.catalina.realm.CdiEventRealm) GenericPrincipal(org.apache.catalina.realm.GenericPrincipal) Test(org.junit.Test)

Example 23 with GenericPrincipal

use of org.apache.catalina.realm.GenericPrincipal in project tomee by apache.

the class TomcatSecurityService method createSubject.

private Subject createSubject(final Realm realm, final Principal principal) {
    final Subject subject = new Subject();
    // 1. Add the principal as is
    subject.getPrincipals().add(principal);
    subject.getPrincipals().add(new TomcatUser(realm, principal));
    Principal p = principal;
    if (principal instanceof TomcatUser) {
        // should never happen
        p = ((TomcatUser) principal).getTomcatPrincipal();
        subject.getPrincipals().add(p);
    }
    if (p instanceof GenericPrincipal) {
        final GenericPrincipal genericPrincipal = (GenericPrincipal) p;
        subject.getPrincipals().add(genericPrincipal.getUserPrincipal());
        // todo should we create credentials with the roles? groups?
        subject.getPrivateCredentials().add(p);
    }
    return subject;
}
Also used : GenericPrincipal(org.apache.catalina.realm.GenericPrincipal) Subject(javax.security.auth.Subject) GenericPrincipal(org.apache.catalina.realm.GenericPrincipal) CallerPrincipal(org.apache.openejb.spi.CallerPrincipal) Principal(java.security.Principal)

Example 24 with GenericPrincipal

use of org.apache.catalina.realm.GenericPrincipal in project keycloak by keycloak.

the class CatalinaCookieTokenStore method isCached.

@Override
public boolean isCached(RequestAuthenticator authenticator) {
    // Assuming authenticatedPrincipal set by previous call of checkCurrentToken() during this request
    if (authenticatedPrincipal != null) {
        log.fine("remote logged in already. Establish state from cookie");
        RefreshableKeycloakSecurityContext securityContext = authenticatedPrincipal.getKeycloakSecurityContext();
        if (!securityContext.getRealm().equals(deployment.getRealm())) {
            log.fine("Account from cookie is from a different realm than for the request.");
            return false;
        }
        securityContext.setCurrentRequestInfo(deployment, this);
        Set<String> roles = AdapterUtils.getRolesFromSecurityContext(securityContext);
        GenericPrincipal principal = principalFactory.createPrincipal(request.getContext().getRealm(), authenticatedPrincipal, roles);
        request.setAttribute(KeycloakSecurityContext.class.getName(), securityContext);
        request.setUserPrincipal(principal);
        request.setAuthType("KEYCLOAK");
        return true;
    } else {
        return false;
    }
}
Also used : GenericPrincipal(org.apache.catalina.realm.GenericPrincipal) RefreshableKeycloakSecurityContext(org.keycloak.adapters.RefreshableKeycloakSecurityContext) RefreshableKeycloakSecurityContext(org.keycloak.adapters.RefreshableKeycloakSecurityContext) KeycloakSecurityContext(org.keycloak.KeycloakSecurityContext)

Example 25 with GenericPrincipal

use of org.apache.catalina.realm.GenericPrincipal in project keycloak by keycloak.

the class CatalinaSessionTokenStore method saveAccountInfo.

@Override
public void saveAccountInfo(OidcKeycloakAccount account) {
    RefreshableKeycloakSecurityContext securityContext = (RefreshableKeycloakSecurityContext) account.getKeycloakSecurityContext();
    Set<String> roles = account.getRoles();
    GenericPrincipal principal = principalFactory.createPrincipal(request.getContext().getRealm(), account.getPrincipal(), roles);
    SerializableKeycloakAccount sAccount = new SerializableKeycloakAccount(roles, account.getPrincipal(), securityContext);
    Session session = request.getSessionInternal(true);
    session.setPrincipal(principal);
    session.setAuthType("KEYCLOAK");
    session.getSession().setAttribute(SerializableKeycloakAccount.class.getName(), sAccount);
    session.getSession().setAttribute(KeycloakSecurityContext.class.getName(), account.getKeycloakSecurityContext());
    String username = securityContext.getToken().getSubject();
    log.fine("userSessionManagement.login: " + username);
    this.sessionManagement.login(session);
}
Also used : GenericPrincipal(org.apache.catalina.realm.GenericPrincipal) RefreshableKeycloakSecurityContext(org.keycloak.adapters.RefreshableKeycloakSecurityContext) RefreshableKeycloakSecurityContext(org.keycloak.adapters.RefreshableKeycloakSecurityContext) KeycloakSecurityContext(org.keycloak.KeycloakSecurityContext) Session(org.apache.catalina.Session)

Aggregations

GenericPrincipal (org.apache.catalina.realm.GenericPrincipal)33 Principal (java.security.Principal)12 Subject (javax.security.auth.Subject)9 Test (org.junit.Test)9 Session (org.apache.catalina.Session)5 ArrayList (java.util.ArrayList)4 CdiEventRealm (org.apache.tomee.catalina.realm.CdiEventRealm)4 CallerPrincipalCallback (jakarta.security.auth.message.callback.CallerPrincipalCallback)3 CallbackHandler (javax.security.auth.callback.CallbackHandler)3 KeycloakSecurityContext (org.keycloak.KeycloakSecurityContext)3 RefreshableKeycloakSecurityContext (org.keycloak.adapters.RefreshableKeycloakSecurityContext)3 GroupPrincipalCallback (jakarta.security.auth.message.callback.GroupPrincipalCallback)2 PasswordValidationCallback (jakarta.security.auth.message.callback.PasswordValidationCallback)2 IOException (java.io.IOException)2 ObjectInputStream (java.io.ObjectInputStream)2 ObjectOutputStream (java.io.ObjectOutputStream)2 Callback (javax.security.auth.callback.Callback)2 HttpSession (javax.servlet.http.HttpSession)2 CallerPrincipal (org.apache.openejb.spi.CallerPrincipal)2 AuthException (jakarta.security.auth.message.AuthException)1