Search in sources :

Example 31 with GenericPrincipal

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

the class TestCallbackHandlerImpl method testPasswordValidationCallback.

@Test
public void testPasswordValidationCallback() throws Exception {
    CallbackHandler callbackHandler = createCallbackHandler(null);
    Container container = new TestContainer();
    container.setRealm(new TestRealm());
    ((Contained) callbackHandler).setContainer(container);
    Subject clientSubject = new Subject();
    PasswordValidationCallback pvc1 = new PasswordValidationCallback(clientSubject, "name1", "password".toCharArray());
    callbackHandler.handle(new Callback[] { pvc1 });
    Assert.assertTrue(pvc1.getResult());
    PasswordValidationCallback pvc2 = new PasswordValidationCallback(clientSubject, "name2", "invalid".toCharArray());
    callbackHandler.handle(new Callback[] { pvc2 });
    Assert.assertFalse(pvc2.getResult());
    Set<Object> credentials = clientSubject.getPrivateCredentials();
    Assert.assertTrue(credentials.size() == 1);
    GenericPrincipal gp = (GenericPrincipal) credentials.iterator().next();
    Assert.assertEquals("name1", gp.getName());
}
Also used : Contained(org.apache.catalina.Contained) CallbackHandler(javax.security.auth.callback.CallbackHandler) Container(org.apache.catalina.Container) GenericPrincipal(org.apache.catalina.realm.GenericPrincipal) PasswordValidationCallback(jakarta.security.auth.message.callback.PasswordValidationCallback) Subject(javax.security.auth.Subject) Test(org.junit.Test)

Example 32 with GenericPrincipal

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

the class DeltaRequest method setPrincipal.

/**
 * Only support principals from type {@link GenericPrincipal GenericPrincipal}
 * @param p Session principal
 * @see GenericPrincipal
 */
public void setPrincipal(Principal p) {
    int action = (p == null) ? ACTION_REMOVE : ACTION_SET;
    GenericPrincipal gp = null;
    if (p != null) {
        if (p instanceof GenericPrincipal) {
            gp = (GenericPrincipal) p;
            if (log.isDebugEnabled()) {
                log.debug(sm.getString("deltaRequest.showPrincipal", p.getName(), getSessionId()));
            }
        } else {
            log.error(sm.getString("deltaRequest.wrongPrincipalClass", p.getClass().getName()));
        }
    }
    addAction(TYPE_PRINCIPAL, action, NAME_PRINCIPAL, gp);
}
Also used : GenericPrincipal(org.apache.catalina.realm.GenericPrincipal)

Example 33 with GenericPrincipal

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

the class MemoryUserDatabaseTests method testSerializePrincipal.

@Test
public void testSerializePrincipal() throws Exception {
    User user = db.findUser("admin");
    GenericPrincipal gpIn = new UserDatabaseRealm.UserDatabasePrincipal(user, db);
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    ObjectOutputStream oos = new ObjectOutputStream(bos);
    oos.writeObject(gpIn);
    byte[] data = bos.toByteArray();
    ByteArrayInputStream bis = new ByteArrayInputStream(data);
    ObjectInputStream ois = new ObjectInputStream(bis);
    GenericPrincipal gpOut = (GenericPrincipal) ois.readObject();
    Assert.assertEquals("admin", gpOut.getName());
    assertPrincipalNames(gpOut.getRoles(), user.getRoles());
}
Also used : User(org.apache.catalina.User) GenericPrincipal(org.apache.catalina.realm.GenericPrincipal) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) ObjectInputStream(java.io.ObjectInputStream) Test(org.junit.Test)

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