Search in sources :

Example 1 with PasswordValidationCallback

use of jakarta.security.auth.message.callback.PasswordValidationCallback in project tomcat by apache.

the class CallbackHandlerImpl method handle.

@Override
public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
    String name = null;
    Principal principal = null;
    Subject subject = null;
    String[] groups = null;
    if (callbacks != null) {
        // Process the callbacks
        for (Callback callback : callbacks) {
            if (callback instanceof CallerPrincipalCallback) {
                CallerPrincipalCallback cpc = (CallerPrincipalCallback) callback;
                name = cpc.getName();
                principal = cpc.getPrincipal();
                subject = cpc.getSubject();
            } else if (callback instanceof GroupPrincipalCallback) {
                GroupPrincipalCallback gpc = (GroupPrincipalCallback) callback;
                groups = gpc.getGroups();
            } else if (callback instanceof PasswordValidationCallback) {
                if (container == null) {
                    log.warn(sm.getString("callbackHandlerImpl.containerMissing", callback.getClass().getName()));
                } else if (container.getRealm() == null) {
                    log.warn(sm.getString("callbackHandlerImpl.realmMissing", callback.getClass().getName(), container.getName()));
                } else {
                    PasswordValidationCallback pvc = (PasswordValidationCallback) callback;
                    principal = container.getRealm().authenticate(pvc.getUsername(), String.valueOf(pvc.getPassword()));
                    pvc.setResult(principal != null);
                    subject = pvc.getSubject();
                }
            } else {
                log.error(sm.getString("callbackHandlerImpl.jaspicCallbackMissing", callback.getClass().getName()));
            }
        }
        // Create the GenericPrincipal
        Principal gp = getPrincipal(principal, name, groups);
        if (subject != null && gp != null) {
            subject.getPrivateCredentials().add(gp);
        }
    }
}
Also used : CallerPrincipalCallback(jakarta.security.auth.message.callback.CallerPrincipalCallback) GroupPrincipalCallback(jakarta.security.auth.message.callback.GroupPrincipalCallback) PasswordValidationCallback(jakarta.security.auth.message.callback.PasswordValidationCallback) GroupPrincipalCallback(jakarta.security.auth.message.callback.GroupPrincipalCallback) CallerPrincipalCallback(jakarta.security.auth.message.callback.CallerPrincipalCallback) Callback(javax.security.auth.callback.Callback) PasswordValidationCallback(jakarta.security.auth.message.callback.PasswordValidationCallback) GenericPrincipal(org.apache.catalina.realm.GenericPrincipal) Principal(java.security.Principal) Subject(javax.security.auth.Subject)

Example 2 with PasswordValidationCallback

use of jakarta.security.auth.message.callback.PasswordValidationCallback 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)

Aggregations

PasswordValidationCallback (jakarta.security.auth.message.callback.PasswordValidationCallback)2 Subject (javax.security.auth.Subject)2 GenericPrincipal (org.apache.catalina.realm.GenericPrincipal)2 CallerPrincipalCallback (jakarta.security.auth.message.callback.CallerPrincipalCallback)1 GroupPrincipalCallback (jakarta.security.auth.message.callback.GroupPrincipalCallback)1 Principal (java.security.Principal)1 Callback (javax.security.auth.callback.Callback)1 CallbackHandler (javax.security.auth.callback.CallbackHandler)1 Contained (org.apache.catalina.Contained)1 Container (org.apache.catalina.Container)1 Test (org.junit.Test)1